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