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 save_object()
145 {
146 if(!$this->is_account) return;
147 $propose_device = "";
148 if(isset($_POST['s_Device']) && empty($_POST['s_Device']) &&
149 isset($_POST['s_Type']) && $_POST['s_Type'] != $this->s_Type){
150 $propose_device = $this->a_Devices[$_POST['s_Type']].$this->DevID;
151 }
153 plugin::save_object();
155 if(isset($_POST['gotoLpdEnable_entry_posted']) && isset($_POST['s_WriteOnly'])){
156 $this->s_WriteOnly = "Y";
157 }else{
158 $this->s_WriteOnly = "N";
159 }
161 if(!empty($propose_device)){
162 $this->s_Device = $propose_device;
163 }
165 foreach($this->attributes as $attr){
166 $this->data[$this->DevID][$attr] = $this->$attr;
167 }
169 /* Device ID has changed */
170 if(isset($_POST['DevID']) && $_POST['DevID'] != $this->DevID){
171 $this->DevID = $_POST['DevID'];
172 foreach($this->attributes as $attr){
173 $this->$attr = $this->data[$this->DevID][$attr];
174 }
175 }
177 if(isset($_POST['del_printer'])){
178 unset($this->data[$this->DevID]);
179 $this->data = array_values($this->data);
180 if(count($this->data) == 0){
181 $this->is_account = FALSE;
182 }
183 $this->DevID = key($this->data);
184 }
185 if(isset($_POST['add_printer'])){
186 if(count($this->data) < 3){
187 $entry = $this->default_entry;
188 $entry['i_Port'] = $entry['i_Port'] + count($this->data);
189 $entry['s_Device'] = $entry['s_Device'].count($this->data);
190 $this->DevID = count($this->data);
191 $this->data[] = $entry;
192 foreach($this->attributes as $attr){
193 $this->$attr = $this->data[$this->DevID][$attr];
194 }
195 }
196 }
197 }
199 public function save()
200 {
201 $ret = array();
202 $attrs = array("s_Type","s_Device","i_Port","s_Speed","s_FlowControl","s_Parity","i_Bit","s_WriteOnly","s_Options");
203 foreach($this->data as $entry){
204 $str = "";
205 foreach($attrs as $attr){
206 $str .= $entry[$attr].":";
207 }
208 $ret[] = preg_replace("/:$/","",$str);
209 }
210 return($ret);
211 }
214 /* Allow every action, check are made in class service not here.
215 */
216 public function acl_is_writeable($attr,$skip_write = FALSE)
217 {
218 return(TRUE);
219 }
220 public function acl_is_removeable($skip_write = FALSE)
221 {
222 return(TRUE);
223 }
224 public function acl_is_createable($skip_write = FALSE)
225 {
226 return(TRUE);
227 }
228 }
230 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
231 ?>