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 /* Save to LDAP */
136 function save()
137 {
138 /* return the selected PPD, and in future the selected options too */
139 $this->ppdManager->saveProperties($this->selectedPPD['link'],$this->ppdConfig);
140 return($this->selectedPPD['link']);
141 }
143 function getPPDInformation()
144 {
145 /* Get Information for a single PPD entry
146 * This will be shown on top of template
147 */
148 $str = "none";
149 if(!empty($this->selectedPPD)){
150 $str = $this->selectedPPD['link'];
151 $str = $this->ppdManager->loadDescription($this->selectedPPD['link']);
152 }
153 return($str) ;
154 }
156 function generateProperties()
157 {
158 /* In future there will be a schema parser that provide us all settings that can be made in the selected PPD file.
159 * This function will generate a userfriendly post based form with this informations
160 */
162 $HeadLine = "<h2>"._("Options")." :</h2>";
164 /* Set Headline */
165 $str = $HeadLine;
167 /* If ppd exists and is readable */
168 if((!empty($this->selectedPPD['link']))&&(file_exists($this->selectedPPD['link']))){
170 /* If there is no initial Configuration, load it */
171 if($this->ppdConfig == false){
172 $this->ppdConfig = $this->ppdManager->loadProperties($this->selectedPPD['link']);
173 }
175 /* Create a table */
176 $str .= "<div style='padding-left:10px;'><table>";
178 /* Input all data to the table */
179 foreach($this->ppdConfig as $cat => $obj){
181 /* Add new category */
182 $str .= "<tr><td colspan='2'><p class='seperator'> </p><br>";
183 $str .= "<b>".$cat."</b><br>";
184 $str .= "</tr></td>";
186 /* Add attributes of the current category */
187 foreach($obj as $attr => $settings){
189 /* Skip all entries beginning with _ */
190 if($attr[0] == "_") continue;
192 /* Prepare data */
193 $values = array();
194 $name = $settings['_name'];
195 $default = $settings['_default'];
196 $type = $settings['_type'];
198 /* Add name to table */
199 $str .= "<tr><td style='padding-left:10px;'>\n";
200 $str .= $name."<br>\n";
201 $str .= "</td><td>\n";
203 /* Get all values */
204 foreach( $settings as $vname => $value){
205 if($vname[0] != "_"){
206 $values[$vname]= $vname;
207 }
208 }
210 /* preparing Html output
211 * Supported types are PickOne/Boolean
212 */
214 /* If type is PickOne, create a select box */
215 if(($type == "PickOne")||(($type=="Boolean")&&(count($values)>1))){
217 $str .= "<select name='".base64_encode($name)."'>\n";
218 foreach($values as $value){
219 $selected = "";
220 if($value == $default){
221 $selected = " selected ";
222 }
223 $str .= "<option value='".$value."' ".$selected.">".$value."</option>\n";
224 }
225 $str .= "</select>\n";
227 }elseif($type == "Boolean"){
229 /* If type is Boolean & no values are given */
230 $str .= "<select name='".base64_encode($name)."'>\n";
231 if($default == "False"){
232 $str .= "<option value='True' >"._("True")."</option>\n";
233 $str .= "<option value='False' selected>"._("False")."</option>\n";
234 }else{
235 $str .= "<option value='True' selected>"._("True")."</option>\n";
236 $str .= "<option value='False' >"._("False")."</option>\n";
237 }
238 $str .= "</select>\n";
240 }else{
241 print_red(sprintf(_("Unsupported PPD type '%s' used for '%s' "),$type,$name));
242 }
243 $str .= "</td></tr>\n";
244 }
245 }
246 $str .= "</table></div>\n";
248 }
249 return($str);
250 }
251 }
252 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
253 ?>