9eaaab93460a3d7f1744ba1e445079c8775cbe09
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 foreach($this->goFonMacroParameter as $para){
64 $tmp = split("!",$para);
65 $num = preg_replace("/[^0-9]/","",$tmp[1]);
66 $tmp2[$num]['var'] = $tmp[1];
67 $tmp2[$num]['name'] = $tmp[2];
68 $tmp2[$num]['type'] = $tmp[3];
69 $tmp2[$num]['default'] = $tmp[4];
70 }
72 /* Assign this array */
73 $this->goFonMacroParameter = $tmp2;
74 }
76 function check_paras($content,$goFonMacroParameter)
77 {
78 preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
80 $anz = count($res[0]);
81 $new = array();
83 foreach($res[0] as $val){
84 $num = preg_replace("/[^0-9]/","",$val[0]);
85 $new[$num]['val'] = $val[0];
86 $new[$num]['num'] = $num;
87 }
89 foreach($goFonMacroParameter as $gokey => $goval){
90 foreach($new as $nkey => $nval){
91 if($gokey == $nval['num']){
92 $goFonMacroParameter[$gokey]['check']= true;
93 }
94 }
95 }
96 foreach($new as $key => $val){
97 $goFonMacroParameter[$key]['var']="\${ARG".$key."}";
98 $goFonMacroParameter[$key]['check']= true;
99 if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
100 $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
101 }
102 }
104 foreach($goFonMacroParameter as $key => $val){
105 if(!isset($goFonMacroParameter[$key]['check'])){
106 $goFonMacroParameter[$key]['check']= false;
107 }
108 if(!isset($goFonMacroParameter[$key]['default'])){
109 $goFonMacroParameter[$key]['default'] = "";
110 }
111 }
112 asort($goFonMacroParameter);
113 return($goFonMacroParameter);
115 }
118 function execute()
119 {
120 /* Variables */
121 $vars = "";
122 $tmp = array();
123 $number = 0;
125 $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
127 if(strstr($content,"ARG")){
128 $vorpos = strpos($content,"ARG");
129 $rest = substr($content,$vorpos, strlen($content));
130 }
132 /* Do we represent a valid group? */
133 if (!$this->is_account && $this->parent == NULL){
134 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
135 _("This 'dn' is no phone macro.")."</b>";
136 return ($display);
137 }
139 /* Fill templating stuff */
140 $smarty= get_smarty();
142 /* Assign all vars to Smarty */
143 foreach($this->attributes as $ar){
144 $smarty->assign($ar, $this->$ar);
145 }
147 /* Add an empty Parameter */
148 if(isset($_POST['addvar'])){
149 if(!is_array($this->goFonMacroParameter)){
150 $vars = $this->goFonMacroParameter;
151 $this->goFonMacroParameter = array();
152 $this->goFonMacroParameter[]= $vars;
153 }
154 $number= count($this->goFonMacroParameter);
155 $number++;
156 $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
157 }
159 /*generate Table which shows als parameters */
161 $FonParas = $this->check_paras($content,$this->goFonMacroParameter);
163 foreach($FonParas as $key=>$para) {
165 /* Select correct item of combobox */
166 if(isset($para['type'])){
167 list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
168 }else{
169 list($sel1, $sel2, $sel3)= array("", "", "");
170 }
172 /* Assemble output table */
173 $vars .="<tr>
174 <input name=\"number".$key."\" value='".$key."' type='hidden'>
175 <td><input name='var".$key."' type='hidden' value='".$para['var']."'>".$para['var']."</td>
176 <td><input size=\"45\" name='varname".$key."' value='".$para['name']."'></td>
177 <td>
178 <select name='vartype".$key."'>
179 <option name='vartype".$key."' value='string' ".$sel1.">"._("String")."</option>
180 <option name='vartype".$key."' value='combo' ".$sel2.">"._("Combobox")."</option>
181 <option name='vartype".$key."' value='bool' ".$sel3.">"._("Bool")."</option>
182 </select>
183 </td>
184 <td><input size=\"45\" name='default".$key."' value='".$para['default']."'></td>
185 <td> ";
186 if($para['check']==false) {
187 $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
188 }
190 $vars.=" </td></tr>";
191 }
193 /* Checkboxes */
194 $smarty->assign("base_select", $this->base);
195 $smarty->assign("vars", $vars);
197 /* Show main page */
198 return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
199 }
202 function remove_from_parent()
203 {
204 }
207 /* Save data to object */
208 function save_object()
209 {
210 if (isset($_POST['phoneparameters'])){
211 plugin::save_object();
212 }
213 /* read out post data, and assign it to the parameters */
214 /* And or delete */
215 foreach($_POST as $name=>$value){
217 /* Test if there is a variable begining with "del" */
218 if(preg_match("/del/",$name)){
220 /* Extract entry id to delete */
221 $nr = str_replace("del","",$name) ;
223 /* unset entry */
224 unset($this->goFonMacroParameter[$nr]);
226 }elseif(preg_match("/number/",$name)){
228 /* Set Post vars */
229 $key = $_POST[$name];
231 $this->goFonMacroParameter[$key]['var'] = $_POST["var".$key];
232 $this->goFonMacroParameter[$key]['name'] = $_POST["varname".$key];
233 $this->goFonMacroParameter[$key]['type'] = $_POST["vartype".$key];
234 $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
235 }
236 }
238 }
241 /* Check values */
242 function check()
243 {
244 $message = array();
246 foreach($this->attributes as $attr){
247 if(chkacl($this->acl,$attr)){
248 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
249 return(array($str));
250 }
251 }
252 foreach($this->goFonMacroParameter as $key=>$val){
253 switch($val['type']){
254 case 'bool' : $possible = array("","0","1","true","false");
255 if(!in_array($val['default'],$possible)) {
256 $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
257 };break;
258 case 'string' :
259 case 'combo' : if(strstr($val['default'],"!")){
260 $message[] = sprintf(_("The parameter %s contains invalid char. '!' is used as delimiter"),$val['name']);
261 };break;
262 default : ;
264 }
265 }
266 return $message;
267 }
270 /* Save to LDAP */
271 function save()
272 {
273 /* Post checks */
275 plugin::save();
277 $this->attrs['goFonMacroParameter']=array();
279 foreach($this->goFonMacroParameter as $key=>$fonpara){
280 $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['var']."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
281 }
283 unset($this->attrs['base']);
285 /* Write back to ldap */
286 $ldap= $this->config->get_ldap_link();
287 $ldap->cat($this->dn);
288 $a= $ldap->fetch();
290 if (count($a)){
291 $ldap->cd($this->dn);
292 $ldap->modify($this->attrs);
293 $this->handle_post_events("modify");
294 } else {
295 if(count($this->attrs['goFonMacroParameter']==0)){
296 unset($this->attrs['goFonMacroParameter']);
297 }
298 $ldap->cd($this->dn);
299 $ldap->create_missing_trees( $this->dn);
300 $ldap->cd($this->dn);
301 $ldap->add($this->attrs);
302 $this->handle_post_events("add");
303 }
304 show_ldap_error($ldap->get_error());
305 }
307 }
308 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
309 ?>