Code

Added several comments.
[gosa.git] / plugins / admin / applications / class_applicationParameters.inc
1 <?php
2 class applicationParameters extends plugin
3 {
4   /* CLI vars */
5   var $cli_summary= "Manage application class parameters";
6   var $cli_description= "Some longer text\nfor help";
7   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9   /* Parameters  */
10   var $option_name= array();
11   var $option_value= array();
13   /* attribute list for save action */
15   var $CopyPasteVars = array("option_name","option_value");
16   var $attributes= array("gosaApplicationParameter");
17   var $objectclasses= array();
19   function applicationParameters ($config, $dn= NULL, $parent= NULL)
20   {
21         plugin::plugin ($config, $dn, $parent);
23         $this->gosaApplicationParameter = array();
25         if (isset($this->attrs['gosaApplicationParameter'])){
26                 $this->is_account= TRUE;
27                 for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
28                         $option= preg_replace('/^[^:]+:/', '',
29                                         $this->attrs['gosaApplicationParameter'][$i]);
30                         $name= preg_replace('/:.*$/', '',
31                                         $this->attrs['gosaApplicationParameter'][$i]);
32                         $this->option_name[$i]= $name;
33                         $this->option_value[$i]= $option;
34                 }
35         } else {
36                 $this->is_account= FALSE;
37         }
38   }
40   function execute()
41   {
42         /* Call parent execute */
43         plugin::execute();
45         /* Do we need to flip is_account state? */
46         if (isset($_POST['modify_state'])){
47                 $this->is_account= !$this->is_account;
48         }
50         /* Show tab dialog headers */
51         $display= "";
52         if ($this->parent != NULL){
53                 if ($this->is_account){
54                         $display= $this->show_header(_("Remove options"),
55                                 _("This application has options. You can disable them by clicking below."));
56                 } else {
57                         $display= $this->show_header(_("Create options"),
58                                 _("This application has options disabled. You can enable them by clicking below."));
59                         $this->parent->by_object['application']->generateTemplate();
60                         return ($display);
61                 }
62         }
64         /* Add option to list */
65         if (isset($_POST['add_option'])){
66                 $i= count($this->option_name);
67                 $this->option_name[$i]= "";
68                 $this->option_value[$i]= "";
69         }
71         /* Remove value from list */
72         for ($i= 0; $i<count($this->option_name); $i++){
73                 if (isset($_POST["remove$i"])){
74                         $k= 0;
75                         $on= array();
76                         $ov= array();
77                         for ($j= 0; $j<count($this->option_name); $j++){
78                                 if ($j != $i){
79                                         $on[$k]= $this->option_name[$j];
80                                         $ov[$k]= $this->option_value[$j];
81                                         $k++;
82                                 }
83                         }
84                         $this->option_name= $on;
85                         $this->option_value= $ov;
86                         break;
87                 }
88         }
90         /* Generate list of attributes */
91         if (count($this->option_name) == 0){
92                 $this->option_name[]= "";
93                 $this->option_value[]= "";
94         }
96         $mode= "";
97         if (chkacl($this->acl, "create") != ""){
98                 $mode= "disabled";
99         }
101         $table= "<table summary=\"\"><tr><td>"._("Variable")."</td><td>"._("Default value")."</td><td></td></tr>";
102         if (count ($this->option_name)){
103                 for ($i= 0; $i < count($this->option_name); $i++){
104                         $table.="<tr><td><input name=\"option$i\" size=25 maxlength=50 ".
105                                 "value=\"".$this->option_name[$i]."\" $mode></td><td><input name=\"value$i\" ".
106                                 "size=60 maxlength=250 value=\"".$this->option_value[$i]."\" $mode><br></td><td>".
107                                 "<input type=\"submit\" name=\"remove$i\" value=\"".
108                                 _("Remove")."\" $mode></td></tr>";
109                 }
110         }
111         $table.= "</table>";
113         if ($mode == ""){
114                 $table.="<input type=\"submit\" name=\"add_option\" value=\""._("Add option")."\">";
115         }
117         /* Show main page */
118         $smarty= get_smarty();
119         $smarty->assign("table", $table);
120         $display.= $smarty->fetch(get_template_path('parameters.tpl', TRUE));
121         $this->parent->by_object['application']->generateTemplate();
122         return ($display);
123   }
125   function remove_from_parent()
126   {
127         $ldap= $this->config->get_ldap_link();
129         /* Zero attributes */
130         $this->attrs= array();
131         $this->attrs['gosaApplicationParameter']= array();
133         $ldap->cd($this->dn);
134         @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
135                 $this->attributes, "Save");
136         $this->cleanup();
137         $ldap->modify ($this->attrs); 
139         show_ldap_error($ldap->get_error(), _("Removing application parameters failed"));
141         /* Optionally execute a command after we're done */
142         $this->handle_post_events('remove');
143   }
146   /* Save data to object */
147   function save_object()
148   {
149         if (isset($_POST['option0'])){
150                 for ($i= 0; $i<count($this->option_name); $i++){
151                         $this->option_name[$i]= $_POST["option$i"];
152                         $this->option_value[$i]= "";
153                         if ($_POST["value$i"] != ""){
154                                 $this->option_value[$i]= $_POST["value$i"];
155                         }
156                 }
157         }
158   }
161   /* Check values */
162   function check()
163   {
164         /* Call common method to give check the hook */
165         $message= plugin::check();
167         /* Check for valid option names */
168         for ($i= 0; $i<count($this->option_name); $i++){
169                 if (!preg_match ("/^[a-z0-9_]+$/i", $this->option_name[$i])){
170                         $message[]= sprintf(_("Value '%s' specified as option name is not valid."), 
171                                         $this->option_name[$i]);
172                 }
173         }
174         
175         return $message;
176   }
179   /* Save to LDAP */
180   function save()
181   {
182         /* Generate values */
183         $this->attrs= array();
184         if (count($this->option_name) == 0){
185                 $this->attrs['gosaApplicationParameter']= array();
186         } else {
187                 for ($i= 0; $i<count($this->option_name); $i++){
188                         $this->attrs['gosaApplicationParameter'][]= $this->option_name[$i].
189                                 ":".$this->option_value[$i];
190                 }
191         }
193         /* Write back to ldap */
194         $ldap= $this->config->get_ldap_link();
195         $ldap->cd($this->dn);
196         @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
197                 $this->attributes, "Save");
198         $this->cleanup();
199         $ldap->modify ($this->attrs); 
201         show_ldap_error($ldap->get_error(), _("Saving applications parameters failed"));
203         /* Optionally execute a command after we're done */
204         $this->handle_post_events('modify');
205   }
209 ?>