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();
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("U:/dev/usb/lp0:9100:9600:S:E:0:Y:options",
37 "U:/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 "U" => _("USB"),
51 "P" => _("Parallel port"),
52 "S" => _("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 "._("bit/s"),
62 "9600" => "9.600 "._("bit/s"),
63 "19200" => "19.200 "._("bit/s"),
64 "38400" => "38.400 "._("bit/s"),
65 "57600" => "57.600 "._("bit/s"),
66 "115200"=> "115.200 "._("bit/s"),
67 "230400"=> "230.400 "._("bit/s"),
68 "460800"=> "460.800 "._("bit/s"));
69 $this->a_Bits = array(
70 5,6,7,8);
71 $this->a_Devices = array(
72 "U" => "/dev/usb/lp",
73 "P" => "/dev/lp",
74 "S" => "/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){
88 if(substr_count($dat,":") < 8) continue;
89 list($s_Type,$s_Device,$i_Port,$s_Speed,$s_FlowControl,$s_Parity,$i_Bit,$s_WriteOnly,$s_Options) = split("\:",$dat);
90 $entry = array();
91 foreach($this->attributes as $attr){
92 $entry[$attr] = $$attr;
93 }
94 $this->data[] = $entry;
95 }
97 /* Set first entry values
98 From "$this->data[0]" to "$this->"
99 */
100 if(count($this->data)){
101 foreach($this->attributes as $attr){
102 $this->$attr = $this->data[$this->DevID][$attr];
103 }
104 $this->is_account = TRUE;
105 }
106 }
107 $this->initially_was_account = $this->is_account;
108 }
111 /*! \brief Create HTML output of this plugin.
112 Depending on the current plugin status, there is a
113 'create' and a 'remove'account button displayed on top
114 followed by the gotoLpdEnable options.
115 @param .
116 @return String HTML content
117 */
118 public function execute()
119 {
120 $display = "";
122 /* Set smarty variables
123 */
124 $smarty = get_smarty();
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 {
170 if(!isset($_POST['gotoLpdEnable_entry_posted'])){
171 return;
172 }
174 /* Handle account add/remove
175 */
176 if(isset($_POST['gotoLpdEnable_enabled'])){
177 $this->is_account = TRUE;
178 }else{
179 $this->is_account = FALSE;
180 }
182 /* Check if we have to propose device settings
183 (current device info is empty and the printer type has changed)
184 */
185 $propose_device = "";
186 if(isset($_POST['s_Device']) && empty($_POST['s_Device']) &&
187 isset($_POST['s_Type']) && $_POST['s_Type'] != $this->s_Type){
188 $propose_device = $this->a_Devices[$_POST['s_Type']].$this->DevID;
189 }
191 plugin::save_object();
193 if(!empty($propose_device)){
194 $this->s_Device = $propose_device;
195 }
197 /* Get checkbox changes
198 */
199 if(isset($_POST['s_WriteOnly'])){
200 $this->s_WriteOnly = "Y";
201 }else{
202 $this->s_WriteOnly = "N";
203 }
205 /* Write back attributes to data array ($this->data)
206 */
207 foreach($this->attributes as $attr){
208 $this->data[$this->DevID][$attr] = $this->$attr;
209 }
211 /* Device ID has changed another printer was selected * /
212 if(isset($_POST['DevID']) && $_POST['DevID'] != $this->DevID){
213 $this->DevID = $_POST['DevID'];
214 foreach($this->attributes as $attr){
215 $this->$attr = $this->data[$this->DevID][$attr];
216 }
217 }
218 */
220 /* Add and remove additional printer settings
221 if(isset($_POST['del_printer'])){
222 unset($this->data[$this->DevID]);
223 $this->data = array_values($this->data);
224 if(count($this->data) == 0){
225 $this->is_account = FALSE;
226 }
227 $this->DevID = key($this->data);
228 }
229 if(isset($_POST['add_printer'])){
230 if(count($this->data) < 3){
231 $entry = $this->default_entry;
232 $entry['i_Port'] = $entry['i_Port'] + count($this->data);
233 $entry['s_Device'] = $entry['s_Device'].count($this->data);
234 $this->DevID = count($this->data);
235 $this->data[] = $entry;
236 foreach($this->attributes as $attr){
237 $this->$attr = $this->data[$this->DevID][$attr];
238 }
239 }
240 }
241 */
242 }
245 /*! \brief Creates an array containing all gotoLpdEnabled values,
246 see constructor for more details.
247 @param .
248 @return Array of gotoLpdEnabled Strings.
249 */
250 public function save()
251 {
252 $ret = array();
253 $serial_only = array("s_Speed","s_FlowControl","s_Parity","i_Bit");
254 $attrs = array("s_Type","s_Device","i_Port","s_Speed","s_FlowControl","s_Parity","i_Bit","s_WriteOnly","s_Options");
255 foreach($this->data as $entry){
256 $str = "";
257 foreach($attrs as $attr){
258 if(in_array($attr,$serial_only) && $entry['s_Type'] != "Serial"){
259 $str .= ":";
260 }else{
261 $str .= $entry[$attr].":";
262 }
263 }
264 $ret[] = preg_replace("/:$/","",$str);
265 }
266 return($ret);
267 }
270 /*! \brief .
271 @param .
272 @return .
273 */
274 public function acl_is_writeable($attr,$skip_write = FALSE)
275 {
276 return(TRUE);
277 }
280 /*! \brief .
281 @param .
282 @return .
283 */
284 public function acl_is_removeable($skip_write = FALSE)
285 {
286 return(TRUE);
287 }
290 /*! \brief .
291 @param .
292 @return .
293 */
294 public function acl_is_createable($skip_write = FALSE)
295 {
296 return(TRUE);
297 }
298 }
300 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
301 ?>