Code

Updated usage dialog
[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     if(!$this->initially_was_account){
58       $this->init_successfull = TRUE;
59     }else{
61       $res = $this->si->getLicenseUsage($this->cn);
62       if($this->si->is_error()){
63         $this->init_successfull = FALSE;
64         return;
65       }
66       $this->licenseUses = $res;
68       $res = $this->si->getReservedLicensesForHost($this->cn);
69       if($this->si->is_error()){
70         $this->init_successfull = FALSE;
71         return;
72       }
73       $this->reservedLicenses = $res;
74       $this->init_reservedLicenses = $res;
75       $this->init_successfull = TRUE;
76     }
78     $this->availableLicenses = array("Dummy","Schinken");
79   }
82   function execute()
83   {
84     // Handle initialization failures.
85     if(isset($_POST['retry_init'])) $this->init();
86     if(!$this->init_successfull){
87       $smarty = get_smarty();
88       $smarty->assign("init_successfull", $this->init_successfull);
89       return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
90     }
91     
92     // Create usage list
93     $list = new divSelectBox("licenseUsage");
94     $list->setHeight(150);
95     foreach($this->licenseUses as $license){
96       $f3 = array("string" => $license['licenseKey'][0]);
97       $f2 = array("string" => $license['licensePoolId'][0]);
98       $f4 = array("string" => $license['softwareLicenseId'][0]);
99       $list->addEntry(array($f2,$f3,$f4));
100     } 
102     // Create reserved list
103     $list2 = new divSelectBox("reservedLicenses");
104     $list2->setHeight(150);
105     foreach($this->reservedLicenses as $key => $license){
106       $action = "<input class='center' type='image' src='images/lists/trash.png' 
107         name='removeReservation_{$key}'>";
108       $f1 = array("string" => $license['licensePoolId'][0]);
109 #      $f2 = array("string" => $license['licenseKey'][0]);
110       $f3 = array("string" => $license['softwareLicenseId'][0]);
111       $f4 = array("string" => $action,
112                   "attach" => "style='border-right:0px;'");
113 #      $list2->addEntry(array($f1,$f2,$f3, $f4));
114       $list2->addEntry(array($f1,$f3, $f4));
115     } 
117     $smarty = get_smarty();
119     // Assign ACls 
120     $plInfo = $this->plInfo();
121     foreach($plInfo['plProvidedAcls'] as $name => $desc){
122       $smarty->assign("{$name}ACL",$this->getacl($name));
123     }
124     foreach($this->attributes as $attr){
125       $smarty->assign($attr,$this->$attr);
126     }
128     $smarty->assign("licenseUses", $list->DrawList());
129     $smarty->assign("licenseReserved", $list2->DrawList());
130     $smarty->assign("init_successfull", $this->init_successfull);
131     $smarty->assign("availableLicenses", $this->availableLicenses);
132     $smarty->assign("initially_was_account", $this->initially_was_account);
133     return($smarty->fetch(get_template_path('licenseUsageByHost.tpl',TRUE,dirname(__FILE__))));
134   }
136  
137   /* Save HTML inputs
138    */
139   function save_object()
140   {
141     if(isset($_POST['opsiLicenseUsagePosted'])){
142       plugin::save_object();  
143   
144       // Check if we've to remove reservations 
145       foreach($_POST as $name => $value){
146         if(preg_match("/^removeReservation_/", $name)){
147           $id = preg_replace("/^removeReservation_(.*)_.$/", "\\1" ,$name);
148           if(isset($this->reservedLicenses[$id])) {
149             unset($this->reservedLicenses[$id]);
150           }
151           break;
152         }
153       } 
155       // Check if we've to add reservations
156       if(isset($_POST['availableLicense']) && isset($_POST['addReservation'])){
157         echo get_post('availableLicense');
158       }
159     }
160   }  
163   /* Check user input and return a list of 'invalid input' messages.
164    */
165   function check()
166   {
167     $message = plugin::check();
168     return($message);
169   }
170   
172   function save(){ }
173   function remove_from_parent(){ }
175  
176   static function plInfo()
177   {
178     return (array(
179           "plShortName"   => _("Usage"),
180           "plDescription" => _("License usage"),
181           "plSelfModify"  => FALSE,
182           "plDepends"     => array(),
183           "plPriority"    => 1,
184           "plSection"     => array("administration"),
185           "plCategory"    => array("opsi"),
186           "plProvidedAcls"=> array(
187             "cn"                => _("Name"),
188             "description" => _("Description"))
189           ));
190   }
194 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
195 ?>