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 /* Do we need to flip is_account state? */
54 if (isset($_POST['modify_state'])){
55 $this->is_account= !$this->is_account;
56 }
58 /* Do we represent a valid group? */
59 if (!$this->is_account && $this->parent == NULL){
60 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
61 _("This 'dn' is no appgroup.")."</b>";
62 return ($display);
63 }
65 /* Show tab dialog headers */
66 $display= "";
67 if ($this->parent != NULL){
68 if ($this->is_account){
69 $display= $this->show_header(_("Remove applications"),
70 _("This group has application features enabled. You can disable them by clicking below."));
71 } else {
72 $display.= $this->show_header(_("Create applications"),
73 _("This group has application features disabled. You can enable them by clicking below."));
74 return ($display);
75 }
76 }
78 /* Check sorting variable */
79 $this->reload();
81 /* Delete app from group */
82 if (isset($_POST['del_app']) && isset ($_POST['used_apps'])){
83 foreach ($_POST['used_apps'] as $value){
84 unset ($this->used_apps["$value"]);
85 $this->removeApp($value);
86 }
87 }
89 /* Add app to group */
90 if (isset($_POST['add_app']) && isset($_POST['apps'])){
91 foreach ($_POST['apps'] as $value){
92 $this->used_apps["$value"]= $this->apps[$value];
93 asort($this->used_apps);
94 $this->addApp($value);
95 }
96 }
98 /* Edit application options? */
99 if (isset($_POST['edit_options']) && isset($_POST['used_apps'])){
100 $appname= $_POST['used_apps'][0];
102 /* We've got the appname, get parameters from ldap */
103 $ldap= $this->config->get_ldap_link();
104 $ldap->cd($this->config->current['BASE']);
105 $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
106 if ($ldap->count() != 1){
107 print_red (_("The selected application name is not uniq. Please check your LDAP."));
108 } else {
109 $attrs= $ldap->fetch();
110 if(isset($attrs['gosaApplicationParameter'])){
111 $this->dialog= TRUE;
113 /* Fill name and value arrays */
114 for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
115 $option= preg_replace('/^[^:]+:/', '',
116 $attrs['gosaApplicationParameter'][$i]);
117 $name= preg_replace('/:.*$/', '',
118 $attrs['gosaApplicationParameter'][$i]);
119 $this->option_name[$i]= $name;
121 /* Fill with values from application, default should be
122 loaded by the external scripts */
123 if (isset($this->appoption[$name])){
124 $this->option_value[$i]= $this->appoption[$name];
125 }
126 }
128 /* Create edit field */
129 $table= "<table summary=\"\">";
130 for ($i= 0; $i < count($this->option_name); $i++){
131 if (isset($this->option_value[$i])){
132 $value= $this->option_value[$i];
133 } else {
134 $value= "";
135 }
136 $table.="<tr><td>".$this->option_name[$i]."</td><td>".
137 "<input name=\"value$i\" size=60 maxlength=250 ".
138 "value=\"".$value."\"><br></td></tr>";
139 }
140 $table.= "</table>";
141 $this->table= $table;
142 } else {
143 print_red (_("The selected application has no options."));
144 }
145 }
146 }
148 /* Cancel edit options? */
149 if (isset($_POST['edit_options_cancel'])){
150 $this->dialog= FALSE;
151 }
153 /* Finish edit options? */
154 if (isset($_POST['edit_options_finish'])){
155 $this->dialog= FALSE;
157 /* Save informations passed by the user */
158 $this->option_value= array();
159 for ($i= 0; $i<count($this->option_name); $i++){
160 $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
161 $this->is_modified= TRUE;
162 }
163 }
165 /* Prepare templating stuff */
166 $smarty= get_smarty();
167 $smarty->assign("used_apps", $this->used_apps);
168 $apps= array();
169 foreach ($this->apps as $key => $value){
170 if (!array_key_exists($key, $this->used_apps)){
171 $apps["$key"]= "$value";
172 }
173 }
174 $smarty->assign("apps", $apps);
176 /* Show main page */
177 if ($this->dialog){
178 $smarty->assign("table", $this->table);
179 $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
180 } else {
181 $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
182 }
184 return ($display);
185 }
188 function remove_from_parent()
189 {
190 plugin::remove_from_parent();
192 $this->attrs["gosaMemberApplication"]= array();
194 $ldap= $this->config->get_ldap_link();
195 $ldap->cd($this->dn);
196 $ldap->modify($this->attrs);
197 show_ldap_error($ldap->get_error());
199 /* Optionally execute a command after we're done */
200 $this->handle_post_events("remove");
201 }
204 /* Save data to object */
205 # function save_object()
206 # {
207 # plugin::save_object();
208 # }
211 /* Save to LDAP */
212 function save()
213 {
214 plugin::save();
216 /* Copy members */
217 $this->attrs["gosaMemberApplication"]= array();
218 foreach ($this->gosaMemberApplication as $val){
219 $this->attrs["gosaMemberApplication"][]= stripslashes($val);
220 }
222 /* Are there application parameters to be saved */
223 $this->attrs['gosaApplicationParameter']= array();
224 foreach($this->appoption as $name => $value){
225 if ($value != ""){
226 $this->attrs['gosaApplicationParameter'][]= "$name:$value";
227 }
228 }
230 /* Write back to LDAP */
231 $ldap= $this->config->get_ldap_link();
232 $ldap->cd($this->dn);
233 $ldap->modify($this->attrs);
234 show_ldap_error($ldap->get_error());
236 /* Optionally execute a command after we're done */
237 if ($this->initially_was_account == $this->is_account){
238 if ($this->is_modified){
239 $this->handle_post_events("mofify");
240 }
241 } else {
242 $this->handle_post_events("add");
243 }
245 }
247 function check()
248 {
249 $message= array();
250 return ($message);
251 }
254 function reload()
255 {
256 /* Generate applist */
257 $this->apps= array();
258 $ldap= $this->config->get_ldap_link();
259 $ldap->cd ($this->config->current['BASE']);
260 $ldap->search ("(objectClass=gosaApplication)");
261 while ($attrs= $ldap->fetch()){
262 if (isset($attrs["description"][0])){
263 $this->apps[$attrs["cn"][0]]=
264 $attrs["cn"][0]." (".
265 $attrs["description"][0].")";
266 } else {
267 $this->apps[$attrs["cn"][0]]=
268 $attrs["cn"][0];
269 }
270 }
271 natcasesort ($this->apps);
272 reset ($this->apps);
274 $this->used_apps= array();
276 foreach ($this->gosaMemberApplication as $value){
277 if(!isset($this->apps[$value])){
278 print_red(sprintf(_("The application named %s is no longer available and has been removed."),$value));
279 $test = $this->gosaMemberApplication;
280 $test = array_flip($test);
281 unset($test[$value]);
282 $this->gosaMemberApplication = array_flip($test);
283 }else{
284 $this->used_apps[$value]= $this->apps[$value];
285 }
286 }
287 }
289 function addApp($cn)
290 {
291 $this->gosaMemberApplication[]= $cn;
292 $this->gosaMemberApplication= array_unique($this->gosaMemberApplication);
293 $this->is_modified= TRUE;
294 }
297 function removeApp($cn)
298 {
299 $temp= array();
300 foreach ($this->gosaMemberApplication as $value){
301 if ($value != $cn){
302 $temp[]= $value;
303 }
304 }
305 $this->gosaMemberApplication= $temp;
306 $this->is_modified= TRUE;
307 }
309 }
311 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
312 ?>