Code

Updated license management
[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();
38   var $availableProductIds = array();
39   var $attributes =array("cn","description");
40   var $si = NULL;
42   var $opsiHosts = array();
44   function __construct(&$config,$dn)
45   {
46     $this->config = $config;
47     $this->dn = $this->orig_dn = $dn;
49     $this->si = new opsiLicenceHandler($this->config);
51     $this->is_account=TRUE;
52     if($this->dn == "new"){
53       $this->initially_was_account = FALSE;
54     }else{
55       $this->initially_was_account = TRUE;
56       $this->cn = $this->orig_cn = preg_replace("/^opsi:cn=([^,]*),.*$/","\\1",$dn);
57     }
59     // Extract pool name out of the fake dn.
60     $this->init();
61   }
63   
64   function init()
65   {
66     // Load local Boot Products 
67     $res = $this->si->get_local_products();
68     $this->availableProductIds=array();
69     if($this->si->is_error()){
70       $this->init_successfull = FALSE;
71       return(FALSE);
72     }
73     $this->availableProductIds=array_keys($res);
75     // Load opsi hosts
76     $res = $this->si->list_clients();
77     $this->opsiHosts=array();
78     if($this->si->is_error()){
79       $this->init_successfull = FALSE;
80       return(FALSE);
81     }
82     $this->opsiHosts=$res;
84     // Load Pool
85     if(!$this->initially_was_account){
86       $this->init_successfull = TRUE;
87     }else{
89       $res = $this->si->getPool($this->cn);
90       if($this->si->is_error()){    
91         $this->init_successfull = FALSE;
92         return(FALSE);
93       }else{
94         $this->data = $this->orig_data = $res;
95         $this->description = $this->data['description'][0];
97         // Load software IDs 
98         $this->softwareIds = array();
99         if(isset($this->data['softwareId'])){
100           for($i = 0; $i < $this->data['softwareId']['count']; $i++){
101             $this->softwareIds[] = $this->data['softwareId'][$i];
102           }
103         }
105         // Load product IDs 
106         $this->productIds = array();
107         if(isset($this->data['productId'])){
108           for($i = 0; $i < $this->data['productId']['count']; $i++){
109             $this->productIds[] = $this->data['productId'][$i];
110           }
111         }
113         // Load Licences 
114         $this->licenses = array();
115         if(isset($this->data['licenses'])){
116           $this->licenses = $this->data['licenses'];
117           unset($this->licenses['count']);
118         }
119         $this->init_successfull = TRUE;
120         return;
121       }
122     }
123   }
126   function execute()
127   {
128     // Handle initialization failures.
129     if(isset($_POST['retry_init'])) $this->init();
130     if(!$this->init_successfull){
131       $smarty = get_smarty();
132       $smarty->assign("init_successfull", $this->init_successfull);
133       return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
134     }
136     // Display dialogs! 
137     if($this->dialog instanceOf plugin){
138       $this->dialog->save_object();
139       $display = $this->dialog->execute();
140       $display.= 
141         "<p style=\"text-align:right\">
142         <input type=submit name=\"license_finish\" style=\"width:80px\"
143         value=\"".msgPool::okButton(). "\">&nbsp;
144       <input type=submit name=\"license_cancel\"
145         value=\"".msgPool::cancelButton()."\">
146         </p>";
147       return($display);
148     }
150     $smarty = get_smarty();
152     // Assign ACls 
153     $plInfo = $this->plInfo();
154     foreach($plInfo['plProvidedAcls'] as $name => $desc){
155       $smarty->assign("{$name}ACL",$this->getacl($name));
156     }
157     foreach($this->attributes as $attr){
158       $smarty->assign($attr,$this->$attr);
159     }
160     $smarty->assign("init_successfull", $this->init_successfull);
161     $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
162     $smarty->assign("productIds", $this->productIds);
163     $smarty->assign("softwareIds", $this->softwareIds);
164     $smarty->assign("licenses", $this->getLicenseList());
165     $smarty->assign("initially_was_account", $this->initially_was_account);
166     return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
167   }
170   function getLicenseList()
171   {
172     $list = new divSelectBox("test");
173     $list->setHeight(100);
174     foreach($this->licenses as $i => $license){
175       $keys = "";
176       foreach($license['LICENSEKEYS'] as $key_by_pool){
177         foreach($key_by_pool as $key){
178           $keys .= $key[0]['VALUE'].", ";
179         }
180       }
182       $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
183       $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
185       $f1 = array("string" => $license['SOFTWARELICENSEID'][0]['VALUE'], "attach" => "");
186       $f2 = array("string" => $license['LICENSETYPE'][0]['VALUE'], "attach" => "");
187       $f3 = array("string" => $license['EXPIRATIONDATE'][0]['VALUE'], "attach" => "");
188       $f4 = array("string" => $license['MAXINSTALLATIONS'][0]['VALUE'], "attach" => "");
189       $f5 = array("string" => rtrim($keys,", "));
190       $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
192       $list->addEntry(array($f1,$f2,$f3,$f4,$f5,$f6));
193     }
194     return($list->DrawList());
195   }
196  
197  
198   /* Save HTML inputs
199    */
200   function save_object()
201   {
202     // Close license edit dialogs.
203     if($this->dialog instanceOf plugin && isset($_POST['license_cancel'])){
204       $this->dialog = NULL;
205       return;
206     }
208     // Save license modifications
209     if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){
210       echo "Save";
211       $this->dialog = NULL;
212       return;
213     }
215     if(isset($_POST['opsiLicensePoolPosted'])){
216       plugin::save_object();  
218       // Restore license cn, to avoid creating a copy...
219       if($this->initially_was_account) $this->cn = $this->orig_cn;
221       // We've to add prodcuts here 
222       if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
223         $pro = get_post('availableProduct');
224         if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
225           $this->productIds[] =$this->availableProductIds[$pro];
226         }
227       }
229       // We've to remove products here
230       if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
231         foreach($_POST['productIds'] as $key){
232           if(isset($this->productIds[$key])){
233             unset($this->productIds[$key]);
234           }
235         }
236       }
238       // We've to add software here 
239       if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
240         $soft = trim(get_post('newSoftwareId'));
241         if(!empty($soft) && !in_array($soft, $this->softwareIds)){
242           $this->softwareIds[] = $soft;
243         }
244       }
246       // We've to remove software Ids here
247       if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
248         foreach($_POST['softwareIds'] as $key){
249           if(isset($this->softwareIds[$key])){
250             unset($this->softwareIds[$key]);
251           }
252         }
253       }
255       // Search post for image button clicks.
256       foreach($_POST as $name => $value){
257         if(preg_match("/^editLicense_/",$name)){
258           $id = preg_replace("/^editLicense_([^_]*)_.*$/","\\1",$name);
259           if(isset($this->licenses[$id])){
260             $this->dialog = new licenseGeneric($this->config,$this->licenses[$id], $this->opsiHosts);
261           }
262           break;
263         }
264         if(preg_match("/^removeLicense_/",$name)){
265           $id = preg_replace("/^removeLicense_([^_]*)_.*$/","\\1",$name);
266           if(isset($this->licenses[$id])){
267             unset($this->licenses[$id]);
268           }
269           break;
270         }
271       }
272     }
273   }  
276   /* Check user input and return a list of 'invalid input' messages.
277    */
278   function check()
279   {
280     $message = plugin::check();
281     return($message);
282   }
283   
285  
286   /* Removes the object from the opsi database
287    */ 
288   function remove_from_parent()
289   {
290     $this->si->deletePool($this->orig_cn);
291     if($this->si->is_error()){
292       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
293     }else{
295       // Trigger remove signal
296       $this->handle_post_events("remove");
297     }
299     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
300   }
303   /* Saves object modifications
304    */  
305   function save()
306   {
307     plugin::save();
309     // Send modify/add events
310     $mode = "modify";
311     if($this->orig_dn == "new"){
312       $mode = "add";
313     }
315     $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
316     if($this->si->is_error()){
317       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
318     }else{
319       $this->handle_post_events($mode);
320     }
322     // Log action
323     if($mode == "modify"){
324       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
325     }else{
326       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
327     }
329     return 0;
330   }
331  
332   static function plInfo()
333   {
334     return (array(
335           "plShortName"   => _("Generic"),
336           "plDescription" => _("License generic"),
337           "plSelfModify"  => FALSE,
338           "plDepends"     => array(),
339           "plPriority"    => 1,
340           "plSection"     => array("administration"),
341           "plCategory"    => array("opsi"),
342           "plProvidedAcls"=> array(
343             "cn"                => _("Name"),
344             "description" => _("Description"))
345           ));
346   }
350 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
351 ?>