Code

Removed usePrototype flag, its activated always now.
[gosa.git] / gosa-plugins / opsi / admin / opsiLicenses / class_licenseUsageByHost.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 licenseUsageByHost extends plugin 
25 {
27   var $cn = "";
28   var $licenseUses = array();
29   var $reservedLicenses = array();
30   var $init_successfull = FALSE;
32   var $availableLicenses = array();
34   function __construct(&$config,$dn)
35   {
36     $this->config = $config;
37     $this->dn = $this->orig_dn = $dn;
38     $this->si = new opsiLicenceHandler($this->config);
40     $this->is_account=TRUE;
41     if($this->dn == "new"){
42       $this->initially_was_account = FALSE;
43     }else{
44       $this->initially_was_account = TRUE;
45       $this->cn = $this->orig_cn = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$dn);
46     }
48     // Extract pool name out of the fake dn.
49     $this->init();
51     // Prepare lists
52     $this->reservedList = new sortableListing();
53     $this->reservedList->setDeleteable(true);
54     $this->reservedList->setEditable(false);
55     $this->reservedList->setWidth("100%");
56     $this->reservedList->setHeight("220px");
57     $this->reservedList->setColspecs(array('200px','*'));
58     $this->reservedList->setHeader(array(_("Pool"),_("License ID")));
59     $this->reservedList->setDefaultSortColumn(1);
60     $this->reservedList->setAcl('rwcdm'); // All ACLs, we filter on our own here.
62     $this->usedList = new sortableListing();
63     $this->usedList->setDeleteable(false);
64     $this->usedList->setEditable(false);
65     $this->usedList->setWidth("100%");
66     $this->usedList->setHeight("220px");
67     $this->usedList->setColspecs(array('200px','*'));
68     $this->usedList->setHeader(array(_("Key"),_("Pool"),_("License ID")));
69     $this->usedList->setDefaultSortColumn(1);
70     $this->usedList->setAcl('rwcdm'); // All ACLs, we filter on our own here.
71   }
73   
74   function init()
75   {
76     $this->licenseUses = array();
77     $this->reservedLicenses = array();
78     $this->init_reservedLicenses = array();
79     if(!$this->initially_was_account){
80       $this->init_successfull = TRUE;
81     }else{
83       // Get license usage  
84       $res = $this->si->getLicenseUsage($this->cn);
85       if($this->si->is_error()){
86         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
87         $this->init_successfull = FALSE;
88         return;
89       }
90       $this->licenseUses = $res;
92       // Get reservations
93       $res = $this->si->getReservedLicensesForHost($this->cn);
94       if($this->si->is_error()){
95         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
96         $this->init_successfull = FALSE;
97         return;
98       }
99       foreach($res as $pool){
100         $l = $pool['softwareLicenseId'][0];
101         $this->reservedLicenses[$l] = $l;
102       }
103       $this->init_reservedLicenses = $this->reservedLicenses;
105       // Get list of license pools 
106       $res = $this->si->listLicenses();
107       if($this->si->is_error()){
108         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
109         $this->init_successfull = FALSE;
110         return;
111       }
112       $this->availableLicenses = array();
113       foreach($res as $license){
114         if(!isset($license['licensePoolId'])) $license['licensePoolId'][0] = '-';
115         $this->availableLicenses[$license['softwareLicenseId'][0]] = $license;
116       }
118       $this->init_successfull = TRUE;
119     }
120   }
123   function execute()
124   {
125     plugin::execute();
127     // Handle initialization failures.
128     if(isset($_POST['retry_init'])) $this->init();
129     if(!$this->init_successfull){
130       $smarty = get_smarty();
131       $smarty->assign("init_successfull", $this->init_successfull);
132       return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
133     }
134    
135     // Create usage list
136     $data = array();
137     $this->usedList->setAcl($this->getacl('hostId'));
138     foreach($this->licenseUses as $license){
139         $data[] = array('data' => array(
140                     $license['licenseKey'][0],
141                     $license['licensePoolId'][0],
142                     $license['softwareLicenseId'][0]));
143     } 
144     $this->usedList->setListData($data,$data);
145     $this->usedList->update();
147     // Create reserved list
148     $data = $lData = array();
149     $this->reservedList->setAcl($this->getacl('boundToHost'));
150     foreach($this->reservedLicenses as $key => $license){
151         $l = $this->availableLicenses[$key];
152         $data[$key] = $key;
153         $lData[$key] = array('data' => array(
154                     $l['softwareLicenseId'][0],
155                     $l['licensePoolId'][0]));
156     } 
157     $this->reservedList->setListData($data,$lData);
158     $this->reservedList->update();
159     
160     $smarty = get_smarty();
162     // Assign ACls 
163     $plInfo = $this->plInfo();
164     foreach($plInfo['plProvidedAcls'] as $name => $desc){
165       $smarty->assign("{$name}ACL",$this->getacl($name));
166     }
167     foreach($this->attributes as $attr){
168       $smarty->assign($attr,$this->$attr);
169     }
171     // build up a available licenses list. 
172     $licenses = array();
173     foreach($this->availableLicenses as $key => $license){
174       if(!in_array($license['softwareLicenseId'][0], $this->reservedLicenses)){
175         $licenses[$key] = $license['softwareLicenseId'][0]." [".$license['licensePoolId'][0]."]";
176       }
177     }
179     $smarty->assign("licenseUses", $this->usedList->render());
180     $smarty->assign("licenseReserved", $this->reservedList->render());
181     $smarty->assign("init_successfull", $this->init_successfull);
182     $smarty->assign("availableLicenses", $licenses);
183     $smarty->assign("initially_was_account", $this->initially_was_account);
184     return($smarty->fetch(get_template_path('licenseUsageByHost.tpl',TRUE,dirname(__FILE__))));
185   }
187  
188   /* Save HTML inputs
189    */
190   function save_object()
191   {
192     if(isset($_POST['opsiLicenseUsagePosted'])){
193       plugin::save_object();  
194   
195       if(preg_match("/w/i",$this->getacl('boundToHost'))){
197         $this->reservedList->save_object();
198         $action = $this->reservedList->getAction();
199         if($action['action'] == "delete") {
200             $id = $this->reservedList->getKey($action['targets'][0]);
201             if(isset($this->reservedLicenses[$id])) {
202                 unset($this->reservedLicenses[$id]);
203             }
204         }
206         // Check if we've to add reservations
207         if(isset($_POST['availableLicense']) && isset($_POST['addReservation'])){
208           $id = get_post('availableLicense');
209           if(isset($this->availableLicenses[$id]) && !in_array($this->availableLicenses[$id],$this->reservedLicenses)){
210             $this->reservedLicenses[$id] =  $this->availableLicenses[$id]['softwareLicenseId'][0];
211           }
212         }
213       }
214     }
215   }  
218   /* Check user input and return a list of 'invalid input' messages.
219    */
220   function check()
221   {
222     $message = plugin::check();
223     return($message);
224   }
225   
227   function save()
228   {
229     $del = array_diff($this->init_reservedLicenses, $this->reservedLicenses);
230     $add = array_diff($this->reservedLicenses, $this->init_reservedLicenses);
232     foreach($del as $license){
233       $this->si->removeLicenseReservationFromHost($license, $this->cn);
234       if($this->si->is_error()){
235         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
236       }
237     }
239     foreach($add as $license){
240       $this->si->reserverLicenseForHost($license, $this->cn);
241       if($this->si->is_error()){
242         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
243       }
244     }
245   }
248   function remove_from_parent(){ }
250  
251   static function plInfo()
252   {
253     return (array(
254           "plShortName"   => _("Usage by host"),
255           "plDescription" => _("License usage by host"),
256           "plSelfModify"  => FALSE,
257           "plDepends"     => array(),
258           "plPriority"    => 13,
259           "plSection"     => array("administration"),
260           "plCategory"    => array("opsi"),
261           "plProvidedAcls"=> array(
262             "hostId" => _("Used by"),
263             "boundToHost" => _("License reservation"))
264           ));
265   }
269 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
270 ?>