Code

Updated locales
[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         $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
252         if(preg_match("/w/", $this->getacl("licenses"))){
253           $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
254         }
255         $f1 = array("string" => $license['cn']);
257         $maxInst = "";
258         if($license['maximumInstallations'] == 0){
259           $maxInst = "("._("unlimited").")";
260         }else{
261           $maxInst = "(".$license['maximumInstallations'].")";;
262         }
264         $map = array(
265           "VOLUME" => sprintf(_("Volume license (#%s)"), $maxInst),
266           "OEM"=>_("OEM"),
267           "RETAIL"=>_("Retail"),
268           "CONCURRENT"=>_("Concurrent"));
270         $f2 = array("string" => $map[$license['licenseModel']]);
271         $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
272         $list->addEntry(array($f1,$f2,$f6));
273       }
274     }
275     return($list->DrawList());
276   }
277  
278  
279   /* Save HTML inputs
280    */
281   function save_object()
282   {
283     // Close license edit dialogs.
284     if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
285       $this->dialog = NULL;
286       return;
287     }
289     // Save license modifications
290     if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
291       $this->dialog->save_object();
293       $msgs = $this->dialog->check();
294       if(count($msgs)){
295         msg_dialog::displayChecks($msgs);
296       }else{
297         $attrs = $this->dialog->save();
298         $attrs['MODIFIED'] = TRUE;
299         $this->licenses[$attrs['cn']] = $attrs;
300         $this->dialog = NULL;
301       }
302       return;
303     }
305     if(isset($_POST['opsiLicensePoolPosted'])){
306       plugin::save_object();  
308       // Restore license cn, to avoid creating a copy...
309       if($this->initially_was_account) $this->cn = $this->orig_cn;
311       // We've to add prodcuts here 
312       if(preg_match("/w/",$this->getacl("productIds"))){
313         if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
314           $pro = get_post('availableProduct');
315           if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
316             $this->productIds[] =$this->availableProductIds[$pro];
317           }
318         }
319       }
321       // We've to remove products here
322       if(preg_match("/w/",$this->getacl("productIds"))){
323         if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
324           foreach($_POST['productIds'] as $key){
325             if(isset($this->productIds[$key])){
326               unset($this->productIds[$key]);
327             }
328           }
329         }
330       }
332       // We've to add software here 
333       if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
334         if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
335           $soft = trim(get_post('newSoftwareId'));
336           if(!empty($soft) && !in_array($soft, $this->softwareIds)){
337             $this->softwareIds[] = $soft;
338           }
339         }
340       }
342       // We've to remove software Ids here
343       if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
344         if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
345           foreach($_POST['softwareIds'] as $key){
346             if(isset($this->softwareIds[$key])){
347               unset($this->softwareIds[$key]);
348             }
349           }
350         }
351       }
353       // We've to create a new license
354       if(preg_match("/w/",$this->getacl("licenses"))){
355         if(isset($_POST['addLicense'])){
356           $this->dialog = new licenseGeneric($this->config,$this->dn,array(), $this->opsiHosts);
357           $this->dialog->set_acl_base($this->config->current['BASE']);
358         }
359       }
360   
361       // Search post for image button clicks.
362       if(preg_match("/r/",$this->getacl("licenses"))){
363         foreach($_POST as $name => $value){
364           if(preg_match("/^editLicense_/",$name)){
365             $id = preg_replace("/^editLicense_(.*)_.$/","\\1",$name);
366             if(isset($this->licenses[$id])){
367               $this->dialog = new licenseGeneric($this->config,$this->dn,$this->licenses[$id], $this->opsiHosts);
368               $this->dialog->set_acl_base($this->config->current['BASE']);
369             }
370             break;
371           }
372           if(preg_match("/^removeLicense_/",$name)){
373             $id = preg_replace("/^removeLicense_(.*)_.$/","\\1",$name);
374             if(isset($this->licenses[$id])){
375               unset($this->licenses[$id]);
376             }
377             break;
378           }
379         }
380       }
381     }
382   }  
385   /* Check user input and return a list of 'invalid input' messages.
386    */
387   function check()
388   {
389     $message = plugin::check();
390     return($message);
391   }
392   
394  
395   /* Removes the object from the opsi database
396    */ 
397   function remove_from_parent()
398   {
399     $this->si->deletePool($this->orig_cn);
400     if($this->si->is_error()){
401       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
402     }else{
404       // Trigger remove signal
405       $this->handle_post_events("remove");
406     }
408     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
409   }
412   /* Saves object modifications
413    */  
414   function save()
415   {
416     plugin::save();
418     // Send modify/add events
419     $mode = "modify";
420     if($this->orig_dn == "new"){
421       $mode = "add";
422     }
424     // Create or update the pool
425     $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
426     if($this->si->is_error()){
427       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
428     }else{
429       $this->handle_post_events($mode);
430     }
432     // Create, remove or update licenses
433     $add    = array_diff_assoc($this->licenses,$this->orig_licenses);
434     $del    = array_diff_assoc($this->orig_licenses,$this->licenses);
435     $update = array_intersect($this->licenses,$this->orig_licenses);
437     // Remove licenses 
438     foreach($del as $license){
439       $this->si->removeLicenseFromPool($this->cn, $license['cn']);
440       if($this->si->is_error()){
441         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
442       }
443     }
444     
445     // Add licenses 
446     foreach($add as $license){
447       $this->si->createLicense(
448         $this->cn,  // Pool id
449         $license['cn'], 
450         $license['licenseKey'][0],
451         $license['licenseModel'],
452         $license['partner'],
453         $license['conclusionDate'],
454         $license['notificationDate'],
455         $license['description'],
456         $license['cn'],
457         $license['maximumInstallations'],
458         $license['boundToHost'],
459         $license['expirationDate']);
461       if($this->si->is_error()){
462         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
463       }
464     }
466     // Update licenses
467     foreach($update as $license){
469       // Do not save untouched licenses
470       if(!$license['MODIFIED']){
471         continue;
472       }
474       $this->si->createLicense(
475         $this->cn,  // Pool id
476         $license['cn'],
477         $license['licenseKey'][0],
478         $license['licenseModel'],
479         $license['partner'],
480         $license['conclusionDate'],
481         $license['notificationDate'],
482         $license['description'],
483         $license['cn'],
484         $license['maximumInstallations'],
485         $license['boundToHost'],
486         $license['expirationDate']);
488       if($this->si->is_error()){
489         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
490       }
491     }
493     // Log action
494     if($mode == "modify"){
495       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
496     }else{
497       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
498     }
500     return 0;
501   }
502  
503   static function plInfo()
504   {
505     return (array(
506           "plShortName"   => _("Pool generic"),
507           "plDescription" => _("License pool generic"),
508           "plSelfModify"  => FALSE,
509           "plDepends"     => array(),
510           "plPriority"    => 1,
511           "plSection"     => array("administration"),
512           "plCategory"    => array("opsi"),
513           "plProvidedAcls"=> array(
514             "cn"                => _("Name"),
515             "description" => _("Description"),
516             "productIds"  => _("Applications"),
517             "windowsSoftwareIds"  => _("Windows software IDs"),
518             "licenses"  => _("Licenses"))
519           ));
520   }
524 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
525 ?>