Code

Backport from trunk
[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, set_post($this->$attr));
129         }
130         foreach(array("DevID","a_Types","a_FlowControl","a_Parities","a_Speeds","a_Bits") as $attr){
131             $smarty->assign($attr,set_post($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[get_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         } 
215     }
218     /*! \brief  Creates an array containing all gotoLpdEnabled values,
219       see constructor for more details.
220       @param  .
221       @return Array of gotoLpdEnabled Strings.
222      */
223     public function save()
224     {
225         $ret = array();
226         $serial_only = array("s_Speed","s_FlowControl","s_Parity","i_Bit");
227         $attrs = array("s_Type","s_Device","i_Port","s_Speed","s_FlowControl","s_Parity","i_Bit","s_WriteOnly","s_Options"); 
228         foreach($this->data as $entry){
229             $str = "";
230             foreach($attrs as $attr){
231                 if(in_array_strict($attr,$serial_only) && $entry['s_Type'] != "Serial"){
232                     $str .= ":";
233                 }else{
234                     $str .= $entry[$attr].":";
235                 }
236             }
237             $ret[] = preg_replace("/:$/","",$str);
238         }
239         return($ret);
240     }
243     /*! \brief  .  
244       @param  .
245       @return .
246      */
247     public function acl_is_writeable($attr,$skip_write = FALSE)
248     { 
249         return(TRUE);
250     }
253     /*! \brief  .  
254       @param  .
255       @return .
256      */
257     public function acl_is_removeable($skip_write = FALSE)
258     { 
259         return(TRUE);
260     }
263     /*! \brief  .  
264       @param  .
265       @return .
266      */
267     public function acl_is_createable($skip_write = FALSE)
268     { 
269         return(TRUE);
270     }
273 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
274 ?>