~ubuntu-branches/ubuntu/vivid/gosa/vivid

« back to all changes in this revision

Viewing changes to plugins/gofon/macro/class_gofonMacroParameters.inc

Tags: 2.7.1-1
* New upstream release
* Updated packaging to not include smarty (Closes: #620489)
* Fixed case of POSIX (Closes: #620486)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
//! This class handles the goFonMacroParameter
3
 
/*! In this class all parameters from goFonMacroContent \n
4
 
    and all (if given) already defined parameters are managed \n
5
 
    \n
6
 
    Parameters will be saved to openldap like this :\n
7
 
    goFonMacroParameter: ID!NAME:!TYPE(string:combo:bool)!DEFAULT_VALUE\n    
8
 
    In Case of type=combo the DEFAULT_VALUE specifies the entries in the listbox, like this\n
9
 
    "first:second:third:last" \n
10
 
*/
11
 
class macroParameter extends plugin
12
 
{
13
 
  /* Parameter Count*/
14
 
  var $para_count = 0 ; 
15
 
  /*! macro base  */
16
 
  var $base= "";
17
 
  
18
 
  /*! This array contains all Parameter defined for the macro*/
19
 
  var $goFonMacroParameter =array();
20
 
 
21
 
  /*! This are the available types for a macro */
22
 
  var $type_shortcut= array("string" => array("selected", "", ""),
23
 
      "combo"  => array("", "selected", ""),
24
 
      "bool"   => array("", "", "selected"));
25
 
 
26
 
  /*! attribute list for save action */
27
 
  var $attributes= array("base","goFonMacroParameter");
28
 
  
29
 
  /*! Objectclasses needed by the class*/  
30
 
  var $objectclasses= array("top", "goFonMacro");
31
 
 
32
 
  //! The konstructor of macroParameter    
33
 
  /*! The konstructor of macroParameter...
34
 
     - reads goFonMacroParameter and parses them to an array 
35
 
     - Set attributes from openldap (edit)
36
 
     - Set attributes from default (new)
37
 
  */
38
 
  function macroParameter ($config, $dn= NULL, $parent= NULL)
39
 
  {
40
 
    plugin::plugin ($config, $dn, $parent);
41
 
 
42
 
    $tmp = array();  // temporary Var 
43
 
    $tmp2 = array(); // temporary Var ...
44
 
    $tmp3 = "";
45
 
    $ldap= $config->get_ldap_link();
46
 
 
47
 
    $this->dn = $dn;
48
 
 
49
 
    /* This is always an account */
50
 
    $this->is_account= TRUE;
51
 
 
52
 
    /* Edit or new one ?*/
53
 
    if ($this->dn == "new"){
54
 
      $ui= get_userinfo();
55
 
      $this->base= dn2base($ui->dn);
56
 
    } else {
57
 
      $this->base= dn2base($this->dn);
58
 
    }
59
 
 
60
 
    /* initialising macro parameter */
61
 
    if(isset($this->attrs['goFonMacroParameter']) &&
62
 
        isset($this->attrs['goFonMacroParameter']['count'])){
63
 
      unset($this->attrs['goFonMacroParameter']['count']);
64
 
    }
65
 
 
66
 
    /* Set Parameters, or a new array if ther are no parameters */
67
 
    if(isset($this->attrs['goFonMacroParameter'])){
68
 
      $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
69
 
    }else{
70
 
      $this->goFonMacroParameter =array();
71
 
    }
72
 
 
73
 
    /* Create an array for parameters if not given yet */
74
 
    if(!is_array($this->goFonMacroParameter)){
75
 
      $tmp3 = $this->goFonMacroParameter;
76
 
      $this->goFonMacroParameter =array();      
77
 
      if(!empty($tmp3)) {
78
 
        $this->goFonMacroParameter[]  = $tmp3;
79
 
      }
80
 
    }
81
 
 
82
 
    /* Load parametersettings*/
83
 
    foreach($this->goFonMacroParameter as $para){
84
 
      $tmp = split("!",$para);
85
 
      $num = $tmp[0];
86
 
      $tmp2[$num]['name']        = base64_decode($tmp[1]);
87
 
      $tmp2[$num]['type']        = $tmp[2];
88
 
      $tmp2[$num]['default']     = $tmp[3];
89
 
      $tmp2[$num]['var']         = "var".$num;
90
 
    }
91
 
 
92
 
    
93
 
    /* Assign this array */
94
 
    $this->goFonMacroParameter = $tmp2;
95
 
 
96
 
    $this->para_count = count ($tmp2);
97
 
   
98
 
    $ui= get_userinfo();
99
 
    $acl= get_permissions ($ui->dn, $ui->subtreeACL);
100
 
    $this->acl= get_module_permission($acl, "goFonMacro", $ui->dn);
101
 
  }
102
 
 
103
 
  //! Perform Parameter check 
104
 
  /*! 
105
 
      Compares the given parameters (goFonMacroParameters) with the parameters defined in goFonContent\n 
106
 
      -> Decide which attrs are new and which are unused\n
107
 
      -> Sort result array (containing both parameters 'goFonMacroParameters/goFonContent' and new / unused info)\n
108
 
      \param $content The given goFonContent for this macro\n
109
 
      \param $goFonMacroParameter Array with the already given parameters \n
110
 
   */
111
 
  function check_paras($content,$goFonMacroParameter)
112
 
  { 
113
 
    /* Check contents for parameters */
114
 
    preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
115
 
 
116
 
    $new = array();
117
 
 
118
 
    /* Detect parameters with positions */
119
 
    foreach($res[0] as $val){
120
 
      $num = preg_replace("/[^0-9]/","",$val[0]); 
121
 
      $new[$num]['val'] = $val[0];
122
 
      $new[$num]['num'] = $num;
123
 
    }
124
 
 
125
 
    /* Compare content parameter and macro parameter */
126
 
    foreach($goFonMacroParameter as $gokey => $goval){
127
 
      foreach($new as $nkey => $nval){
128
 
        if($gokey == $nval['num']){
129
 
          /* sign this as OK */
130
 
          $goFonMacroParameter[$gokey]['check']= true;
131
 
        }
132
 
      }
133
 
    }
134
 
 
135
 
    /* Now check if there is new parameter in the content, which is not assigned yet */
136
 
    foreach($new as $key => $val){
137
 
      /* Assign std values */
138
 
      $goFonMacroParameter[$key]['var']="var".$key;
139
 
      $goFonMacroParameter[$key]['check']= true;
140
 
 
141
 
      /* If this is a new Parameter, name it ${ARG#} by default*/
142
 
      if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
143
 
        $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
144
 
      }
145
 
    }  
146
 
 
147
 
    foreach($goFonMacroParameter as $key => $val){
148
 
      /* All attributes with check == false, are unneeded so mark them with ['check']= false */
149
 
      if(!isset($goFonMacroParameter[$key]['check'])){
150
 
        $goFonMacroParameter[$key]['check']= false;
151
 
      }
152
 
      /* Ah no default given assign ="" to prevent unsigned index  */
153
 
      if(!isset($goFonMacroParameter[$key]['default'])){
154
 
        $goFonMacroParameter[$key]['default'] = "";
155
 
      }
156
 
    }
157
 
 
158
 
    /* Sort output for better reading */
159
 
    asort($goFonMacroParameter);
160
 
    return($goFonMacroParameter);
161
 
 
162
 
  }
163
 
 
164
 
  //! Execute this Plugin
165
 
  /*! 
166
 
      Perform Parameter check \n
167
 
      Draw paramter table\n
168
 
      Show tpl   \n 
169
 
  */
170
 
  function execute()
171
 
  {
172
 
        /* Call parent execute */
173
 
        plugin::execute();
174
 
 
175
 
    /* Variables */
176
 
    $vars       = "";
177
 
    $tmp        = array();
178
 
    $number = 0; 
179
 
  
180
 
    $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
181
 
 
182
 
    if(strstr($content,"ARG")){
183
 
      $vorpos = strpos($content,"ARG");
184
 
      $rest   = substr($content,$vorpos, strlen($content));
185
 
    }    
186
 
 
187
 
    /* Do we represent a valid group? */
188
 
    if (!$this->is_account && $this->parent == NULL){
189
 
      $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
190
 
        _("This 'dn' is no phone macro.")."</b>";
191
 
      return ($display);
192
 
    }
193
 
 
194
 
    /* Fill templating stuff */
195
 
    $smarty= get_smarty();
196
 
 
197
 
    /* Assign all vars to Smarty */
198
 
    foreach($this->attributes as $ar){
199
 
      $smarty->assign($ar, $this->$ar);
200
 
    }
201
 
 
202
 
    /* Add an empty Parameter */
203
 
    if(isset($_POST['addvar'])){
204
 
      if(!is_array($this->goFonMacroParameter)){
205
 
        $vars = $this->goFonMacroParameter;
206
 
        $this->goFonMacroParameter = array();
207
 
        $this->goFonMacroParameter[]= $vars;
208
 
      }
209
 
      $number= count($this->goFonMacroParameter);
210
 
      $number++;
211
 
      $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
212
 
    }
213
 
 
214
 
    /*generate Table which shows als parameters */
215
 
    $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
216
 
 
217
 
    /* Sort by Parameterid, and keep keys */    
218
 
    ksort($FonParas);
219
 
 
220
 
    foreach($FonParas as $key=>$para)   {
221
 
 
222
 
      /* Select correct item of combobox */
223
 
      if(isset($para['type'])){
224
 
        list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
225
 
      }else{
226
 
        list($sel1, $sel2, $sel3)= array("", "", "");
227
 
      }
228
 
 
229
 
      /* Assemble output table */
230
 
      $vars .="<tr>
231
 
        <td>
232
 
          <input name=\"number".$key."\" value='".$key."' type='hidden'>
233
 
          <input name='var".$key."' type='hidden'   value='".$para['var']."'>ARG".$key."
234
 
        </td>
235
 
        <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."'></td>
236
 
        <td>
237
 
          <select name='vartype".$key."'>
238
 
            <option  value='string' ".$sel1.">"._("String")."&nbsp;</option>
239
 
            <option  value='combo'   ".$sel2.">"._("Combobox")."&nbsp;</option>
240
 
            <option  value='bool'   ".$sel3.">"._("Bool")."&nbsp;</option>
241
 
          </select>
242
 
        </td>
243
 
        <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'></td>
244
 
        <td>&nbsp;";
245
 
      if($para['check']==false) {
246
 
        $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
247
 
      }
248
 
 
249
 
      $vars.=" </td></tr>";
250
 
    }
251
 
 
252
 
    /* Checkboxes */
253
 
    $smarty->assign("base_select", $this->base);
254
 
    $smarty->assign("vars", $vars);
255
 
 
256
 
    /* Show main page */
257
 
    return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
258
 
  }
259
 
  
260
 
  //! Unused here 
261
 
  /*!
262
 
      Unused here because goFonMacro will remove this Macro\n 
263
 
  */
264
 
  function remove_from_parent()
265
 
  {
266
 
  }
267
 
 
268
 
  //! Save our data
269
 
  /*! 
270
 
      Save POST data to object \n
271
 
      This gives us the possibility to leave a tab, without losing our typed informations\n
272
 
      \n
273
 
      Read the POST fields for the parameters and saves their info the the class\n
274
 
  */
275
 
  function save_object()
276
 
  {
277
 
    if (isset($_POST['phoneparameters'])){
278
 
      plugin::save_object();
279
 
    }
280
 
    /* read out post data, and assign it to the parameters */
281
 
    /* And or delete */
282
 
    foreach($_POST as $name=>$value){
283
 
 
284
 
      /* Test if there is a variable begining with "del" */
285
 
      if(preg_match("/del/",$name)){
286
 
 
287
 
        /* Extract entry id to delete */
288
 
        $nr = str_replace("del","",$name) ;
289
 
 
290
 
        /* unset entry */
291
 
        unset($this->goFonMacroParameter[$nr]);
292
 
 
293
 
      }elseif(preg_match("/number/",$name)){
294
 
 
295
 
        /* Set Post vars */
296
 
        $key = $_POST[$name];
297
 
 
298
 
        $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
299
 
        $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
300
 
        $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
301
 
        $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
302
 
      }
303
 
    }
304
 
 
305
 
  }
306
 
 
307
 
 
308
 
  //! Checks given values 
309
 
  /*! 
310
 
      Check values\n 
311
 
      If a user enters an invalid value, then this function will output an error msg\n
312
 
      (In better words :prepare the errormessages that will be put out )\n
313
 
  */
314
 
  function check()
315
 
  {
316
 
    /* Call common method to give check the hook */
317
 
    $message= plugin::check();
318
 
 
319
 
    foreach($this->attributes as $attr){
320
 
      if(chkacl($this->acl,"edit")){
321
 
        $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
322
 
        return(array($str));
323
 
      }
324
 
    }
325
 
 
326
 
 
327
 
    foreach($this->goFonMacroParameter as $key=>$val){
328
 
      if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
329
 
        $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
330
 
      }
331
 
      switch($val['type']){
332
 
        case 'bool'   :   $possible = array("","0","1");
333
 
                          if(!in_array($val['default'],$possible)) {
334
 
                            $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
335
 
                          };break;
336
 
        case 'string' :
337
 
        case 'combo'  : 
338
 
        default : ;
339
 
 
340
 
      }
341
 
    }
342
 
    return $message;
343
 
  }
344
 
 
345
 
  //! Save changes to openldap
346
 
  /*!
347
 
      Save to LDAP 
348
 
      This function saves given attributes to the ldap
349
 
  */
350
 
  function save()
351
 
  {
352
 
    /* Post checks */
353
 
 
354
 
    plugin::save();
355
 
 
356
 
    $this->attrs['goFonMacroParameter']=array();
357
 
 
358
 
    foreach($this->goFonMacroParameter as $key=>$fonpara){
359
 
      $this->attrs['goFonMacroParameter'][]=$key."!".base64_encode($fonpara['name'])."!".$fonpara['type']."!".$fonpara['default'];
360
 
    }
361
 
 
362
 
    if($this->para_count != count($this->attrs['goFonMacroParameter'])){
363
 
      print_red(_("Parameter count of the macro changed, you must update each user which are using this macro '%s'."),$this->dn);
364
 
    }
365
 
 
366
 
    unset($this->attrs['base']);
367
 
 
368
 
    /* Write back to ldap */
369
 
    $ldap= $this->config->get_ldap_link();
370
 
    $ldap->cat($this->dn, array('dn'));
371
 
    $a= $ldap->fetch();
372
 
 
373
 
    if (count($a)){
374
 
      $ldap->cd($this->dn);
375
 
      $this->cleanup();
376
 
      $ldap->modify ($this->attrs); 
377
 
 
378
 
      $this->handle_post_events("modify");
379
 
    } else {
380
 
      if(count($this->attrs['goFonMacroParameter']==0)){
381
 
        unset($this->attrs['goFonMacroParameter']);
382
 
      }         
383
 
      $ldap->cd($this->dn);
384
 
      $ldap->create_missing_trees( $this->dn);
385
 
      $ldap->cd($this->dn);
386
 
      $ldap->add($this->attrs);
387
 
      $this->handle_post_events("add");
388
 
    }
389
 
    show_ldap_error($ldap->get_error(), _("Saving phone macro parameters failed"));
390
 
  }
391
 
 
392
 
}
393
 
// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
394
 
?>