Code

Added lsiting of license usages
[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       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
90       $this->init_successfull = FALSE;
91       return(FALSE);
92     }
93     $this->availableProductIds=array_keys($res);
95     // Load opsi hosts
96     $res = $this->si->list_clients();
97     $this->opsiHosts=array();
98     if($this->si->is_error()){
99       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
100       $this->init_successfull = FALSE;
101       return(FALSE);
102     }
103     $this->opsiHosts=$res;
105     // Load Pool
106     if(!$this->initially_was_account){
107       $this->init_successfull = TRUE;
108     }else{
110       $res = $this->si->getPool($this->cn);
112       if($this->si->is_error()){    
113         $this->init_successfull = FALSE;
114         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
115         return(FALSE);
116       }else{
117         $this->data = $this->orig_data = $res;
118         $this->description = $this->data['description'][0];
120         // Load software IDs 
121         $this->softwareIds = array();
122         if(isset($this->data['softwareId'])){
123           for($i = 0; $i < $this->data['softwareId']['count']; $i++){
124             $this->softwareIds[] = $this->data['softwareId'][$i];
125           }
126         }
128         // Load product IDs 
129         $this->productIds = array();
130         if(isset($this->data['productId'])){
131           for($i = 0; $i < $this->data['productId']['count']; $i++){
132             $this->productIds[] = $this->data['productId'][$i];
133           }
134         }
136         // Load Licences 
137         $this->licenses = array();
138         if(isset($this->data['licenses'])){
139           for($i =0; $i< $this->data['licenses']['count']; $i++){
140             $license = $this->data['licenses'][$i];
142             // Do not parse invalid licenses
143             if(!is_array($license) || !isset($license['LICENSEPOOLIDS'])){
144               echo "Invalid license";
145               continue;
146             }
148             // Prepare Software License Key(s)
149             $upper = strtoupper($license['LICENSEPOOLIDS'][0]['VALUE']);
150             $license['LICENSEKEYS'] = $license['LICENSEKEYS'][0][$upper];
152             $entry = array();
153             foreach($this->licenseMap as $source => $target){
154               $entry[$target] = "";
155               if(isset($license[$source])){
156                 if(count($license[$source]) >= 2){
157                   $entry[$target] = array();
158                   foreach($license[$source] as $sub){
159                     $entry[$target][] = $sub['VALUE'];
160                   }
161                 }elseif(isset($license[$source][0]['VALUE'])){
162                   $entry[$target] = $license[$source][0]['VALUE'];
163                 }
164               }
165             }
167             $lData= $license['LICENSECONTRACTDATA'][0];
168             foreach($this->licenseContractMap as $source => $target){
169               if(isset($lData[$source])){
170                 if(count($lData[$source]) >= 2){
171                   $entry[$target] = array();
172                   foreach($lData[$source] as $sub){
173                     $entry[$target][] = $sub['VALUE'];
174                   }
175                 }elseif(isset($lData[$source][0]['VALUE'])){
176                   $entry[$target] = $lData[$source][0]['VALUE'];
177                 }
178               }
179             }
181             // There are some multi value attributes - force them to be of type array.
182             foreach(array("boundToHost","usedByHost","licenseKey") as $attr){
183               if(!is_array($entry[$attr])){
184                 $entry[$attr] = array($entry[$attr]);
185               }
186             }
187             $entry['MODIFIED'] = FALSE;
188             $this->licenses[$entry['cn']] = $entry;
189           }
190         }
192         $this->orig_licenses = $this->licenses;
193         $this->init_successfull = TRUE;
194         return;
195       }
196     }
197   }
200   function execute()
201   {
202     // Handle initialization failures.
203     if(isset($_POST['retry_init'])) $this->init();
204     if(!$this->init_successfull){
205       $smarty = get_smarty();
206       $smarty->assign("init_successfull", $this->init_successfull);
207       return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
208     }
210     // Display dialogs! 
211     if($this->dialog instanceOf plugin){
212       $this->dialog->save_object();
213       $display = $this->dialog->execute();
214       $display.= 
215         "<p style=\"text-align:right\">
216         <input type=submit name=\"license_finish\" style=\"width:80px\"
217         value=\"".msgPool::okButton(). "\">&nbsp;
218       <input type=submit name=\"license_cancel\"
219         value=\"".msgPool::cancelButton()."\">
220         </p>";
221       return($display);
222     }
224     $smarty = get_smarty();
226     // Assign ACls 
227     $plInfo = $this->plInfo();
228     foreach($plInfo['plProvidedAcls'] as $name => $desc){
229       $smarty->assign("{$name}ACL",$this->getacl($name));
230     }
231     foreach($this->attributes as $attr){
232       $smarty->assign($attr,$this->$attr);
233     }
234     $smarty->assign("init_successfull", $this->init_successfull);
235     $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
236     $smarty->assign("productIds", $this->productIds);
237     $smarty->assign("softwareIds", $this->softwareIds);
238     $smarty->assign("licenses", $this->getLicenseList());
239     $smarty->assign("initially_was_account", $this->initially_was_account);
240     return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
241   }
244   function getLicenseList()
245   {
246     $list = new divSelectBox("test");
247     $list->setHeight(100);
248     foreach($this->licenses as $i => $license){
249       $keys = implode($license['licenseKey'],", ");
250       $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
251       $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
253       $f1 = array("string" => $license['cn']);
254       $f2 = array("string" => $license['licenseModel']);
255       $f3 = array("string" => $license['expirationDate']);
256       $f4 = array("string" => $license['maximumInstallations']);
257       $f5 = array("string" => rtrim($keys,", "));
258       $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
260       $list->addEntry(array($f1,$f2,$f3,$f4,$f5,$f6));
261     }
262     return($list->DrawList());
263   }
264  
265  
266   /* Save HTML inputs
267    */
268   function save_object()
269   {
270     // Close license edit dialogs.
271     if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
272       $this->dialog = NULL;
273       return;
274     }
276     // Save license modifications
277     if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
278       $this->dialog->save_object();
280       $msgs = $this->dialog->check();
281       if(count($msgs)){
282         msg_dialog::displayChecks($msgs);
283       }else{
284         $attrs = $this->dialog->save();
285         $attrs['MODIFIED'] = TRUE;
286         $this->licenses[$attrs['cn']] = $attrs;
287         $this->dialog = NULL;
288       }
289       return;
290     }
292     if(isset($_POST['opsiLicensePoolPosted'])){
293       plugin::save_object();  
295       // Restore license cn, to avoid creating a copy...
296       if($this->initially_was_account) $this->cn = $this->orig_cn;
298       // We've to add prodcuts here 
299       if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
300         $pro = get_post('availableProduct');
301         if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
302           $this->productIds[] =$this->availableProductIds[$pro];
303         }
304       }
306       // We've to remove products here
307       if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
308         foreach($_POST['productIds'] as $key){
309           if(isset($this->productIds[$key])){
310             unset($this->productIds[$key]);
311           }
312         }
313       }
315       // We've to add software here 
316       if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
317         $soft = trim(get_post('newSoftwareId'));
318         if(!empty($soft) && !in_array($soft, $this->softwareIds)){
319           $this->softwareIds[] = $soft;
320         }
321       }
323       // We've to remove software Ids here
324       if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
325         foreach($_POST['softwareIds'] as $key){
326           if(isset($this->softwareIds[$key])){
327             unset($this->softwareIds[$key]);
328           }
329         }
330       }
332       // We've to create a new license
333       if(isset($_POST['addLicense'])){
334         $this->dialog = new licenseGeneric($this->config,array(), $this->opsiHosts);
335         $this->dialog->set_acl_base($this->config->current['BASE']);
336       }
337   
338       // Search post for image button clicks.
339       foreach($_POST as $name => $value){
340         if(preg_match("/^editLicense_/",$name)){
341           $id = preg_replace("/^editLicense_(.*)_.$/","\\1",$name);
342           if(isset($this->licenses[$id])){
343             $this->dialog = new licenseGeneric($this->config,$this->licenses[$id], $this->opsiHosts);
344             $this->dialog->set_acl_base($this->config->current['BASE']);
345           }
346           break;
347         }
348         if(preg_match("/^removeLicense_/",$name)){
349           $id = preg_replace("/^removeLicense_(.*)_.$/","\\1",$name);
350           if(isset($this->licenses[$id])){
351             unset($this->licenses[$id]);
352           }
353           break;
354         }
355       }
356     }
357   }  
360   /* Check user input and return a list of 'invalid input' messages.
361    */
362   function check()
363   {
364     $message = plugin::check();
365     return($message);
366   }
367   
369  
370   /* Removes the object from the opsi database
371    */ 
372   function remove_from_parent()
373   {
374     $this->si->deletePool($this->orig_cn);
375     if($this->si->is_error()){
376       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
377     }else{
379       // Trigger remove signal
380       $this->handle_post_events("remove");
381     }
383     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
384   }
387   /* Saves object modifications
388    */  
389   function save()
390   {
391     plugin::save();
393     // Send modify/add events
394     $mode = "modify";
395     if($this->orig_dn == "new"){
396       $mode = "add";
397     }
399     // Create or update the pool
400     $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
401     if($this->si->is_error()){
402       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
403     }else{
404       $this->handle_post_events($mode);
405     }
407     // Create, remove or update licenses
408     $add    = array_diff_assoc($this->licenses,$this->orig_licenses);
409     $del    = array_diff_assoc($this->orig_licenses,$this->licenses);
410     $update = array_intersect($this->licenses,$this->orig_licenses);
412     // Remove licenses 
413     foreach($del as $license){
414       $this->si->removeLicenseFromPool($this->cn, $license['cn']);
415       if($this->si->is_error()){
416         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
417       }
418     }
419     
420     // Add licenses 
421     foreach($add as $license){
422       $this->si->createLicense(
423         $this->cn,  // Pool id
424         $license['cn'], 
425         $license['licenseKey'][0],
426         $license['licenseModel'],
427         $license['partner'],
428         $license['conclusionDate'],
429         $license['notificationDate'],
430         $license['description'],
431         $license['cn'],
432         $license['maximumInstallations'],
433         $license['boundToHost'],
434         $license['expirationDate']);
436       if($this->si->is_error()){
437         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
438       }
439     }
441     // Update licenses
442     foreach($update as $license){
444       // Do not save untouched licenses
445       if(!$license['MODIFIED']){
446         continue;
447       }
449       $this->si->createLicense(
450         $this->cn,  // Pool id
451         $license['cn'],
452         $license['licenseKey'][0],
453         $license['licenseModel'],
454         $license['partner'],
455         $license['conclusionDate'],
456         $license['notificationDate'],
457         $license['description'],
458         $license['cn'],
459         $license['maximumInstallations'],
460         $license['boundToHost'],
461         $license['expirationDate']);
463       if($this->si->is_error()){
464         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
465       }
466     }
468     // Log action
469     if($mode == "modify"){
470       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
471     }else{
472       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
473     }
475     return 0;
476   }
477  
478   static function plInfo()
479   {
480     return (array(
481           "plShortName"   => _("Generic"),
482           "plDescription" => _("License generic"),
483           "plSelfModify"  => FALSE,
484           "plDepends"     => array(),
485           "plPriority"    => 1,
486           "plSection"     => array("administration"),
487           "plCategory"    => array("opsi"),
488           "plProvidedAcls"=> array(
489             "cn"                => _("Name"),
490             "description" => _("Description"))
491           ));
492   }
496 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
497 ?>