Code

Input fields must be disabled in case of missing ACL
[gosa.git] / plugins / gofon / phoneaccount / class_phoneAccount.inc
index 0a99471a393a9ab6484ae5d1ada5bb74fa5bff11..0a9601386ec7c9856418606a92106ee48cc99018 100644 (file)
@@ -8,26 +8,31 @@ class phoneAccount extends plugin
   var $has_mailAccount= FALSE;
 
   /* Attributes */
-  var $telephoneNumber= "";
-  var $goFonHardware= "";
-  var $goFonForwarding= "";
-  var $goFonFormat= "";
-  var $goFonPIN= "";
-  var $goFonDeliveryMode= "";
-  var $phoneNumbers= array();
-  var $forwarders= array();
-  var $mail= "";
-  var $hardware_list= array();
-  var $used_hardware= array();
+  var $telephoneNumber        = "";
+  var $goFonHardware          = "";
+  var $goFonForwarding        = "";
+  var $goFonFormat            = "";
+  var $goFonPIN               = "";
+  var $goFonDeliveryMode      = "";
+  var $phoneNumbers           = array();
+  var $forwarders             = array();
+  var $mail                   = "";
+  var $hardware_list          = array();
+  var $used_hardware          = array();
+  var $goFonMacro             = "";
+  var $macro                  = 0;              // Selected Macor
+  var $macros                 = array();        // List of macros for smarty select box
+  var $macroarray             = array();        // All needed macro informations
+  var $macrostillavailable    = false;
 
   /* CLI vars */
-  var $cli_summary= "Manage users phone account";
-  var $cli_description= "Some longer text\nfor help";
-  var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
+  var $cli_summary            = "Manage users phone account";
+  var $cli_description        = "Some longer text\nfor help";
+  var $cli_parameters         = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
 
   /* attribute list for save action */
-  var $attributes= array("goFonDeliveryMode", "goFonForwarding", "goFonFormat",
-      "goFonHardware", "goFonPIN", "telephoneNumber");
+  var $attributes             = array("goFonDeliveryMode", "goFonForwarding", "goFonFormat",
+      "goFonHardware", "goFonPIN", "telephoneNumber", "goFonMacro","macro");
   var $objectclasses= array("goFonAccount");
 
   function phoneAccount ($config, $dn= NULL)
@@ -81,7 +86,93 @@ class phoneAccount extends plugin
         $description= "";
       }
       $this->hardware_list[$cn]= "$cn$description";
-      
+
+    }
+
+    /* Prepare templating */
+    $smarty= get_smarty();
+
+
+    /* Perform search, to get Macro Parameters,Name,Dn,Displayname etc*/
+    $ldap->search("(objectClass=goFonMacro)", array("*"));
+
+    /* Add none for no macro*/
+    $this->macros['none']=_("no macro");    
+    $this->macro ="none";
+
+
+    /* Fetch all Macros*/
+    while ($attrs= $ldap->fetch()){
+
+      /* Only visisble */
+      if((isset($attrs['goFonMacroVisible'][0]))&&($attrs['goFonMacroVisible'][0] ==1)){
+
+        /* unset Count, we don't need that here */
+        unset($attrs['displayName']['count']);
+
+        /* fill Selectfield variable with Macros */
+        if(isset($attrs['displayName'][0])){
+          $this->macros[$attrs['dn']] = $attrs['displayName'][0]." (".$attrs['cn'][0].")";
+        }else{
+          $this->macros[$attrs['dn']] = _("undefined");
+        }
+
+        /* Parse macro data, unset count for parameterarrays  */
+        unset($attrs['goFonMacroParameter']['count']);
+
+        /* Go through available parameters and parse all attributes, like parametername, type, default ...*/
+        if((isset($attrs['goFonMacroParameter']))&&(is_array($attrs['goFonMacroParameter']))){
+
+          foreach($attrs['goFonMacroParameter'] as $pkey=>$pval){
+
+            /* Split Data in readable values, by delimiter !  */
+            $data = split("!",$attrs['goFonMacroParameter'][$pkey]);
+
+            /* Set all attrs */
+            $id = $data[0];
+            $this->macroarray[$attrs['dn']][$id]['var']    ="var".$id;
+            $this->macroarray[$attrs['dn']][$id]['choosen']="N/A";
+            $this->macroarray[$attrs['dn']][$id]['id']     = $id;
+            $this->macroarray[$attrs['dn']][$id]['name']   =$data[1];
+            $this->macroarray[$attrs['dn']][$id]['type']   =$data[2];
+            $this->macroarray[$attrs['dn']][$id]['default']=$data[3];
+          }//foreach
+        }//is_array
+      }//visible = 1
+    }//while
+
+    /* Go through already saved values, for a parameter */
+    $tmp = split("!",$this->goFonMacro);
+
+    /* it is possible that nothing has been saved yet */
+    if(is_array($tmp)){
+
+      /* First value is the macroname */
+      $this->macro = $tmp[0];
+
+      /* Macroname saved, delete that index */
+      unset($tmp[0]);
+
+      /* Check if makro has been removed */
+      if(!isset($this->macroarray[$this->macro])){
+        $this->macrostillavailable = false;
+      }else{
+        $this->macrostillavailable = true;
+      }
+
+      /* for each parametervalues ( parameterID#value like 25#twentyfive) */
+      foreach($tmp as $var){
+
+        /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
+        $varar = split("#",$var);
+
+        /* Only insert if the parameter still exists */
+        if(isset($this->macroarray[$this->macro][$varar[0]])){
+
+          /* Assign value */
+          $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
+        }
+      }
     }
 
     /* Eventually colorize phones */
