Code

Replaced in_array calls for gosa-plugins
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_gotoLpdEnabled.inc
index 8c8152522061c1045ebda419b1d0af924b67a2fe..44b106a2a56ba74d4ad8c48c2cfd4454efa39c89 100644 (file)
@@ -8,7 +8,7 @@ class gotoLpdEnable extends plugin
   private $data = array();
   private $default_entry = array();
 
-  protected $s_Type = "USB";
+  protected $s_Type = "U";
   protected $s_Device = "/dev/usb/lp";
   protected $i_Port = 9100;   
   protected $s_Options = "";
@@ -26,15 +26,14 @@ class gotoLpdEnable extends plugin
   private $a_Speeds = array();      // 
   private $a_Bits = array();        //
   private $a_Devices = array();
-
   public    $attributes = array("s_Type","s_Device","i_Port","s_Options","s_WriteOnly",
       "s_Speed","s_FlowControl","s_Parity","i_Bit");
 
   /*! \brief  Create gotoLpdEnable dialog, allows to edit printer settings.
       @param  Object  $config  The GOsa configuration object.
       @param  Array   $data    The values for attribute 'gotoLpdEnable'
-                      e.g.     array("USB:/dev/usb/lp0:9100:9600:S:E:0:Y:options",
-                               "USB:/dev/usb/lp1:9101:9600:S:E:5:Y:", ...)
+                      e.g.     array("U:/dev/usb/lp0:9100:9600:S:E:0:Y:options",
+                               "U:/dev/usb/lp1:9101:9600:S:E:5:Y:", ...)
       @param  Object  $parent  A reference to the parent plugin-object. 
       @return Object  gotoLpdEnable (plugin) 
   */
@@ -47,9 +46,9 @@ class gotoLpdEnable extends plugin
     /* Predefine selectable values 
      */ 
     $this->a_Types = array(
-        "USB" => _("USB"),
-        "Parallel" => _("Parallel port"),
-        "Serial" => _("Serial"));
+        "U" => _("USB"),
+        "P" => _("Parallel port"),
+        "S" => _("Serial"));
     $this->a_FlowControl = array(
         "S" => _("Software"),
         "H" => _("Hardware"));
@@ -69,9 +68,9 @@ class gotoLpdEnable extends plugin
     $this->a_Bits = array(
         5,6,7,8);
     $this->a_Devices = array(
-        "USB" => "/dev/usb/lp",
-        "Parallel" => "/dev/lp",
-        "Serial" => "/dev/ttyS");
+        "U" => "/dev/usb/lp",
+        "P" => "/dev/lp",
+        "S" => "/dev/ttyS");
 
     /* Create default entry 
      */
