Code

Updated plugin description
[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     // initialize gosa-si handle for opsi licenses
69     $this->si = new opsiLicenceHandler($this->config);
71     // Detect if this is a new or existings license
72     $this->is_account=TRUE;
73     if($this->dn == "new"){
74       $this->initially_was_account = FALSE;
75     }else{
76       $this->initially_was_account = TRUE;
78       // Extract pool name out of the fake dn.
79       $this->cn = $this->orig_cn = preg_replace("/^opsi:cn=([^,]*),.*$/","\\1",$dn);
80     }
82     $this->init();
84     // Prepare lists
85     $this->licenseList = new sortableListing();
86     $this->licenseList->setDeleteable(true);
87     $this->licenseList->setEditable(true);
88     $this->licenseList->setWidth("100%");
89     $this->licenseList->setHeight("120px");
90     $this->licenseList->setColspecs(array('200px','*'));
91     $this->licenseList->setHeader(array(_("Section"),_("Description")));
92     $this->licenseList->setDefaultSortColumn(1);
94   }
96   
97   function init()
98   {
99     // Load local Boot Products 
100     $res = $this->si->get_local_products();
101     $this->availableProductIds=array();
102     if($this->si->is_error()){
103       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
104       $this->init_successfull = FALSE;
105       return(FALSE);
106     }
107     $this->availableProductIds=array_keys($res);
109     // Load opsi hosts
110     $res = $this->si->list_clients();
111     $this->opsiHosts=array();
112     if($this->si->is_error()){
113       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
114       $this->init_successfull = FALSE;
115       return(FALSE);
116     }
117     $this->opsiHosts=$res;
119     // Load Pool
120     if(!$this->initially_was_account){
121       $this->init_successfull = TRUE;
122     }else{
123       $res = $this->si->getPool($this->cn);
124       if($this->si->is_error()){    
125         $this->init_successfull = FALSE;
126         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
127         return(FALSE);
128       }else{
129         $this->data = $this->orig_data = $res;
130         $this->description = $this->data['description'][0];
132         // Load software IDs 
133         $this->softwareIds = array();
134         if(isset($this->data['softwareId'])){
135           for($i = 0; $i < $this->data['softwareId']['count']; $i++){
136             $this->softwareIds[] = $this->data['softwareId'][$i];
137           }
138         }
140         // Load product IDs 
141         $this->productIds = array();
142         if(isset($this->data['productId'])){
143           for($i = 0; $i < $this->data['productId']['count']; $i++){
144             $this->productIds[] = $this->data['productId'][$i];
145           }
146         }
148         // Load Licences 
149         $this->licenses = array();
150         if(isset($this->data['licenses'])){
151           for($i =0; $i< $this->data['licenses']['count']; $i++){
152             $license = $this->data['licenses'][$i];
154             // Do not parse invalid licenses
155             if(!is_array($license) || !isset($license['LICENSEPOOLIDS'])){
156               continue;
157             }
159             // Prepare Software License Key(s)
160             $upper = strtoupper($license['LICENSEPOOLIDS'][0]['VALUE']);
161             $license['LICENSEKEYS'] = $license['LICENSEKEYS'][0][$upper];
163             // Map license attributes to a useable format
164             $entry = array();
165             foreach($this->licenseMap as $source => $target){
166               $entry[$target] = "";
167               if(isset($license[$source])){
168                 if(count($license[$source]) >= 2){
169                   $entry[$target] = array();
170                   foreach($license[$source] as $sub){
171                     $entry[$target][] = $sub['VALUE'];
172                   }
173                 }elseif(isset($license[$source][0]['VALUE'])){
174                   $entry[$target] = $license[$source][0]['VALUE'];
175                 }
176               }
177             }
179             // Extract contract data
180             $lData= $license['LICENSECONTRACTDATA'][0];
181             foreach($this->licenseContractMap as $source => $target){
182               if(isset($lData[$source])){
183                 if(count($lData[$source]) >= 2){
184                   $entry[$target] = array();
185                   foreach($lData[$source] as $sub){
186                     $entry[$target][] = $sub['VALUE'];
187                   }
188                 }elseif(isset($lData[$source][0]['VALUE'])){
189                   $entry[$target] = $lData[$source][0]['VALUE'];
190                 }
191               }
192             }
194             // There are some multi value attributes - force them to be of type array.
195             foreach(array("boundToHost","usedByHost","licenseKey") as $attr){
196               if(!is_array($entry[$attr])){
197                 $entry[$attr] = array($entry[$attr]);
198               }
199             }
200             $entry['MODIFIED'] = FALSE;
201             $this->licenses[$entry['cn']] = $entry;
202           }
203         }
205         $this->orig_licenses = $this->licenses;
206         $this->init_successfull = TRUE;
207         return;
208       }
209     }
210   }
213   function execute()
214   {
215     plugin::execute();
217     $this->licenseList->setAcl($this->getacl('licenses'));
219     // Handle initialization failures.
220     if(isset($_POST['retry_init'])) $this->init();
221     if(!$this->init_successfull){
222       $smarty = get_smarty();
223       $smarty->assign("init_successfull", $this->init_successfull);
224       return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
225     }
227     // Display dialogs! 
228     if($this->dialog instanceOf plugin){
229       $this->dialog->save_object();
230       $display = $this->dialog->execute();
231       $display.= "<hr><div class='plugin-actions'>";
232       if($this->acl_is_writeable("licenses")){
233         $display.="<button type='submit' name='license_finish'>".msgPool::okButton()."</button>";
234       }
235       $display.="<button type='submit' name='license_cancel'>".msgPool::cancelButton()."</button>";
236       $display.="</div>";
237       return($display);
238     }
240     $smarty = get_smarty();
242     // Assign ACls 
243     $plInfo = $this->plInfo();
244     foreach($plInfo['plProvidedAcls'] as $name => $desc){
245       $smarty->assign("{$name}ACL",$this->getacl($name));
246     }
247     foreach($this->attributes as $attr){
248       $smarty->assign($attr,$this->$attr);
249     }
250     $smarty->assign("init_successfull", $this->init_successfull);
251     $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
252     $smarty->assign("productIds", $this->productIds);
253     $smarty->assign("softwareIds", $this->softwareIds);
254     $smarty->assign("licenses", $this->getLicenseList());
255     $smarty->assign("initially_was_account", $this->initially_was_account);
256     return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
257   }
260   function getLicenseList()
261   {
262     $data = $lData = array();
263     if($this->acl_is_readable("licenses")){
264       foreach($this->licenses as $i => $license){
265         $maxInst = "";
266         if($license['maximumInstallations'] == 0){
267           $maxInst = _("unlimited");
268         }else{
269           $maxInst = $license['maximumInstallations'];
270         }
272         $map = array(
273           "VOLUME" => sprintf(_("Volume license (#%s)"), $maxInst),
274           "OEM"=>_("OEM"),
275           "RETAIL"=>_("Retail"),
276           "CONCURRENT"=>_("Concurrent"));
278         $data[$license['cn']] = $license['cn'];
279         $lData[$license['cn']] = array('data' => array($license['cn'],$map[$license['licenseModel']]));
280       }
281     }
282     $this->licenseList->setListData($data,$lData);
283     return($this->licenseList->render());
284   }
285  
286  
287   /* Save HTML inputs
288    */
289   function save_object()
290   {
292     $this->licenseList->save_object();
293     $tmp = $this->licenseList->getAction();
295     // Allow license edit via href links
296     if($tmp['action'] == 'edit'){
297         $id = $this->licenseList->getKey($tmp['targets'][0]); 
298         if(isset($this->licenses[$id])){
299             $this->dialog = new licenseGeneric($this->config,$this->dn,$this->licenses[$id], $this->opsiHosts);
300             $this->dialog->set_acl_category('opsi');
301             $this->dialog->set_acl_base($this->config->current['BASE']);
302         }
303     }
304     if($tmp['action'] == 'delete'){
305         $id = $this->licenseList->getKey($tmp['targets'][0]); 
306         if(isset($this->licenses[$id])){
307             unset($this->licenses[$id]);
308         }
309     }
311     // Close license edit dialogs.
312     if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
313       $this->dialog = NULL;
314       return;
315     }
317     // Save license modifications
318     if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
319       $this->dialog->save_object();
320       $msgs = $this->dialog->check();
321       if(count($msgs)){
322         msg_dialog::displayChecks($msgs);
323       }else{
324         $attrs = $this->dialog->save();
325         $attrs['MODIFIED'] = TRUE;
326         $this->licenses[$attrs['cn']] = $attrs;
327         $this->dialog = NULL;
328       }
329       return;
330     }
332     if(isset($_POST['opsiLicensePoolPosted'])){
333       plugin::save_object();  
335       // Restore license cn, to avoid creating a copy...
336       if($this->initially_was_account) $this->cn = $this->orig_cn;
338       // We've to add prodcuts here 
339       if(preg_match("/w/",$this->getacl("productIds"))){
340         if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
341           $pro = get_post('availableProduct');
342           if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
343             $this->productIds[] =$this->availableProductIds[$pro];
344           }
345         }
346       }
348       // We've to remove products here
349       if(preg_match("/w/",$this->getacl("productIds"))){
350         if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
351           foreach($_POST['productIds'] as $key){
352             if(isset($this->productIds[$key])){
353               unset($this->productIds[$key]);
354             }
355           }
356         }
357       }
359       // We've to add software here 
360       if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
361         if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
362           $soft = trim(get_post('newSoftwareId'));
363           if(!empty($soft) && !in_array($soft, $this->softwareIds)){
364             $this->softwareIds[] = $soft;
365           }
366         }
367       }
369       // We've to remove software Ids here
370       if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
371         if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
372           foreach($_POST['softwareIds'] as $key){
373             if(isset($this->softwareIds[$key])){
374               unset($this->softwareIds[$key]);
375             }
376           }
377         }
378       }
380       // We've to create a new license
381       if(preg_match("/w/",$this->getacl("licenses"))){
382         if(isset($_POST['addLicense'])){
383           $this->dialog = new licenseGeneric($this->config,$this->dn,array(), $this->opsiHosts);
384           $this->dialog->set_acl_category('opsi');
385           $this->dialog->set_acl_base($this->config->current['BASE']);
386         }
387       }
388     }
389   }  
392   /* Check user input and return a list of 'invalid input' messages.
393    */
394   function check()
395   {
396     $message = plugin::check();
397     return($message);
398   }
399   
401  
402   /* Removes the object from the opsi database
403    */ 
404   function remove_from_parent()
405   {
406     $this->si->deletePool($this->orig_cn);
407     if($this->si->is_error()){
408       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
409     }else{
411       // Trigger remove signal
412       $this->handle_post_events("remove");
413     }
415     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
416   }
419   /* Saves object modifications
420    */  
421   function save()
422   {
423     plugin::save();
425     // Send modify/add events
426     $mode = "modify";
427     if($this->orig_dn == "new"){
428       $mode = "add";
429     }
431     // Create or update the pool
432     $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
433     if($this->si->is_error()){
434       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
435     }else{
436       $this->handle_post_events($mode);
437     }
439     // Create, remove or update licenses
440     $add    = array_diff_assoc($this->licenses,$this->orig_licenses);
441     $del    = array_diff_assoc($this->orig_licenses,$this->licenses);
442     $update = array_intersect($this->licenses,$this->orig_licenses);
444     // Remove licenses 
445     foreach($del as $license){
446       $this->si->removeLicenseFromPool($this->cn, $license['cn']);
447       if($this->si->is_error()){
448         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
449       }
450     }
451     
452     // Add licenses 
453     foreach($add as $license){
454       $this->si->createLicense(
455         $this->cn,  // Pool id
456         $license['cn'], 
457         $license['licenseKey'][0],
458         $license['licenseModel'],
459         $license['partner'],
460         $license['conclusionDate'],
461         $license['notificationDate'],
462         $license['description'],
463         $license['cn'],
464         $license['maximumInstallations'],
465         $license['boundToHost'],
466         $license['expirationDate']);
468       if($this->si->is_error()){
469         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
470       }
471     }
473     // Update licenses
474     foreach($update as $license){
476       // Do not save untouched licenses
477       if(!$license['MODIFIED']){
478         continue;
479       }
481       $this->si->createLicense(
482         $this->cn,  // Pool id
483         $license['cn'],
484         $license['licenseKey'][0],
485         $license['licenseModel'],
486         $license['partner'],
487         $license['conclusionDate'],
488         $license['notificationDate'],
489         $license['description'],
490         $license['cn'],
491         $license['maximumInstallations'],
492         $license['boundToHost'],
493         $license['expirationDate']);
495       if($this->si->is_error()){
496         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
497       }
498     }
500     // Log action
501     if($mode == "modify"){
502       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
503     }else{
504       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
505     }
507     return 0;
508   }
509  
510   static function plInfo()
511   {
512     return (array(
513           "plShortName"   => _("Pool generic"),
514           "plDescription" => _("License pool generic"),
515           "plSelfModify"  => FALSE,
516           "plDepends"     => array(),
517           "plPriority"    => 7,
518           "plSection"     => array("administration"),
519           "plCategory"    => array("opsi"),
520           "plProvidedAcls"=> array(
521             "cn"                => _("Name"),
522             "description" => _("Description"),
523             "productIds"  => _("Applications"),
524             "windowsSoftwareIds"  => _("Windows software IDs"),
525             "licenses"  => _("Licenses"))
526           ));
527   }
531 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
532 ?>