f8f93a19ab9de8848c48de1d609dea0334b2b65b
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 }
157 #FIXME: Der Transfer der momentanen Variablen muss nach save_object(),
158 # damit kann in save() auf den execute() Aufruf verzichtet werden!
160 /* read out post data, and assign it to the parameters */
161 /* And or delete */
162 foreach($_POST as $name=>$value){
164 /* Test if there is a variable begining with "del" */
165 if(preg_match("/del/",$name)){
167 /* Extract entry id to delete */
168 $nr = str_replace("del","",$name) ;
170 /* unset entry */
171 unset($this->goFonMacroParameter[$nr]);
173 }elseif(preg_match("/number/",$name)){
175 /* Set Post vars */
176 $key = $_POST[$name];
177 $this->goFonMacroParameter[$key]['name'] = $_POST["varname".$key];
178 $this->goFonMacroParameter[$key]['type'] = $_POST["vartype".$key];
179 $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
180 }
181 }
183 /*generate Table which shows als parameters */
185 $FonParas = $this->check_paras($content,$this->goFonMacroParameter);
187 foreach($FonParas as $key=>$para) {
189 /* Select correct item of combobox */
190 if(isset($para['type'])){
191 list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
192 }else{
193 list($sel1, $sel2, $sel3)= array("", "", "");
194 }
196 #FIXME: Der Name des Parameters ist nicht gesetzt
197 # -> Argument (ARG#) Name [....] Typ [...^] Default [....]
199 /* Assemble output table */
200 $vars .="<tr>
201 <td><input name=\"number".$key."\" value='".$key."' type='hidden'> </td>
202 <td><input name='varname".$key."' type='hidden' value='".$para['name']."'>".$para['name']."</td>
203 <td>
204 <select name='vartype".$key."'>
205 <option name='vartype".$key."' value='string' ".$sel1.">"._("String")."</option>
206 <option name='vartype".$key."' value='combo' ".$sel2.">"._("Combobox")."</option>
207 <option name='vartype".$key."' value='bool' ".$sel3.">"._("Bool")."</option>
208 </select>
209 </td>
210 <td><input name='default".$key."' value='".$para['default']."'></td>
211 <td>";
212 if($para['check']==true) {
213 $vars.=_("Ok");
214 $vars.=" <td></td>";
215 }else{
216 $vars.="<b>"._("Unused")."</b>";
217 $vars.=" <td><input name='del".$key."' value='delete' type='submit'></td>";
218 }
220 $vars.=" </tr>";
221 }
223 /* Checkboxes */
224 $smarty->assign("base_select", $this->base);
225 $smarty->assign("vars", $vars);
227 /* Show main page */
228 return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
229 }
232 function remove_from_parent()
233 {
234 }
237 /* Save data to object */
238 function save_object()
239 {
240 if (isset($_POST['phoneparameters'])){
241 plugin::save_object();
242 }
243 }
246 /* Check values */
247 function check()
248 {
249 $message = array();
251 foreach($this->attributes as $attr){
252 if(chkacl($this->acl,$attr)){
253 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
254 return(array($str));
255 }
256 }
257 return $message;
258 }
261 /* Save to LDAP */
262 function save()
263 {
264 /* Post checks */
266 # FIXME: das hat hier nix zu suchen...
267 $this->execute();
269 plugin::save();
271 $this->attrs['goFonMacroParameter']=array();
273 foreach($this->goFonMacroParameter as $key=>$fonpara){
274 $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
275 }
277 unset($this->attrs['base']);
279 /* Write back to ldap */
280 $ldap= $this->config->get_ldap_link();
281 $ldap->cat($this->dn);
282 $a= $ldap->fetch();
284 if (count($a)){
285 $ldap->cd($this->dn);
286 $ldap->modify($this->attrs);
287 $this->handle_post_events("modify");
288 } else {
289 if(count($this->attrs['goFonMacroParameter']==0)){
290 unset($this->attrs['goFonMacroParameter']);
291 }
292 $ldap->cd($this->dn);
293 $ldap->create_missing_trees( $this->dn);
294 $ldap->cd($this->dn);
295 $ldap->add($this->attrs);
296 $this->handle_post_events("add");
297 }
298 show_ldap_error($ldap->get_error());
299 }
301 }
302 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
303 ?>