Code

Reviewed licensePool class
[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, $dn, $license, $hosts = array())
54   {
55   
56     $this->config = $config;
57     $this->data = $license;
58     $this->dn = $dn;
59     $this->si = new opsiLicenceHandler($this->config);
60     $this->opsiHosts = $hosts;
62     // Detect account state.
63     if(count($this->data) == 0){
64       $this->initially_was_account = FALSE;
65     }else{
66       $this->initially_was_account = TRUE;
67     }
68     $this->is_account = TRUE;
70     // Extract pool name out of the fake dn.
71     $this->init();
72   }
74   
75   function init()
76   {
77     $this->boundToHost = array('0'=>"");
78     $this->usedByHost = array('0'=>"");
79     $this->licenseKey = array('0'=>"");
80     if($this->initially_was_account){
81       foreach($this->attributes as $attr){
82         $this->$attr = $this->data[$attr];
83       }
84     }
86     $this->orig_cn = $this->cn;
87     $this->orig_licenseModel = $this->licenseModel;
88     $this->init_successfull = TRUE;
89     return;    
90   }
93   function execute()
94   {
95     // Handle initialization failures.
96     if(isset($_POST['retry_init'])) $this->init();
97     if(!$this->init_successfull){
98       $smarty = get_smarty();
99       $smarty->assign("init_successfull", $this->init_successfull);
100       return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
101     }
103     $smarty = get_smarty();
105     // Assign ACls 
106     $plInfo = $this->plInfo();
107     foreach($plInfo['plProvidedAcls'] as $name => $desc){
108       $smarty->assign("{$name}ACL",$this->getacl($name));
109     }
110     foreach($this->attributes as $attr){
111       $smarty->assign($attr,$this->$attr);
112     }
114     $smarty->assign("licenseModels",array(
115           "RETAIL" => _("Retail"),
116           "OEM"=>_("OEM"),
117           "VOLUME" => _("Volume")));
120     $smarty->assign("init_successfull", $this->init_successfull);
121     $smarty->assign("initially_was_account", $this->initially_was_account);
122     $smarty->assign("hosts", $this->getHosts());
124     $ui = get_userinfo();
126     $acl_base = $this->dn;
127     if($acl_base == "new"){
128       $acl_base = $this->config->current['BASE'];
129     }
131     $smarty->assign("licenseACL", $ui->get_permissions($acl_base,"opsi/licensePoolGeneric","licenses"));
132     $smarty->assign("writeable", preg_match("/w/",$ui->get_permissions($acl_base,"opsi/licensePoolGeneric","licenses")));
134     $smarty->assign("notUsedHosts", array_diff($this->getHosts(), $this->usedByHost));
135     $smarty->assign("boundToHost", $this->boundToHost[0]);
136     $smarty->assign("licenseKey", $this->licenseKey[0]);
137     return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
138   }
141   function getHosts()
142   {
143     $ret = array();
144     foreach($this->opsiHosts as $host){
145       $cn = $host['NAME'][0]['VALUE'];
146       $ret[$cn] = $cn;
147     }
148     return($ret);
149   }
152  
153   /* Save HTML inputs
154    */
155   function save_object()
156   {
158     if(isset($_POST['opsiLicensesPosted'])){
159       plugin::save_object();
161       if(isset($_POST['addLicenseUsage']) && isset($_POST['selectedHostToAdd'])){
162         $host = get_post('selectedHostToAdd');
163         if(!empty($host) && 
164             in_array($host,$this->getHosts()) && 
165             !in_array($host, $this->usedByHost)){
166           $this->usedByHost[] = $host;
167         }
168       }
170       if(isset($_POST['removeLicenseUsage']) && isset($_POST['selectedUsedHosts'])){
171         $todel = $_POST['selectedUsedHosts'];
172         foreach($todel as $host){
173           if(isset($this->usedByHost[$host])){
174             unset($this->usedByHost[$host]);
175           }
176         }
177       }
179       // Force licenseKey to be of type array.
180       if(!is_array($this->licenseKey)){
181         $this->licenseKey = array($this->licenseKey);
182       }
184       // BoundToHost maybe multiple too, later.
185       if(!is_array($this->boundToHost)){
186         $this->boundToHost = array($this->boundToHost);
187       }    
189       if($this->initially_was_account){
190         $this->cn = $this->orig_cn;
191         $this->licenseModel = $this->orig_licenseModel;
192       }
193     }
194   }  
197   /* Check user input and return a list of 'invalid input' messages.
198    */
199   function check()
200   {
201     $message = plugin::check();
203     // Very simple date input checks
204     if(!empty($this->expirationDate) && 
205        !preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/",$this->expirationDate)){
206       $message[] = msgPool::invalid(_("Expiration date"),$this->expirationDate,"","2009-02-23");
207     }
208     if(!empty($this->conclusionDate) && 
209        !preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/",$this->conclusionDate)){
210       $message[] = msgPool::invalid(_("Expiration date"),$this->conclusionDate,"","2009-02-23");
211     }
212     if(!empty($this->notificationDate) && 
213        !preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/",$this->notificationDate)){
214       $message[] = msgPool::invalid(_("Expiration date"),$this->notificationDate,"","2009-02-23");
215     }
217     if(empty($this->cn)){
218       $message[] = msgPool::required(_("Name"));
219     }
221     if(empty($this->licenseKey[0])){
222       $message[] = msgPool::required(_("License key"));
223     }
225     return($message);
226   }
227   
228  
229   /* Removes the object from the opsi database
230    */ 
231   function remove_from_parent() {}
234   /* Saves object modifications
235    */  
236   function save()
237   {
238     $data = array();
239     foreach($this->attributes as $target){
240       $data[$target] = $this->$target;
241     }
242     return($data);
243   }
244  
245   static function plInfo()
246   {
247     return (array(
248           "plShortName"   => _("Generic"),
249           "plDescription" => _("License generic"),
250           "plSelfModify"  => FALSE,
251           "plDepends"     => array(),
252           "plPriority"    => 1,
253           "plSection"     => array("administration"),
254           "plCategory"    => array("opsi"),
255           "plProvidedAcls"=> array(
256             "cn"                => _("Name"),
257             "description" => _("Description"))
258           ));
259   }
263 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
264 ?>