@@ -95,7 +186,7 @@ class phoneAccount extends plugin
         }
       }
     }
-    
+
     $this->hardware_list["automatic"]= _("automatic");
     ksort($this->hardware_list);
   }
@@ -108,6 +199,81 @@ class phoneAccount extends plugin
       $this->is_account= !$this->is_account;
     }
 
+    /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
+    if(empty($this->macro)){
+      $this->macro ="none";
+    }
+
+    /* tell user that the pluging selected is no longer available*/
+    if((!$this->macrostillavailable)&&($this->macro!="none")){
+      print_red(_("The macro you selected in the past, is no longer available for you, please choose another one."));
+    }
+
+    /* Prepare templating */
+    $smarty= get_smarty();
+
+    /* Assing macroselectbox values  */
+    $smarty->assign("macros",$this->macros);   
+    $smarty->assign("macro", $this->macro);   
+
+    /* Create parameter table, skip if no parameters given */
+    if(!isset($this->macroarray[$this->macro])){
+      $macrotab="";
+    }else{
+
+      $macrotab ="<table summary=\""._("Parameter")."\">";
+      /* for every single parameter-> display textfile,combo, or true false switch*/
+      foreach($this->macroarray[$this->macro] as $paras){
+
+        /* get al vars */
+        $var        = $paras['var'];           
+        $name       = $paras['name'];           
+        $default    = $paras['default'];
+        $type       = $paras['type'];
+        $choosen    = $paras['choosen'] ; 
+        $str        = $default;
+
+        /* in case of a combo box display a combobox with selected attr */
+        $macrotab.= "<tr>";
+        switch ($type){
+
+          case "combo":
+            $str= "<select name='".$var."' ".chkacl($this->acl, "goFonMacro")."  ".chkacl($this->acl, "goFonMacro").">";
+            foreach(split(":",$default) as $choice){
+              if($choosen==$choice){
+                $str.= "\n<option value='".$choice."' selected>".$choice."&nbsp;</option>";
+              }else{
+                $str.= "\n<option value='".$choice."'>".$choice."&nbsp;</option>";
+              }
+            }
+            $str.="</select>";
+            $macrotab.= "<td>$name</td><td>$str";
+            break;
+
+          case "bool":
+            if(!$choosen){
+              $str="\n<input type='checkbox' name='".$var."' value='1' ".chkacl($this->acl, "goFonMacro")." >";
+            }else{
+              $str="\n<input type='checkbox' name='".$var."' value='1' checked  ".chkacl($this->acl, "goFonMacro").">";
+            }
+            $macrotab.= "<td colspan='2'>$str&nbsp;$name";
+            break;
+
+          case "string":
+            $str="<input name='".$var."' value='".$choosen."' ".chkacl($this->acl, "goFonMacro").">";
+            $macrotab.= "<td>$name</td><td>$str";
+            break;
+
+        }
+        $macrotab.= "</td</tr>";
+
+      }
+      $macrotab.="</table>";
+    }//is_array()
+
+    /* Give smarty the table */
+    $smarty->assign("macrotab",$macrotab);
+
     /* Do we represent a valid account? */
     if (!$this->is_account && $this->parent == NULL){
       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
@@ -161,19 +327,21 @@ class phoneAccount extends plugin
       }
     }
 
-    /* Prepare templating */
-    $smarty= get_smarty();
-
     /* Transfer ACL's */
     foreach($this->attributes as $val){
       $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
+      $smarty->assign($val,$this->$val);
     }
 
     /* Fill arrays */
     $smarty->assign ("goFonHardware", $this->goFonHardware);
-    $smarty->assign ("phoneNumbers", $this->phoneNumbers);
+    if (!count($this->phoneNumbers)){
+      $smarty->assign ("phoneNumbers", array(""));
+    } else {
+      $smarty->assign ("phoneNumbers", $this->phoneNumbers);
+    }
     $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
-         _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
+      _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
     foreach ($this->hardware_list as $cn => $description){
       if ($cn == $this->goFonHardware){
         $selected= "selected";
@@ -185,7 +353,7 @@ class phoneAccount extends plugin
       } else {
         $color= "";
       }
-      $hl.= "  <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
+      $hl.= "  <option $color label=\"$cn\" value=\"$cn\" $selected>$description&nbsp;</option>\n";
     }
     $hl.= "</select>\n";
     $smarty->assign ("hardware_list", $hl);
