Code

Made template w3c conform
[gosa.git] / gosa-plugins / opsi / admin / opsiLicenses / class_licenseGeneric.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 licenseGeneric extends plugin 
25 {
27   // License attributes
28   var $si = NULL;
29   var $data = array();
30   var $cn = "";
31   var $orig_cn = "";
32   var $description = "";  
33   var $partner = "";
35   // Date attributes 
36   var $conclusionDate = "";
37   var $expirationDate = "";
38   var $notificationDate = "";
40   // License model related attribues
41   var $licenseModel = "";
42   var $licenseKey = array();
43   var $orig_licenseModel = "";
44   var $licensePoolId = "";
45   var $boundToHost= array(); // Reserved for Host.   
46   var $usedByHost = array(); // Used by Host.   
48   var $maximumInstallations = 0;
49   var $opsiHosts; 
50   
51   var $attributes = array(
52         "cn","description","partner","conclusionDate","expirationDate",
53         "notificationDate","licenseModel","licenseKey","maximumInstallations",
54         "licensePoolId", "usedByHost","boundToHost");
56   function __construct(&$config, $dn, $license, $hosts = array())
57   {
58   
59     $this->config = $config;
60     $this->data = $license;
61     $this->dn = $dn;
62     $this->si = new opsiLicenceHandler($this->config);
63     $this->opsiHosts = $hosts;
65     // Detect account state.
66     if(count($this->data) == 0){
67       $this->initially_was_account = FALSE;
68     }else{
69       $this->initially_was_account = TRUE;
70     }
71     $this->is_account = TRUE;
73     // Extract pool name out of the fake dn.
74     $this->init();
75   }
77   
78   function init()
79   {
80     // Extract license information out of the license object ($this->data)
81     $this->boundToHost = array('0'=>"");
82     $this->usedByHost = array('0'=>"");
83     $this->licenseKey = array('0'=>"");
84     if($this->initially_was_account){
85       foreach($this->attributes as $attr){
86         $this->$attr = $this->data[$attr];
87       }
89       // Fix dates 
90       foreach(array("notificationDate","expirationDate","conclusionDate") as $date) {
91         if(!empty($this->$date)){
92           $this->$date = date("d.m.Y",strtotime($this->$date));
93         }
94       }
95     }
97     $this->orig_cn = $this->cn;
98     $this->orig_licenseModel = $this->licenseModel;
99     $this->init_successfull = TRUE;
100     return;    
101   }
104   function execute()
105   {
106     plugin::execute();
107     // Handle initialization failures.
108     if(isset($_POST['retry_init'])) $this->init();
109     if(!$this->init_successfull){
110       $smarty = get_smarty();
111       $smarty->assign("init_successfull", $this->init_successfull);
112       return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
113     }
115     $smarty = get_smarty();
117     // Assign attributes and its ACls 
118     $plInfo = $this->plInfo();
119     foreach($plInfo['plProvidedAcls'] as $name => $desc){
120       $smarty->assign("{$name}ACL",$this->getacl($name));
121     }
122     foreach($this->attributes as $attr){
123       $smarty->assign($attr,$this->$attr);
124     }
126     // Assign list of available license models
127     $smarty->assign("licenseModels",array(
128           "RETAIL" => _("Retail"),
129           "OEM"=>_("OEM"),
130           "VOLUME" => _("Volume")));
132     $smarty->assign("usePrototype", "true");
133     $smarty->assign("init_successfull", $this->init_successfull);
134     $smarty->assign("initially_was_account", $this->initially_was_account);
135     $smarty->assign("hosts", $this->getHosts());
137     $ui = get_userinfo();
138     $acl_base = $this->dn;
139     if($acl_base == "new"){
140       $acl_base = $this->config->current['BASE'];
141     }
142     $smarty->assign("licenseACL", $ui->get_permissions($acl_base,"opsi/licensePoolGeneric","licenses"));
143     $smarty->assign("writeable", preg_match("/w/",$ui->get_permissions($acl_base,"opsi/licensePoolGeneric","licenses")));
144     $smarty->assign("notUsedHosts", array_diff($this->getHosts(), $this->usedByHost));
145     $smarty->assign("boundToHost", $this->boundToHost[0]);
146     $smarty->assign("licenseKey", $this->licenseKey[0]);
148     // w3c fix, do not show empty options.
149     $tmp = $this->usedByHost;
150     if(isset($tmp[0]) && empty($tmp[0])) unset($tmp[0]);
151     $smarty->assign('usedByHost', $tmp);
153     foreach(array("notificationDate","expirationDate","conclusionDate") as $date) {
154       $smarty->assign($date."Writeable", $this->acl_is_writeable($date));
155     }
157     return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
158   }
161   function getHosts()
162   {
163     $ret = array();
164     foreach($this->opsiHosts as $host){
165       $cn = $host['NAME'][0]['VALUE'];
166       $ret[$cn] = $cn;
167     }
168     return($ret);
169   }
172  
173   /* Save HTML inputs
174    */
175   function save_object()
176   {
178     if(isset($_POST['opsiLicensesPosted'])){
179       plugin::save_object();
181       if(isset($_POST['addLicenseUsage']) && isset($_POST['selectedHostToAdd'])){
182         $host = get_post('selectedHostToAdd');
183         if(!empty($host) && 
184             in_array($host,$this->getHosts()) && 
185             !in_array($host, $this->usedByHost)){
186           $this->usedByHost[] = $host;
187         }
188       }
190       if(isset($_POST['removeLicenseUsage']) && isset($_POST['selectedUsedHosts'])){
191         $todel = $_POST['selectedUsedHosts'];
192         foreach($todel as $host){
193           if(isset($this->usedByHost[$host])){
194             unset($this->usedByHost[$host]);
195           }
196         }
197       }
199       // Force licenseKey to be of type array.
200       if(!is_array($this->licenseKey)){
201         $this->licenseKey = array($this->licenseKey);
202       }
204       // BoundToHost maybe multiple too, later.
205       if(!is_array($this->boundToHost)){
206         $this->boundToHost = array($this->boundToHost);
207       }    
209       if($this->initially_was_account){
210         $this->cn = $this->orig_cn;
211         $this->licenseModel = $this->orig_licenseModel;
212       }
213     }
214   }  
217   /* Check user input and return a list of 'invalid input' messages.
218    */
219   function check()
220   {
221     $message = plugin::check();
223     // Very simple date input checks
224     if(!empty($this->expirationDate) && 
225        !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->expirationDate)){
226       $message[] = msgPool::invalid(_("Expiration date"),$this->expirationDate,"","23.02.2009");
227     }
228     if(!empty($this->conclusionDate) && 
229        !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->conclusionDate)){
230       $message[] = msgPool::invalid(_("Expiration date"),$this->conclusionDate,"","23.02.2009");
231     }
232     if(!empty($this->notificationDate) && 
233        !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->notificationDate)){
234       $message[] = msgPool::invalid(_("Expiration date"),$this->notificationDate,"","23.02.2009");
235     }
237     if(empty($this->cn)){
238       $message[] = msgPool::required(_("Name"));
239     }
241     if(empty($this->licenseKey[0])){
242       $message[] = msgPool::required(_("License key"));
243     }
245     return($message);
246   }
247   
248  
249   /* Removes the object from the opsi database
250    */ 
251   function remove_from_parent() {}
254   /* Saves object modifications
255    */  
256   function save()
257   {
258     $data = array();
259     foreach($this->attributes as $target){
260       $data[$target] = $this->$target;
261     }
262     
263     // Return opsi like dates.
264     foreach(array("notificationDate","expirationDate","conclusionDate") as $date) {
265       if(!empty($this->$date)){
266         $data[$date] = date("Y-m-d",strtotime($this->$date));
267       }
268     }
270     return($data);
271   }
272  
273   static function plInfo()
274   {
275     return (array(
276           "plShortName"   => _("Generic"),
277           "plDescription" => _("License generic"),
278           "plSelfModify"  => FALSE,
279           "plDepends"     => array(),
280           "plPriority"    => 8,
281           "plSection"     => array("administration"),
282           "plCategory"    => array("opsi"),
283           "plProvidedAcls"=> array(
284             "cn"                => _("Name"),
285             "description" => _("Description"))
286           ));
287   }
291 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
292 ?>