Code

Enabled license listing
[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   function __construct(&$config,$dn)
43   {
44     $this->config = $config;
45     $this->dn = $this->orig_dn = $dn;
47     $this->si = new opsiLicenceHandler($this->config);
49     $this->is_account=TRUE;
50     if($this->dn == "new"){
51       $this->initially_was_account = FALSE;
52     }else{
53       $this->initially_was_account = TRUE;
54       $this->cn = $this->orig_cn = preg_replace("/^opsi:cn=([^,]*),.*$/","\\1",$dn);
55     }
57     // Extract pool name out of the fake dn.
58     $this->init();
59   }
61   
62   function init()
63   {
64     // Load local Boot Products 
65     $res = $this->si->get_local_products();
66     if($this->si->is_error()){
67       $this->init_successfull = FALSE;
68       return(FALSE);
69     }
70     $this->availableProductIds=array_keys($res);
72     // Load Pool
73     if(!$this->initially_was_account){
74       $this->init_successfull = TRUE;
75     }else{
77       $res = $this->si->getPool($this->cn);
78       if($this->si->is_error()){    
79         $this->init_successfull = FALSE;
80         return(FALSE);
81       }else{
82         $this->data = $this->orig_data = $res;
83         $this->description = $this->data['description'][0];
85         // Load software IDs 
86         $this->softwareIds = array();
87         if(isset($this->data['softwareId'])){
88           for($i = 0; $i < $this->data['softwareId']['count']; $i++){
89             $this->softwareIds[] = $this->data['softwareId'][$i];
90           }
91         }
93         // Load product IDs 
94         $this->productIds = array();
95         if(isset($this->data['productId'])){
96           for($i = 0; $i < $this->data['productId']['count']; $i++){
97             $this->productIds[] = $this->data['productId'][$i];
98           }
99         }
101         // Load Licences 
102         $this->licenses = array();
103         if(isset($this->data['licenses'])){
104           $this->licenses = $this->data['licenses'];
105         }
106         $this->init_successfull = TRUE;
107         return;
108       }
109     }
110   }
113   function execute()
114   {
115     // Handle initialization failures.
116     if(isset($_POST['retry_init'])) $this->init();
117     if(!$this->init_successfull){
118       $smarty = get_smarty();
119       $smarty->assign("init_successfull", $this->init_successfull);
120       return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
121     }
123     $smarty = get_smarty();
125     // Assign ACls 
126     $plInfo = $this->plInfo();
127     foreach($plInfo['plProvidedAcls'] as $name => $desc){
128       $smarty->assign("{$name}ACL",$this->getacl($name));
129     }
130     foreach($this->attributes as $attr){
131       $smarty->assign($attr,$this->$attr);
132     }
133     $smarty->assign("init_successfull", $this->init_successfull);
134     $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
135     $smarty->assign("productIds", $this->productIds);
136     $smarty->assign("softwareIds", $this->softwareIds);
137     $smarty->assign("licenses", $this->getLicenseList());
138     $smarty->assign("initially_was_account", $this->initially_was_account);
139     return($smarty->fetch(get_template_path('licensePoolGeneric.tpl',TRUE,dirname(__FILE__))));
140   }
143   function getLicenseList()
144   {
145     $list = new divSelectBox("test");
146     $list->setHeight(100);
147     for($i=0 ; $i<$this->licenses['count'] ; $i++){
148     
149       $license = $this->licenses[$i];
151       $keys = "";
152       foreach($license['LICENSEKEYS'] as $key_by_pool){
153         foreach($key_by_pool as $key){
154           $keys .= $key[0]['VALUE'].", ";
155         }
156       }
158       $link = "<input type='image' class='center' src='images/lists/edit.png' name='editLicense_{$i}'>";
159       $link.= "<input type='image' class='center' src='images/lists/trash.png' name='removeLicense_{$i}'>";
161       $f1 = array("string" => $license['SOFTWARELICENSEID'][0]['VALUE'], "attach" => "");
162       $f2 = array("string" => $license['LICENSETYPE'][0]['VALUE'], "attach" => "");
163       $f3 = array("string" => $license['EXPIRATIONDATE'][0]['VALUE'], "attach" => "");
164       $f4 = array("string" => $license['MAXINSTALLATIONS'][0]['VALUE'], "attach" => "");
165       $f5 = array("string" => rtrim($keys,", "));
166       $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'");
168       $list->addEntry(array($f1,$f2,$f3,$f4,$f5,$f6));
169     }
170     return($list->DrawList());
171   }
172  
173  
174   /* Save HTML inputs
175    */
176   function save_object()
177   {
178     if(isset($_POST['opsiLicensesPosted'])){
179       plugin::save_object();  
181       // Restore license cn, to avoid creating a copy...
182       if($this->initially_was_account) $this->cn = $this->orig_cn;
184       // We've to add prodcuts here 
185       if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
186         $pro = get_post('availableProduct');
187         if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
188           $this->productIds[] =$this->availableProductIds[$pro];
189         }
190       }
192       // We've to remove products here
193       if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
194         foreach($_POST['productIds'] as $key){
195           if(isset($this->productIds[$key])){
196             unset($this->productIds[$key]);
197           }
198         }
199       }
201       // We've to add software here 
202       if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
203         $soft = trim(get_post('newSoftwareId'));
204         if(!empty($soft) && !in_array($soft, $this->softwareIds)){
205           $this->softwareIds[] = $soft;
206         }
207       }
209       // We've to remove software Ids here
210       if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
211         foreach($_POST['softwareIds'] as $key){
212           if(isset($this->softwareIds[$key])){
213             unset($this->softwareIds[$key]);
214           }
215         }
216       }
217     }
218   }  
221   /* Check user input and return a list of 'invalid input' messages.
222    */
223   function check()
224   {
225     $message = plugin::check();
226     return($message);
227   }
228   
230  
231   /* Removes the object from the opsi database
232    */ 
233   function remove_from_parent()
234   {
235     $this->si->deletePool($this->orig_cn);
236     if($this->si->is_error()){
237       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
238     }else{
240       // Trigger remove signal
241       $this->handle_post_events("remove");
242     }
244     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
245   }
248   /* Saves object modifications
249    */  
250   function save()
251   {
252     plugin::save();
254     // Send modify/add events
255     $mode = "modify";
256     if($this->orig_dn == "new"){
257       $mode = "add";
258     }
260     $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
261     if($this->si->is_error()){
262       msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
263     }else{
264       $this->handle_post_events($mode);
265     }
267     // Log action
268     if($mode == "modify"){
269       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
270     }else{
271       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
272     }
274     return 0;
275   }
276  
277   static function plInfo()
278   {
279     return (array(
280           "plShortName"   => _("Generic"),
281           "plDescription" => _("License generic"),
282           "plSelfModify"  => FALSE,
283           "plDepends"     => array(),
284           "plPriority"    => 1,
285           "plSection"     => array("administration"),
286           "plCategory"    => array("opsi"),
287           "plProvidedAcls"=> array(
288             "cn"                => _("Name"),
289             "description" => _("Description"))
290           ));
291   }
295 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
296 ?>