@@ -212,27 +380,6 @@ class phoneAccount extends plugin
     }
     $smarty->assign("forwarder_list", $forwarder_list);
 
-
-
-    /* Check box */
-    if ($this->parent->by_object['mailAccount'] &&
-        $this->parent->by_object['mailAccount']->is_account &&
-        preg_match("/M/i", $this->goFonDeliveryMode)){
-      $smarty->assign("fon_to_mail", "checked");
-    } else {
-      $smarty->assign("fon_to_mail", "");
-    }
-
-    if (!isset($this->parent->by_object['mailAccount'])) {
-      $smarty->assign("has_mailaccount", "false");
-      $this->has_mailAccount= false;
-    } elseif ( !$this->parent->by_object['mailAccount']->is_account){
-      $smarty->assign("has_mailaccount", "false");
-      $this->has_mailAccount= false;
-    } else {
-      $smarty->assign("has_mailaccount", "true");
-    }
-
     /* Show main page */
     $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
     return($display);
@@ -268,13 +415,33 @@ class phoneAccount extends plugin
         }
       }
 
-      /* Check if mail account is active and correct the internal
-         reference to represent the current status. */
-      if ($this->parent->by_object['mailAccount']->is_account){
-        $this->has_mailAccount= TRUE;
+      /* Every macro in the select box are available */
+      if((isset($_POST['macro']))){
+        $this->macrostillavailable=true;
+      }
+
+      /* get all Postvars */
+      if(isset($this->macroarray[$this->macro])){ 
+        foreach($this->macroarray[$this->macro] as $key => $paras){
+          if(isset($_POST[$paras['var']])){
+            $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']]; 
+          }
+        
+          /* Checkboxes are special, they are not Posted if they are not selected, so the won't be changed with the above code 
+             We need this code below to read and save checkboxes correct
+          */
+          if($this->macroarray[$this->macro][$key]['type']=="bool"){
+            if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
+              $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
+            }else{
+              $this->macroarray[$this->macro][$key]['choosen']=false;
+            }
+          }
+        }
       }
     }
 
+
   }
 
   function check()
@@ -287,6 +454,16 @@ class phoneAccount extends plugin
       $message[]= sprintf(_("You need to specify at least one phone number!"));
     }
 
+    if(($this->goFonPIN)==""){
+      $this->goFonPIN = array();
+    }else{
+      if(strcmp ((int)($this->goFonPIN),($this->goFonPIN))){
+        $message[] = sprintf(_("The given PIN is not valid, only numbers are allowed for this type."));
+      }elseif(strlen($this->goFonPIN) < 4){
+        $message[] = sprintf(_("The given PIN is too short"));
+      }
+
+    }
     /* Check timestamps and phonenumbers */
     foreach ($this->forwarders as $fw){
 
@@ -304,7 +481,15 @@ class phoneAccount extends plugin
         $message[]= sprintf(_("The timeout '%s' contains invalid characters!"), $timeout);
       }
     }
-
+    
+    /* check for ! in any parameter setting*/
+    if(isset($this->macroarray[$this->macro])){
+      foreach($this->macroarray[$this->macro] as $val){
+        if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
+          $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
+        }
+      }
+    }
     return ($message);
   }
 
@@ -314,13 +499,6 @@ class phoneAccount extends plugin
   {
     plugin::save();
 
-    /* goFonAccount has "mail" as must! Block if no mailaddress is specified... */
-    if (isset($this->parent->by_object['mailAccount']) &&
-        !$this->parent->by_object['mailAccount']->is_account) {
-
-      $this->goFonDeliveryMode= preg_replace("/M/i", "", $this->goFonDeliveryMode);
-    }
-
     /* Save arrays */
     $this->attrs['telephoneNumber']= array();
     foreach ($this->phoneNumbers as $number){
@@ -331,6 +509,22 @@ class phoneAccount extends plugin
       $this->attrs['goFonForwarding'][]= "$index;$number";
     }
 
+    /* Save settings, or remove goFonMacro attribute*/
+    if($this->macro!="none"){    
+      $this->attrs['goFonMacro']=$this->macro;
+      if(isset($this->macroarray[$this->macro])){
+        foreach($this->macroarray[$this->macro] as $paras)  {
+          $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
+        }
+      }
+    }else{
+      $this->attrs['goFonMacro']=array();
+    }
+    unset($this->attrs['macro'])  ;
+
+    if($this->attrs['goFonMacro']==""){
+      $this->attrs['goFonMacro']=array();
+    }
     /* Write back to ldap */
     $ldap= $this->config->get_ldap_link();
     $ldap->cd($this->dn);
@@ -397,11 +591,16 @@ class phoneAccount extends plugin
 
   function remove_from_parent()
   {
+    /* unset macro attr, it will cause an error */
+    $tmp = array_flip($this->attributes);
+    unset($tmp['macro']);
+    $this->attributes=array_flip($tmp);
+  
     /* Cancel if there's nothing to do here */
     if (!$this->initially_was_account){
       return;
     }
-    
+
     plugin::remove_from_parent();
 
     /* Just keep one phone number */