Code

c2b96e6f1e826e1f94b2b464d9bd193d28487e21
[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();
50   }
52   
53   function init()
54   {
55     $this->licenseUses = array();
56     $this->reservedLicenses = array();
57     $this->init_reservedLicenses = array();
58     if(!$this->initially_was_account){
59       $this->init_successfull = TRUE;
60     }else{
62       // Get license usage  
63       $res = $this->si->getLicenseUsage($this->cn);
64       if($this->si->is_error()){
65         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
66         $this->init_successfull = FALSE;
67         return;
68       }
69       $this->licenseUses = $res;
71       // Get reservations
72       $res = $this->si->getReservedLicensesForHost($this->cn);
73       if($this->si->is_error()){
74         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
75         $this->init_successfull = FALSE;
76         return;
77       }
78       foreach($res as $pool){
79         $l = $pool['softwareLicenseId'][0];
80         $this->reservedLicenses[$l] = $l;
81       }
82       $this->init_reservedLicenses = $this->reservedLicenses;
84       // Get list of license pools 
85       $res = $this->si->listLicenses();
86       if($this->si->is_error()){
87         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
88         $this->init_successfull = FALSE;
89         return;
90       }
91       $this->availableLicenses = array();
92       foreach($res as $license){
93         if(!isset($license['licensePoolId'])) $license['licensePoolId'][0] = '-';
94         $this->availableLicenses[$license['softwareLicenseId'][0]] = $license;
95       }
97       $this->init_successfull = TRUE;
98     }
99   }
102   function execute()
103   {
105     // Handle initialization failures.
106     if(isset($_POST['retry_init'])) $this->init();
107     if(!$this->init_successfull){
108       $smarty = get_smarty();
109       $smarty->assign("init_successfull", $this->init_successfull);
110       return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
111     }
112     
113     // Create usage list
114     $list = new divSelectBox("licenseUsage");
115     $list->setHeight(150);
116     foreach($this->licenseUses as $license){
117       if(preg_match("/r/i",$this->getacl('hostId'))){
118         $f3 = array("string" => $license['licenseKey'][0]);
119         $f2 = array("string" => $license['licensePoolId'][0]);
120         $f4 = array("string" => $license['softwareLicenseId'][0]);
121         $list->addEntry(array($f2,$f3,$f4));
122       }
123     } 
125     // Create reserved list
126     $list2 = new divSelectBox("reservedLicenses");
127     $list2->setHeight(150);
128     foreach($this->reservedLicenses as $key => $license){
129       if(preg_match("/r/i",$this->getacl('boundToHost'))){
131         $l = $this->availableLicenses[$key];
133         // Display remove button in case of write permissions
134         $action = "";
135         if(preg_match("/w/i",$this->getacl('boundToHost'))){
136           $action = "<input class='center' type='image' src='images/lists/trash.png' 
137             name='removeReservation_{$key}'>";
138         }
139   
140         $f1 = array("string" => $l['softwareLicenseId'][0]);
141         $f2 = array("string" => $l['licensePoolId'][0]);
142         $f4 = array("string" => $action,
143             "attach" => "style='border-right:0px; width:16px;'");
144         $list2->addEntry(array($f1,$f4));
145       }
146     } 
148     $smarty = get_smarty();
150     // Assign ACls 
151     $plInfo = $this->plInfo();
152     foreach($plInfo['plProvidedAcls'] as $name => $desc){
153       $smarty->assign("{$name}ACL",$this->getacl($name));
154     }
155     foreach($this->attributes as $attr){
156       $smarty->assign($attr,$this->$attr);
157     }
159     // build up a available licenses list. 
160     $licenses = array();
161     foreach($this->availableLicenses as $key => $license){
162       if(!in_array($license['softwareLicenseId'][0], $this->reservedLicenses)){
163         $licenses[$key] = $license['softwareLicenseId'][0]." [".$license['licensePoolId'][0]."]";
164       }
165     }
167     $smarty->assign("licenseUses", $list->DrawList());
168     $smarty->assign("licenseReserved", $list2->DrawList());
169     $smarty->assign("init_successfull", $this->init_successfull);
170     $smarty->assign("availableLicenses", $licenses);
171     $smarty->assign("initially_was_account", $this->initially_was_account);
172     return($smarty->fetch(get_template_path('licenseUsageByHost.tpl',TRUE,dirname(__FILE__))));
173   }
175  
176   /* Save HTML inputs
177    */
178   function save_object()
179   {
180     if(isset($_POST['opsiLicenseUsagePosted'])){
181       plugin::save_object();  
182   
183       if(preg_match("/w/i",$this->getacl('boundToHost'))){
185         // Check if we've to remove reservations 
186         foreach($_POST as $name => $value){
187           if(preg_match("/^removeReservation_/", $name)){
188             $id = preg_replace("/^removeReservation_(.*)_.$/", "\\1" ,$name);
189             if(isset($this->reservedLicenses[$id])) {
190               unset($this->reservedLicenses[$id]);
191             }
192             break;
193           }
194         } 
196         // Check if we've to add reservations
197         if(isset($_POST['availableLicense']) && isset($_POST['addReservation'])){
198           $id = get_post('availableLicense');
199           if(isset($this->availableLicenses[$id]) && !in_array($this->availableLicenses[$id],$this->reservedLicenses)){
200             $this->reservedLicenses[$id] =  $this->availableLicenses[$id]['softwareLicenseId'][0];
201           }
202         }
203       }
204     }
205   }  
208   /* Check user input and return a list of 'invalid input' messages.
209    */
210   function check()
211   {
212     $message = plugin::check();
213     return($message);
214   }
215   
217   function save()
218   {
219     $del = array_diff($this->init_reservedLicenses, $this->reservedLicenses);
220     $add = array_diff($this->reservedLicenses, $this->init_reservedLicenses);
222     foreach($del as $license){
223       $this->si->removeLicenseReservationFromHost($license, $this->cn);
224       if($this->si->is_error()){
225         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
226       }
227     }
229     foreach($add as $license){
230       $this->si->reserverLicenseForHost($license, $this->cn);
231       if($this->si->is_error()){
232         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
233       }
234     }
235   }
238   function remove_from_parent(){ }
240  
241   static function plInfo()
242   {
243     return (array(
244           "plShortName"   => _("Usage"),
245           "plDescription" => _("License usage"),
246           "plSelfModify"  => FALSE,
247           "plDepends"     => array(),
248           "plPriority"    => 1,
249           "plSection"     => array("administration"),
250           "plCategory"    => array("opsi"),
251           "plProvidedAcls"=> array(
252             "hostId" => _("Used by"),
253             "boundToHost" => _("License reservation"))
254           ));
255   }
259 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
260 ?>