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 $cn = "";
28 var $orig_cn = "";
29 var $description = "";
31 var $orig_dn = "";
32 var $data = array();
33 var $orig_data = array();
35 var $productIds = array();
36 var $softwareIds= array();
37 var $licenses= array();
39 var $availableProductIds = array();
41 var $attributes =array("cn","description");
43 var $si = NULL;
45 function __construct(&$config,$dn)
46 {
47 $this->config = $config;
48 $this->dn = $this->orig_dn = $dn;
50 $this->si = new opsiLicenceHandler($this->config);
52 $this->is_account=TRUE;
53 if($this->dn == "new"){
54 $this->initially_was_account = FALSE;
55 }else{
56 $this->initially_was_account = TRUE;
57 $this->cn = $this->orig_cn = preg_replace("/^opsi:cn=([^,]*),.*$/","\\1",$dn);
58 }
60 // Extract pool name out of the fake dn.
61 $this->init();
62 }
65 function init()
66 {
67 // Load local Boot Products
68 $res = $this->si->get_local_products();
69 if($this->si->is_error()){
70 $this->init_successfull = FALSE;
71 return(FALSE);
72 }
73 $this->availableProductIds=array_keys($res);
75 // Load Pool
76 if(!$this->initially_was_account){
77 $this->init_successfull = TRUE;
78 }else{
80 $res = $this->si->getPool($this->cn);
81 if($this->si->is_error()){
82 $this->init_successfull = FALSE;
83 return(FALSE);
84 }else{
85 $this->data = $this->orig_data = $res;
86 $this->description = $this->data['description'][0];
88 // Load software IDs
89 $this->softwareIds = array();
90 if(isset($this->data['softwareId'])){
91 for($i = 0; $i < $this->data['softwareId']['count']; $i++){
92 $this->softwareIds[] = $this->data['softwareId'][$i];
93 }
94 }
96 // Load product IDs
97 $this->productIds = array();
98 if(isset($this->data['productId'])){
99 for($i = 0; $i < $this->data['productId']['count']; $i++){
100 $this->productIds[] = $this->data['productId'][$i];
101 }
102 }
103 $this->init_successfull = TRUE;
104 return;
105 }
106 }
107 }
110 function execute()
111 {
112 // Handle initialization failures.
113 if(isset($_POST['retry_init'])) $this->init();
114 if(!$this->init_successfull){
115 $smarty = get_smarty();
116 $smarty->assign("init_successfull", $this->init_successfull);
117 return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
118 }
120 $smarty = get_smarty();
122 // Assign ACls
123 $plInfo = $this->plInfo();
124 foreach($plInfo['plProvidedAcls'] as $name => $desc){
125 $smarty->assign("{$name}ACL",$this->getacl($name));
126 }
127 foreach($this->attributes as $attr){
128 $smarty->assign($attr,$this->$attr);
129 }
131 $smarty->assign("init_successfull", $this->init_successfull);
132 $smarty->assign("availableProductIds", array_diff( $this->availableProductIds, $this->productIds));
133 $smarty->assign("productIds", $this->productIds);
134 $smarty->assign("softwareIds", $this->softwareIds);
135 $smarty->assign("licenses", $this->licenses);
136 $smarty->assign("initially_was_account", $this->initially_was_account);
137 return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
138 }
141 /* Save HTML inputs
142 */
143 function save_object()
144 {
145 if(isset($_POST['opsiLicensesPosted'])){
146 plugin::save_object();
148 // Restore license cn, to avoid creating a copy...
149 if($this->initially_was_account) $this->cn = $this->orig_cn;
151 // We've to add prodcuts here
152 if(isset($_POST['availableProduct']) && isset($_POST['addProduct'])){
153 $pro = get_post('availableProduct');
154 if(isset($this->availableProductIds[$pro]) && !in_array($this->availableProductIds[$pro], $this->productIds)){
155 $this->productIds[] =$this->availableProductIds[$pro];
156 }
157 }
159 // We've to remove products here
160 if(isset($_POST['productIds']) && isset($_POST['removeProduct'])){
161 foreach($_POST['productIds'] as $key){
162 if(isset($this->productIds[$key])){
163 unset($this->productIds[$key]);
164 }
165 }
166 }
168 // We've to add software here
169 if(isset($_POST['newSoftwareId']) && isset($_POST['addSoftware'])){
170 $soft = trim(get_post('newSoftwareId'));
171 if(!empty($soft) && !in_array($soft, $this->softwareIds)){
172 $this->softwareIds[] = $soft;
173 }
174 }
176 // We've to remove software Ids here
177 if(isset($_POST['softwareIds']) && isset($_POST['removeSoftware'])){
178 foreach($_POST['softwareIds'] as $key){
179 if(isset($this->softwareIds[$key])){
180 unset($this->softwareIds[$key]);
181 }
182 }
183 }
184 }
185 }
188 /* Check user input and return a list of 'invalid input' messages.
189 */
190 function check()
191 {
192 $message = plugin::check();
193 return($message);
194 }
198 /* Removes the object from the opsi database
199 */
200 function remove_from_parent()
201 {
202 $this->si->deletePool($this->orig_cn);
203 if($this->si->is_error()){
204 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
205 }else{
207 // Trigger remove signal
208 $this->handle_post_events("remove");
209 }
211 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
212 }
215 /* Saves object modifications
216 */
217 function save()
218 {
219 plugin::save();
221 // Send modify/add events
222 $mode = "modify";
223 if($this->orig_dn == "new"){
224 $mode = "add";
225 }
227 $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);#
228 if($this->si->is_error()){
229 msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
230 }else{
231 $this->handle_post_events($mode);
232 }
234 // Log action
235 if($mode == "modify"){
236 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
237 }else{
238 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error());
239 }
241 return 0;
242 }
244 static function plInfo()
245 {
246 return (array(
247 "plShortName" => _("Generic"),
248 "plDescription" => _("License generic"),
249 "plSelfModify" => FALSE,
250 "plDepends" => array(),
251 "plPriority" => 1,
252 "plSection" => array("administration"),
253 "plCategory" => array("opsi"),
254 "plProvidedAcls"=> array(
255 "cn" => _("Name"),
256 "description" => _("Description"))
257 ));
258 }
259 }
262 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
263 ?>