Code

Do not display any license stuff until the gosaSupportDaemon is configured.
[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();
83   }
85   
86   function init()
87   {
88     // Load local Boot Products 
89     $res = $this->si->get_local_products();
90     $this->availableProductIds=array();
91     if($this->si->is_error()){
92       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
93       $this->init_successfull = FALSE;
94       return(FALSE);
95     }
96     $this->availableProductIds=array_keys($res);
98     // Load opsi hosts
99     $res = $this->si->list_clients();
100     $this->opsiHosts=array();
101     if($this->si->is_error()){
102       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
103       $this->init_successfull = FALSE;
104       return(FALSE);
105     }
106     $this->opsiHosts=$res;
108     // Load Pool
109     if(!$this->initially_was_account){
110       $this->init_successfull = TRUE;
111     }else{
112       $res = $this->si->getPool($this->cn);
113       if($this->si->is_error()){    
114         $this->init_successfull = FALSE;
115         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
116         return(FALSE);
117       }else{
118         $this->data = $this->orig_data = $res;
119         $this->description = $this->data['description'][0];
121         // Load software IDs 
122         $this->softwareIds = array();
123         if(isset($this->data['softwareId'])){
124           for($i = 0; $i < $this->data['softwareId']['count']; $i++){
125             $this->softwareIds[] = $this->data['softwareId'][$i];
126           }
127         }
129         // Load product IDs 
130         $this->productIds = array();
131         if(isset($this->data['productId'])){
132           for($i = 0; $i < $this->data['productId']['count']; $i++){
133             $this->productIds[] = $this->data['productId'][$i];
134           }
135         }
137         // Load Licences 
138         $this->licenses = array();
139         if(isset($this->data['licenses'])){
140           for($i =0; $i< $this->data['licenses']['count']; $i++){
141             $license = $this->data['licenses'][$i];
143             // Do not parse invalid licenses
144             if(!is_array($license) || !isset($license['LICENSEPOOLIDS'])){
145               continue;
146             }
148             // Prepare Software License Key(s)
149             $upper = strtoupper($license['LICENSEPOOLIDS'][0]['VALUE']);
150             $license['LICENSEKEYS'] = $license['LICENSEKEYS'][0][$upper];
152             // Map license attributes to a useable format
153             $entry = array();
154             foreach($this->licenseMap as $source => $target){
155               $entry[$target] = "";
156               if(isset($license[$source])){
157                 if(count($license[$source]) >= 2){
158                   $entry[$target] = array();
159                   foreach($license[$source] as $sub){
160                     $entry[$target][] = $sub['VALUE'];
161                   }
162                 }elseif(isset($license[$source][0]['VALUE'])){
163                   $entry[$target] = $license[$source][0]['VALUE'];
164                 }
165               }
166             }
168             // Extract contract data
169             $lData= $license['LICENSECONTRACTDATA'][0];
170             foreach($this->licenseContractMap as $source => $target){
171               if(isset($lData[$source])){
172                 if(count($lData[$source]) >= 2){
173                   $entry[$target] = array();
174                   foreach($lData[$source] as $sub){
175                     $entry[$target][] = $sub['VALUE'];
176                   }
177                 }elseif(isset($lData[$source][0]['VALUE'])){
178                   $entry[$target] = $lData[$source][0]['VALUE'];
179                 }
180               }
181             }
183             // There are some multi value attributes - force them to be of type array.
184             foreach(array("boundToHost","usedByHost","licenseKey") as $attr){
185               if(!is_array($entry[$attr])){
186                 $entry[$attr] = array($entry[$attr]);
187               }
188             }
189             $entry['MODIFIED'] = FALSE;
190             $this->licenses[$entry['cn']] = $entry;
191           }
192         }
194         $this->orig_licenses = $this->licenses;
195         $this->init_successfull = TRUE;
196         return;
197       }
198     }
199   }
202   function execute()
203   {
204     // Handle initialization failures.
205     if(isset($_POST['retry_init'])) $this->init();
206     if(!$this->init_successfull){
207       $smarty = get_smarty();
208       $smarty->assign("init_successfull", $this->init_successfull);
209       return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
210     }
212     // Display dialogs! 
213     if($this->dialog instanceOf plugin){
214       $this->dialog->save_object();
215       $display = $this->dialog->execute();
216       $display.= "<p style=\"text-align:right\">";
217   
218       if($this->acl_is_writeable("licenses")){
219         $display.="<input type=submit name=\"license_finish\" style=\"width:80px\"
220           value=\"".msgPool::okButton(). "\">&nbsp;";
221       }
222       
223       $display.="<input type=submit name=\"license_cancel\" value=\"".msgPool::cancelButton()."\"></p>";
224       return($display);
225     }
227     $smarty = get_smarty();
229     // Assign ACls 
230     $plInfo = $this->plInfo();
231     foreach($plInfo['plProvidedAcls'] as $name => $desc){
232       $smarty->assign("{$name}ACL",$this->getacl($name));
233     }
234     foreach($this->attributes as $attr){
235       $smarty->assign($attr,$this->$attr);
236     }
237     $smarty->assign("init_successfull", $this->init_successfull);
238     $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
239     $smarty->assign("productIds", $this->productIds);
240     $smarty->assign("softwareIds", $this->softwareIds);
241     $smarty->assign("licenses", $this->getLicenseList());
242     $smarty->assign("initially_was_account", $this->initially_was_account);
243     return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
244   }
247   // Creates a divSelectBox and fills it with license entries
248   function getLicenseList()
249   {
250     $list = new divSelectBox("test");
251     $list->setHeight(100);
252     if($this->acl_is_readable("licenses")){
253       $editlink = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=%s'>%s</a>";
254       foreach($this->licenses as $i => $license){
255         $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
256         if(preg_match("/w/", $this->getacl("licenses"))){
257           $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
258         }
260         $maxInst = "";
261         if($license['maximumInstallations'] == 0){
262           $maxInst = _("unlimited");
263         }else{
264           $maxInst = $license['maximumInstallations'];
265         }
268         $map = array(
269           "VOLUME" => sprintf(_("Volume license (#%s)"), $maxInst),
270           "OEM"=>_("OEM"),
271           "RETAIL"=>_("Retail"),
272           "CONCURRENT"=>_("Concurrent"));
274         $f1 = array("string" => sprintf($editlink,$i,$license['cn']));
275         $f2 = array("string" => sprintf($editlink,$i,$map[$license['licenseModel']]));
276         $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
277         $list->addEntry(array($f1,$f2,$f6));
278       }
279     }
280     return($list->DrawList());
281   }
282  
283  
284   /* Save HTML inputs
285    */
286   function save_object()
287   {
288     // Allow license edit via href links
289     if(isset($_GET['act']) && $_GET['act'] == 'edit' && isset($_GET['id'])){
290       $id = trim($_GET['id']);
291       if(isset($this->licenses[$id])){
292         $this->dialog = new licenseGeneric($this->config,$this->dn,$this->licenses[$id], $this->opsiHosts);
293         $this->dialog->set_acl_base($this->config->current['BASE']);
294       }
295     }
296   
297     // Close license edit dialogs.
298     if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
299       $this->dialog = NULL;
300       return;
301     }
303     // Save license modifications
304     if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
305       $this->dialog->save_object();
306       $msgs = $this->dialog->check();
307       if(count($msgs)){
308         msg_dialog::displayChecks($msgs);
309       }else{
310         $attrs = $this->dialog->save();
311         $attrs['MODIFIED'] = TRUE;
312         $this->licenses[$attrs['cn']] = $attrs;
313         $this->dialog = NULL;
314       }
315       return;
316     }
318     if(isset($_POST['opsiLicensePoolPosted'])){
319       plugin::save_object();  
321       // Restore license cn, to avoid creating a copy...
322       if($this->initially_was_account) $this->cn = $this->orig_cn;
324       // We've to add prodcuts here 
325       if(preg_match("/w/",$this->getacl("productIds"))){
326         if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
327           $pro = get_post('availableProduct');
328           if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
329             $this->productIds[] =$this->availableProductIds[$pro];
330           }
331         }
332       }
334       // We've to remove products here
335       if(preg_match("/w/",$this->getacl("productIds"))){
336         if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
337           foreach($_POST['productIds'] as $key){
338             if(isset($this->productIds[$key])){
339               unset($this->productIds[$key]);
340             }
341           }
342         }
343       }
345       // We've to add software here 
346       if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
347         if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
348           $soft = trim(get_post('newSoftwareId'));
349           if(!empty($soft) && !in_array($soft, $this->softwareIds)){
350             $this->softwareIds[] = $soft;
351           }
352         }
353       }
355       // We've to remove software Ids here
356       if(preg_match("/w/",$this->getacl("windowsSoftwareIds"))){
357         if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
358           foreach($_POST['softwareIds'] as $key){
359             if(isset($this->softwareIds[$key])){
360               unset($this->softwareIds[$key]);
361             }
362           }
363         }
364       }
366       // We've to create a new license
367       if(preg_match("/w/",$this->getacl("licenses"))){
368         if(isset($_POST['addLicense'])){
369           $this->dialog = new licenseGeneric($this->config,$this->dn,array(), $this->opsiHosts);
370           $this->dialog->set_acl_base($this->config->current['BASE']);
371         }
372       }
374       // Search post for image button clicks.
375       if(preg_match("/r/",$this->getacl("licenses"))){
376         foreach($_POST as $name => $value){
377           if(preg_match("/^editLicense_/",$name)){
378             $id = preg_replace("/^editLicense_(.*)_.$/","\\1",$name);
379             if(isset($this->licenses[$id])){
380               $this->dialog = new licenseGeneric($this->config,$this->dn,$this->licenses[$id], $this->opsiHosts);
381               $this->dialog->set_acl_base($this->config->current['BASE']);
382             }
383             break;
384           }
385           if(preg_match("/^removeLicense_/",$name)){
386             $id = preg_replace("/^removeLicense_(.*)_.$/","\\1",$name);
387             if(isset($this->licenses[$id])){
388               unset($this->licenses[$id]);
389             }
390             break;
391           }
392         }
393       }
394     }
395   }  
398   /* Check user input and return a list of 'invalid input' messages.
399    */
400   function check()
401   {
402     $message = plugin::check();
403     return($message);
404   }
405   
407  
408   /* Removes the object from the opsi database
409    */ 
410   function remove_from_parent()
411   {
412     $this->si->deletePool($this->orig_cn);
413     if($this->si->is_error()){
414       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
415     }else{
417       // Trigger remove signal
418       $this->handle_post_events("remove");
419     }
421     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
422   }
425   /* Saves object modifications
426    */  
427   function save()
428   {
429     plugin::save();
431     // Send modify/add events
432     $mode = "modify";
433     if($this->orig_dn == "new"){
434       $mode = "add";
435     }
437     // Create or update the pool
438     $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
439     if($this->si->is_error()){
440       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
441     }else{
442       $this->handle_post_events($mode);
443     }
445     // Create, remove or update licenses
446     $add    = array_diff_assoc($this->licenses,$this->orig_licenses);
447     $del    = array_diff_assoc($this->orig_licenses,$this->licenses);
448     $update = array_intersect($this->licenses,$this->orig_licenses);
450     // Remove licenses 
451     foreach($del as $license){
452       $this->si->removeLicenseFromPool($this->cn, $license['cn']);
453       if($this->si->is_error()){
454         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
455       }
456     }
457     
458     // Add licenses 
459     foreach($add as $license){
460       $this->si->createLicense(
461         $this->cn,  // Pool id
462         $license['cn'], 
463         $license['licenseKey'][0],
464         $license['licenseModel'],
465         $license['partner'],
466         $license['conclusionDate'],
467         $license['notificationDate'],
468         $license['description'],
469         $license['cn'],
470         $license['maximumInstallations'],
471         $license['boundToHost'],
472         $license['expirationDate']);
474       if($this->si->is_error()){
475         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
476       }
477     }
479     // Update licenses
480     foreach($update as $license){
482       // Do not save untouched licenses
483       if(!$license['MODIFIED']){
484         continue;
485       }
487       $this->si->createLicense(
488         $this->cn,  // Pool id
489         $license['cn'],
490         $license['licenseKey'][0],
491         $license['licenseModel'],
492         $license['partner'],
493         $license['conclusionDate'],
494         $license['notificationDate'],
495         $license['description'],
496         $license['cn'],
497         $license['maximumInstallations'],
498         $license['boundToHost'],
499         $license['expirationDate']);
501       if($this->si->is_error()){
502         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
503       }
504     }
506     // Log action
507     if($mode == "modify"){
508       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
509     }else{
510       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
511     }
513     return 0;
514   }
515  
516   static function plInfo()
517   {
518     return (array(
519           "plShortName"   => _("Pool generic"),
520           "plDescription" => _("License pool generic"),
521           "plSelfModify"  => FALSE,
522           "plDepends"     => array(),
523           "plPriority"    => 7,
524           "plSection"     => array("administration"),
525           "plCategory"    => array("opsi"),
526           "plProvidedAcls"=> array(
527             "cn"                => _("Name"),
528             "description" => _("Description"),
529             "productIds"  => _("Applications"),
530             "windowsSoftwareIds"  => _("Windows software IDs"),
531             "licenses"  => _("Licenses"))
532           ));
533   }
537 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
538 ?>