65f6614ca3034af6bc8a65b580d641f665669056
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]['name'] = $tmp[1];
66 $tmp2[$num]['type'] = $tmp[2];
67 $tmp2[$num]['default'] = $tmp[3];
68 }
70 /* Assign this array */
71 $this->goFonMacroParameter = $tmp2;
72 }
74 function check_paras($content,$goFonMacroParameter)
75 {
76 preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
78 $anz = count($res[0]);
79 $new = array();
81 foreach($res[0] as $val){
82 $num = preg_replace("/[^0-9]/","",$val[0]);
83 $new[$num]['val'] = $val[0];
84 $new[$num]['num'] = $num;
85 }
87 foreach($goFonMacroParameter as $gokey => $goval){
88 foreach($new as $nkey => $nval){
89 if($gokey == $nval['num']){
90 $goFonMacroParameter[$gokey]['check']= true;
91 }
92 }
93 }
94 foreach($new as $key => $val){
95 $goFonMacroParameter[$key]['check']= true;
96 if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
97 $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
98 }
99 }
101 foreach($goFonMacroParameter as $key => $val){
102 if(!isset($goFonMacroParameter[$key]['check'])){
103 $goFonMacroParameter[$key]['check']= false;
104 }
105 if(!isset($goFonMacroParameter[$key]['default'])){
106 $goFonMacroParameter[$key]['default'] = "";
107 }
108 }
109 asort($goFonMacroParameter);
110 return($goFonMacroParameter);
112 }
115 function execute()
116 {
117 /* Variables */
118 $vars = "";
119 $tmp = array();
120 $number = 0;
122 $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
124 if(strstr($content,"ARG")){
125 $vorpos = strpos($content,"ARG");
126 $rest = substr($content,$vorpos, strlen($content));
127 }
129 /* Do we represent a valid group? */
130 if (!$this->is_account && $this->parent == NULL){
131 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
132 _("This 'dn' is no phone macro.")."</b>";
133 return ($display);
134 }
136 /* Fill templating stuff */
137 $smarty= get_smarty();
139 /* Assign all vars to Smarty */
140 foreach($this->attributes as $ar){
141 $smarty->assign($ar, $this->$ar);
142 }
144 /* Add an empty Parameter */
145 if(isset($_POST['addvar'])){
146 if(!is_array($this->goFonMacroParameter)){
147 $vars = $this->goFonMacroParameter;
148 $this->goFonMacroParameter = array();
149 $this->goFonMacroParameter[]= $vars;
150 }
151 $number= count($this->goFonMacroParameter);
152 $number++;
153 $this->goFonMacroParameter[]=array("name"=>"new","type"=>"string","default"=>"test");
154 }
156 /*generate Table which shows als parameters */
158 $FonParas = $this->check_paras($content,$this->goFonMacroParameter);
160 foreach($FonParas as $key=>$para) {
162 /* Select correct item of combobox */
163 if(isset($para['type'])){
164 list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
165 }else{
166 list($sel1, $sel2, $sel3)= array("", "", "");
167 }
169 /* Assemble output table */
170 $vars .="<tr>
171 <td><input name=\"number".$key."\" value='".$key."' type='hidden'> </td>
172 <td><input name='varname".$key."' type='hidden' value='".$para['name']."'>".$para['name']."</td>
173 <td>
174 <select name='vartype".$key."'>
175 <option name='vartype".$key."' value='string' ".$sel1.">"._("String")."</option>
176 <option name='vartype".$key."' value='combo' ".$sel2.">"._("Combobox")."</option>
177 <option name='vartype".$key."' value='bool' ".$sel3.">"._("Bool")."</option>
178 </select>
179 </td>
180 <td><input name='default".$key."' value='".$para['default']."'></td>
181 <td>";
182 if($para['check']==true) {
183 $vars.=_("Ok");
184 $vars.=" <td></td>";
185 }else{
186 $vars.="<b>"._("Unused")."</b>";
187 $vars.=" <td><input name='del".$key."' value='delete' type='submit'></td>";
188 }
190 $vars.=" </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];
230 $this->goFonMacroParameter[$key]['name'] = $_POST["varname".$key];
231 $this->goFonMacroParameter[$key]['type'] = $_POST["vartype".$key];
232 $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
233 }
234 }
236 }
239 /* Check values */
240 function check()
241 {
242 $message = array();
244 foreach($this->attributes as $attr){
245 if(chkacl($this->acl,$attr)){
246 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
247 return(array($str));
248 }
249 }
250 return $message;
251 }
254 /* Save to LDAP */
255 function save()
256 {
257 /* Post checks */
259 plugin::save();
261 $this->attrs['goFonMacroParameter']=array();
263 foreach($this->goFonMacroParameter as $key=>$fonpara){
264 $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
265 }
267 unset($this->attrs['base']);
269 /* Write back to ldap */
270 $ldap= $this->config->get_ldap_link();
271 $ldap->cat($this->dn);
272 $a= $ldap->fetch();
274 if (count($a)){
275 $ldap->cd($this->dn);
276 $ldap->modify($this->attrs);
277 $this->handle_post_events("modify");
278 } else {
279 if(count($this->attrs['goFonMacroParameter']==0)){
280 unset($this->attrs['goFonMacroParameter']);
281 }
282 $ldap->cd($this->dn);
283 $ldap->create_missing_trees( $this->dn);
284 $ldap->cd($this->dn);
285 $ldap->add($this->attrs);
286 $this->handle_post_events("add");
287 }
288 show_ldap_error($ldap->get_error());
289 }
291 }
292 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
293 ?>