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 $this->init_successfull = FALSE;
66 return;
67 }
68 $this->licenseUses = $res;
70 // Get reservations
71 $res = $this->si->getReservedLicensesForHost($this->cn);
72 if($this->si->is_error()){
73 $this->init_successfull = FALSE;
74 return;
75 }
76 foreach($res as $pool){
77 $this->reservedLicenses[] = $pool['licensePoolId'][0];
78 }
79 $this->init_reservedLicenses = $this->reservedLicenses;
81 // Get list of license pools
82 $res = $this->si->listPools();
83 if($this->si->is_error()){
84 $this->init_successfull = FALSE;
85 return;
86 }
87 $this->availableLicenses = array();
88 foreach($res as $pool){
89 $this->availableLicenses[] = $pool['cn'][0];
90 }
92 $this->init_successfull = TRUE;
93 }
94 }
97 function execute()
98 {
99 // Handle initialization failures.
100 if(isset($_POST['retry_init'])) $this->init();
101 if(!$this->init_successfull){
102 $smarty = get_smarty();
103 $smarty->assign("init_successfull", $this->init_successfull);
104 return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
105 }
107 // Create usage list
108 $list = new divSelectBox("licenseUsage");
109 $list->setHeight(150);
110 foreach($this->licenseUses as $license){
111 $f3 = array("string" => $license['licenseKey'][0]);
112 $f2 = array("string" => $license['licensePoolId'][0]);
113 $f4 = array("string" => $license['softwareLicenseId'][0]);
114 $list->addEntry(array($f2,$f3,$f4));
115 }
117 // Create reserved list
118 $list2 = new divSelectBox("reservedLicenses");
119 $list2->setHeight(150);
120 foreach($this->reservedLicenses as $key => $license){
121 $action = "<input class='center' type='image' src='images/lists/trash.png'
122 name='removeReservation_{$key}'>";
123 $f1 = array("string" => $license);
124 $f4 = array("string" => $action,
125 "attach" => "style='border-right:0px; width:16px;'");
126 $list2->addEntry(array($f1,$f4));
127 }
129 $smarty = get_smarty();
131 // Assign ACls
132 $plInfo = $this->plInfo();
133 foreach($plInfo['plProvidedAcls'] as $name => $desc){
134 $smarty->assign("{$name}ACL",$this->getacl($name));
135 }
136 foreach($this->attributes as $attr){
137 $smarty->assign($attr,$this->$attr);
138 }
140 $smarty->assign("licenseUses", $list->DrawList());
141 $smarty->assign("licenseReserved", $list2->DrawList());
142 $smarty->assign("init_successfull", $this->init_successfull);
143 $smarty->assign("availableLicenses", array_diff($this->availableLicenses, $this->reservedLicenses));
144 $smarty->assign("initially_was_account", $this->initially_was_account);
145 return($smarty->fetch(get_template_path('licenseUsageByHost.tpl',TRUE,dirname(__FILE__))));
146 }
149 /* Save HTML inputs
150 */
151 function save_object()
152 {
153 if(isset($_POST['opsiLicenseUsagePosted'])){
154 plugin::save_object();
156 // Check if we've to remove reservations
157 foreach($_POST as $name => $value){
158 if(preg_match("/^removeReservation_/", $name)){
159 $id = preg_replace("/^removeReservation_(.*)_.$/", "\\1" ,$name);
160 if(isset($this->reservedLicenses[$id])) {
161 unset($this->reservedLicenses[$id]);
162 }
163 break;
164 }
165 }
167 // Check if we've to add reservations
168 if(isset($_POST['availableLicense']) && isset($_POST['addReservation'])){
169 $id = get_post('availableLicense');
170 if(isset($this->availableLicenses[$id])){
171 $this->reservedLicenses[] = $this->availableLicenses[$id];
172 }
173 }
174 }
175 }
178 /* Check user input and return a list of 'invalid input' messages.
179 */
180 function check()
181 {
182 $message = plugin::check();
183 return($message);
184 }
187 function save()
188 {
189 $del = array_diff($this->init_reservedLicenses, $this->reservedLicenses);
190 $add = array_diff($this->reservedLicenses, $this->init_reservedLicenses);
192 foreach($del as $pool){
193 $this->si->removeLicenseFromHost($pool, $this->cn);
194 if($this->si->is_error()){
195 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
196 }
197 }
199 foreach($add as $pool){
200 $this->si->addLicenseToHost($pool, $this->cn);
201 if($this->si->is_error()){
202 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
203 }
204 }
205 }
208 function remove_from_parent(){ }
211 static function plInfo()
212 {
213 return (array(
214 "plShortName" => _("Usage"),
215 "plDescription" => _("License usage"),
216 "plSelfModify" => FALSE,
217 "plDepends" => array(),
218 "plPriority" => 1,
219 "plSection" => array("administration"),
220 "plCategory" => array("opsi"),
221 "plProvidedAcls"=> array(
222 "cn" => _("Name"),
223 "description" => _("Description"))
224 ));
225 }
226 }
229 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
230 ?>