Code

Fixed opsi host license ussage
[gosa.git] / gosa-plugins / opsi / admin / opsiLicenses / class_licensePoolGeneric.inc
1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id: class_opsiLicenses.inc 13520 2009-03-09 14:54:13Z hickert $$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
24 class licensePoolGeneric extends plugin 
25 {
27   var $cn = "";
28   var $orig_cn = "";
29   var $description = "";
30   var $orig_dn    = "";
32   var $data       = array();
33   var $orig_data  = array();
34   var $productIds = array();
35   var $softwareIds= array();
36   var $licenses   = array();
37   var $orig_licenses   = array();
39   var $availableProductIds = array();
40   var $attributes =array("cn","description");
41   var $si = NULL;
43   var $opsiHosts = array();
45   var $licenseMap = array( 
46       "BOUNDTOHOST"=> "boundToHost",
47       "HOSTIDS"=> "usedByHost",
48       "LICENSEPOOLIDS"=> "licensePoolId",
49       "LICENSETYPE"=> "licenseModel",
50       "LICENSEKEYS"=> "licenseKey",
51       "MAXINSTALLATIONS"=> "maximumInstallations",
52       "EXPIRATIONDATE"=> "expirationDate",
53       "SOFTWARELICENSEID"=> "cn"
54       );
56   var $licenseContractMap = array( 
57       "CONCLUSIONDATE"=> "conclusionDate",
58       "NOTIFICATIONDATE"=> "notificationDate",
59       "NOTES"=> "description",
60       "PARTNER"=> "partner"
61       );
63   function __construct(&$config,$dn)
64   {
65     $this->config = $config;
66     $this->dn = $this->orig_dn = $dn;
68     $this->si = new opsiLicenceHandler($this->config);
70     $this->is_account=TRUE;
71     if($this->dn == "new"){
72       $this->initially_was_account = FALSE;
73     }else{
74       $this->initially_was_account = TRUE;
75       $this->cn = $this->orig_cn = preg_replace("/^opsi:cn=([^,]*),.*$/","\\1",$dn);
76     }
78     // Extract pool name out of the fake dn.
79     $this->init();
80   }
82   
83   function init()
84   {
85     // Load local Boot Products 
86     $res = $this->si->get_local_products();
87     $this->availableProductIds=array();
88     if($this->si->is_error()){
89       $this->init_successfull = FALSE;
90       return(FALSE);
91     }
92     $this->availableProductIds=array_keys($res);
94     // Load opsi hosts
95     $res = $this->si->list_clients();
96     $this->opsiHosts=array();
97     if($this->si->is_error()){
98       $this->init_successfull = FALSE;
99       return(FALSE);
100     }
101     $this->opsiHosts=$res;
103     // Load Pool
104     if(!$this->initially_was_account){
105       $this->init_successfull = TRUE;
106     }else{
108       $res = $this->si->getPool($this->cn);
110       if($this->si->is_error()){    
111         $this->init_successfull = FALSE;
112         return(FALSE);
113       }else{
114         $this->data = $this->orig_data = $res;
115         $this->description = $this->data['description'][0];
117         // Load software IDs 
118         $this->softwareIds = array();
119         if(isset($this->data['softwareId'])){
120           for($i = 0; $i < $this->data['softwareId']['count']; $i++){
121             $this->softwareIds[] = $this->data['softwareId'][$i];
122           }
123         }
125         // Load product IDs 
126         $this->productIds = array();
127         if(isset($this->data['productId'])){
128           for($i = 0; $i < $this->data['productId']['count']; $i++){
129             $this->productIds[] = $this->data['productId'][$i];
130           }
131         }
133         // Load Licences 
134         $this->licenses = array();
135         if(isset($this->data['licenses'])){
136           for($i =0; $i< $this->data['licenses']['count']; $i++){
137             $license = $this->data['licenses'][$i];
139             // Do not parse invalid licenses
140             if(!is_array($license) || !isset($license['LICENSEPOOLIDS'])){
141               echo "Invalid license";
142               continue;
143             }
145             // Prepare Software License Key(s)
146             $upper = strtoupper($license['LICENSEPOOLIDS'][0]['VALUE']);
147             $license['LICENSEKEYS'] = $license['LICENSEKEYS'][0][$upper];
149             $entry = array();
150             foreach($this->licenseMap as $source => $target){
151               $entry[$target] = "";
152               if(isset($license[$source])){
153                 if(count($license[$source]) >= 2){
154                   $entry[$target] = array();
155                   foreach($license[$source] as $sub){
156                     $entry[$target][] = $sub['VALUE'];
157                   }
158                 }elseif(isset($license[$source][0]['VALUE'])){
159                   $entry[$target] = $license[$source][0]['VALUE'];
160                 }
161               }
162             }
164             $lData= $license['LICENSECONTRACTDATA'][0];
165             foreach($this->licenseContractMap as $source => $target){
166               if(isset($lData[$source])){
167                 if(count($lData[$source]) >= 2){
168                   $entry[$target] = array();
169                   foreach($lData[$source] as $sub){
170                     $entry[$target][] = $sub['VALUE'];
171                   }
172                 }elseif(isset($lData[$source][0]['VALUE'])){
173                   $entry[$target] = $lData[$source][0]['VALUE'];
174                 }
175               }
176             }
178             // There are some multi value attributes - force them to be of type array.
179             foreach(array("boundToHost","usedByHost","licenseKey") as $attr){
180               if(!is_array($entry[$attr])){
181                 $entry[$attr] = array($entry[$attr]);
182               }
183             }
184             $entry['MODIFIED'] = FALSE;
185             $this->licenses[$entry['cn']] = $entry;
186           }
187         }
189         $this->orig_licenses = $this->licenses;
190         $this->init_successfull = TRUE;
191         return;
192       }
193     }
194   }
197   function execute()
198   {
199     // Handle initialization failures.
200     if(isset($_POST['retry_init'])) $this->init();
201     if(!$this->init_successfull){
202       $smarty = get_smarty();
203       $smarty->assign("init_successfull", $this->init_successfull);
204       return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
205     }
207     // Display dialogs! 
208     if($this->dialog instanceOf plugin){
209       $this->dialog->save_object();
210       $display = $this->dialog->execute();
211       $display.= 
212         "<p style=\"text-align:right\">
213         <input type=submit name=\"license_finish\" style=\"width:80px\"
214         value=\"".msgPool::okButton(). "\">&nbsp;
215       <input type=submit name=\"license_cancel\"
216         value=\"".msgPool::cancelButton()."\">
217         </p>";
218       return($display);
219     }
221     $smarty = get_smarty();
223     // Assign ACls 
224     $plInfo = $this->plInfo();
225     foreach($plInfo['plProvidedAcls'] as $name => $desc){
226       $smarty->assign("{$name}ACL",$this->getacl($name));
227     }
228     foreach($this->attributes as $attr){
229       $smarty->assign($attr,$this->$attr);
230     }
231     $smarty->assign("init_successfull", $this->init_successfull);
232     $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
233     $smarty->assign("productIds", $this->productIds);
234     $smarty->assign("softwareIds", $this->softwareIds);
235     $smarty->assign("licenses", $this->getLicenseList());
236     $smarty->assign("initially_was_account", $this->initially_was_account);
237     return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
238   }
241   function getLicenseList()
242   {
243     $list = new divSelectBox("test");
244     $list->setHeight(100);
245     foreach($this->licenses as $i => $license){
246       $keys = implode($license['licenseKey'],", ");
247       $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
248       $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
250       $f1 = array("string" => $license['cn']);
251       $f2 = array("string" => $license['licenseModel']);
252       $f3 = array("string" => $license['expirationDate']);
253       $f4 = array("string" => $license['maximumInstallations']);
254       $f5 = array("string" => rtrim($keys,", "));
255       $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
257       $list->addEntry(array($f1,$f2,$f3,$f4,$f5,$f6));
258     }
259     return($list->DrawList());
260   }
261  
262  
263   /* Save HTML inputs
264    */
265   function save_object()
266   {
267     // Close license edit dialogs.
268     if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
269       $this->dialog = NULL;
270       return;
271     }
273     // Save license modifications
274     if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
275       $this->dialog->save_object();
277       $msgs = $this->dialog->check();
278       if(count($msgs)){
279         msg_dialog::displayChecks($msgs);
280       }else{
281         $attrs = $this->dialog->save();
282         $attrs['MODIFIED'] = TRUE;
283         $this->licenses[$attrs['cn']] = $attrs;
284         $this->dialog = NULL;
285       }
286       return;
287     }
289     if(isset($_POST['opsiLicensePoolPosted'])){
290       plugin::save_object();  
292       // Restore license cn, to avoid creating a copy...
293       if($this->initially_was_account) $this->cn = $this->orig_cn;
295       // We've to add prodcuts here 
296       if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
297         $pro = get_post('availableProduct');
298         if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
299           $this->productIds[] =$this->availableProductIds[$pro];
300         }
301       }
303       // We've to remove products here
304       if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
305         foreach($_POST['productIds'] as $key){
306           if(isset($this->productIds[$key])){
307             unset($this->productIds[$key]);
308           }
309         }
310       }
312       // We've to add software here 
313       if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
314         $soft = trim(get_post('newSoftwareId'));
315         if(!empty($soft) && !in_array($soft, $this->softwareIds)){
316           $this->softwareIds[] = $soft;
317         }
318       }
320       // We've to remove software Ids here
321       if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
322         foreach($_POST['softwareIds'] as $key){
323           if(isset($this->softwareIds[$key])){
324             unset($this->softwareIds[$key]);
325           }
326         }
327       }
329       // We've to create a new license
330       if(isset($_POST['addLicense'])){
331         $this->dialog = new licenseGeneric($this->config,array(), $this->opsiHosts);
332         $this->dialog->set_acl_base($this->config->current['BASE']);
333       }
334   
335       // Search post for image button clicks.
336       foreach($_POST as $name => $value){
337         if(preg_match("/^editLicense_/",$name)){
338           $id = preg_replace("/^editLicense_(.*)_.$/","\\1",$name);
339           if(isset($this->licenses[$id])){
340             $this->dialog = new licenseGeneric($this->config,$this->licenses[$id], $this->opsiHosts);
341             $this->dialog->set_acl_base($this->config->current['BASE']);
342           }
343           break;
344         }
345         if(preg_match("/^removeLicense_/",$name)){
346           $id = preg_replace("/^removeLicense_(.*)_.$/","\\1",$name);
347           if(isset($this->licenses[$id])){
348             unset($this->licenses[$id]);
349           }
350           break;
351         }
352       }
353     }
354   }  
357   /* Check user input and return a list of 'invalid input' messages.
358    */
359   function check()
360   {
361     $message = plugin::check();
362     return($message);
363   }
364   
366  
367   /* Removes the object from the opsi database
368    */ 
369   function remove_from_parent()
370   {
371     $this->si->deletePool($this->orig_cn);
372     if($this->si->is_error()){
373       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
374     }else{
376       // Trigger remove signal
377       $this->handle_post_events("remove");
378     }
380     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
381   }
384   /* Saves object modifications
385    */  
386   function save()
387   {
388     plugin::save();
390     // Send modify/add events
391     $mode = "modify";
392     if($this->orig_dn == "new"){
393       $mode = "add";
394     }
396     // Create or update the pool
397     $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
398     if($this->si->is_error()){
399       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
400     }else{
401       $this->handle_post_events($mode);
402     }
404     // Create, remove or update licenses
405     $add    = array_diff_assoc($this->licenses,$this->orig_licenses);
406     $del    = array_diff_assoc($this->orig_licenses,$this->licenses);
407     $update = array_intersect($this->licenses,$this->orig_licenses);
409     // Remove licenses 
410     foreach($del as $license){
411       $this->si->removeLicenseFromPool($this->cn, $license['cn']);
412       if($this->si->is_error()){
413         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
414       }
415     }
416     
417     // Add licenses 
418     foreach($add as $license){
419       $this->si->createLicense(
420         $this->cn,  // Pool id
421         $license['cn'], 
422         $license['licenseKey'][0],
423         $license['licenseModel'],
424         $license['partner'],
425         $license['conclusionDate'],
426         $license['notificationDate'],
427         $license['description'],
428         $license['cn'],
429         $license['maximumInstallations'],
430         $license['boundToHost'],
431         $license['expirationDate']);
433       if($this->si->is_error()){
434         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
435       }
436     }
438     // Update licenses
439     foreach($update as $license){
441       // Do not save untouched licenses
442       if(!$license['MODIFIED']){
443         continue;
444       }
446       $this->si->createLicense(
447         $this->cn,  // Pool id
448         $license['cn'],
449         $license['licenseKey'][0],
450         $license['licenseModel'],
451         $license['partner'],
452         $license['conclusionDate'],
453         $license['notificationDate'],
454         $license['description'],
455         $license['cn'],
456         $license['maximumInstallations'],
457         $license['boundToHost'],
458         $license['expirationDate']);
460       if($this->si->is_error()){
461         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
462       }
463     }
465     // Log action
466     if($mode == "modify"){
467       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
468     }else{
469       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
470     }
472     return 0;
473   }
474  
475   static function plInfo()
476   {
477     return (array(
478           "plShortName"   => _("Generic"),
479           "plDescription" => _("License generic"),
480           "plSelfModify"  => FALSE,
481           "plDepends"     => array(),
482           "plPriority"    => 1,
483           "plSection"     => array("administration"),
484           "plCategory"    => array("opsi"),
485           "plProvidedAcls"=> array(
486             "cn"                => _("Name"),
487             "description" => _("Description"))
488           ));
489   }
493 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
494 ?>