Code

Added gotoLpd to terminals
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_gotoLpdEnabled.inc
1 <?php
3 class gotoLpdEnable extends plugin
4 {
5         public $parent;
7   protected $DevID = 0;
8   private $data = array();
9   private $default_entry = array();
11   protected $s_Type = "USB";
12   protected $s_Device = "/dev/usb/lp";
13   protected $i_Port = 9100;   
14   protected $s_Options = "";
15   protected $s_WriteOnly = "Y";
17   protected $s_Speed = 9600;
18   protected $s_FlowControl = "S";
19   protected $s_Parity = "E";
20   protected $i_Bit = 5;
22   /* Option predefinition */
23   private $a_Types = array();
24   private $a_FlowControls = array(); //  
25   private $a_Parities = array();    //  
26   private $a_Speeds = array();      // 
27   private $a_Bits = array();        //
28   private $a_Devices = array();
30   public    $attributes = array("s_Type","s_Device","i_Port","s_Options","s_WriteOnly",
31       "s_Speed","s_FlowControl","s_Parity","i_Bit");
33   /*! \brief  Create gotoLpdEnable dialog, allows to edit printer settings.
34       @param  Object  $config  The GOsa configuration object.
35       @param  Array   $data    The values for attribute 'gotoLpdEnable'
36                       e.g.     array("USB:/dev/usb/lp0:9100:9600:S:E:0:Y:options",
37                                "USB:/dev/usb/lp1:9101:9600:S:E:5:Y:", ...)
38       @param  Object  $parent  A reference to the parent plugin-object. 
39       @return Object  gotoLpdEnable (plugin) 
40   */
41         public function __construct($config,$data,&$parent)
42         {
43                 plugin::plugin($config,NULL);
44                 $this->parent = $parent;
45     $this->DevID = 0;
47     /* Predefine selectable values 
48      */ 
49     $this->a_Types = array(
50         "USB" => _("USB"),
51         "Parallel" => _("Parallel port"),
52         "Serial" => _("Serial"));
53     $this->a_FlowControl = array(
54         "S" => _("Software"),
55         "H" => _("Hardware"));
56     $this->a_Parities = array(
57         "E"  => _("Even"),
58         "O"  => _("Odd"),
59         "N"  => _("None"));
60     $this->a_Speeds = array(
61         "4800"  => "4.800&nbsp;"._("bit/s"),
62         "9600"  => "9.600&nbsp;"._("bit/s"),
63         "19200" => "19.200&nbsp;"._("bit/s"),
64         "38400" => "38.400&nbsp;"._("bit/s"),
65         "57600" => "57.600&nbsp;"._("bit/s"),
66         "115200"=> "115.200&nbsp;"._("bit/s"),
67         "230400"=> "230.400&nbsp;"._("bit/s"),
68         "460800"=> "460.800&nbsp;"._("bit/s"));
69     $this->a_Bits = array(
70         5,6,7,8);
71     $this->a_Devices = array(
72         "USB" => "/dev/usb/lp",
73         "Parallel" => "/dev/lp",
74         "Serial" => "/dev/ttyS");
76     /* Create default entry 
77      */
78     foreach($this->attributes as $attr){
79       $this->default_entry[$attr] = $this->$attr;
80     }
82     /* Load current settings 
83      */
84     if(count($data) == 0){
85       $this->is_account = FALSE;
86     }else{
87       foreach($data as $dat){
89         if(substr_count($dat,":") < 8) continue;
91         list($s_Type,$s_Device,$i_Port,$s_Speed,$s_FlowControl,$s_Parity,$i_Bit,$s_WriteOnly,$s_Options) = split("\:",$dat);
92         $entry = array();
93         foreach($this->attributes as $attr){
94           $entry[$attr] = $$attr;
95         }
96         $this->data[] = $entry;
97       }
99       if(count($this->data)){
100       /* Set first entry values 
101        */
102       foreach($this->attributes as $attr){
103         $this->$attr = $this->data[$this->DevID][$attr];
104       }
105         $this->is_account = TRUE;
106       }      
107     }
108     $this->initially_was_account = $this->is_account;
109   }
112   /*! \brief  Create HTML output of this plugin.
113               Depending on the current plugin status, there is a 
114                 'create' and a 'remove'account button displayed on top
115                followed by the gotoLpdEnable options.
116       @param  .
117       @return String  HTML content
118   */
119         public function execute()
120         {
121     $display = "";
122     if(isset($_POST['modify_state'])){
123       $this->is_account = !$this->is_account;
124       if(!$this->is_account){
125         $this->data = array();
126       }else{
127         $entry = $this->default_entry;
128         $entry['i_Port'] = $entry['i_Port'] + count($this->data);
129         $entry['s_Device'] = $entry['s_Device'].count($this->data);
130         $this->data = array(0 => $entry);
131         $this->DevID = 0; 
132         foreach($this->attributes as $attr){
133           $this->$attr = $this->data[$this->DevID][$attr];
134         }
135       }
136     }
138     if($this->is_account){
139         $display .= $this->show_disable_header(msgPool::removeFeaturesButton(_("Printer")),"",FALSE);
140     }else{
141         $display .= $this->show_enable_header(msgPool::addFeaturesButton(_("Printer")),"",FALSE);
142     }
143                 $smarty = get_smarty();
144     foreach($this->attributes as $attr){
145       $smarty->assign($attr,$this->$attr);
146     }
147     foreach(array("DevID","a_Types","a_FlowControl","a_Parities","a_Speeds","a_Bits") as $attr){
148       $smarty->assign($attr,$this->$attr);
149     }
150     ksort($this->data);
151     $smarty->assign("data_cnt",count($this->data));
152     $smarty->assign("data",$this->data);
153     $smarty->assign("a_DevIDs",array_keys($this->data));
154     $smarty->assign("s_WriteOnly", $this->s_WriteOnly == "Y");
155     $smarty->assign("is_account",$this->is_account);
156                 return($display.$smarty->fetch(get_template_path("gotoLpdEnable.tpl",TRUE,dirname(__FILE__))));
157         }
160   /*! \brief  Checks the given informations and returns an array 
161                with the error messages or an empty array if everything went fine.
162       @param  .
163       @return Array of Strings  Error messages.
164   */
165   public function check()
166   {
167     $messages = plugin::check();
168     if(!$this->is_account){
169       return($messages);
170     }
171     foreach($this->data as $id => $entry){
172       if(!tests::is_id($entry['i_Port'])){
173         $messages[] = msgPool::invalid(_("Port"));
174       }
175       if(preg_match("/:/",$entry['s_Options'])){
176         $messages[] = msgPool::invalid(_("Options"));
177       }
178     }
179     return($messages);
180   }  
183   /*! \brief  Save all ui inputs. 
184       @param  .
185       @return .
186   */
187   public function save_object()
188   {
189     if(!$this->is_account) return;
190     $propose_device = "";
191     if(isset($_POST['s_Device']) && empty($_POST['s_Device']) && 
192         isset($_POST['s_Type']) && $_POST['s_Type'] != $this->s_Type){
193       $propose_device = $this->a_Devices[$_POST['s_Type']].$this->DevID;
194     }
196     plugin::save_object();
198     if(isset($_POST['gotoLpdEnable_entry_posted']) && isset($_POST['s_WriteOnly'])){
199       $this->s_WriteOnly = "Y";
200     }else{
201       $this->s_WriteOnly = "N";
202     }
204     if(!empty($propose_device)){
205       $this->s_Device = $propose_device;
206     }
208     foreach($this->attributes as $attr){
209       $this->data[$this->DevID][$attr] = $this->$attr;
210     } 
212     /* Device ID has changed */
213     if(isset($_POST['DevID']) && $_POST['DevID'] != $this->DevID){
214       $this->DevID = $_POST['DevID'];
215       foreach($this->attributes as $attr){
216         $this->$attr = $this->data[$this->DevID][$attr];
217       }
218     }
220     if(isset($_POST['del_printer'])){
221       unset($this->data[$this->DevID]);
222       $this->data = array_values($this->data);
223       if(count($this->data) == 0){
224         $this->is_account = FALSE;
225       }
226       $this->DevID = key($this->data);
227     }
228     if(isset($_POST['add_printer'])){
229       if(count($this->data) < 3){
230         $entry = $this->default_entry;
231         $entry['i_Port'] = $entry['i_Port'] + count($this->data);
232         $entry['s_Device'] = $entry['s_Device'].count($this->data);
233         $this->DevID = count($this->data);
234         $this->data[] = $entry; 
235         foreach($this->attributes as $attr){
236           $this->$attr = $this->data[$this->DevID][$attr];
237         }
238       }
239     }
240   }
243   /*! \brief  Creates an array containing all gotoLpdEnabled values,
244                see constructor for more details.
245       @param  .
246       @return Array of gotoLpdEnabled Strings.
247   */
248   public function save()
249   {
250     $ret = array();
251     $attrs = array("s_Type","s_Device","i_Port","s_Speed","s_FlowControl","s_Parity","i_Bit","s_WriteOnly","s_Options"); 
252     foreach($this->data as $entry){
253       $str = "";
254       foreach($attrs as $attr){
255         $str .= $entry[$attr].":";
256       }
257       $ret[] = preg_replace("/:$/","",$str);
258     }
259     return($ret);
260   }
263   /*! \brief  .  
264       @param  .
265       @return .
266   */
267   public function acl_is_writeable($attr,$skip_write = FALSE)
268   { 
269     return(TRUE);
270   }
273   /*! \brief  .  
274       @param  .
275       @return .
276   */
277   public function acl_is_removeable($skip_write = FALSE)
278   { 
279     return(TRUE);
280   }
283   /*! \brief  .  
284       @param  .
285       @return .
286   */
287   public function acl_is_createable($skip_write = FALSE)
288   { 
289     return(TRUE);
290   }
293 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
294 ?>