Code

fixed error handling
[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         $this->reservedLicenses[] = $pool['licensePoolId'][0];
80       }
81       $this->init_reservedLicenses = $this->reservedLicenses;
83       // Get list of license pools 
84       $res = $this->si->listPools();
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->availableLicenses = array();
92       echo "We require Licenses here not Pools";
94       foreach($res as $pool){
95         $this->availableLicenses[] = $pool['cn'][0];
96       }
98       $this->init_successfull = TRUE;
99     }
100   }
103   function execute()
104   {
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         // Display remove button in case of write permissions
132         $action = "";
133         if(preg_match("/w/i",$this->getacl('boundToHost'))){
134           $action = "<input class='center' type='image' src='images/lists/trash.png' 
135             name='removeReservation_{$key}'>";
136         }
137         $f1 = array("string" => $license);
138         $f4 = array("string" => $action,
139             "attach" => "style='border-right:0px; width:16px;'");
140         $list2->addEntry(array($f1,$f4));
141       }
142     } 
144     $smarty = get_smarty();
146     // Assign ACls 
147     $plInfo = $this->plInfo();
148     foreach($plInfo['plProvidedAcls'] as $name => $desc){
149       $smarty->assign("{$name}ACL",$this->getacl($name));
150     }
151     foreach($this->attributes as $attr){
152       $smarty->assign($attr,$this->$attr);
153     }
155     $smarty->assign("licenseUses", $list->DrawList());
156     $smarty->assign("licenseReserved", $list2->DrawList());
157     $smarty->assign("init_successfull", $this->init_successfull);
158     $smarty->assign("availableLicenses", array_diff($this->availableLicenses, $this->reservedLicenses));
159     $smarty->assign("initially_was_account", $this->initially_was_account);
160     return($smarty->fetch(get_template_path('licenseUsageByHost.tpl',TRUE,dirname(__FILE__))));
161   }
163  
164   /* Save HTML inputs
165    */
166   function save_object()
167   {
168     if(isset($_POST['opsiLicenseUsagePosted'])){
169       plugin::save_object();  
170   
171       if(preg_match("/w/i",$this->getacl('boundToHost'))){
173         // Check if we've to remove reservations 
174         foreach($_POST as $name => $value){
175           if(preg_match("/^removeReservation_/", $name)){
176             $id = preg_replace("/^removeReservation_(.*)_.$/", "\\1" ,$name);
177             if(isset($this->reservedLicenses[$id])) {
178               unset($this->reservedLicenses[$id]);
179             }
180             break;
181           }
182         } 
184         // Check if we've to add reservations
185         if(isset($_POST['availableLicense']) && isset($_POST['addReservation'])){
186           $id = get_post('availableLicense');
187           if(isset($this->availableLicenses[$id]) && !in_array($this->availableLicenses[$id],$this->reservedLicenses)){
188             $this->reservedLicenses[] =  $this->availableLicenses[$id];
189           }
190         }
191       }
192     }
193   }  
196   /* Check user input and return a list of 'invalid input' messages.
197    */
198   function check()
199   {
200     $message = plugin::check();
201     return($message);
202   }
203   
205   function save()
206   {
207     $del = array_diff($this->init_reservedLicenses, $this->reservedLicenses);
208     $add = array_diff($this->reservedLicenses, $this->init_reservedLicenses);
210     foreach($del as $license){
211       $this->si->removeLicenseReservationFromHost($license, $this->cn);
212       if($this->si->is_error()){
213         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
214       }
215     }
217     foreach($add as $license){
218       $this->si->reserverLicenseForHost($license, $this->cn);
219       if($this->si->is_error()){
220         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
221       }
222     }
223   }
226   function remove_from_parent(){ }
228  
229   static function plInfo()
230   {
231     return (array(
232           "plShortName"   => _("Usage"),
233           "plDescription" => _("License usage"),
234           "plSelfModify"  => FALSE,
235           "plDepends"     => array(),
236           "plPriority"    => 1,
237           "plSection"     => array("administration"),
238           "plCategory"    => array("opsi"),
239           "plProvidedAcls"=> array(
240             "hostId" => _("Used by host")."&nbsp;("._("read only").")",
241             "boundToHost" => _("License revervation"))
242           ));
243   }
247 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
248 ?>