Code

Removed debug output
[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               continue;
145             }
147             // Prepare Software License Key(s)
148             $upper = strtoupper($license['LICENSEPOOLIDS'][0]['VALUE']);
149             $license['LICENSEKEYS'] = $license['LICENSEKEYS'][0][$upper];
151             $entry = array();
152             foreach($this->licenseMap as $source => $target){
153               $entry[$target] = "";
154               if(isset($license[$source])){
155                 if(count($license[$source]) >= 2){
156                   $entry[$target] = array();
157                   foreach($license[$source] as $sub){
158                     $entry[$target][] = $sub['VALUE'];
159                   }
160                 }elseif(isset($license[$source][0]['VALUE'])){
161                   $entry[$target] = $license[$source][0]['VALUE'];
162                 }
163               }
164             }
166             $lData= $license['LICENSECONTRACTDATA'][0];
167             foreach($this->licenseContractMap as $source => $target){
168               if(isset($lData[$source])){
169                 if(count($lData[$source]) >= 2){
170                   $entry[$target] = array();
171                   foreach($lData[$source] as $sub){
172                     $entry[$target][] = $sub['VALUE'];
173                   }
174                 }elseif(isset($lData[$source][0]['VALUE'])){
175                   $entry[$target] = $lData[$source][0]['VALUE'];
176                 }
177               }
178             }
180             // There are some multi value attributes - force them to be of type array.
181             foreach(array("boundToHost","usedByHost","licenseKey") as $attr){
182               if(!is_array($entry[$attr])){
183                 $entry[$attr] = array($entry[$attr]);
184               }
185             }
186             $entry['MODIFIED'] = FALSE;
187             $this->licenses[$entry['cn']] = $entry;
188           }
189         }
191         $this->orig_licenses = $this->licenses;
192         $this->init_successfull = TRUE;
193         return;
194       }
195     }
196   }
199   function execute()
200   {
201     // Handle initialization failures.
202     if(isset($_POST['retry_init'])) $this->init();
203     if(!$this->init_successfull){
204       $smarty = get_smarty();
205       $smarty->assign("init_successfull", $this->init_successfull);
206       return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
207     }
209     // Display dialogs! 
210     if($this->dialog instanceOf plugin){
211       $this->dialog->save_object();
212       $display = $this->dialog->execute();
213       $display.= "<p style=\"text-align:right\">";
214   
215       if($this->acl_is_writeable("licenses")){
216         $display.="<input type=submit name=\"license_finish\" style=\"width:80px\"
217           value=\"".msgPool::okButton(). "\">&nbsp;";
218       }
219       
220       $display.="<input type=submit name=\"license_cancel\" value=\"".msgPool::cancelButton()."\"></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);
249     if($this->acl_is_readable("licenses")){
250       foreach($this->licenses as $i => $license){
251         $keys = implode($license['licenseKey'],", ");
252         $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
253         if(preg_match("/w/", $this->getacl("licenses"))){
254           $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
255         }
256         $f1 = array("string" => $license['cn']);
257         $f2 = array("string" => $license['licenseModel']);
258         $f3 = array("string" => $license['expirationDate']);
259         $f4 = array("string" => $license['maximumInstallations']);
260         $f5 = array("string" => rtrim($keys,", "));
261         $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
262         $list->addEntry(array($f1,$f2,$f3,$f4,$f5,$f6));
263       }
264     }
265     return($list->DrawList());
266   }
267  
268  
269   /* Save HTML inputs
270    */
271   function save_object()
272   {
273     // Close license edit dialogs.
274     if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
275       $this->dialog = NULL;
276       return;
277     }
279     // Save license modifications
280     if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
281       $this->dialog->save_object();
283       $msgs = $this->dialog->check();
284       if(count($msgs)){
285         msg_dialog::displayChecks($msgs);
286       }else{
287         $attrs = $this->dialog->save();
288         $attrs['MODIFIED'] = TRUE;
289         $this->licenses[$attrs['cn']] = $attrs;
290         $this->dialog = NULL;
291       }
292       return;
293     }
295     if(isset($_POST['opsiLicensePoolPosted'])){
296       plugin::save_object();  
298       // Restore license cn, to avoid creating a copy...
299       if($this->initially_was_account) $this->cn = $this->orig_cn;
301       // We've to add prodcuts here 
302       if(preg_match("/w/",$this->getacl("productIds"))){
303         if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
304           $pro = get_post('availableProduct');
305           if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
306             $this->productIds[] =$this->availableProductIds[$pro];
307           }
308         }
309       }
311       // We've to remove products here
312       if(preg_match("/w/",$this->getacl("productIds"))){
313         if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
314           foreach($_POST['productIds'] as $key){
315             if(isset($this->productIds[$key])){
316               unset($this->productIds[$key]);
317             }
318           }
319         }
320       }
322       // We've to add software here 
323       if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
324         if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
325           $soft = trim(get_post('newSoftwareId'));
326           if(!empty($soft) && !in_array($soft, $this->softwareIds)){
327             $this->softwareIds[] = $soft;
328           }
329         }
330       }
332       // We've to remove software Ids here
333       if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
334         if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
335           foreach($_POST['softwareIds'] as $key){
336             if(isset($this->softwareIds[$key])){
337               unset($this->softwareIds[$key]);
338             }
339           }
340         }
341       }
343       // We've to create a new license
344       if(preg_match("/w/",$this->getacl("licenses"))){
345         if(isset($_POST['addLicense'])){
346           $this->dialog = new licenseGeneric($this->config,$this->dn,array(), $this->opsiHosts);
347           $this->dialog->set_acl_base($this->config->current['BASE']);
348         }
349       }
350   
351       // Search post for image button clicks.
352       if(preg_match("/r/",$this->getacl("licenses"))){
353         foreach($_POST as $name => $value){
354           if(preg_match("/^editLicense_/",$name)){
355             $id = preg_replace("/^editLicense_(.*)_.$/","\\1",$name);
356             if(isset($this->licenses[$id])){
357               $this->dialog = new licenseGeneric($this->config,$this->dn,$this->licenses[$id], $this->opsiHosts);
358               $this->dialog->set_acl_base($this->config->current['BASE']);
359             }
360             break;
361           }
362           if(preg_match("/^removeLicense_/",$name)){
363             $id = preg_replace("/^removeLicense_(.*)_.$/","\\1",$name);
364             if(isset($this->licenses[$id])){
365               unset($this->licenses[$id]);
366             }
367             break;
368           }
369         }
370       }
371     }
372   }  
375   /* Check user input and return a list of 'invalid input' messages.
376    */
377   function check()
378   {
379     $message = plugin::check();
380     return($message);
381   }
382   
384  
385   /* Removes the object from the opsi database
386    */ 
387   function remove_from_parent()
388   {
389     $this->si->deletePool($this->orig_cn);
390     if($this->si->is_error()){
391       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
392     }else{
394       // Trigger remove signal
395       $this->handle_post_events("remove");
396     }
398     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
399   }
402   /* Saves object modifications
403    */  
404   function save()
405   {
406     plugin::save();
408     // Send modify/add events
409     $mode = "modify";
410     if($this->orig_dn == "new"){
411       $mode = "add";
412     }
414     // Create or update the pool
415     $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
416     if($this->si->is_error()){
417       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
418     }else{
419       $this->handle_post_events($mode);
420     }
422     // Create, remove or update licenses
423     $add    = array_diff_assoc($this->licenses,$this->orig_licenses);
424     $del    = array_diff_assoc($this->orig_licenses,$this->licenses);
425     $update = array_intersect($this->licenses,$this->orig_licenses);
427     // Remove licenses 
428     foreach($del as $license){
429       $this->si->removeLicenseFromPool($this->cn, $license['cn']);
430       if($this->si->is_error()){
431         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
432       }
433     }
434     
435     // Add licenses 
436     foreach($add as $license){
437       $this->si->createLicense(
438         $this->cn,  // Pool id
439         $license['cn'], 
440         $license['licenseKey'][0],
441         $license['licenseModel'],
442         $license['partner'],
443         $license['conclusionDate'],
444         $license['notificationDate'],
445         $license['description'],
446         $license['cn'],
447         $license['maximumInstallations'],
448         $license['boundToHost'],
449         $license['expirationDate']);
451       if($this->si->is_error()){
452         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
453       }
454     }
456     // Update licenses
457     foreach($update as $license){
459       // Do not save untouched licenses
460       if(!$license['MODIFIED']){
461         continue;
462       }
464       $this->si->createLicense(
465         $this->cn,  // Pool id
466         $license['cn'],
467         $license['licenseKey'][0],
468         $license['licenseModel'],
469         $license['partner'],
470         $license['conclusionDate'],
471         $license['notificationDate'],
472         $license['description'],
473         $license['cn'],
474         $license['maximumInstallations'],
475         $license['boundToHost'],
476         $license['expirationDate']);
478       if($this->si->is_error()){
479         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
480       }
481     }
483     // Log action
484     if($mode == "modify"){
485       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
486     }else{
487       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
488     }
490     return 0;
491   }
492  
493   static function plInfo()
494   {
495     return (array(
496           "plShortName"   => _("Pool generic"),
497           "plDescription" => _("License pool generic"),
498           "plSelfModify"  => FALSE,
499           "plDepends"     => array(),
500           "plPriority"    => 1,
501           "plSection"     => array("administration"),
502           "plCategory"    => array("opsi"),
503           "plProvidedAcls"=> array(
504             "cn"                => _("Name"),
505             "description" => _("Description"),
506             "productIds"  => _("Applications"),
507             "windowsSoftwareIds"  => _("Windows software IDs"),
508             "licenses"  => _("Licenses"))
509           ));
510   }
514 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
515 ?>