Code

Updated terminal template
[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 = "U";
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();
29   public    $attributes = array("s_Type","s_Device","i_Port","s_Options","s_WriteOnly",
30       "s_Speed","s_FlowControl","s_Parity","i_Bit");
32   /*! \brief  Create gotoLpdEnable dialog, allows to edit printer settings.
33       @param  Object  $config  The GOsa configuration object.
34       @param  Array   $data    The values for attribute 'gotoLpdEnable'
35                       e.g.     array("U:/dev/usb/lp0:9100:9600:S:E:0:Y:options",
36                                "U:/dev/usb/lp1:9101:9600:S:E:5:Y:", ...)
37       @param  Object  $parent  A reference to the parent plugin-object. 
38       @return Object  gotoLpdEnable (plugin) 
39   */
40         public function __construct($config,$data,&$parent)
41         {
42                 plugin::plugin($config,NULL);
43                 $this->parent = $parent;
44     $this->DevID = 0;
46     /* Predefine selectable values 
47      */ 
48     $this->a_Types = array(
49         "U" => _("USB"),
50         "P" => _("Parallel port"),
51         "S" => _("Serial"));
52     $this->a_FlowControl = array(
53         "S" => _("Software"),
54         "H" => _("Hardware"));
55     $this->a_Parities = array(
56         "E"  => _("Even"),
57         "O"  => _("Odd"),
58         "N"  => _("None"));
59     $this->a_Speeds = array(
60         "4800"  => "4.800&nbsp;"._("bit/s"),
61         "9600"  => "9.600&nbsp;"._("bit/s"),
62         "19200" => "19.200&nbsp;"._("bit/s"),
63         "38400" => "38.400&nbsp;"._("bit/s"),
64         "57600" => "57.600&nbsp;"._("bit/s"),
65         "115200"=> "115.200&nbsp;"._("bit/s"),
66         "230400"=> "230.400&nbsp;"._("bit/s"),
67         "460800"=> "460.800&nbsp;"._("bit/s"));
68     $this->a_Bits = array(
69         5,6,7,8);
70     $this->a_Devices = array(
71         "U" => "/dev/usb/lp",
72         "P" => "/dev/lp",
73         "S" => "/dev/ttyS");
75     /* Create default entry 
76      */
77     foreach($this->attributes as $attr){
78       $this->default_entry[$attr] = $this->$attr;
79     }
81     /* Load current settings 
82      */
83     if(count($data) == 0){
84       $this->is_account = FALSE;
85     }else{
86       foreach($data as $dat){
87         if(substr_count($dat,":") < 8) continue;
88         list($s_Type,$s_Device,$i_Port,$s_Speed,$s_FlowControl,$s_Parity,$i_Bit,$s_WriteOnly,$s_Options) = explode(":",$dat);
89         $entry = array();
90         foreach($this->attributes as $attr){
91           $entry[$attr] = $$attr;
92         }
93         $this->data[] = $entry;
94       }
96       /* Set first entry values 
97           From "$this->data[0]" to "$this->"
98        */
99       if(count($this->data)){
100         foreach($this->attributes as $attr){
101           $this->$attr = $this->data[$this->DevID][$attr];
102         }
103         $this->is_account = TRUE;
104       }      
105     }
106     $this->initially_was_account = $this->is_account;
107   }
110   /*! \brief  Create HTML output of this plugin.
111               Depending on the current plugin status, there is a 
112                 'create' and a 'remove'account button displayed on top
113                followed by the gotoLpdEnable options.
114       @param  .
115       @return String  HTML content
116   */
117         public function execute()
118         {
119     plugin::execute();
121     $display = "";
123     /* Set smarty variables 
124      */
125                 $smarty = get_smarty();
126     $smarty->assign("acl",$this->parent->getacl("gotoLpdEnable"));
127     foreach($this->attributes as $attr){
128       $smarty->assign($attr,$this->$attr);
129     }
130     foreach(array("DevID","a_Types","a_FlowControl","a_Parities","a_Speeds","a_Bits") as $attr){
131       $smarty->assign($attr,$this->$attr);
132     }
133     ksort($this->data);
134     $smarty->assign("data_cnt",count($this->data));
135     $smarty->assign("data",$this->data);
136     $smarty->assign("a_DevIDs",array_keys($this->data));
137     $smarty->assign("is_account",$this->is_account);
138                 return($display.$smarty->fetch(get_template_path("gotoLpdEnable.tpl",TRUE,dirname(__FILE__))));
139         }
142   /*! \brief  Checks the given informations and returns an array 
143                with the error messages or an empty array if everything went fine.
144       @param  .
145       @return Array of Strings  Error messages.
146   */
147   public function check()
148   {
149     $messages = plugin::check();
150     if(!$this->is_account){
151       return($messages);
152     }
153     foreach($this->data as $id => $entry){
154       if(!tests::is_id($entry['i_Port'])){
155         $messages[] = msgPool::invalid(_("Port"));
156       }
157       if(preg_match("/:/",$entry['s_Options'])){
158         $messages[] = msgPool::invalid(_("Options"));
159       }
160     }
161     return($messages);
162   }  
165   /*! \brief  Save all ui inputs. 
166       @param  .
167       @return .
168   */
169   public function save_object()
170   {
171     if(!$this->parent->acl_is_writeable("gotoLpdEnable")){
172       return;
173     }
175     if(!isset($_POST['gotoLpdEnable_entry_posted'])){
176       return;
177     }
179     /* Handle account add/remove 
180      */
181     if(isset($_POST['gotoLpdEnable_enabled'])){
182       $this->is_account = TRUE;
183     }else{
184       $this->is_account = FALSE;
185     }
187     /* Check if we have to propose device settings 
188         (current device info is empty and the printer type has changed)
189      */
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(!empty($propose_device)){
199       $this->s_Device = $propose_device;
200     }
202     /* Get checkbox changes 
203       */
204     if(isset($_POST['s_WriteOnly'])){
205       $this->s_WriteOnly = "Y";
206     }else{
207       $this->s_WriteOnly = "N";
208     }
210     /* Write back attributes to data array ($this->data)
211      */
212     foreach($this->attributes as $attr){
213       $this->data[$this->DevID][$attr] = $this->$attr;
214     } 
216     /* Device ID has changed another printer was selected * /
217     if(isset($_POST['DevID']) && $_POST['DevID'] != $this->DevID){
218       $this->DevID = $_POST['DevID'];
219       foreach($this->attributes as $attr){
220         $this->$attr = $this->data[$this->DevID][$attr];
221       }
222     } 
223     */
225     /* Add and remove additional printer settings 
226     if(isset($_POST['del_printer'])){
227       unset($this->data[$this->DevID]);
228       $this->data = array_values($this->data);
229       if(count($this->data) == 0){
230         $this->is_account = FALSE;
231       }
232       $this->DevID = key($this->data);
233     }
234     if(isset($_POST['add_printer'])){
235       if(count($this->data) < 3){
236         $entry = $this->default_entry;
237         $entry['i_Port'] = $entry['i_Port'] + count($this->data);
238         $entry['s_Device'] = $entry['s_Device'].count($this->data);
239         $this->DevID = count($this->data);
240         $this->data[] = $entry; 
241         foreach($this->attributes as $attr){
242           $this->$attr = $this->data[$this->DevID][$attr];
243         }
244       }
245     }
246     */
247   }
250   /*! \brief  Creates an array containing all gotoLpdEnabled values,
251                see constructor for more details.
252       @param  .
253       @return Array of gotoLpdEnabled Strings.
254   */
255   public function save()
256   {
257     $ret = array();
258     $serial_only = array("s_Speed","s_FlowControl","s_Parity","i_Bit");
259     $attrs = array("s_Type","s_Device","i_Port","s_Speed","s_FlowControl","s_Parity","i_Bit","s_WriteOnly","s_Options"); 
260     foreach($this->data as $entry){
261       $str = "";
262       foreach($attrs as $attr){
263         if(in_array($attr,$serial_only) && $entry['s_Type'] != "Serial"){
264           $str .= ":";
265         }else{
266           $str .= $entry[$attr].":";
267         }
268       }
269       $ret[] = preg_replace("/:$/","",$str);
270     }
271     return($ret);
272   }
275   /*! \brief  .  
276       @param  .
277       @return .
278   */
279   public function acl_is_writeable($attr,$skip_write = FALSE)
280   { 
281     return(TRUE);
282   }
285   /*! \brief  .  
286       @param  .
287       @return .
288   */
289   public function acl_is_removeable($skip_write = FALSE)
290   { 
291     return(TRUE);
292   }
295   /*! \brief  .  
296       @param  .
297       @return .
298   */
299   public function acl_is_createable($skip_write = FALSE)
300   { 
301     return(TRUE);
302   }
305 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
306 ?>