Code

Added date selectors to license generic
[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     // Handle initialization failures.
107     if(isset($_POST['retry_init'])) $this->init();
108     if(!$this->init_successfull){
109       $smarty = get_smarty();
110       $smarty->assign("init_successfull", $this->init_successfull);
111       return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
112     }
114     $smarty = get_smarty();
116     // Assign attributes and its ACls 
117     $plInfo = $this->plInfo();
118     foreach($plInfo['plProvidedAcls'] as $name => $desc){
119       $smarty->assign("{$name}ACL",$this->getacl($name));
120     }
121     foreach($this->attributes as $attr){
122       $smarty->assign($attr,$this->$attr);
123     }
125     // Assign list of available license models
126     $smarty->assign("licenseModels",array(
127           "RETAIL" => _("Retail"),
128           "OEM"=>_("OEM"),
129           "VOLUME" => _("Volume")));
131     $smarty->assign("usePrototype", "true");
132     $smarty->assign("init_successfull", $this->init_successfull);
133     $smarty->assign("initially_was_account", $this->initially_was_account);
134     $smarty->assign("hosts", $this->getHosts());
136     $ui = get_userinfo();
137     $acl_base = $this->dn;
138     if($acl_base == "new"){
139       $acl_base = $this->config->current['BASE'];
140     }
141     $smarty->assign("licenseACL", $ui->get_permissions($acl_base,"opsi/licensePoolGeneric","licenses"));
142     $smarty->assign("writeable", preg_match("/w/",$ui->get_permissions($acl_base,"opsi/licensePoolGeneric","licenses")));
143     $smarty->assign("notUsedHosts", array_diff($this->getHosts(), $this->usedByHost));
144     $smarty->assign("boundToHost", $this->boundToHost[0]);
145     $smarty->assign("licenseKey", $this->licenseKey[0]);
146     return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
147   }
150   function getHosts()
151   {
152     $ret = array();
153     foreach($this->opsiHosts as $host){
154       $cn = $host['NAME'][0]['VALUE'];
155       $ret[$cn] = $cn;
156     }
157     return($ret);
158   }
161  
162   /* Save HTML inputs
163    */
164   function save_object()
165   {
167     if(isset($_POST['opsiLicensesPosted'])){
168       plugin::save_object();
170       if(isset($_POST['addLicenseUsage']) && isset($_POST['selectedHostToAdd'])){
171         $host = get_post('selectedHostToAdd');
172         if(!empty($host) && 
173             in_array($host,$this->getHosts()) && 
174             !in_array($host, $this->usedByHost)){
175           $this->usedByHost[] = $host;
176         }
177       }
179       if(isset($_POST['removeLicenseUsage']) && isset($_POST['selectedUsedHosts'])){
180         $todel = $_POST['selectedUsedHosts'];
181         foreach($todel as $host){
182           if(isset($this->usedByHost[$host])){
183             unset($this->usedByHost[$host]);
184           }
185         }
186       }
188       // Force licenseKey to be of type array.
189       if(!is_array($this->licenseKey)){
190         $this->licenseKey = array($this->licenseKey);
191       }
193       // BoundToHost maybe multiple too, later.
194       if(!is_array($this->boundToHost)){
195         $this->boundToHost = array($this->boundToHost);
196       }    
198       if($this->initially_was_account){
199         $this->cn = $this->orig_cn;
200         $this->licenseModel = $this->orig_licenseModel;
201       }
202     }
203   }  
206   /* Check user input and return a list of 'invalid input' messages.
207    */
208   function check()
209   {
210     $message = plugin::check();
212     // Very simple date input checks
213     if(!empty($this->expirationDate) && 
214        !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->expirationDate)){
215       $message[] = msgPool::invalid(_("Expiration date"),$this->expirationDate,"","2009-02-23");
216     }
217     if(!empty($this->conclusionDate) && 
218        !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->conclusionDate)){
219       $message[] = msgPool::invalid(_("Expiration date"),$this->conclusionDate,"","2009-02-23");
220     }
221     if(!empty($this->notificationDate) && 
222        !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->notificationDate)){
223       $message[] = msgPool::invalid(_("Expiration date"),$this->notificationDate,"","2009-02-23");
224     }
226     if(empty($this->cn)){
227       $message[] = msgPool::required(_("Name"));
228     }
230     if(empty($this->licenseKey[0])){
231       $message[] = msgPool::required(_("License key"));
232     }
234     return($message);
235   }
236   
237  
238   /* Removes the object from the opsi database
239    */ 
240   function remove_from_parent() {}
243   /* Saves object modifications
244    */  
245   function save()
246   {
247     $data = array();
248     foreach($this->attributes as $target){
249       $data[$target] = $this->$target;
250     }
251     
252     // Return opsi like dates.
253     foreach(array("notificationDate","expirationDate","conclusionDate") as $date) {
254       if(!empty($this->$date)){
255         $data[$date] = date("Y-m-d",strtotime($this->$date));
256       }
257     }
259     return($data);
260   }
261  
262   static function plInfo()
263   {
264     return (array(
265           "plShortName"   => _("Generic"),
266           "plDescription" => _("License generic"),
267           "plSelfModify"  => FALSE,
268           "plDepends"     => array(),
269           "plPriority"    => 1,
270           "plSection"     => array("administration"),
271           "plCategory"    => array("opsi"),
272           "plProvidedAcls"=> array(
273             "cn"                => _("Name"),
274             "description" => _("Description"))
275           ));
276   }
280 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
281 ?>