Code

Backport from trunk
[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->initTime = microtime(TRUE);
37         $this->config = $config;
38         $this->dn = $this->orig_dn = $dn;
39         $this->si = new opsiLicenceHandler($this->config);
41         $this->is_account=TRUE;
42         if($this->dn == "new"){
43             $this->initially_was_account = FALSE;
44         }else{
45             $this->initially_was_account = TRUE;
46             $this->cn = $this->orig_cn = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$dn);
47         }
49         // Extract pool name out of the fake dn.
50         $this->init();
52         // Prepare lists
53         $this->reservedList = new sortableListing();
54         $this->reservedList->setDeleteable(true);
55         $this->reservedList->setEditable(false);
56         $this->reservedList->setWidth("100%");
57         $this->reservedList->setHeight("220px");
58         $this->reservedList->setColspecs(array('200px','*'));
59         $this->reservedList->setHeader(array(_("Pool"),_("License ID")));
60         $this->reservedList->setDefaultSortColumn(1);
61         $this->reservedList->setAcl('rwcdm'); // All ACLs, we filter on our own here.
63         $this->usedList = new sortableListing();
64         $this->usedList->setDeleteable(false);
65         $this->usedList->setEditable(false);
66         $this->usedList->setWidth("100%");
67         $this->usedList->setHeight("220px");
68         $this->usedList->setColspecs(array('200px','*'));
69         $this->usedList->setHeader(array(_("Key"),_("Pool"),_("License ID")));
70         $this->usedList->setDefaultSortColumn(1);
71         $this->usedList->setAcl('rwcdm'); // All ACLs, we filter on our own here.
73         // Create statistic table entry
74         stats::log('plugin', $class = get_class($this), $category = array($this->acl_category),  $action = 'open',
75                 $amount = 1, $duration = (microtime(TRUE) - $this->initTime));
76     }
79     function init()
80     {
81         $this->licenseUses = array();
82         $this->reservedLicenses = array();
83         $this->init_reservedLicenses = array();
84         if(!$this->initially_was_account){
85             $this->init_successfull = TRUE;
86         }else{
88             // Get license usage  
89             $res = $this->si->getLicenseUsage($this->cn);
90             if($this->si->is_error()){
91                 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
92                 $this->init_successfull = FALSE;
93                 return;
94             }
95             $this->licenseUses = $res;
97             // Get reservations
98             $res = $this->si->getReservedLicensesForHost($this->cn);
99             if($this->si->is_error()){
100                 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
101                 $this->init_successfull = FALSE;
102                 return;
103             }
104             foreach($res as $pool){
105                 $l = $pool['softwareLicenseId'][0];
106                 $this->reservedLicenses[$l] = $l;
107             }
108             $this->init_reservedLicenses = $this->reservedLicenses;
110             // Get list of license pools 
111             $res = $this->si->listLicenses();
112             if($this->si->is_error()){
113                 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
114                 $this->init_successfull = FALSE;
115                 return;
116             }
117             $this->availableLicenses = array();
118             foreach($res as $license){
119                 if(!isset($license['licensePoolId'])) $license['licensePoolId'][0] = '-';
120                 $this->availableLicenses[$license['softwareLicenseId'][0]] = $license;
121             }
123             $this->init_successfull = TRUE;
124         }
125     }
128     function execute()
129     {
130         plugin::execute();
132         // Handle initialization failures.
133         if(isset($_POST['retry_init'])) $this->init();
134         if(!$this->init_successfull){
135             $smarty = get_smarty();
136             $smarty->assign("init_successfull", $this->init_successfull);
137             return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
138         }
140         // Create usage list
141         $data = array();
142         $this->usedList->setAcl($this->getacl('hostId'));
143         foreach($this->licenseUses as $license){
144             $data[] = array('data' => array(
145                         $license['licenseKey'][0],
146                         $license['licensePoolId'][0],
147                         $license['softwareLicenseId'][0]));
148         } 
149         $this->usedList->setListData($data,$data);
150         $this->usedList->update();
152         // Create reserved list
153         $data = $lData = array();
154         $this->reservedList->setAcl($this->getacl('boundToHost'));
155         foreach($this->reservedLicenses as $key => $license){
156             $l = $this->availableLicenses[$key];
157             $data[$key] = $key;
158             $lData[$key] = array('data' => array(
159                         $l['softwareLicenseId'][0],
160                         $l['licensePoolId'][0]));
161         } 
162         $this->reservedList->setListData($data,$lData);
163         $this->reservedList->update();
165         $smarty = get_smarty();
167         // Assign ACls 
168         $plInfo = $this->plInfo();
169         foreach($plInfo['plProvidedAcls'] as $name => $desc){
170             $smarty->assign("{$name}ACL",$this->getacl($name));
171         }
172         foreach($this->attributes as $attr){
173             $smarty->assign($attr,set_post($this->$attr));
174         }
176         // build up a available licenses list. 
177         $licenses = array();
178         foreach($this->availableLicenses as $key => $license){
179             if(!in_array_strict($license['softwareLicenseId'][0], $this->reservedLicenses)){
180                 $licenses[$key] = $license['softwareLicenseId'][0]." [".$license['licensePoolId'][0]."]";
181             }
182         }
184         $smarty->assign("licenseUses", $this->usedList->render());
185         $smarty->assign("licenseReserved", $this->reservedList->render());
186         $smarty->assign("init_successfull", $this->init_successfull);
187         $smarty->assign("availableLicenses", $licenses);
188         $smarty->assign("initially_was_account", $this->initially_was_account);
189         return($smarty->fetch(get_template_path('licenseUsageByHost.tpl',TRUE,dirname(__FILE__))));
190     }
193     /* Save HTML inputs
194      */
195     function save_object()
196     {
197         if(isset($_POST['opsiLicenseUsagePosted'])){
198             plugin::save_object();  
200             if(preg_match("/w/i",$this->getacl('boundToHost'))){
202                 $this->reservedList->save_object();
203                 $action = $this->reservedList->getAction();
204                 if($action['action'] == "delete") {
205                     $id = $this->reservedList->getKey($action['targets'][0]);
206                     if(isset($this->reservedLicenses[$id])) {
207                         unset($this->reservedLicenses[$id]);
208                     }
209                 }
211                 // Check if we've to add reservations
212                 if(isset($_POST['availableLicense']) && isset($_POST['addReservation'])){
213                     $id = get_post('availableLicense');
214                     if(isset($this->availableLicenses[$id]) && !in_array_strict($this->availableLicenses[$id],$this->reservedLicenses)){
215                         $this->reservedLicenses[$id] =  $this->availableLicenses[$id]['softwareLicenseId'][0];
216                     }
217                 }
218             }
219         }
220     }  
223     /* Check user input and return a list of 'invalid input' messages.
224      */
225     function check()
226     {
227         $message = plugin::check();
228         return($message);
229     }
232     function save()
233     {
234         $del = array_diff($this->init_reservedLicenses, $this->reservedLicenses);
235         $add = array_diff($this->reservedLicenses, $this->init_reservedLicenses);
237         foreach($del as $license){
238             $this->si->removeLicenseReservationFromHost($license, $this->cn);
239             if($this->si->is_error()){
240                 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
241             }
242         }
244         foreach($add as $license){
245             $this->si->reserverLicenseForHost($license, $this->cn);
246             if($this->si->is_error()){
247                 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
248             }
249         }
250     }
253     function remove_from_parent(){ }
256     static function plInfo()
257     {
258         return (array(
259                     "plShortName"   => _("Usage by host"),
260                     "plDescription" => _("License usage by host"),
261                     "plSelfModify"  => FALSE,
262                     "plDepends"     => array(),
263                     "plPriority"    => 13,
264                     "plSection"     => array("administration"),
265                     "plCategory"    => array("opsi"),
266                     "plProvidedAcls"=> array(
267                         "hostId" => _("Used by"),
268                         "boundToHost" => _("License reservation"))
269                     ));
270     }
274 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
275 ?>