Code

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