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 }
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 {
104 plugin::execute();
106 // Handle initialization failures.
107 if(isset($_POST['retry_init'])) $this->init();
108 if(!$this->init_successfull){
109 $smarty = get_smarty();
110 $smarty->assign("init_successfull", $this->init_successfull);
111 return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
112 }
114 // Create usage list
115 $list = new divSelectBox("licenseUsage");
116 $list->setHeight(150);
117 foreach($this->licenseUses as $license){
118 if(preg_match("/r/i",$this->getacl('hostId'))){
119 $f3 = array("string" => $license['licenseKey'][0]);
120 $f2 = array("string" => $license['licensePoolId'][0]);
121 $f4 = array("string" => $license['softwareLicenseId'][0]);
122 $list->addEntry(array($f2,$f3,$f4));
123 }
124 }
126 // Create reserved list
127 $list2 = new divSelectBox("reservedLicenses");
128 $list2->setHeight(150);
129 foreach($this->reservedLicenses as $key => $license){
130 if(preg_match("/r/i",$this->getacl('boundToHost'))){
132 $l = $this->availableLicenses[$key];
134 // Display remove button in case of write permissions
135 $action = "";
136 if(preg_match("/w/i",$this->getacl('boundToHost'))){
137 $action = "<input class='center' type='image' src='images/lists/trash.png'
138 name='removeReservation_{$key}'>";
139 }
141 $f1 = array("string" => $l['softwareLicenseId'][0]);
142 $f2 = array("string" => $l['licensePoolId'][0]);
143 $f4 = array("string" => $action,
144 "attach" => "style='border-right:0px; width:16px;'");
145 $list2->addEntry(array($f1,$f4));
146 }
147 }
149 $smarty = get_smarty();
151 // Assign ACls
152 $plInfo = $this->plInfo();
153 foreach($plInfo['plProvidedAcls'] as $name => $desc){
154 $smarty->assign("{$name}ACL",$this->getacl($name));
155 }
156 foreach($this->attributes as $attr){
157 $smarty->assign($attr,$this->$attr);
158 }
160 // build up a available licenses list.
161 $licenses = array();
162 foreach($this->availableLicenses as $key => $license){
163 if(!in_array($license['softwareLicenseId'][0], $this->reservedLicenses)){
164 $licenses[$key] = $license['softwareLicenseId'][0]." [".$license['licensePoolId'][0]."]";
165 }
166 }
168 $smarty->assign("licenseUses", $list->DrawList());
169 $smarty->assign("licenseReserved", $list2->DrawList());
170 $smarty->assign("init_successfull", $this->init_successfull);
171 $smarty->assign("availableLicenses", $licenses);
172 $smarty->assign("initially_was_account", $this->initially_was_account);
173 return($smarty->fetch(get_template_path('licenseUsageByHost.tpl',TRUE,dirname(__FILE__))));
174 }
177 /* Save HTML inputs
178 */
179 function save_object()
180 {
181 if(isset($_POST['opsiLicenseUsagePosted'])){
182 plugin::save_object();
184 if(preg_match("/w/i",$this->getacl('boundToHost'))){
186 // Check if we've to remove reservations
187 foreach($_POST as $name => $value){
188 if(preg_match("/^removeReservation_/", $name)){
189 $id = preg_replace("/^removeReservation_(.*)_.$/", "\\1" ,$name);
190 if(isset($this->reservedLicenses[$id])) {
191 unset($this->reservedLicenses[$id]);
192 }
193 break;
194 }
195 }
197 // Check if we've to add reservations
198 if(isset($_POST['availableLicense']) && isset($_POST['addReservation'])){
199 $id = get_post('availableLicense');
200 if(isset($this->availableLicenses[$id]) && !in_array($this->availableLicenses[$id],$this->reservedLicenses)){
201 $this->reservedLicenses[$id] = $this->availableLicenses[$id]['softwareLicenseId'][0];
202 }
203 }
204 }
205 }
206 }
209 /* Check user input and return a list of 'invalid input' messages.
210 */
211 function check()
212 {
213 $message = plugin::check();
214 return($message);
215 }
218 function save()
219 {
220 $del = array_diff($this->init_reservedLicenses, $this->reservedLicenses);
221 $add = array_diff($this->reservedLicenses, $this->init_reservedLicenses);
223 foreach($del as $license){
224 $this->si->removeLicenseReservationFromHost($license, $this->cn);
225 if($this->si->is_error()){
226 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
227 }
228 }
230 foreach($add as $license){
231 $this->si->reserverLicenseForHost($license, $this->cn);
232 if($this->si->is_error()){
233 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
234 }
235 }
236 }
239 function remove_from_parent(){ }
242 static function plInfo()
243 {
244 return (array(
245 "plShortName" => _("Usage by host"),
246 "plDescription" => _("License usage by host"),
247 "plSelfModify" => FALSE,
248 "plDepends" => array(),
249 "plPriority" => 13,
250 "plSection" => array("administration"),
251 "plCategory" => array("opsi"),
252 "plProvidedAcls"=> array(
253 "hostId" => _("Used by"),
254 "boundToHost" => _("License reservation"))
255 ));
256 }
257 }
260 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
261 ?>