Code

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