Code

Only show date selector while dates are writeable
[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]);
147     foreach(array("notificationDate","expirationDate","conclusionDate") as $date) {
148       $smarty->assign($date."Writeable", $this->acl_is_writeable($date));
149     }
151     return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
152   }
155   function getHosts()
156   {
157     $ret = array();
158     foreach($this->opsiHosts as $host){
159       $cn = $host['NAME'][0]['VALUE'];
160       $ret[$cn] = $cn;
161     }
162     return($ret);
163   }
166  
167   /* Save HTML inputs
168    */
169   function save_object()
170   {
172     if(isset($_POST['opsiLicensesPosted'])){
173       plugin::save_object();
175       if(isset($_POST['addLicenseUsage']) && isset($_POST['selectedHostToAdd'])){
176         $host = get_post('selectedHostToAdd');
177         if(!empty($host) && 
178             in_array($host,$this->getHosts()) && 
179             !in_array($host, $this->usedByHost)){
180           $this->usedByHost[] = $host;
181         }
182       }
184       if(isset($_POST['removeLicenseUsage']) && isset($_POST['selectedUsedHosts'])){
185         $todel = $_POST['selectedUsedHosts'];
186         foreach($todel as $host){
187           if(isset($this->usedByHost[$host])){
188             unset($this->usedByHost[$host]);
189           }
190         }
191       }
193       // Force licenseKey to be of type array.
194       if(!is_array($this->licenseKey)){
195         $this->licenseKey = array($this->licenseKey);
196       }
198       // BoundToHost maybe multiple too, later.
199       if(!is_array($this->boundToHost)){
200         $this->boundToHost = array($this->boundToHost);
201       }    
203       if($this->initially_was_account){
204         $this->cn = $this->orig_cn;
205         $this->licenseModel = $this->orig_licenseModel;
206       }
207     }
208   }  
211   /* Check user input and return a list of 'invalid input' messages.
212    */
213   function check()
214   {
215     $message = plugin::check();
217     // Very simple date input checks
218     if(!empty($this->expirationDate) && 
219        !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->expirationDate)){
220       $message[] = msgPool::invalid(_("Expiration date"),$this->expirationDate,"","2009-02-23");
221     }
222     if(!empty($this->conclusionDate) && 
223        !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->conclusionDate)){
224       $message[] = msgPool::invalid(_("Expiration date"),$this->conclusionDate,"","2009-02-23");
225     }
226     if(!empty($this->notificationDate) && 
227        !preg_match("/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/",$this->notificationDate)){
228       $message[] = msgPool::invalid(_("Expiration date"),$this->notificationDate,"","2009-02-23");
229     }
231     if(empty($this->cn)){
232       $message[] = msgPool::required(_("Name"));
233     }
235     if(empty($this->licenseKey[0])){
236       $message[] = msgPool::required(_("License key"));
237     }
239     return($message);
240   }
241   
242  
243   /* Removes the object from the opsi database
244    */ 
245   function remove_from_parent() {}
248   /* Saves object modifications
249    */  
250   function save()
251   {
252     $data = array();
253     foreach($this->attributes as $target){
254       $data[$target] = $this->$target;
255     }
256     
257     // Return opsi like dates.
258     foreach(array("notificationDate","expirationDate","conclusionDate") as $date) {
259       if(!empty($this->$date)){
260         $data[$date] = date("Y-m-d",strtotime($this->$date));
261       }
262     }
264     return($data);
265   }
266  
267   static function plInfo()
268   {
269     return (array(
270           "plShortName"   => _("Generic"),
271           "plDescription" => _("License generic"),
272           "plSelfModify"  => FALSE,
273           "plDepends"     => array(),
274           "plPriority"    => 8,
275           "plSection"     => array("administration"),
276           "plCategory"    => array("opsi"),
277           "plProvidedAcls"=> array(
278             "cn"                => _("Name"),
279             "description" => _("Description"))
280           ));
281   }
285 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
286 ?>