Code

Parameters are sorted now
[gosa.git] / plugins / gofon / macro / class_gofonMacroParameters.inc
1 <?php
3 class macroParameter extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Handling of GOsa's application object";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* application attributes */
11   var $base= "";
12   var $goFonMacroParameter =array();
13   var $type_shortcut= array("string" => array("selected", "", ""),
14       "combo"  => array("", "selected", ""),
15       "bool"   => array("", "", "selected"));
17   /* attribute list for save action */
18   var $attributes= array("base","goFonMacroParameter");
19   var $objectclasses= array("top", "goFonMacro");
22   function macroParameter ($config, $dn= NULL)
23   {
24     plugin::plugin ($config, $dn);
26     $tmp = array();  // temporary Var 
27     $tmp2 = array(); // temporary Var ...
28     $tmp3 = "";
29     $ldap= $config->get_ldap_link();
31     $this->dn = $dn;
33     /* This is always an account */
34     $this->is_account= TRUE;
36     /* Edit or new one ?*/
37     if ($this->dn == "new"){
38       $ui= get_userinfo();
39       $this->base= dn2base($ui->dn);
40     } else {
41       $this->base= dn2base($this->dn);
42     }
44     /* initialising macro parameter */
45     unset($this->attrs['goFonMacroParameter']['count']);
47     /* Set Parameters, or a new array if ther are no parameters */
48     if(isset($this->attrs['goFonMacroParameter'])){
49       $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
50     }else{
51       $this->goFonMacroParameter =array();
52     }
54     /* Create an array for parameters if not given yet */
55     if(!is_array($this->goFonMacroParameter)){
56       $tmp3 = $this->goFonMacroParameter;
57       $this->goFonMacroParameter =array();      
58       if(!empty($tmp3)) {
59         $this->goFonMacroParameter[]  = $tmp3;
60       }
61     }
63     /* Load parametersettings*/
64     foreach($this->goFonMacroParameter as $para){
65       $tmp = split("!",$para);
66       $num = $tmp[0];
67       $tmp2[$num]['name']        = $tmp[1];
68       $tmp2[$num]['type']        = $tmp[2];
69       $tmp2[$num]['default']     = $tmp[3];
70       $tmp2[$num]['var']         = "var".$num;
71     }
73     /* Assign this array */
74     $this->goFonMacroParameter = $tmp2;
75   }
77   function check_paras($content,$goFonMacroParameter)
78   { 
79     /* Check contents for parameters */
80     preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
82     $new = array();
84     /* Detect parameters with positions */
85     foreach($res[0] as $val){
86       $num = preg_replace("/[^0-9]/","",$val[0]); 
87       $new[$num]['val'] = $val[0];
88       $new[$num]['num'] = $num;
89     }
91     /* Compare content parameter and macro parameter */
92     foreach($goFonMacroParameter as $gokey => $goval){
93       foreach($new as $nkey => $nval){
94         if($gokey == $nval['num']){
95           /* sign this as OK */
96           $goFonMacroParameter[$gokey]['check']= true;
97         }
98       }
99     }
101     /* Now check if there is new parameter in the content, which is not assigned yet */
102     foreach($new as $key => $val){
103       /* Assign std values */
104       $goFonMacroParameter[$key]['var']="var".$key;
105       $goFonMacroParameter[$key]['check']= true;
107       /* If this is a new Parameter, name it ${ARG#} by default*/
108       if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
109         $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
110       }
111     }  
113     foreach($goFonMacroParameter as $key => $val){
114       /* All attributes with check == false, are unneeded so mark them with ['check']= false */
115       if(!isset($goFonMacroParameter[$key]['check'])){
116         $goFonMacroParameter[$key]['check']= false;
117       }
118       /* Ah no default given assign ="" to prevent unsigned index  */
119       if(!isset($goFonMacroParameter[$key]['default'])){
120         $goFonMacroParameter[$key]['default'] = "";
121       }
122     }
124     /* Sort output for better reading */
125     asort($goFonMacroParameter);
126     return($goFonMacroParameter);
128   }
131   function execute()
132   {
133     /* Variables */
134     $vars       = "";
135     $tmp        = array();
136     $number = 0; 
138     $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
140     if(strstr($content,"ARG")){
141       $vorpos = strpos($content,"ARG");
142       $rest   = substr($content,$vorpos, strlen($content));
143     }    
145     /* Do we represent a valid group? */
146     if (!$this->is_account && $this->parent == NULL){
147       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
148         _("This 'dn' is no phone macro.")."</b>";
149       return ($display);
150     }
152     /* Fill templating stuff */
153     $smarty= get_smarty();
155     /* Assign all vars to Smarty */
156     foreach($this->attributes as $ar){
157       $smarty->assign($ar, $this->$ar);
158     }
160     /* Add an empty Parameter */
161     if(isset($_POST['addvar'])){
162       if(!is_array($this->goFonMacroParameter)){
163         $vars = $this->goFonMacroParameter;
164         $this->goFonMacroParameter = array();
165         $this->goFonMacroParameter[]= $vars;
166       }
167       $number= count($this->goFonMacroParameter);
168       $number++;
169       $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
170     }
172     /*generate Table which shows als parameters */
173     $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
175     /* Sort by Parameterid, and keep keys */    
176     ksort($FonParas);
177     
178     foreach($FonParas as $key=>$para)   {
180       /* Select correct item of combobox */
181       if(isset($para['type'])){
182         list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
183       }else{
184         list($sel1, $sel2, $sel3)= array("", "", "");
185       }
187       /* Assemble output table */
188       $vars .="<tr>
189         <input name=\"number".$key."\" value='".$key."' type='hidden'>
190         <td><input name='var".$key."' type='hidden'   value='".$para['var']."'>".$para['var']."</td>
191         <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."'></td>
192         <td>
193         <select name='vartype".$key."'>
194         <option name='vartype".$key."' value='string' ".$sel1.">"._("String")."</option>
195         <option name='vartype".$key."' value='combo'   ".$sel2.">"._("Combobox")."</option>
196         <option name='vartype".$key."' value='bool'   ".$sel3.">"._("Bool")."</option>
197         </select>
198         </td>
199         <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'></td>
200         <td>&nbsp;";
201       if($para['check']==false) {
202         $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
203       }
205       $vars.=" </td></tr>";
206     }
208     /* Checkboxes */
209     $smarty->assign("base_select", $this->base);
210     $smarty->assign("vars", $vars);
212     /* Show main page */
213     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
214   }
217   function remove_from_parent()
218   {
219   }
222   /* Save data to object */
223   function save_object()
224   {
225     if (isset($_POST['phoneparameters'])){
226       plugin::save_object();
227     }
228     /* read out post data, and assign it to the parameters */
229     /* And or delete */
230     foreach($_POST as $name=>$value){
232       /* Test if there is a variable begining with "del" */
233       if(preg_match("/del/",$name)){
235         /* Extract entry id to delete */
236         $nr = str_replace("del","",$name) ;
238         /* unset entry */
239         unset($this->goFonMacroParameter[$nr]);
241       }elseif(preg_match("/number/",$name)){
243         /* Set Post vars */
244         $key = $_POST[$name];
246         $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
247         $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
248         $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
249         $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
250       }
251     }
253   }
256   /* Check values */
257   function check()
258   {
259     $message = array();
261     foreach($this->attributes as $attr){
262       if(chkacl($this->acl,$attr)){
263         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
264         return(array($str));
265       }
266     }
269     foreach($this->goFonMacroParameter as $key=>$val){
270       if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
271         $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
272       }
273       switch($val['type']){
274         case 'bool'   :   $possible = array("","0","1");
275                           if(!in_array($val['default'],$possible)) {
276                             $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
277                           };break;
278         case 'string' :
279         case 'combo'  : 
280         default : ;
282       }
283     }
284     return $message;
285   }
288   /* Save to LDAP */
289   function save()
290   {
291     /* Post checks */
293     plugin::save();
295     $this->attrs['goFonMacroParameter']=array();
297     foreach($this->goFonMacroParameter as $key=>$fonpara){
298       $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
299     }
301     unset($this->attrs['base']);
303     /* Write back to ldap */
304     $ldap= $this->config->get_ldap_link();
305     $ldap->cat($this->dn);
306     $a= $ldap->fetch();
308     if (count($a)){
309       $ldap->cd($this->dn);
310       $ldap->modify($this->attrs);
311       $this->handle_post_events("modify");
312     } else {
313       if(count($this->attrs['goFonMacroParameter']==0)){
314         unset($this->attrs['goFonMacroParameter']);
315       }         
316       $ldap->cd($this->dn);
317       $ldap->create_missing_trees( $this->dn);
318       $ldap->cd($this->dn);
319       $ldap->add($this->attrs);
320       $this->handle_post_events("add");
321     }
322     show_ldap_error($ldap->get_error());
323   }
326 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
327 ?>