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_Boolean = array();
25 private $a_FlowControls = array(); //
26 private $a_Parities = array(); //
27 private $a_Speeds = array(); //
28 private $a_Bits = array(); //
29 private $a_Devices = array();
31 public $attributes = array("s_Type","s_Device","i_Port","s_Options","s_WriteOnly",
32 "s_Speed","s_FlowControl","s_Parity","i_Bit");
34 public function __construct($config,$data,&$parent)
35 {
36 plugin::plugin($config,NULL);
37 $this->parent = $parent;
38 $this->DevID = 0;
40 /****
41 Predefine values
42 ****/
43 $this->a_Types = array(
44 "USB" => _("USB"),
45 "Parallel" => _("Parallel port"),
46 "Serial" => _("Serial"));
47 $this->a_Boolean = array(
48 "Y" => _("Yes"),
49 "N" => _("No"));
50 $this->a_FlowControl = array(
51 "S" => _("Software"),
52 "H" => _("Hardware"));
53 $this->a_Parities = array(
54 "E" => _("Even"),
55 "O" => _("Odd"),
56 "N" => _("None"));
57 $this->a_Speeds = array(
58 "4800" => "4.800 "._("bit/s"),
59 "9600" => "9.600 "._("bit/s"),
60 "19200" => "19.200 "._("bit/s"),
61 "38400" => "38.400 "._("bit/s"),
62 "57600" => "57.600 "._("bit/s"),
63 "115200"=> "115.200 "._("bit/s"),
64 "230400"=> "230.400 "._("bit/s"),
65 "460800"=> "460.800 "._("bit/s"));
66 $this->a_Bits = array(
67 5,6,7,8);
68 $this->a_Devices = array(
69 "USB" => "/dev/usb/lp",
70 "Parallel" => "/dev/lp",
71 "Serial" => "/dev/ttyS");
73 /* Create default entry
74 */
75 foreach($this->attributes as $attr){
76 $this->default_entry[$attr] = $this->$attr;
77 }
79 /* Load current settings
80 */
81 if(count($data) == 0){
82 $this->is_account = FALSE;
83 }else{
84 $this->is_account = TRUE;
85 foreach($data as $dat){
86 list($s_Type,$s_Device,$i_Port,$s_Speed,$s_FlowControl,$s_Parity,$i_Bit,$s_WriteOnly,$s_Options) = split("\:",$dat);
87 $entry = array();
88 foreach($this->attributes as $attr){
89 $entry[$attr] = $$attr;
90 }
91 $this->data[] = $entry;
92 }
94 /* Set first entry values
95 */
96 foreach($this->attributes as $attr){
97 $this->$attr = $this->data[$this->DevID][$attr];
98 }
99 }
100 $this->initially_was_account = $this->is_account;
101 }
103 public function execute()
104 {
105 $display = "";
106 if(isset($_POST['modify_state'])){
107 $this->is_account = !$this->is_account;
108 if(!$this->is_account){
109 $this->data = array();
110 }else{
111 $entry = $this->default_entry;
112 $entry['i_Port'] = $entry['i_Port'] + count($this->data);
113 $entry['s_Device'] = $entry['s_Device'].count($this->data);
114 $this->data = array(0 => $entry);
115 $this->DevID = 0;
116 foreach($this->attributes as $attr){
117 $this->$attr = $this->data[$this->DevID][$attr];
118 }
119 }
120 }
122 if($this->is_account){
123 $display .= $this->show_disable_header(msgPool::removeFeaturesButton(_("Printer")),"",FALSE);
124 }else{
125 $display .= $this->show_enable_header(msgPool::addFeaturesButton(_("Printer")),"",FALSE);
126 }
127 $smarty = get_smarty();
128 foreach($this->attributes as $attr){
129 $smarty->assign($attr,$this->$attr);
130 }
131 foreach(array("DevID","a_Types","a_Boolean","a_FlowControl","a_Parities","a_Speeds","a_Bits") as $attr){
132 $smarty->assign($attr,$this->$attr);
133 }
134 ksort($this->data);
135 $smarty->assign("data_cnt",count($this->data));
136 $smarty->assign("data",$this->data);
137 $smarty->assign("a_DevIDs",array_keys($this->data));
138 $smarty->assign("s_WriteOnly", $this->s_WriteOnly == "Y");
139 $smarty->assign("is_account",$this->is_account);
140 return($display.$smarty->fetch(get_template_path("gotoLpdEnable.tpl",TRUE,dirname(__FILE__))));
141 }
144 public function check()
145 {
146 $messages = plugin::check();
147 if(!$this->is_account){
148 return($messages);
149 }
150 foreach($this->data as $id => $entry){
151 if(!tests::is_id($entry['i_Port'])){
152 $messages[] = msgPool::invalid(_("Port"));
153 }
154 if(preg_match("/:/",$entry['s_Options'])){
155 $messages[] = msgPool::invalid(_("Options"));
156 }
157 }
158 return($messages);
159 }
162 public function save_object()
163 {
164 if(!$this->is_account) return;
165 $propose_device = "";
166 if(isset($_POST['s_Device']) && empty($_POST['s_Device']) &&
167 isset($_POST['s_Type']) && $_POST['s_Type'] != $this->s_Type){
168 $propose_device = $this->a_Devices[$_POST['s_Type']].$this->DevID;
169 }
171 plugin::save_object();
173 if(isset($_POST['gotoLpdEnable_entry_posted']) && isset($_POST['s_WriteOnly'])){
174 $this->s_WriteOnly = "Y";
175 }else{
176 $this->s_WriteOnly = "N";
177 }
179 if(!empty($propose_device)){
180 $this->s_Device = $propose_device;
181 }
183 foreach($this->attributes as $attr){
184 $this->data[$this->DevID][$attr] = $this->$attr;
185 }
187 /* Device ID has changed */
188 if(isset($_POST['DevID']) && $_POST['DevID'] != $this->DevID){
189 $this->DevID = $_POST['DevID'];
190 foreach($this->attributes as $attr){
191 $this->$attr = $this->data[$this->DevID][$attr];
192 }
193 }
195 if(isset($_POST['del_printer'])){
196 unset($this->data[$this->DevID]);
197 $this->data = array_values($this->data);
198 if(count($this->data) == 0){
199 $this->is_account = FALSE;
200 }
201 $this->DevID = key($this->data);
202 }
203 if(isset($_POST['add_printer'])){
204 if(count($this->data) < 3){
205 $entry = $this->default_entry;
206 $entry['i_Port'] = $entry['i_Port'] + count($this->data);
207 $entry['s_Device'] = $entry['s_Device'].count($this->data);
208 $this->DevID = count($this->data);
209 $this->data[] = $entry;
210 foreach($this->attributes as $attr){
211 $this->$attr = $this->data[$this->DevID][$attr];
212 }
213 }
214 }
215 }
217 public function save()
218 {
219 $ret = array();
220 $attrs = array("s_Type","s_Device","i_Port","s_Speed","s_FlowControl","s_Parity","i_Bit","s_WriteOnly","s_Options");
221 foreach($this->data as $entry){
222 $str = "";
223 foreach($attrs as $attr){
224 $str .= $entry[$attr].":";
225 }
226 $ret[] = preg_replace("/:$/","",$str);
227 }
228 return($ret);
229 }
232 /* Allow every action, check are made in class service not here.
233 */
234 public function acl_is_writeable($attr,$skip_write = FALSE)
235 {
236 return(TRUE);
237 }
238 public function acl_is_removeable($skip_write = FALSE)
239 {
240 return(TRUE);
241 }
242 public function acl_is_createable($skip_write = FALSE)
243 {
244 return(TRUE);
245 }
246 }
248 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
249 ?>