1 <?php
2 class macroParameter extends plugin
3 {
4 /* CLI vars */
5 var $cli_summary= "Handling of GOsa's application object";
6 var $cli_description= "Some longer text\nfor help";
7 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9 /* application attributes */
10 var $base= "";
11 var $goFonMacroParameter =array();
12 var $type_shortcut= array("string" => array("selected", "", ""),
13 "combo" => array("", "selected", ""),
14 "bool" => array("", "", "selected"));
16 /* attribute list for save action */
17 var $attributes= array("base","goFonMacroParameter");
18 var $objectclasses= array("top", "goFonMacro");
21 function macroParameter ($config, $dn= NULL)
22 {
23 plugin::plugin ($config, $dn);
25 $tmp = array(); // temporary Var
26 $tmp2 = array(); // temporary Var ...
27 $tmp3 = "";
28 $ldap= $config->get_ldap_link();
30 $this->dn = $dn;
32 /* This is always an account */
33 $this->is_account= TRUE;
35 /* Edit or new one ?*/
36 if ($this->dn == "new"){
37 $ui= get_userinfo();
38 $this->base= dn2base($ui->dn);
39 } else {
40 $this->base= dn2base($this->dn);
41 }
43 /* initialising macro parameter */
44 unset($this->attrs['goFonMacroParameter']['count']);
46 /* Set Parameters, or a new array if ther are no parameters */
47 if(isset($this->attrs['goFonMacroParameter'])){
48 $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
49 }else{
50 $this->goFonMacroParameter =array();
51 }
53 /* Create an array for parameters if not given yet */
54 if(!is_array($this->goFonMacroParameter)){
55 $tmp3 = $this->goFonMacroParameter;
56 $this->goFonMacroParameter =array();
57 if(!empty($tmp3)) {
58 $this->goFonMacroParameter[] = $tmp3;
59 }
60 }
62 foreach($this->goFonMacroParameter as $para){
63 $tmp = split("!",$para);
64 $num = preg_replace("/[^0-9]/","",$tmp[1]);
65 $tmp2[$num]['var'] = $tmp[1];
66 $tmp2[$num]['name'] = $tmp[2];
67 $tmp2[$num]['type'] = $tmp[3];
68 $tmp2[$num]['default'] = $tmp[4];
69 }
71 /* Assign this array */
72 $this->goFonMacroParameter = $tmp2;
73 }
75 function check_paras($content,$goFonMacroParameter)
76 {
77 preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
79 $anz = count($res[0]);
80 $new = array();
82 foreach($res[0] as $val){
83 $num = preg_replace("/[^0-9]/","",$val[0]);
84 $new[$num]['val'] = $val[0];
85 $new[$num]['num'] = $num;
86 }
88 foreach($goFonMacroParameter as $gokey => $goval){
89 foreach($new as $nkey => $nval){
90 if($gokey == $nval['num']){
91 $goFonMacroParameter[$gokey]['check']= true;
92 }
93 }
94 }
95 foreach($new as $key => $val){
96 $goFonMacroParameter[$key]['var']="\${ARG".$key."}";
97 $goFonMacroParameter[$key]['check']= true;
98 if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
99 $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
100 }
101 }
103 foreach($goFonMacroParameter as $key => $val){
104 if(!isset($goFonMacroParameter[$key]['check'])){
105 $goFonMacroParameter[$key]['check']= false;
106 }
107 if(!isset($goFonMacroParameter[$key]['default'])){
108 $goFonMacroParameter[$key]['default'] = "";
109 }
110 }
111 asort($goFonMacroParameter);
112 return($goFonMacroParameter);
114 }
117 function execute()
118 {
119 /* Variables */
120 $vars = "";
121 $tmp = array();
122 $number = 0;
124 $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
126 if(strstr($content,"ARG")){
127 $vorpos = strpos($content,"ARG");
128 $rest = substr($content,$vorpos, strlen($content));
129 }
131 /* Do we represent a valid group? */
132 if (!$this->is_account && $this->parent == NULL){
133 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
134 _("This 'dn' is no phone macro.")."</b>";
135 return ($display);
136 }
138 /* Fill templating stuff */
139 $smarty= get_smarty();
141 /* Assign all vars to Smarty */
142 foreach($this->attributes as $ar){
143 $smarty->assign($ar, $this->$ar);
144 }
146 /* Add an empty Parameter */
147 if(isset($_POST['addvar'])){
148 if(!is_array($this->goFonMacroParameter)){
149 $vars = $this->goFonMacroParameter;
150 $this->goFonMacroParameter = array();
151 $this->goFonMacroParameter[]= $vars;
152 }
153 $number= count($this->goFonMacroParameter);
154 $number++;
155 $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
156 }
158 /*generate Table which shows als parameters */
160 $FonParas = $this->check_paras($content,$this->goFonMacroParameter);
162 foreach($FonParas as $key=>$para) {
164 /* Select correct item of combobox */
165 if(isset($para['type'])){
166 list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
167 }else{
168 list($sel1, $sel2, $sel3)= array("", "", "");
169 }
171 /* Assemble output table */
172 $vars .="<tr>
173 <input name=\"number".$key."\" value='".$key."' type='hidden'>
174 <td><input name='var".$key."' type='hidden' value='".$para['var']."'>".$para['var']."</td>
175 <td><input name='varname".$key."' value='".$para['name']."'></td>
176 <td>
177 <select name='vartype".$key."'>
178 <option name='vartype".$key."' value='string' ".$sel1.">"._("String")."</option>
179 <option name='vartype".$key."' value='combo' ".$sel2.">"._("Combobox")."</option>
180 <option name='vartype".$key."' value='bool' ".$sel3.">"._("Bool")."</option>
181 </select>
182 </td>
183 <td><input name='default".$key."' value='".$para['default']."'></td>
184 <td>";
185 if($para['check']==true) {
186 $vars.="</td><td></td>";
187 }else{
188 $vars.="<b>"._("Unused")."</b></td>";
189 $vars.=" <td><input name='del".$key."' value='delete' type='submit'></td>";
190 }
192 $vars.=" </tr>";
193 }
195 /* Checkboxes */
196 $smarty->assign("base_select", $this->base);
197 $smarty->assign("vars", $vars);
199 /* Show main page */
200 return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
201 }
204 function remove_from_parent()
205 {
206 }
209 /* Save data to object */
210 function save_object()
211 {
212 if (isset($_POST['phoneparameters'])){
213 plugin::save_object();
214 }
215 /* read out post data, and assign it to the parameters */
216 /* And or delete */
217 foreach($_POST as $name=>$value){
219 /* Test if there is a variable begining with "del" */
220 if(preg_match("/del/",$name)){
222 /* Extract entry id to delete */
223 $nr = str_replace("del","",$name) ;
225 /* unset entry */
226 unset($this->goFonMacroParameter[$nr]);
228 }elseif(preg_match("/number/",$name)){
230 /* Set Post vars */
231 $key = $_POST[$name];
233 $this->goFonMacroParameter[$key]['var'] = $_POST["var".$key];
234 $this->goFonMacroParameter[$key]['name'] = $_POST["varname".$key];
235 $this->goFonMacroParameter[$key]['type'] = $_POST["vartype".$key];
236 $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
237 }
238 }
240 }
243 /* Check values */
244 function check()
245 {
246 $message = array();
248 foreach($this->attributes as $attr){
249 if(chkacl($this->acl,$attr)){
250 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
251 return(array($str));
252 }
253 }
254 return $message;
255 }
258 /* Save to LDAP */
259 function save()
260 {
261 /* Post checks */
263 plugin::save();
265 $this->attrs['goFonMacroParameter']=array();
267 foreach($this->goFonMacroParameter as $key=>$fonpara){
268 $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['var']."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
269 }
271 unset($this->attrs['base']);
273 /* Write back to ldap */
274 $ldap= $this->config->get_ldap_link();
275 $ldap->cat($this->dn);
276 $a= $ldap->fetch();
278 if (count($a)){
279 $ldap->cd($this->dn);
280 $ldap->modify($this->attrs);
281 $this->handle_post_events("modify");
282 } else {
283 if(count($this->attrs['goFonMacroParameter']==0)){
284 unset($this->attrs['goFonMacroParameter']);
285 }
286 $ldap->cd($this->dn);
287 $ldap->create_missing_trees( $this->dn);
288 $ldap->cd($this->dn);
289 $ldap->add($this->attrs);
290 $this->handle_post_events("add");
291 }
292 show_ldap_error($ldap->get_error());
293 }
295 }
296 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
297 ?>