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 /* attribute list for save action */
23 var $attributes= array();
24 var $objectclasses= array("gosaApplicationGroup");
26 function appgroup ($config, $dn= NULL)
27 {
28 plugin::plugin ($config, $dn);
30 /* Load member applications */
31 if (isset ($this->attrs["gosaMemberApplication"][0])){
32 for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
33 $this->gosaMemberApplication[]=
34 $this->attrs["gosaMemberApplication"][$i];
35 }
36 }
38 /* Load application options */
39 if (isset($this->attrs['gosaApplicationParameter'])){
40 for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
41 $option= preg_replace('/^[^:]+:/', '',
42 $this->attrs['gosaApplicationParameter'][$i]);
43 $name= preg_replace('/:.*$/', '',
44 $this->attrs['gosaApplicationParameter'][$i]);
45 $this->appoption[$name]= $option;
46 }
47 }
49 }
51 function execute()
52 {
53 /* Call parent execute */
54 plugin::execute();
56 /* Do we need to flip is_account state? */
57 if (isset($_POST['modify_state'])){
58 $this->is_account= !$this->is_account;
59 }
61 /* Do we represent a valid group? */
62 if (!$this->is_account && $this->parent == NULL){
63 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
64 _("This 'dn' is no appgroup.")."</b>";
65 return ($display);
66 }
68 /* Show tab dialog headers */
69 $display= "";
70 if ($this->parent != NULL){
71 if ($this->is_account){
72 $display= $this->show_header(_("Remove applications"),
73 _("This group has application features enabled. You can disable them by clicking below."));
74 } else {
75 $display.= $this->show_header(_("Create applications"),
76 _("This group has application features disabled. You can enable them by clicking below."));
77 return ($display);
78 }
79 }
81 /* Check sorting variable */
82 $this->reload();
84 /* Delete app from group */
85 if (isset($_POST['del_app']) && isset ($_POST['used_apps'])){
86 foreach ($_POST['used_apps'] as $value){
87 unset ($this->used_apps["$value"]);
88 $this->removeApp($value);
89 }
90 }
92 /* Add app to group */
93 if (isset($_POST['add_app']) && isset($_POST['apps'])){
94 foreach ($_POST['apps'] as $value){
95 $this->used_apps["$value"]= $this->apps[$value];
96 asort($this->used_apps);
97 $this->addApp($value);
98 }
99 }
101 /* Edit application options? */
102 if (isset($_POST['edit_options']) && isset($_POST['used_apps'])){
103 $appname= $_POST['used_apps'][0];
105 /* We've got the appname, get parameters from ldap */
106 $ldap= $this->config->get_ldap_link();
107 $ldap->cd($this->config->current['BASE']);
108 $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
109 if ($ldap->count() != 1){
110 print_red (_("The selected application name is not uniq. Please check your LDAP."));
111 } else {
112 $attrs= $ldap->fetch();
113 if(isset($attrs['gosaApplicationParameter'])){
114 $this->dialog= TRUE;
116 /* Fill name and value arrays */
117 for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
118 $option= preg_replace('/^[^:]+:/', '',
119 $attrs['gosaApplicationParameter'][$i]);
120 $name= preg_replace('/:.*$/', '',
121 $attrs['gosaApplicationParameter'][$i]);
122 $this->option_name[$i]= $name;
124 /* Fill with values from application, default should be
125 loaded by the external scripts */
126 if (isset($this->appoption[$name])){
127 $this->option_value[$i]= $this->appoption[$name];
128 }
129 }
131 /* Create edit field */
132 $table= "<table summary=\"\">";
133 for ($i= 0; $i < count($this->option_name); $i++){
134 if (isset($this->option_value[$i])){
135 $value= $this->option_value[$i];
136 } else {
137 $value= "";
138 }
139 $table.="<tr><td>".$this->option_name[$i]."</td><td>".
140 "<input name=\"value$i\" size=60 maxlength=250 ".
141 "value=\"".$value."\"><br></td></tr>";
142 }
143 $table.= "</table>";
144 $this->table= $table;
145 } else {
146 print_red (_("The selected application has no options."));
147 }
148 }
149 }
151 /* Cancel edit options? */
152 if (isset($_POST['edit_options_cancel'])){
153 $this->dialog= FALSE;
154 }
156 /* Finish edit options? */
157 if (isset($_POST['edit_options_finish'])){
158 $this->dialog= FALSE;
160 /* Save informations passed by the user */
161 $this->option_value= array();
162 for ($i= 0; $i<count($this->option_name); $i++){
163 $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
164 $this->is_modified= TRUE;
165 }
166 }
168 /* Prepare templating stuff */
169 $smarty= get_smarty();
170 $smarty->assign("used_apps", $this->used_apps);
171 $apps= array();
172 foreach ($this->apps as $key => $value){
173 if (!array_key_exists($key, $this->used_apps)){
174 $apps["$key"]= "$value";
175 }
176 }
177 $smarty->assign("apps", $apps);
179 /* Show main page */
180 if ($this->dialog){
181 $smarty->assign("table", $this->table);
182 $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
183 } else {
184 $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
185 }
187 return ($display);
188 }
191 function remove_from_parent()
192 {
193 plugin::remove_from_parent();
195 $this->attrs["gosaMemberApplication"]= array();
197 $ldap= $this->config->get_ldap_link();
198 $ldap->cd($this->dn);
199 $ldap->modify($this->attrs);
200 show_ldap_error($ldap->get_error());
202 /* Optionally execute a command after we're done */
203 $this->handle_post_events("remove");
204 }
207 /* Save data to object */
208 # function save_object()
209 # {
210 # plugin::save_object();
211 # }
214 /* Save to LDAP */
215 function save()
216 {
217 plugin::save();
219 /* Copy members */
220 $this->attrs["gosaMemberApplication"]= array();
221 foreach ($this->gosaMemberApplication as $val){
222 $this->attrs["gosaMemberApplication"][]= stripslashes($val);
223 }
225 /* Are there application parameters to be saved */
226 $this->attrs['gosaApplicationParameter']= array();
227 foreach($this->appoption as $name => $value){
228 if ($value != ""){
229 $this->attrs['gosaApplicationParameter'][]= "$name:$value";
230 }
231 }
233 /* Write back to LDAP */
234 $ldap= $this->config->get_ldap_link();
235 $ldap->cd($this->dn);
236 $ldap->modify($this->attrs);
237 show_ldap_error($ldap->get_error());
239 /* Optionally execute a command after we're done */
240 if ($this->initially_was_account == $this->is_account){
241 if ($this->is_modified){
242 $this->handle_post_events("mofify");
243 }
244 } else {
245 $this->handle_post_events("add");
246 }
248 }
250 function check()
251 {
252 $message= array();
253 return ($message);
254 }
257 function reload()
258 {
259 /* Generate applist */
260 $this->apps= array();
261 $ldap= $this->config->get_ldap_link();
262 $ldap->cd ($this->config->current['BASE']);
263 $ldap->search ("(objectClass=gosaApplication)");
264 while ($attrs= $ldap->fetch()){
265 if (isset($attrs["description"][0])){
266 $this->apps[$attrs["cn"][0]]=
267 $attrs["cn"][0]." (".
268 $attrs["description"][0].")";
269 } else {
270 $this->apps[$attrs["cn"][0]]=
271 $attrs["cn"][0];
272 }
273 }
274 natcasesort ($this->apps);
275 reset ($this->apps);
277 $this->used_apps= array();
279 foreach ($this->gosaMemberApplication as $value){
280 if(!isset($this->apps[$value])){
281 print_red(sprintf(_("The application named %s is no longer available and has been removed."),$value));
282 $test = $this->gosaMemberApplication;
283 $test = array_flip($test);
284 unset($test[$value]);
285 $this->gosaMemberApplication = array_flip($test);
286 }else{
287 $this->used_apps[$value]= $this->apps[$value];
288 }
289 }
290 }
292 function addApp($cn)
293 {
294 $this->gosaMemberApplication[]= $cn;
295 $this->gosaMemberApplication= array_unique($this->gosaMemberApplication);
296 $this->is_modified= TRUE;
297 }
300 function removeApp($cn)
301 {
302 $temp= array();
303 foreach ($this->gosaMemberApplication as $value){
304 if ($value != $cn){
305 $temp[]= $value;
306 }
307 }
308 $this->gosaMemberApplication= $temp;
309 $this->is_modified= TRUE;
310 }
312 }
314 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
315 ?>