Code

new version of french locale
[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         }
248         /* Display switch for true false*/
249         if($type == "bool"){
251           $str="<select name='".$var."' ".chkacl($this->acl, "goFonMacro").">";
253           if($choosen){
254             $str.="\n<option name='".$var."' value='0'>"._("false")."</option>";
255             $str.="\n<option name='".$var."' value='1' selected>"._("true")."</option>";
256           }else{
257             $str.="\n<option name='".$var."' value='0' selected>"._("false")."</option>";
258             $str.="\n<option name='".$var."' value='1'>"._("true")."</option>";
259           }
260           $str.="<select name='".$var."'>";
261         }
263         /* display simple textfield */ 
264         if($type=="string"){
265           $str="<input name='".$var."' value='".$choosen."' ".chkacl($this->acl, "goFonMacro").">";
266         }
268         /* create table entry*/
269         $macrotab.= "\n<tr><td>".$name."</td><td>".$str."</td></tr>"; 
271       }
272       $macrotab.="</table>";
273     }//is_array()
275     /* Give smarty the table */
276     $smarty->assign("macrotab",$macrotab);
278     /* Do we represent a valid account? */
279     if (!$this->is_account && $this->parent == NULL){
280       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
281         _("This account has no phone extensions.")."</b>";
282       $display.= back_to_main();
283       return($display);
284     }
286     $display= "";
288     /* Show tab dialog headers */
289     if ($this->parent != NULL){
290       if ($this->is_account){
291         $display= $this->show_header(_("Remove phone account"),
292             _("This account has phone features enabled. You can disable them by clicking below."));
293       } else {
294         $display= $this->show_header(_("Create phone account"),
295             _("This account has phone features disabled. You can enable them by clicking below."));
296         return ($display);
297       }
298     }
300     /* Add phone number */
301     if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
302       if (is_phone_nr($_POST['phonenumber'])){
303         $number= $_POST["phonenumber"];
304         $this->phoneNumbers[$number]= $number;
305         $this->is_modified= TRUE;
306       } else {
307         print_red(_("Please enter a valid phone number!"));
308       }
309     }
311     /* Remove phone number */
312     if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
313       foreach ($_POST['phonenumber_list'] as $number){
314         unset($this->phoneNumbers[$number]);
315         $this->is_modified= TRUE;
316       }
317     }
319     /* Check for forwarding action */
320     foreach ($this->forwarders as $nr => $fw){
322       /* Buttons pressed? */
323       if (isset($_POST["add_fw$nr"])){
324         $this->forwarders= $this->insert_after("", $nr, $this->forwarders);
325       }
326       if (isset($_POST["remove_fw$nr"])){
327         unset($this->forwarders[$nr]);
328       }
329     }
331     /* Transfer ACL's */
332     foreach($this->attributes as $val){
333       $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
334     }
336     /* Fill arrays */
337     $smarty->assign ("goFonHardware", $this->goFonHardware);
338     if (!count($this->phoneNumbers)){
339       $smarty->assign ("phoneNumbers", array(""));
340     } else {
341       $smarty->assign ("phoneNumbers", $this->phoneNumbers);
342     }
343     $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
344       _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
345     foreach ($this->hardware_list as $cn => $description){
346       if ($cn == $this->goFonHardware){
347         $selected= "selected";
348       } else {
349         $selected= "";
350       }
351       if (isset($this->used_hardware[$cn])){
352         $color= "style=\"color:#A0A0A0\"";
353       } else {
354         $color= "";
355       }
356       $hl.= "  <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
357     }
358     $hl.= "</select>\n";
359     $smarty->assign ("hardware_list", $hl);
361     /* Generate forwarder view */
362     $forwarder_list="";
363     $acl= chkacl($this->acl, "goFonForwaring");
364     foreach ($this->forwarders as $nr => $fw){
365       if ($fw == ""){
366         $number= ""; $timeout= "";
367       } else {
368         list($number, $timeout)= split(";", $fw);
369       }
370       $forwarder_list.= "<tr><td>";
371       $forwarder_list.= "<input name=\"fwn$nr\" size=25 align=\"middle\" maxlength=60 value=\"$number\" $acl>";
372       $forwarder_list.= "</td><td>";
373       $forwarder_list.= "<input name=\"fwt$nr\" size=5 align=\"middle\" maxlength=5 value=\"$timeout\" $acl>";
374       $forwarder_list.= "</td><td>";
375       $forwarder_list.= "<input type=\"submit\" value=\""._("Add")."\" name=\"add_fw$nr\" $acl>";
376       if (count($this->forwarders) > 1){
377         $forwarder_list.= "<input type=\"submit\" value=\""._("Remove")."\" name=\"remove_fw$nr\" $acl>";
378       }
379       $forwarder_list.= "</td></tr>";
380     }
381     $smarty->assign("forwarder_list", $forwarder_list);
383     /* Show main page */
384     $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
385     return($display);
386   }
389   function save_object()
390   {
391     if (isset($_POST["phoneTab"])){
392       plugin::save_object();
394       /* Save checkbox */
395       if (isset($_POST['fon_to_mail'])){
396         $tmp= "[M]";
397       } else {
398         $tmp= "[]";
399       }
400       if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
401         if ($this->goFonDeliveryMode != $tmp){
402           $this->is_modified= TRUE;
403         }
404         $this->goFonDeliveryMode= $tmp;
405       }
407       /* Save forwarding numbers and timeouts */
408       if (chkacl ($this->acl, "goFonForwarder") == ""){
409         foreach ($this->forwarders as $nr => $fw){
410           $tmp= $_POST["fwn$nr"].";".$_POST["fwt$nr"];
411           if ($this->forwarders[$nr] != $tmp){
412             $this->is_modified= TRUE;
413           }
414           $this->forwarders[$nr]= $tmp;
415         }
416       }
418       /* Every macro in the select box are available */
419       if((isset($_POST['macro']))){
420         $this->macrostillavailable=true;
421       }
423       /* get all Postvars */
424       if(isset($this->macroarray[$this->macro])){ 
425         foreach($this->macroarray[$this->macro] as $key => $paras){
426           if(isset($_POST[$paras['var']])){
427             $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']]; 
428           }
429         }
430       }
431     }
434   }
436   function check()
437   {
438     /* Reset message array */
439     $message= array();
441     /* We need at least one phone number */
442     if (count($this->phoneNumbers) == 0){
443       $message[]= sprintf(_("You need to specify at least one phone number!"));
444     }
446     /* Check timestamps and phonenumbers */
447     foreach ($this->forwarders as $fw){
449       /* Skip empty values */
450       if ($fw == ";"){
451         continue;
452       }         
454       /* Check */
455       list($number, $timeout)= split(";", $fw);
456       if (!is_phone_nr($number)){
457         $message[]= sprintf(_("The number '%s' is no valid phone number!"), $number);
458       }
459       if (!is_id($timeout)){
460         $message[]= sprintf(_("The timeout '%s' contains invalid characters!"), $timeout);
461       }
462     }
463     
464     /* check for ! in any parameter setting*/
465     if(isset($this->macroarray[$this->macro])){
466       foreach($this->macroarray[$this->macro] as $val){
467         if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
468           $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
469         }
470       }
471     }
472     return ($message);
473   }
477   function save()
478   {
479     plugin::save();
481     /* Save arrays */
482     $this->attrs['telephoneNumber']= array();
483     foreach ($this->phoneNumbers as $number){
484       $this->attrs['telephoneNumber'][]= $number;
485     }
486     $this->attrs['goFonForwarding']= array();
487     foreach ($this->forwarders as $index => $number){
488       $this->attrs['goFonForwarding'][]= "$index;$number";
489     }
491     /* Save settings, or remove goFonMacro attribute*/
492     if($this->macro!="none"){    
493       $this->attrs['goFonMacro']=$this->macro;
494       if(isset($this->macroarray[$this->macro])){
495         foreach($this->macroarray[$this->macro] as $paras)  {
496           $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
497         }
498       }
499     }else{
500       $this->attrs['goFonMacro']=array();
501     }
502     unset($this->attrs['macro'])  ;
504     /* Write back to ldap */
505     $ldap= $this->config->get_ldap_link();
506     $ldap->cd($this->dn);
507     $ldap->modify($this->attrs);
508     show_ldap_error($ldap->get_error());
510     /* Optionally execute a command after we're done */
511     if ($this->initially_was_account == $this->is_account){
512       if ($this->is_modified){
513         $this->handle_post_events("modify");
514       }
515     } else {
516       $this->handle_post_events("add");
517     }
519   }
522   function insert_after($entry, $nr, $list)
523   {
524     /* Is the entry free? No? Make it free... */
525     if (isset($list[$nr])) {
526       $dest= array();
527       $newidx= 0;
529       foreach ($list as $idx => $contents){
530         $dest[$newidx++]= $contents;
531         if ($idx == $nr){
532           $dest[$newidx++]= $entry;
533         }
534       }
535     } else {
536       $dest= $list;
537       $dest[$nr]= $entry;
538     }
540     return ($dest);
541   }
544   function adapt_from_template($dn)
545   {
546     plugin::adapt_from_template($dn);
548     /* Assemble phone numbers */
549     if (isset($this->attrs['telephoneNumber'])){
550       for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
551         $number= $this->attrs['telephoneNumber'][$i];
552         $this->phoneNumbers[$number]= $number;
553       }
554     }
556     /* Assemble forwarders */
557     if (isset($this->attrs['goFonForwarding'])){
558       for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
559         list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
560         $this->forwarders[$num]= "$v1;$v2";
561       }
562     } else {
563       $this->forwarders= array("");
564     }
565   }
568   function remove_from_parent()
569   {
570     /* Cancel if there's nothing to do here */
571     if (!$this->initially_was_account){
572       return;
573     }
575     plugin::remove_from_parent();
577     /* Just keep one phone number */
578     if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
579       $this->attrs['telephoneNumber']= $this->telephoneNumber[0];
580     } else {
581       $this->attrs['telephoneNumber']= array();
582     }
584     $ldap= $this->config->get_ldap_link();
585     $ldap->cd($this->dn);
586     $ldap->modify($this->attrs);
587     show_ldap_error($ldap->get_error());
589     /* Optionally execute a command after we're done */
590     $this->handle_post_events('remove');
591   }
596 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
597 ?>