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 $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();
91 foreach($res as $pool){
92 $this->availableLicenses[] = $pool['cn'][0];
93 }
95 $this->init_successfull = TRUE;
96 }
97 }
100 function execute()
101 {
102 // Handle initialization failures.
103 if(isset($_POST['retry_init'])) $this->init();
104 if(!$this->init_successfull){
105 $smarty = get_smarty();
106 $smarty->assign("init_successfull", $this->init_successfull);
107 return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
108 }
110 // Create usage list
111 $list = new divSelectBox("licenseUsage");
112 $list->setHeight(150);
113 foreach($this->licenseUses as $license){
114 if(preg_match("/r/i",$this->getacl('hostId'))){
115 $f3 = array("string" => $license['licenseKey'][0]);
116 $f2 = array("string" => $license['licensePoolId'][0]);
117 $f4 = array("string" => $license['softwareLicenseId'][0]);
118 $list->addEntry(array($f2,$f3,$f4));
119 }
120 }
122 // Create reserved list
123 $list2 = new divSelectBox("reservedLicenses");
124 $list2->setHeight(150);
125 foreach($this->reservedLicenses as $key => $license){
126 if(preg_match("/r/i",$this->getacl('boundToHost'))){
128 // Display remove button in case of write permissions
129 $action = "";
130 if(preg_match("/w/i",$this->getacl('boundToHost'))){
131 $action = "<input class='center' type='image' src='images/lists/trash.png'
132 name='removeReservation_{$key}'>";
133 }
134 $f1 = array("string" => $license);
135 $f4 = array("string" => $action,
136 "attach" => "style='border-right:0px; width:16px;'");
137 $list2->addEntry(array($f1,$f4));
138 }
139 }
141 $smarty = get_smarty();
143 // Assign ACls
144 $plInfo = $this->plInfo();
145 foreach($plInfo['plProvidedAcls'] as $name => $desc){
146 $smarty->assign("{$name}ACL",$this->getacl($name));
147 }
148 foreach($this->attributes as $attr){
149 $smarty->assign($attr,$this->$attr);
150 }
152 $smarty->assign("licenseUses", $list->DrawList());
153 $smarty->assign("licenseReserved", $list2->DrawList());
154 $smarty->assign("init_successfull", $this->init_successfull);
155 $smarty->assign("availableLicenses", array_diff($this->availableLicenses, $this->reservedLicenses));
156 $smarty->assign("initially_was_account", $this->initially_was_account);
157 return($smarty->fetch(get_template_path('licenseUsageByHost.tpl',TRUE,dirname(__FILE__))));
158 }
161 /* Save HTML inputs
162 */
163 function save_object()
164 {
165 if(isset($_POST['opsiLicenseUsagePosted'])){
166 plugin::save_object();
168 if(preg_match("/w/i",$this->getacl('boundToHost'))){
170 // Check if we've to remove reservations
171 foreach($_POST as $name => $value){
172 if(preg_match("/^removeReservation_/", $name)){
173 $id = preg_replace("/^removeReservation_(.*)_.$/", "\\1" ,$name);
174 if(isset($this->reservedLicenses[$id])) {
175 unset($this->reservedLicenses[$id]);
176 }
177 break;
178 }
179 }
181 // Check if we've to add reservations
182 if(isset($_POST['availableLicense']) && isset($_POST['addReservation'])){
183 $id = get_post('availableLicense');
184 if(isset($this->availableLicenses[$id]) && !in_array($this->availableLicenses[$id],$this->reservedLicenses)){
185 $this->reservedLicenses[] = $this->availableLicenses[$id];
186 }
187 }
188 }
189 }
190 }
193 /* Check user input and return a list of 'invalid input' messages.
194 */
195 function check()
196 {
197 $message = plugin::check();
198 return($message);
199 }
202 function save()
203 {
204 $del = array_diff($this->init_reservedLicenses, $this->reservedLicenses);
205 $add = array_diff($this->reservedLicenses, $this->init_reservedLicenses);
207 foreach($del as $pool){
208 $this->si->removeLicenseFromHost($pool, $this->cn);
209 if($this->si->is_error()){
210 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
211 }
212 }
214 foreach($add as $pool){
215 $this->si->addLicenseToHost($pool, $this->cn);
216 if($this->si->is_error()){
217 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
218 }
219 }
220 }
223 function remove_from_parent(){ }
226 static function plInfo()
227 {
228 return (array(
229 "plShortName" => _("Usage"),
230 "plDescription" => _("License usage"),
231 "plSelfModify" => FALSE,
232 "plDepends" => array(),
233 "plPriority" => 1,
234 "plSection" => array("administration"),
235 "plCategory" => array("opsi"),
236 "plProvidedAcls"=> array(
237 "hostId" => _("Used by host")." ("._("read only").")",
238 "boundToHost" => _("License revervation"))
239 ));
240 }
241 }
244 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
245 ?>