Code

Updated template
[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   var $si = NULL;
28   var $data = array();
29   var $cn = "";
30   var $orig_cn = "";
31   var $description = "";  
32   var $partner = "";
34   var $conclusionDate = "";
35   var $expirationDate = "";
36   var $notificationDate = "";
38   var $licenseModel = "";
39   var $licenseKey = array();
40   var $orig_licenseModel = "";
41   var $licensePoolId = "";
42   var $boundToHost= array(); // Reserved for Host.   
43   var $usedByHost = array(); // Used by Host.   
45   var $maximumInstallations = 0;
46   var $opsiHosts; 
47   
48   var $attributes = array(
49         "cn","description","partner","conclusionDate","expirationDate",
50         "notificationDate","licenseModel","licenseKey","maximumInstallations",
51         "licensePoolId", "usedByHost","boundToHost");
53   function __construct(&$config, $license, $hosts = array())
54   {
55     $this->config = $config;
56     $this->data = $license;
57     $this->si = new opsiLicenceHandler($this->config);
58     $this->opsiHosts = $hosts;
60     // Detect account state.
61     if(count($this->data) == 0){
62       $this->initially_was_account = FALSE;
63     }else{
64       $this->initially_was_account = TRUE;
65     }
66     $this->is_account = TRUE;
68     // Extract pool name out of the fake dn.
69     $this->init();
70   }
72   
73   function init()
74   {
75     $this->boundToHost = array('0'=>"");
76     $this->usedByHost = array('0'=>"");
77     $this->licenseKey = array('0'=>"");
78     if($this->initially_was_account){
79       foreach($this->attributes as $attr){
80         $this->$attr = $this->data[$attr];
81       }
82     }
84     $this->orig_cn = $this->cn;
85     $this->orig_licenseModel = $this->licenseModel;
86     $this->init_successfull = TRUE;
87     return;    
88   }
91   function execute()
92   {
93     // Handle initialization failures.
94     if(isset($_POST['retry_init'])) $this->init();
95     if(!$this->init_successfull){
96       $smarty = get_smarty();
97       $smarty->assign("init_successfull", $this->init_successfull);
98       return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
99     }
101     $smarty = get_smarty();
103     // Assign ACls 
104     $plInfo = $this->plInfo();
105     foreach($plInfo['plProvidedAcls'] as $name => $desc){
106       $smarty->assign("{$name}ACL",$this->getacl($name));
107     }
108     foreach($this->attributes as $attr){
109       $smarty->assign($attr,$this->$attr);
110     }
112     $smarty->assign("licenseModels",array(
113           "RETAIL" => _("Retail"),
114           "OEM"=>_("OEM"),
115           "VOLUME" => _("Volume"),
116           "CONCURRENT" => _("Concurrent")));
119     $smarty->assign("init_successfull", $this->init_successfull);
120     $smarty->assign("initially_was_account", $this->initially_was_account);
121     $smarty->assign("hosts", $this->getHosts());
123     $smarty->assign("notUsedHosts", array_diff($this->getHosts(), $this->usedByHost));
124     $smarty->assign("boundToHost", $this->boundToHost[0]);
125     $smarty->assign("licenseKey", $this->licenseKey[0]);
126     return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
127   }
130   function getHosts()
131   {
132     $ret = array();
133     foreach($this->opsiHosts as $host){
134       $cn = $host['NAME'][0]['VALUE'];
135       $ret[$cn] = $cn;
136     }
137     return($ret);
138   }
141  
142   /* Save HTML inputs
143    */
144   function save_object()
145   {
147     if(isset($_POST['opsiLicensesPosted'])){
148       plugin::save_object();
150       if(isset($_POST['addLicenseUsage']) && isset($_POST['selectedHostToAdd'])){
151         $host = get_post('selectedHostToAdd');
152         if(!empty($host) && 
153             in_array($host,$this->getHosts()) && 
154             !in_array($host, $this->usedByHost)){
155           $this->usedByHost[] = $host;
156         }
157       }
159       if(isset($_POST['removeLicenseUsage']) && isset($_POST['selectedUsedHosts'])){
160         $todel = $_POST['selectedUsedHosts'];
161         foreach($todel as $host){
162           if(isset($this->usedByHost[$host])){
163             unset($this->usedByHost[$host]);
164           }
165         }
166       }
168       // Force licenseKey to be of type array.
169       if(!is_array($this->licenseKey)){
170         $this->licenseKey = array($this->licenseKey);
171       }
173       // BoundToHost maybe multiple too, later.
174       if(!is_array($this->boundToHost)){
175         $this->boundToHost = array($this->boundToHost);
176       }    
178       if($this->initially_was_account){
179         $this->cn = $this->orig_cn;
180         $this->licenseModel = $this->orig_licenseModel;
181       }
182     }
183   }  
186   /* Check user input and return a list of 'invalid input' messages.
187    */
188   function check()
189   {
190     $message = plugin::check();
192     // Very simple date input checks
193     if(!empty($this->expirationDate) && 
194        !preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/",$this->expirationDate)){
195       $message[] = msgPool::invalid(_("Expiration date"),$this->expirationDate,"","2009-02-23");
196     }
197     if(!empty($this->conclusionDate) && 
198        !preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/",$this->conclusionDate)){
199       $message[] = msgPool::invalid(_("Expiration date"),$this->conclusionDate,"","2009-02-23");
200     }
201     if(!empty($this->notificationDate) && 
202        !preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/",$this->notificationDate)){
203       $message[] = msgPool::invalid(_("Expiration date"),$this->notificationDate,"","2009-02-23");
204     }
206     if(empty($this->cn)){
207       $message[] = msgPool::required(_("Name"));
208     }
210     if(empty($this->licenseKey[0])){
211       $message[] = msgPool::required(_("License key"));
212     }
214     return($message);
215   }
216   
217  
218   /* Removes the object from the opsi database
219    */ 
220   function remove_from_parent() {}
223   /* Saves object modifications
224    */  
225   function save()
226   {
227     $data = array();
228     foreach($this->attributes as $target){
229       $data[$target] = $this->$target;
230     }
231     return($data);
232   }
233  
234   static function plInfo()
235   {
236     return (array(
237           "plShortName"   => _("Generic"),
238           "plDescription" => _("License generic"),
239           "plSelfModify"  => FALSE,
240           "plDepends"     => array(),
241           "plPriority"    => 1,
242           "plSection"     => array("administration"),
243           "plCategory"    => array("opsi"),
244           "plProvidedAcls"=> array(
245             "cn"                => _("Name"),
246             "description" => _("Description"))
247           ));
248   }
252 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
253 ?>