Code

check for already existing dn, when creating new macro
[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");
21   /*!
22     Konstructor
23    */
24   function macroParameter ($config, $dn= NULL)
25   {
26     plugin::plugin ($config, $dn);
28     $tmp = array();  // temporary Var 
29     $tmp2 = array(); // temporary Var ...
30     $tmp3 = "";
31     $ldap= $config->get_ldap_link();
33     $this->dn = $dn;
35     /* This is always an account */
36     $this->is_account= TRUE;
38     /* Edit or new one ?*/
39     if ($this->dn == "new"){
40       $ui= get_userinfo();
41       $this->base= dn2base($ui->dn);
42     } else {
43       $this->base= dn2base($this->dn);
44     }
46     /* initialising macro parameter */
47     unset($this->attrs['goFonMacroParameter']['count']);
49     /* Set Parameters, or a new array if ther are no parameters */
50     if(isset($this->attrs['goFonMacroParameter'])){
51       $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
52     }else{
53       $this->goFonMacroParameter =array();
54     }
56     /* Create an array for parameters if not given yet */
57     if(!is_array($this->goFonMacroParameter)){
58       $tmp3 = $this->goFonMacroParameter;
59       $this->goFonMacroParameter =array();      
60       if(!empty($tmp3)) {
61         $this->goFonMacroParameter[]  = $tmp3;
62       }
63     }
65     /* Load parametersettings*/
66     foreach($this->goFonMacroParameter as $para){
67       $tmp = split("!",$para);
68       $num = $tmp[0];
69       $tmp2[$num]['name']        = $tmp[1];
70       $tmp2[$num]['type']        = $tmp[2];
71       $tmp2[$num]['default']     = $tmp[3];
72       $tmp2[$num]['var']         = "var".$num;
73     }
75     /* Assign this array */
76     $this->goFonMacroParameter = $tmp2;
77   }
79   /*!
80     Check given parameters from content and from object and compare them
81    */
82   function check_paras($content,$goFonMacroParameter)
83   { 
84     /* Check contents for parameters */
85     preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
87     $new = array();
89     /* Detect parameters with positions */
90     foreach($res[0] as $val){
91       $num = preg_replace("/[^0-9]/","",$val[0]); 
92       $new[$num]['val'] = $val[0];
93       $new[$num]['num'] = $num;
94     }
96     /* Compare content parameter and macro parameter */
97     foreach($goFonMacroParameter as $gokey => $goval){
98       foreach($new as $nkey => $nval){
99         if($gokey == $nval['num']){
100           /* sign this as OK */
101           $goFonMacroParameter[$gokey]['check']= true;
102         }
103       }
104     }
106     /* Now check if there is new parameter in the content, which is not assigned yet */
107     foreach($new as $key => $val){
108       /* Assign std values */
109       $goFonMacroParameter[$key]['var']="var".$key;
110       $goFonMacroParameter[$key]['check']= true;
112       /* If this is a new Parameter, name it ${ARG#} by default*/
113       if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
114         $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
115       }
116     }  
118     foreach($goFonMacroParameter as $key => $val){
119       /* All attributes with check == false, are unneeded so mark them with ['check']= false */
120       if(!isset($goFonMacroParameter[$key]['check'])){
121         $goFonMacroParameter[$key]['check']= false;
122       }
123       /* Ah no default given assign ="" to prevent unsigned index  */
124       if(!isset($goFonMacroParameter[$key]['default'])){
125         $goFonMacroParameter[$key]['default'] = "";
126       }
127     }
129     /* Sort output for better reading */
130     asort($goFonMacroParameter);
131     return($goFonMacroParameter);
133   }
136   function execute()
137   {
138     /* Variables */
139     $vars       = "";
140     $tmp        = array();
141     $number = 0; 
143     $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
145     if(strstr($content,"ARG")){
146       $vorpos = strpos($content,"ARG");
147       $rest   = substr($content,$vorpos, strlen($content));
148     }    
150     /* Do we represent a valid group? */
151     if (!$this->is_account && $this->parent == NULL){
152       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
153         _("This 'dn' is no phone macro.")."</b>";
154       return ($display);
155     }
157     /* Fill templating stuff */
158     $smarty= get_smarty();
160     /* Assign all vars to Smarty */
161     foreach($this->attributes as $ar){
162       $smarty->assign($ar, $this->$ar);
163     }
165     /* Add an empty Parameter */
166     if(isset($_POST['addvar'])){
167       if(!is_array($this->goFonMacroParameter)){
168         $vars = $this->goFonMacroParameter;
169         $this->goFonMacroParameter = array();
170         $this->goFonMacroParameter[]= $vars;
171       }
172       $number= count($this->goFonMacroParameter);
173       $number++;
174       $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
175     }
177     /*generate Table which shows als parameters */
178     $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
180     /* Sort by Parameterid, and keep keys */    
181     ksort($FonParas);
183     foreach($FonParas as $key=>$para)   {
185       /* Select correct item of combobox */
186       if(isset($para['type'])){
187         list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
188       }else{
189         list($sel1, $sel2, $sel3)= array("", "", "");
190       }
192       /* Assemble output table */
193       $vars .="<tr>
194         <td>
195         <input name=\"number".$key."\" value='".$key."' type='hidden'>
196         <td><input name='var".$key."' type='hidden'   value='".$para['var']."'>ARG".$key."</td>
197         <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."'></td>
198         <td>
199         <select name='vartype".$key."'>
200         <option  value='string' ".$sel1.">"._("String")."&nbsp;</option>
201         <option  value='combo'   ".$sel2.">"._("Combobox")."&nbsp;</option>
202         <option  value='bool'   ".$sel3.">"._("Bool")."&nbsp;</option>
203         </select>
204         </td>
205         <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'></td>
206         <td>&nbsp;";
207       if($para['check']==false) {
208         $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
209       }
211       $vars.=" </td></tr>";
212     }
214     /* Checkboxes */
215     $smarty->assign("base_select", $this->base);
216     $smarty->assign("vars", $vars);
218     /* Show main page */
219     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
220   }
223   function remove_from_parent()
224   {
225   }
228   /* Save data to object */
229   function save_object()
230   {
231     if (isset($_POST['phoneparameters'])){
232       plugin::save_object();
233     }
234     /* read out post data, and assign it to the parameters */
235     /* And or delete */
236     foreach($_POST as $name=>$value){
238       /* Test if there is a variable begining with "del" */
239       if(preg_match("/del/",$name)){
241         /* Extract entry id to delete */
242         $nr = str_replace("del","",$name) ;
244         /* unset entry */
245         unset($this->goFonMacroParameter[$nr]);
247       }elseif(preg_match("/number/",$name)){
249         /* Set Post vars */
250         $key = $_POST[$name];
252         $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
253         $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
254         $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
255         $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
256       }
257     }
259   }
262   /* Check values */
263   function check()
264   {
265     $message = array();
267     foreach($this->attributes as $attr){
268       if(chkacl($this->acl,$attr)){
269         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
270         return(array($str));
271       }
272     }
275     foreach($this->goFonMacroParameter as $key=>$val){
276       if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
277         $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
278       }
279       switch($val['type']){
280         case 'bool'   :   $possible = array("","0","1");
281                           if(!in_array($val['default'],$possible)) {
282                             $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
283                           };break;
284         case 'string' :
285         case 'combo'  : 
286         default : ;
288       }
289     }
290     return $message;
291   }
294   /* Save to LDAP */
295   function save()
296   {
297     /* Post checks */
299     plugin::save();
301     $this->attrs['goFonMacroParameter']=array();
303     foreach($this->goFonMacroParameter as $key=>$fonpara){
304       $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
305     }
307     unset($this->attrs['base']);
309     /* Write back to ldap */
310     $ldap= $this->config->get_ldap_link();
311     $ldap->cat($this->dn);
312     $a= $ldap->fetch();
314     if (count($a)){
315       $ldap->cd($this->dn);
316       $ldap->modify($this->attrs);
317       $this->handle_post_events("modify");
318     } else {
319       if(count($this->attrs['goFonMacroParameter']==0)){
320         unset($this->attrs['goFonMacroParameter']);
321       }         
322       $ldap->cd($this->dn);
323       $ldap->create_missing_trees( $this->dn);
324       $ldap->cd($this->dn);
325       $ldap->add($this->attrs);
326       $this->handle_post_events("add");
327     }
328     show_ldap_error($ldap->get_error());
329   }
332 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
333 ?>