Code

Updated gotoLpdEnable class
[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) = split("\:",$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     $display = "";
121     /* Set smarty variables 
122      */
123                 $smarty = get_smarty();
124     $smarty->assign("acl",$this->parent->getacl("gotoLpdEnable"));
125     foreach($this->attributes as $attr){
126       $smarty->assign($attr,$this->$attr);
127     }
128     foreach(array("DevID","a_Types","a_FlowControl","a_Parities","a_Speeds","a_Bits") as $attr){
129       $smarty->assign($attr,$this->$attr);
130     }
131     ksort($this->data);
132     $smarty->assign("data_cnt",count($this->data));
133     $smarty->assign("data",$this->data);
134     $smarty->assign("a_DevIDs",array_keys($this->data));
135     $smarty->assign("is_account",$this->is_account);
136                 return($display.$smarty->fetch(get_template_path("gotoLpdEnable.tpl",TRUE,dirname(__FILE__))));
137         }
140   /*! \brief  Checks the given informations and returns an array 
141                with the error messages or an empty array if everything went fine.
142       @param  .
143       @return Array of Strings  Error messages.
144   */
145   public function check()
146   {
147     $messages = plugin::check();
148     if(!$this->is_account){
149       return($messages);
150     }
151     foreach($this->data as $id => $entry){
152       if(!tests::is_id($entry['i_Port'])){
153         $messages[] = msgPool::invalid(_("Port"));
154       }
155       if(preg_match("/:/",$entry['s_Options'])){
156         $messages[] = msgPool::invalid(_("Options"));
157       }
158     }
159     return($messages);
160   }  
163   /*! \brief  Save all ui inputs. 
164       @param  .
165       @return .
166   */
167   public function save_object()
168   {
169     if(!$this->parent->acl_is_writeable("gotoLpdEnable")){
170       return;
171     }
173     if(!isset($_POST['gotoLpdEnable_entry_posted'])){
174       return;
175     }
177     /* Handle account add/remove 
178      */
179     if(isset($_POST['gotoLpdEnable_enabled'])){
180       $this->is_account = TRUE;
181     }else{
182       $this->is_account = FALSE;
183     }
185     /* Check if we have to propose device settings 
186         (current device info is empty and the printer type has changed)
187      */
188     $propose_device = "";
189     if(isset($_POST['s_Device']) && empty($_POST['s_Device']) && 
190         isset($_POST['s_Type']) && $_POST['s_Type'] != $this->s_Type){
191       $propose_device = $this->a_Devices[$_POST['s_Type']].$this->DevID;
192     }
194     plugin::save_object();
196     if(!empty($propose_device)){
197       $this->s_Device = $propose_device;
198     }
200     /* Get checkbox changes 
201       */
202     if(isset($_POST['s_WriteOnly'])){
203       $this->s_WriteOnly = "Y";
204     }else{
205       $this->s_WriteOnly = "N";
206     }
208     /* Write back attributes to data array ($this->data)
209      */
210     foreach($this->attributes as $attr){
211       $this->data[$this->DevID][$attr] = $this->$attr;
212     } 
214     /* Device ID has changed another printer was selected * /
215     if(isset($_POST['DevID']) && $_POST['DevID'] != $this->DevID){
216       $this->DevID = $_POST['DevID'];
217       foreach($this->attributes as $attr){
218         $this->$attr = $this->data[$this->DevID][$attr];
219       }
220     } 
221     */
223     /* Add and remove additional printer settings 
224     if(isset($_POST['del_printer'])){
225       unset($this->data[$this->DevID]);
226       $this->data = array_values($this->data);
227       if(count($this->data) == 0){
228         $this->is_account = FALSE;
229       }
230       $this->DevID = key($this->data);
231     }
232     if(isset($_POST['add_printer'])){
233       if(count($this->data) < 3){
234         $entry = $this->default_entry;
235         $entry['i_Port'] = $entry['i_Port'] + count($this->data);
236         $entry['s_Device'] = $entry['s_Device'].count($this->data);
237         $this->DevID = count($this->data);
238         $this->data[] = $entry; 
239         foreach($this->attributes as $attr){
240           $this->$attr = $this->data[$this->DevID][$attr];
241         }
242       }
243     }
244     */
245   }
248   /*! \brief  Creates an array containing all gotoLpdEnabled values,
249                see constructor for more details.
250       @param  .
251       @return Array of gotoLpdEnabled Strings.
252   */
253   public function save()
254   {
255     $ret = array();
256     $serial_only = array("s_Speed","s_FlowControl","s_Parity","i_Bit");
257     $attrs = array("s_Type","s_Device","i_Port","s_Speed","s_FlowControl","s_Parity","i_Bit","s_WriteOnly","s_Options"); 
258     foreach($this->data as $entry){
259       $str = "";
260       foreach($attrs as $attr){
261         if(in_array($attr,$serial_only) && $entry['s_Type'] != "Serial"){
262           $str .= ":";
263         }else{
264           $str .= $entry[$attr].":";
265         }
266       }
267       $ret[] = preg_replace("/:$/","",$str);
268     }
269     return($ret);
270   }
273   /*! \brief  .  
274       @param  .
275       @return .
276   */
277   public function acl_is_writeable($attr,$skip_write = FALSE)
278   { 
279     return(TRUE);
280   }
283   /*! \brief  .  
284       @param  .
285       @return .
286   */
287   public function acl_is_removeable($skip_write = FALSE)
288   { 
289     return(TRUE);
290   }
293   /*! \brief  .  
294       @param  .
295       @return .
296   */
297   public function acl_is_createable($skip_write = FALSE)
298   { 
299     return(TRUE);
300   }
303 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
304 ?>