@@ -85,10 +84,8 @@ class gotoLpdEnable extends plugin
       $this->is_account = FALSE;
     }else{
       foreach($data as $dat){
-
         if(substr_count($dat,":") < 8) continue;
-
-        list($s_Type,$s_Device,$i_Port,$s_Speed,$s_FlowControl,$s_Parity,$i_Bit,$s_WriteOnly,$s_Options) = split("\:",$dat);
+        list($s_Type,$s_Device,$i_Port,$s_Speed,$s_FlowControl,$s_Parity,$i_Bit,$s_WriteOnly,$s_Options) = explode(":",$dat);
         $entry = array();
         foreach($this->attributes as $attr){
           $entry[$attr] = $$attr;
@@ -96,12 +93,13 @@ class gotoLpdEnable extends plugin
         $this->data[] = $entry;
       }
 
-      if(count($this->data)){
       /* Set first entry values 
+          From "$this->data[0]" to "$this->"
        */
-      foreach($this->attributes as $attr){
-        $this->$attr = $this->data[$this->DevID][$attr];
-      }
+      if(count($this->data)){
+        foreach($this->attributes as $attr){
+          $this->$attr = $this->data[$this->DevID][$attr];
+        }
         $this->is_account = TRUE;
       }      
     }
@@ -119,28 +117,11 @@ class gotoLpdEnable extends plugin
        public function execute()
        {
     $display = "";
-    if(isset($_POST['modify_state'])){
-      $this->is_account = !$this->is_account;
-      if(!$this->is_account){
-        $this->data = array();
-      }else{
-        $entry = $this->default_entry;
-        $entry['i_Port'] = $entry['i_Port'] + count($this->data);
-        $entry['s_Device'] = $entry['s_Device'].count($this->data);
-        $this->data = array(0 => $entry);
-        $this->DevID = 0; 
-        foreach($this->attributes as $attr){
-          $this->$attr = $this->data[$this->DevID][$attr];
-        }
-      }
-    }
 
-    if($this->is_account){
-        $display .= $this->show_disable_header(msgPool::removeFeaturesButton(_("Printer")),"",FALSE);
-    }else{
-        $display .= $this->show_enable_header(msgPool::addFeaturesButton(_("Printer")),"",FALSE);
-    }
+    /* Set smarty variables 
+     */
                $smarty = get_smarty();
+    $smarty->assign("acl",$this->parent->getacl("gotoLpdEnable"));
     foreach($this->attributes as $attr){
       $smarty->assign($attr,$this->$attr);
     }
@@ -151,7 +132,6 @@ class gotoLpdEnable extends plugin
     $smarty->assign("data_cnt",count($this->data));
     $smarty->assign("data",$this->data);
     $smarty->assign("a_DevIDs",array_keys($this->data));
-    $smarty->assign("s_WriteOnly", $this->s_WriteOnly == "Y");
     $smarty->assign("is_account",$this->is_account);
                return($display.$smarty->fetch(get_template_path("gotoLpdEnable.tpl",TRUE,dirname(__FILE__))));
        }
@@ -186,7 +166,25 @@ class gotoLpdEnable extends plugin
   */
   public function save_object()
   {
-    if(!$this->is_account) return;
+    if(!$this->parent->acl_is_writeable("gotoLpdEnable")){
+      return;
+    }
+
+    if(!isset($_POST['gotoLpdEnable_entry_posted'])){
+      return;
+    }
+
+    /* Handle account add/remove 
+     */
+    if(isset($_POST['gotoLpdEnable_enabled'])){
+      $this->is_account = TRUE;
+    }else{
+      $this->is_account = FALSE;
+    }
+
+    /* Check if we have to propose device settings 
+        (current device info is empty and the printer type has changed)
+     */
     $propose_device = "";
     if(isset($_POST['s_Device']) && empty($_POST['s_Device']) && 
         isset($_POST['s_Type']) && $_POST['s_Type'] != $this->s_Type){
@@ -195,28 +193,34 @@ class gotoLpdEnable extends plugin
 
     plugin::save_object();
 
-    if(isset($_POST['gotoLpdEnable_entry_posted']) && isset($_POST['s_WriteOnly'])){
+    if(!empty($propose_device)){
+      $this->s_Device = $propose_device;
+    }
+
+    /* Get checkbox changes 
+      */
+    if(isset($_POST['s_WriteOnly'])){
       $this->s_WriteOnly = "Y";
     }else{
       $this->s_WriteOnly = "N";
     }
 
-    if(!empty($propose_device)){
-      $this->s_Device = $propose_device;
-    }
-
+    /* Write back attributes to data array ($this->data)
+     */
     foreach($this->attributes as $attr){
       $this->data[$this->DevID][$attr] = $this->$attr;
     } 
 
-    /* Device ID has changed */
+    /* Device ID has changed another printer was selected * /
     if(isset($_POST['DevID']) && $_POST['DevID'] != $this->DevID){
       $this->DevID = $_POST['DevID'];
       foreach($this->attributes as $attr){
         $this->$attr = $this->data[$this->DevID][$attr];
       }
-    }
+    } 
+    */
 
+    /* Add and remove additional printer settings 
     if(isset($_POST['del_printer'])){
       unset($this->data[$this->DevID]);
       $this->data = array_values($this->data);
@@ -237,6 +241,7 @@ class gotoLpdEnable extends plugin
         }
       }
     }
+    */
   }
 
 
@@ -248,11 +253,16 @@ class gotoLpdEnable extends plugin
   public function save()
   {
     $ret = array();
+    $serial_only = array("s_Speed","s_FlowControl","s_Parity","i_Bit");
     $attrs = array("s_Type","s_Device","i_Port","s_Speed","s_FlowControl","s_Parity","i_Bit","s_WriteOnly","s_Options"); 
     foreach($this->data as $entry){
       $str = "";
       foreach($attrs as $attr){
-        $str .= $entry[$attr].":";
+        if(in_array_strict($attr,$serial_only) && $entry['s_Type'] != "Serial"){
+          $str .= ":";
+        }else{
+          $str .= $entry[$attr].":";
+        }
       }
       $ret[] = preg_replace("/:$/","",$str);
     }