Code

e9ec34749523bfd4a9152911a7e751fa069206ee
[gosa.git] / plugins / gofon / phoneaccount / class_phoneAccount.inc
1 <?php
3 class phoneAccount extends plugin
4 {
5   /* Definitions */
6   var $plHeadline= "Phone";
7   var $plDescription= "This does something";
8   var $has_mailAccount= FALSE;
10   /* Attributes */
11   var $telephoneNumber        = "";
12   var $goFonHardware          = "";
13   var $goFonForwarding        = "";
14   var $goFonFormat            = "";
15   var $goFonPIN               = "";
16   var $goFonDeliveryMode      = "";
17   var $phoneNumbers           = array();
18   var $forwarders             = array();
19   var $mail                   = "";
20   var $hardware_list          = array();
21   var $used_hardware          = array();
22   var $goFonMacro             = "";
23   var $macro                  = 0;              // Selected Macor
24   var $macros                 = array();        // List of macros for smarty select box
25   var $macroarray             = array();        // All needed macro informations
26   var $macrostillavailable    = false;
28   /* CLI vars */
29   var $cli_summary            = "Manage users phone account";
30   var $cli_description        = "Some longer text\nfor help";
31   var $cli_parameters         = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
33   /* attribute list for save action */
34   var $attributes             = array("goFonDeliveryMode", "goFonForwarding", "goFonFormat",
35       "goFonHardware", "goFonPIN", "telephoneNumber", "goFonMacro","macro");
36   var $objectclasses= array("goFonAccount");
38   function phoneAccount ($config, $dn= NULL)
39   {
40     plugin::plugin ($config, $dn);
42     /* Set phone hardware */
43     if (!isset($this->attrs['goFonHardware'])){
44       $this->goFonHardware= "automatic";
45     }
47     /* Preset voice format */
48     if (!isset($this->attrs['goFonFormat'])){
49       $this->goFonFormat= "wav";
50     }
52     /* Assemble phone numbers */
53     if (isset($this->attrs['telephoneNumber'])){
54       for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
55         $number= $this->attrs['telephoneNumber'][$i];
56         $this->phoneNumbers[$number]= $number;
57       }
58     }
60     /* Assemble forwarders */
61     if (isset($this->attrs['goFonForwarding'])){
62       for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
63         list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
64         $this->forwarders[$num]= "$v1;$v2";
65       }
66     } else {
67       $this->forwarders= array("");
68     }
70     /* Set up has_mailAccount */
71     if (isset($this->attrs['objectClass'])){
72       if (in_array("gosaMailAccount", $this->attrs['objectClass'])){
73         $this->has_mailAccount= TRUE;
74       }
75     }
77     /* Load hardware list */
78     $ldap= $this->config->get_ldap_link();
79     $ldap->cd($this->config->current['BASE']);
80     $ldap->search("(objectClass=goFonHardware)", array('cn', 'description'));
81     while ($attrs= $ldap->fetch()){
82       $cn= $attrs['cn'][0];
83       if (isset($attrs['description'])){
84         $description= " - ".$attrs['description'][0];
85       } else {
86         $description= "";
87       }
88       $this->hardware_list[$cn]= "$cn$description";
90     }
92     /* Prepare templating */
93     $smarty= get_smarty();
96     /* Perform search, to get Macro Parameters,Name,Dn,Displayname etc*/
97     $ldap->search("(objectClass=goFonMacro)", array("*"));
99     /* Add none for no macro*/
100     $this->macros['none']=_("none");    
101     $this->macro ="none";
104     /* Fetch all Macros*/
105     while ($attrs= $ldap->fetch()){
107       /* Only visisble */
108       if((isset($attrs['goFonMacroVisible'][0]))&&($attrs['goFonMacroVisible'][0] ==1)){
110         /* unset Count, we don't need that here */
111         unset($attrs['displayName']['count']);
113         /* fill Selectfield variable with Macros */
114         if(isset($attrs['displayName'][0])){
115           $this->macros[$attrs['dn']] = $attrs['displayName'][0]." (".$attrs['cn'][0].")";
116         }else{
117           $this->macros[$attrs['dn']] = _("undefined");
118         }
120         /* Parse macro data, unset count for parameterarrays  */
121         unset($attrs['goFonMacroParameter']['count']);
123         /* Go through available parameters and parse all attributes, like parametername, type, default ...*/
124         if((isset($attrs['goFonMacroParameter']))&&(is_array($attrs['goFonMacroParameter']))){
126           foreach($attrs['goFonMacroParameter'] as $pkey=>$pval){
128             /* Split Data in readable values, by delimiter !  */
129             $data = split("!",$attrs['goFonMacroParameter'][$pkey]);
131             /* Set all attrs */
132             $id = $data[0];
133             $this->macroarray[$attrs['dn']][$id]['var']    ="var".$id;
134             $this->macroarray[$attrs['dn']][$id]['choosen']="N/A";
135             $this->macroarray[$attrs['dn']][$id]['id']     = $id;
136             $this->macroarray[$attrs['dn']][$id]['name']   =$data[1];
137             $this->macroarray[$attrs['dn']][$id]['type']   =$data[2];
138             $this->macroarray[$attrs['dn']][$id]['default']=$data[3];
139           }//foreach
140         }//is_array
141       }//visible = 1
142     }//while
143     /* Go through already saved values, for a parameter */
144     $tmp = split("!",$this->goFonMacro);
146     /* it is possible that nothing has been saved yet */
147     if(is_array($tmp)){
149       /* First value is the macroname */
150       $this->macro = $tmp[0];
152       /* Macroname saved, delete that index */
153       unset($tmp[0]);
155       /* Check if makro has been removed */
156       if(!isset($this->macroarray[$this->macro])){
157         $this->macrostillavailable = false;
158       }else{
159         $this->macrostillavailable = true;
160       }
162       /* for each parametervalues ( parameterID#value like 25#twentyfive) */
163       foreach($tmp as $var){
165         /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
166         $varar = split("#",$var);
168         /* Only insert if the parameter still exists */
169         if(isset($this->macroarray[$this->macro][$varar[0]])){
171           /* Assign value */
172           $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
173         }
174       }
175     }
177     /* Eventually colorize phones */
178     $ldap->cd($this->config->current['BASE']);
179     foreach ($this->hardware_list as $cn => $desc){
180       $ldap->search("(goFonHardware=$cn)", array('cn'));
181       if ($ldap->count() > 0){
182         $ldap->fetch();
183         if ($ldap->getDN() != $this->dn){
184           $this->used_hardware[$cn]= $ldap->getDN();
185         }
186       }
187     }
189     $this->hardware_list["automatic"]= _("automatic");
190     ksort($this->hardware_list);
191   }
194   function execute()
195   {
196     /* Do we need to flip is_account state? */
197     if (isset($_POST['modify_state'])){
198       $this->is_account= !$this->is_account;
199     }
201     /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
202     if(empty($this->macro)){
203       $this->macro ="none";
204     }
206     /* tell user that the pluging selected is no longer available*/
207     if((!$this->macrostillavailable)&&($this->macro!="none")){
208       print_red(_("The macro you selected in the past, is no longer available for you, please choose another one."));
209     }
211     /* Prepare templating */
212     $smarty= get_smarty();
214     /* Assing macroselectbox values  */
215     $smarty->assign("macros",$this->macros);   
216     $smarty->assign("macro", $this->macro);   
218     /* Create parameter table, skip if no parameters given */
219     if(!isset($this->macroarray[$this->macro])){
220       $macrotab="";
221     }else{
223       $macrotab ="<table>";
224       /* for every single parameter-> display textfile,combo, or true false switch*/
225       foreach($this->macroarray[$this->macro] as $paras){
227         /* get al vars */
228         $var        = $paras['var'];           
229         $name       = $paras['name'];           
230         $default    = $paras['default'];
231         $type       = $paras['type'];
232         $choosen    = $paras['choosen'] ; 
233         $str        = $default;
235         /* in case of a combo box display a combobox with selected attr */
236         if($type == "combo"){
237           $str="<select name='".$var."' ".chkacl($this->acl, "goFonMacro").">";
238           foreach(split(":",$default) as $choice){
239             if($choosen==$choice){
240               $str.="\n<option name='".$var."' value='".$choice."' selected>".$choice."</option>";
241             }else{
242               $str.="\n<option name='".$var."' value='".$choice."'>".$choice."</option>";
243             }
244           }
245           $str.="</select>";
246         }
249       
250         /* Display switch for true false*/
251         if($type == "bool"){
252           if(!$choosen){
253             $str="\n<input type='checkbox' name='".$var."' value='1'>";
254           }else{
255             $str="\n<input type='checkbox' name='".$var."' value='1' checked>";
256           }
257         }
259         /* display simple textfield */ 
260         if($type=="string"){
261           $str="<input name='".$var."' value='".$choosen."' ".chkacl($this->acl, "goFonMacro").">";
262         }
264         /* create table entry*/
265         $macrotab.= "\n<tr><td>".$name."</td><td>".$str."</td></tr>"; 
267       }
268       $macrotab.="</table>";
269     }//is_array()
271     /* Give smarty the table */
272     $smarty->assign("macrotab",$macrotab);
274     /* Do we represent a valid account? */
275     if (!$this->is_account && $this->parent == NULL){
276       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
277         _("This account has no phone extensions.")."</b>";
278       $display.= back_to_main();
279       return($display);
280     }
282     $display= "";
284     /* Show tab dialog headers */
285     if ($this->parent != NULL){
286       if ($this->is_account){
287         $display= $this->show_header(_("Remove phone account"),
288             _("This account has phone features enabled. You can disable them by clicking below."));
289       } else {
290         $display= $this->show_header(_("Create phone account"),
291             _("This account has phone features disabled. You can enable them by clicking below."));
292         return ($display);
293       }
294     }
296     /* Add phone number */
297     if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
298       if (is_phone_nr($_POST['phonenumber'])){
299         $number= $_POST["phonenumber"];
300         $this->phoneNumbers[$number]= $number;
301         $this->is_modified= TRUE;
302       } else {
303         print_red(_("Please enter a valid phone number!"));
304       }
305     }
307     /* Remove phone number */
308     if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
309       foreach ($_POST['phonenumber_list'] as $number){
310         unset($this->phoneNumbers[$number]);
311         $this->is_modified= TRUE;
312       }
313     }
315     /* Check for forwarding action */
316     foreach ($this->forwarders as $nr => $fw){
318       /* Buttons pressed? */
319       if (isset($_POST["add_fw$nr"])){
320         $this->forwarders= $this->insert_after("", $nr, $this->forwarders);
321       }
322       if (isset($_POST["remove_fw$nr"])){
323         unset($this->forwarders[$nr]);
324       }
325     }
327     /* Transfer ACL's */
328     foreach($this->attributes as $val){
329       $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
330     }
332     /* Fill arrays */
333     $smarty->assign ("goFonHardware", $this->goFonHardware);
334     if (!count($this->phoneNumbers)){
335       $smarty->assign ("phoneNumbers", array(""));
336     } else {
337       $smarty->assign ("phoneNumbers", $this->phoneNumbers);
338     }
339     $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
340       _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
341     foreach ($this->hardware_list as $cn => $description){
342       if ($cn == $this->goFonHardware){
343         $selected= "selected";
344       } else {
345         $selected= "";
346       }
347       if (isset($this->used_hardware[$cn])){
348         $color= "style=\"color:#A0A0A0\"";
349       } else {
350         $color= "";
351       }
352       $hl.= "  <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
353     }
354     $hl.= "</select>\n";
355     $smarty->assign ("hardware_list", $hl);
357     /* Generate forwarder view */
358     $forwarder_list="";
359     $acl= chkacl($this->acl, "goFonForwaring");
360     foreach ($this->forwarders as $nr => $fw){
361       if ($fw == ""){
362         $number= ""; $timeout= "";
363       } else {
364         list($number, $timeout)= split(";", $fw);
365       }
366       $forwarder_list.= "<tr><td>";
367       $forwarder_list.= "<input name=\"fwn$nr\" size=25 align=\"middle\" maxlength=60 value=\"$number\" $acl>";
368       $forwarder_list.= "</td><td>";
369       $forwarder_list.= "<input name=\"fwt$nr\" size=5 align=\"middle\" maxlength=5 value=\"$timeout\" $acl>";
370       $forwarder_list.= "</td><td>";
371       $forwarder_list.= "<input type=\"submit\" value=\""._("Add")."\" name=\"add_fw$nr\" $acl>";
372       if (count($this->forwarders) > 1){
373         $forwarder_list.= "<input type=\"submit\" value=\""._("Remove")."\" name=\"remove_fw$nr\" $acl>";
374       }
375       $forwarder_list.= "</td></tr>";
376     }
377     $smarty->assign("forwarder_list", $forwarder_list);
379     /* Show main page */
380     $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
381     return($display);
382   }
385   function save_object()
386   {
387     if (isset($_POST["phoneTab"])){
388       plugin::save_object();
390       /* Save checkbox */
391       if (isset($_POST['fon_to_mail'])){
392         $tmp= "[M]";
393       } else {
394         $tmp= "[]";
395       }
396       if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
397         if ($this->goFonDeliveryMode != $tmp){
398           $this->is_modified= TRUE;
399         }
400         $this->goFonDeliveryMode= $tmp;
401       }
403       /* Save forwarding numbers and timeouts */
404       if (chkacl ($this->acl, "goFonForwarder") == ""){
405         foreach ($this->forwarders as $nr => $fw){
406           $tmp= $_POST["fwn$nr"].";".$_POST["fwt$nr"];
407           if ($this->forwarders[$nr] != $tmp){
408             $this->is_modified= TRUE;
409           }
410           $this->forwarders[$nr]= $tmp;
411         }
412       }
414       /* Every macro in the select box are available */
415       if((isset($_POST['macro']))){
416         $this->macrostillavailable=true;
417       }
419       /* get all Postvars */
420       if(isset($this->macroarray[$this->macro])){ 
421         foreach($this->macroarray[$this->macro] as $key => $paras){
422           if(isset($_POST[$paras['var']])){
423             $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']]; 
424           }
425         
426           /* Checkboxes are special, they are not Posted if they are not selected, so the won't be changed with the above code 
427              We need this code below to read and save checkboxes correct
428           */
429           if($this->macroarray[$this->macro][$key]['type']=="bool"){
430             if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
431               $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
432             }else{
433               $this->macroarray[$this->macro][$key]['choosen']=false;
434             }
435           }
436         }
437       }
438     }
441   }
443   function check()
444   {
445     /* Reset message array */
446     $message= array();
448     /* We need at least one phone number */
449     if (count($this->phoneNumbers) == 0){
450       $message[]= sprintf(_("You need to specify at least one phone number!"));
451     }
453     /* Check timestamps and phonenumbers */
454     foreach ($this->forwarders as $fw){
456       /* Skip empty values */
457       if ($fw == ";"){
458         continue;
459       }         
461       /* Check */
462       list($number, $timeout)= split(";", $fw);
463       if (!is_phone_nr($number)){
464         $message[]= sprintf(_("The number '%s' is no valid phone number!"), $number);
465       }
466       if (!is_id($timeout)){
467         $message[]= sprintf(_("The timeout '%s' contains invalid characters!"), $timeout);
468       }
469     }
470     
471     /* check for ! in any parameter setting*/
472     if(isset($this->macroarray[$this->macro])){
473       foreach($this->macroarray[$this->macro] as $val){
474         if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
475           $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
476         }
477       }
478     }
479     return ($message);
480   }
484   function save()
485   {
486     plugin::save();
488     /* Save arrays */
489     $this->attrs['telephoneNumber']= array();
490     foreach ($this->phoneNumbers as $number){
491       $this->attrs['telephoneNumber'][]= $number;
492     }
493     $this->attrs['goFonForwarding']= array();
494     foreach ($this->forwarders as $index => $number){
495       $this->attrs['goFonForwarding'][]= "$index;$number";
496     }
498     /* Save settings, or remove goFonMacro attribute*/
499     if($this->macro!="none"){    
500       $this->attrs['goFonMacro']=$this->macro;
501       if(isset($this->macroarray[$this->macro])){
502         foreach($this->macroarray[$this->macro] as $paras)  {
503           $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
504         }
505       }
506     }else{
507       $this->attrs['goFonMacro']=array();
508     }
509     unset($this->attrs['macro'])  ;
511     /* Write back to ldap */
512     $ldap= $this->config->get_ldap_link();
513     $ldap->cd($this->dn);
514     $ldap->modify($this->attrs);
515     show_ldap_error($ldap->get_error());
517     /* Optionally execute a command after we're done */
518     if ($this->initially_was_account == $this->is_account){
519       if ($this->is_modified){
520         $this->handle_post_events("modify");
521       }
522     } else {
523       $this->handle_post_events("add");
524     }
526   }
529   function insert_after($entry, $nr, $list)
530   {
531     /* Is the entry free? No? Make it free... */
532     if (isset($list[$nr])) {
533       $dest= array();
534       $newidx= 0;
536       foreach ($list as $idx => $contents){
537         $dest[$newidx++]= $contents;
538         if ($idx == $nr){
539           $dest[$newidx++]= $entry;
540         }
541       }
542     } else {
543       $dest= $list;
544       $dest[$nr]= $entry;
545     }
547     return ($dest);
548   }
551   function adapt_from_template($dn)
552   {
553     plugin::adapt_from_template($dn);
555     /* Assemble phone numbers */
556     if (isset($this->attrs['telephoneNumber'])){
557       for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
558         $number= $this->attrs['telephoneNumber'][$i];
559         $this->phoneNumbers[$number]= $number;
560       }
561     }
563     /* Assemble forwarders */
564     if (isset($this->attrs['goFonForwarding'])){
565       for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
566         list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
567         $this->forwarders[$num]= "$v1;$v2";
568       }
569     } else {
570       $this->forwarders= array("");
571     }
572   }
575   function remove_from_parent()
576   {
577     /* Cancel if there's nothing to do here */
578     if (!$this->initially_was_account){
579       return;
580     }
582     plugin::remove_from_parent();
584     /* Just keep one phone number */
585     if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
586       $this->attrs['telephoneNumber']= $this->telephoneNumber[0];
587     } else {
588       $this->attrs['telephoneNumber']= array();
589     }
591     $ldap= $this->config->get_ldap_link();
592     $ldap->cd($this->dn);
593     $ldap->modify($this->attrs);
594     show_ldap_error($ldap->get_error());
596     /* Optionally execute a command after we're done */
597     $this->handle_post_events('remove');
598   }
603 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
604 ?>