1 <?php
2 class appgroup extends plugin
3 {
4 /* CLI vars */
5 var $cli_summary= "Manage application groups";
6 var $cli_description= "Some longer text\nfor help";
7 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9 /* Appgroup attributes */
10 var $gosaMemberApplication= array();
12 /* Helpers */
13 var $department= "";
14 var $apps= array();
15 var $used_apps= array();
16 var $opt_edit= FALSE;
17 var $option_name= array();
18 var $option_value= array();
19 var $appoption= array();
20 var $table= "";
22 var $curbase = "";
24 /* attribute list for save action */
25 var $attributes= array();
26 var $objectclasses= array("gosaApplicationGroup");
28 function appgroup ($config, $dn= NULL)
29 {
30 plugin::plugin ($config, $dn);
32 /* Load member applications */
33 if (isset ($this->attrs["gosaMemberApplication"][0])){
34 for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
35 $this->gosaMemberApplication[]=
36 $this->attrs["gosaMemberApplication"][$i];
37 }
38 }
40 /* Load application options */
41 if (isset($this->attrs['gosaApplicationParameter'])){
42 for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
43 $option= preg_replace('/^[^:]+:/', '',
44 $this->attrs['gosaApplicationParameter'][$i]);
45 $name= preg_replace('/:.*$/', '',
46 $this->attrs['gosaApplicationParameter'][$i]);
47 $this->appoption[$name]= $option;
48 }
49 }
50 $this->curbase = $this->config->current['BASE'];
51 }
53 function execute()
54 {
55 /* Call parent execute */
56 plugin::execute();
57 if((isset($_GET['act']))&&($_GET['act']=="depopen")){
58 $dep = base64_decode($_GET['depid']);
59 if(isset($this->config->idepartments[$dep])){
60 $this->curbase = $dep;
61 }
62 }
64 /* Do we need to flip is_account state? */
65 if (isset($_POST['modify_state'])){
66 $this->is_account= !$this->is_account;
67 }
69 /* Do we represent a valid group? */
70 if (!$this->is_account && $this->parent == NULL){
71 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
72 _("This 'dn' is no appgroup.")."</b>";
73 return ($display);
74 }
76 /* Show tab dialog headers */
77 $display= "";
78 if ($this->parent != NULL){
79 if ($this->is_account){
80 $display= $this->show_header(_("Remove applications"),
81 _("This group has application features enabled. You can disable them by clicking below."));
82 } else {
83 $display.= $this->show_header(_("Create applications"),
84 _("This group has application features disabled. You can enable them by clicking below."));
85 return ($display);
86 }
87 }
89 /* Check sorting variable */
90 $this->reload();
92 /* Delete app from group */
93 if (isset($_POST['del_app']) && isset ($_POST['used_apps'])){
94 foreach ($_POST['used_apps'] as $value){
95 unset ($this->used_apps["$value"]);
96 $this->removeApp($value);
97 }
98 }
100 /* Add app to group */
101 if (isset($_POST['add_app']) && isset($_POST['apps'])){
102 foreach ($_POST['apps'] as $value){
103 $this->used_apps["$value"]= $this->apps[$value];
104 asort($this->used_apps);
105 $this->addApp($value);
106 }
107 }
109 /* Add group with post */
110 if((isset($_GET['act']))&&($_GET['act']=="add")){
111 $this->used_apps[$_GET['id']]= $_GET['id'];
112 asort($this->used_apps);
113 $this->addApp($_GET['id']);
114 }
116 /* Edit application options? */
117 if (isset($_POST['edit_options']) && isset($_POST['used_apps'])){
118 $appname= $_POST['used_apps'][0];
120 /* We've got the appname, get parameters from ldap */
121 $ldap= $this->config->get_ldap_link();
122 $ldap->cd($this->config->current['BASE']);
123 $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
124 if ($ldap->count() != 1){
125 print_red (_("The selected application name is not uniq. Please check your LDAP."));
126 } else {
127 $attrs= $ldap->fetch();
128 if(isset($attrs['gosaApplicationParameter'])){
129 $this->dialog= TRUE;
131 /* Fill name and value arrays */
132 for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
133 $option= preg_replace('/^[^:]+:/', '',
134 $attrs['gosaApplicationParameter'][$i]);
135 $name= preg_replace('/:.*$/', '',
136 $attrs['gosaApplicationParameter'][$i]);
137 $this->option_name[$i]= $name;
139 /* Fill with values from application, default should be
140 loaded by the external scripts */
141 if (isset($this->appoption[$name])){
142 $this->option_value[$i]= $this->appoption[$name];
143 }
144 }
146 /* Create edit field */
147 $table= "<table summary=\"\">";
148 for ($i= 0; $i < count($this->option_name); $i++){
149 if (isset($this->option_value[$i])){
150 $value= $this->option_value[$i];
151 } else {
152 $value= "";
153 }
154 $table.="<tr><td>".$this->option_name[$i]."</td><td>".
155 "<input name=\"value$i\" size=60 maxlength=250 ".
156 "value=\"".$value."\"><br></td></tr>";
157 }
158 $table.= "</table>";
159 $this->table= $table;
160 } else {
161 print_red (_("The selected application has no options."));
162 }
163 }
164 }
166 /* Cancel edit options? */
167 if (isset($_POST['edit_options_cancel'])){
168 $this->dialog= FALSE;
169 }
171 /* Finish edit options? */
172 if (isset($_POST['edit_options_finish'])){
173 $this->dialog= FALSE;
175 /* Save informations passed by the user */
176 $this->option_value= array();
177 for ($i= 0; $i<count($this->option_name); $i++){
178 $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
179 $this->is_modified= TRUE;
180 }
181 }
183 /* Prepare templating stuff */
184 $smarty= get_smarty();
185 $smarty->assign("used_apps", $this->used_apps);
186 $apps= array();
187 foreach ($this->apps as $key => $value){
188 if (!array_key_exists($key, $this->used_apps)){
189 $apps["$key"]= "$value";
190 }
191 }
193 $div = new DivSelectBox("appgroup");
195 $div->SetHeight(300);
197 /* NEW LIST MANAGMENT
198 * We also need to search for the departments
199 * So we are able to navigate like in konquerer
200 */
202 $ldap = $this->config->get_ldap_link();
203 $ldap->cd($this->curbase) ;
204 $ldap->ls("(objectClass=gosaDepartment)");
205 $departments= array();
206 $tmp = array();
207 while ($value = $ldap->fetch()){
208 $tmp[strtolower($value['dn']).$value['dn']]=$value;
209 }
210 ksort($tmp);
211 foreach($tmp as $value){
212 if($value["description"][0]!=".."){
213 $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
214 }else{
215 $departments[$value['dn']]=convert_department_dn($value['dn']);
216 }
217 }
219 /* END NEW LIST MANAGMENT
220 */
222 $linkopen = "<a href='?plug=".$_GET['plug']."&act=depopen&depid=%s'>%s</a>";
223 $linkadd = "<a href='?plug=".$_GET['plug']."&act=add&id=%s'>%s</a>";
225 $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
226 if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
227 $div->AddEntry(array(
228 array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
229 "attach"=>"style='border:0px;'")
230 ));
231 }
232 foreach($departments as $key => $app){
233 $div->AddEntry(array(
234 array("string"=>"<img src='images/folder.png' alt='"._("department")."'> ".sprintf($linkopen,base64_encode($key),$app),
235 "attach"=>"style='border:0px;'")
236 ));
237 }
239 foreach($apps as $key => $app){
240 $div->AddEntry(array(
241 array("string"=>"<img src='images/select_application.png' alt='"._("application")."'> ".sprintf($linkadd,$key,$app),
242 "attach"=>"style='border:0px;'")
243 ));
244 }
246 $smarty->assign("List", $div->DrawList());
247 $smarty->assign("apps", $apps);
249 /* Show main page */
250 if ($this->dialog){
251 $smarty->assign("table", $this->table);
252 $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
253 } else {
254 $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
255 }
257 return ($display);
258 }
261 function remove_from_parent()
262 {
263 plugin::remove_from_parent();
265 $this->attrs["gosaMemberApplication"]= array();
267 $ldap= $this->config->get_ldap_link();
268 $ldap->cd($this->dn);
269 $ldap->modify($this->attrs);
270 show_ldap_error($ldap->get_error());
272 /* Optionally execute a command after we're done */
273 $this->handle_post_events("remove");
274 }
277 /* Save data to object */
278 # function save_object()
279 # {
280 # plugin::save_object();
281 # }
284 /* Save to LDAP */
285 function save()
286 {
287 plugin::save();
289 /* Copy members */
290 $this->attrs["gosaMemberApplication"]= array();
291 foreach ($this->gosaMemberApplication as $val){
292 $this->attrs["gosaMemberApplication"][]= stripslashes($val);
293 }
295 /* Are there application parameters to be saved */
296 $this->attrs['gosaApplicationParameter']= array();
297 foreach($this->appoption as $name => $value){
298 if ($value != ""){
299 $this->attrs['gosaApplicationParameter'][]= "$name:$value";
300 }
301 }
303 /* Write back to LDAP */
304 $ldap= $this->config->get_ldap_link();
305 $ldap->cd($this->dn);
306 $ldap->modify($this->attrs);
307 show_ldap_error($ldap->get_error());
309 /* Optionally execute a command after we're done */
310 if ($this->initially_was_account == $this->is_account){
311 if ($this->is_modified){
312 $this->handle_post_events("mofify");
313 }
314 } else {
315 $this->handle_post_events("add");
316 }
318 }
320 function check()
321 {
322 $message= array();
323 return ($message);
324 }
327 function reload()
328 {
329 /* Generate applist */
330 $this->apps= array();
331 $ldap= $this->config->get_ldap_link();
332 $ldap->cd ("ou=apps,".$this->curbase);
334 $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
335 while ($attrs= $ldap->fetch()){
336 if (isset($attrs["description"][0])){
337 $this->apps[$attrs["cn"][0]]=
338 $attrs["cn"][0]." (".
339 $attrs["description"][0].")";
340 } else {
341 $this->apps[$attrs["cn"][0]]=
342 $attrs["cn"][0];
343 }
344 }
345 natcasesort ($this->apps);
346 reset ($this->apps);
348 foreach ($this->gosaMemberApplication as $value){
349 $this->used_apps[$value]= $value;
350 }
351 }
353 function addApp($cn)
354 {
355 $this->gosaMemberApplication[]= $cn;
356 $this->gosaMemberApplication= array_unique($this->gosaMemberApplication);
357 $this->is_modified= TRUE;
358 }
361 function removeApp($cn)
362 {
363 $temp= array();
364 foreach ($this->gosaMemberApplication as $value){
365 if ($value != $cn){
366 $temp[]= $value;
367 }
368 }
369 $this->gosaMemberApplication= $temp;
370 $this->is_modified= TRUE;
371 }
373 }
375 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
376 ?>