b5547fff8d403e7e6deff08c8ed2867bbac5d670
1 <?php
3 class printerPPDDialog extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary = "Manage server basic objects";
7 var $cli_description = "Some longer text\nfor help";
8 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account = TRUE;
12 var $attributes = array();
13 var $objectclasses = array("whatever");
15 var $ppdList = array(); // Contains all Printer models
16 var $ppdListHeader = array(); // Contains all printer vendors
18 var $dialog = NULL;
19 var $selectedPPD = false; // e.g. /var/spool/ppd/vendor/device.ppd
21 var $ppdManager = false; // new ppdManager;
22 var $ppdConfig = false; // $this->ppdManager->loadProperties($this->selectedPPD['link']);
24 function printerPPDDialog ($config, $dn= NULL,$ppdfile=NULL )
25 {
26 plugin::plugin ($config, $dn);
27 $this->depselect = $this->config->current['BASE'];
29 /* Load all available PPD files and sort them into an array
30 */
31 require_once ("class_ppdManager.inc");
32 $this->ppdManager= new ppdManager('/var/spool/ppd/');
33 $tmp = $this->ppdManager->getPrinterList();
35 /* Sort all available files, and create header (Vendor index) */
36 foreach($tmp as $file=>$ppd){
37 $tmp2 = split("\n",$ppd);
38 if(!isset($this->ppdListHeader[$tmp2[0]])){
39 $this->ppdListHeader[$tmp2[0]]=0;
40 }
41 $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
42 $tmp3['link'] =$file;
43 $tmp3['ppd'] =$ppd;
44 $this->ppdListHeader[$tmp2[0]]++;
45 $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
46 }
48 /* The user has already a valid PPD assigned
49 * Get some informations about this PPD
50 */
51 if(($ppdfile!= NULL)&&(strlen($ppdfile)>0)){
52 $tmp2= split("\n", $this->ppdManager->loadDescription($ppdfile));
53 $tmp3['name'] =preg_replace("/^ -/","",$tmp2[1]);
54 $tmp3['link'] =$ppdfile;
55 $tmp3['ppd'] =$this->ppdManager->loadDescription($ppdfile);
56 $this->selectedPPD = $tmp3;
57 }
60 }
62 function execute()
63 {
64 /* Fill templating stuff */
65 $smarty= get_smarty();
66 $display= "";
69 /* Open a dialog that allow us to select different PPDs
70 */
71 if(isset($_POST['SelectPPD'])){
72 $this->dialog= new printerPPDSelectionDialog($this->config,$this->dn,$this->ppdList,$this->ppdListHeader,$this->selectedPPD);
73 }
75 /* The selection dialog fpr PPDs is canceled
76 */
77 if(isset($_POST['ClosePPDSelection'])){
78 unset($this->dialog);
79 $this->dialog=NULL;
80 }
82 /* A new PPDs was selected in the PPDs selection Dialog
83 * Perform a Check. If everything is fine, use the new PPD.
84 */
85 if(isset($_POST['SavePPDSelection'])){
86 if(!isset($_POST['PPDselection'])){
87 print_red(_("Please select a valid PPD."));
88 }else{
89 $this->selectedPPD['link'] = $_POST['PPDselection'];
90 $this->ppdConfig = false;
91 unset($this->dialog);
92 $this->dialog=NULL;
93 }
94 }
96 /* if a dialog is open, print the dialog instead of this class
97 */
98 if($this->dialog!=NULL){
99 $display = $this->dialog->execute();
100 return($display);
101 }
103 /* Give smarty the information it needs */
104 $smarty->assign("ppdString" ,$this->getPPDInformation());
105 $smarty->assign("properties",$this->generateProperties());
107 /* Print out template */
108 $display.= $smarty->fetch(get_template_path('printerPPDDialog.tpl', TRUE,dirname(__FILE__)));
109 return($display);
110 }
112 function check(){
113 /* Check the given data
114 */
115 $message=array();
116 return $message;
117 }
119 function save_object()
120 {
121 if(isset($_POST['PPDDisSubmitted'])){
122 if(is_array($this->ppdConfig)){
123 foreach($this->ppdConfig as $cat => $obj){
124 foreach($obj as $attr => $attributes){
125 if(isset($_POST[base64_encode($attributes['_name'])])){
126 $this->ppdConfig[$cat][$attr]['_default'] = $_POST[base64_encode($attributes['_name'])];
127 }
128 }
129 }
130 }
131 }
133 }
135 function save_ppd(){
136 $this->ppdManager->saveProperties($this->selectedPPD['link'],$this->ppdConfig);
137 }
139 /* Save to LDAP */
140 function save()
141 {
142 /* return the selected PPD, and in future the selected options too */
143 return($this->selectedPPD['link']);
144 }
146 function getPPDInformation()
147 {
148 /* Get Information for a single PPD entry
149 * This will be shown on top of template
150 */
151 $str = "none";
152 if(!empty($this->selectedPPD)){
153 $str = $this->selectedPPD['link'];
154 $str = $this->ppdManager->loadDescription($this->selectedPPD['link']);
155 }
156 return($str) ;
157 }
159 function generateProperties()
160 {
161 /* In future there will be a schema parser that provide us all settings that can be made in the selected PPD file.
162 * This function will generate a userfriendly post based form with this informations
163 */
165 /* Set Headline */
166 $str = "";
168 /* If ppd exists and is readable */
169 if((!empty($this->selectedPPD['link']))&&(file_exists($this->selectedPPD['link']))){
171 /* If there is no initial Configuration, load it */
172 if($this->ppdConfig == false){
173 $this->ppdConfig = $this->ppdManager->loadProperties($this->selectedPPD['link']);
174 }
176 /* Create a table */
177 $str .= "<div style='padding-left:30px;'><table>";
179 /* Input all data to the table */
180 foreach($this->ppdConfig as $cat => $obj){
182 /* Add new category */
183 $str .= "<tr><td colspan='2'><br>";
184 $str .= "<b>".$cat." : </b><br>";
185 $str .= "</tr></td>";
187 /* Add attributes of the current category */
188 foreach($obj as $attr => $settings){
191 /* Skip all entries beginning with _ */
192 if($attr[0] == "_") continue;
194 /* Prepare data */
195 $values = array();
196 $name = $settings['_name'];
197 $default = $settings['_default'];
198 $type = $settings['_type'];
200 /* Add name to table */
201 $str .= "<tr><td style='padding-left:40px;'>\n";
202 $str .= $name."<br>\n";
203 $str .= "</td><td>\n";
205 /* Get all values */
206 foreach( $settings as $vname => $value){
207 if($vname[0] != "_"){
208 $values[$vname]= $value;
209 }
210 }
212 /* preparing Html output
213 * Supported types are PickOne/Boolean
214 */
216 /* If type is PickOne, create a select box */
217 if(($type == "PickOne")||(($type=="Boolean")&&(count($values)>1))){
219 $str .= "<select name='".base64_encode($name)."'>\n";
220 foreach($values as $value){
221 $selected = "";
222 if($value == $default){
223 $selected = " selected ";
224 }
225 $str .= "<option value='".$value."' ".$selected.">".$value."</option>\n";
226 }
227 $str .= "</select>\n";
229 }elseif($type == "Boolean"){
231 /* If type is Boolean & no values are given */
232 $str .= "<select name='".base64_encode($name)."'>\n";
233 if($default == "False"){
234 $str .= "<option value='True' >"._("True")."</option>\n";
235 $str .= "<option value='False' selected>"._("False")."</option>\n";
236 }else{
237 $str .= "<option value='True' selected>"._("True")."</option>\n";
238 $str .= "<option value='False' >"._("False")."</option>\n";
239 }
240 $str .= "</select>\n";
242 }else{
243 print_red(sprintf(_("Unsupported PPD type '%s' used for '%s' "),$type,$name));
244 }
245 $str .= "</td></tr>\n";
246 }
247 }
248 $str .= "</table></div>\n";
250 }
251 return($str);
252 }
253 }
254 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
255 ?>