Code

Fixed ppd selection
[gosa.git] / plugins / admin / systems / class_printerPPDDialog.inc
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   var $pathToPPD            = "";
26   function printerPPDDialog ($config, $dn= NULL,$ppdfile=NULL )
27   {
28     plugin::plugin ($config, $dn);
29     $this->depselect = $this->config->current['BASE'];
31     if(isset($_SESSION['config']->data['MAIN']['PPD_PATH'])){
32       $this->pathToPPD = $_SESSION['config']->data['MAIN']['PPD_PATH'];
33       $this->pathToPPD= preg_replace("/\/\//", "/", $this->pathToPPD);
34     }else{
35       $this->pathToPPD = "";
36     }
38     if(!((!is_dir($this->pathToPPD))||(empty($this->pathToPPD)))){ 
40       /* Load all available PPD files and sort them into an array 
41        */
42       require_once ("class_ppdManager.inc");
43       $this->ppdManager= new ppdManager($this->pathToPPD);
44       $tmp = $this->ppdManager->getPrinterList();
46       /* Sort all available files, and create header (Vendor index) */
47       foreach($tmp as $file=>$ppd){
48         $tmp2 = split("\n",$ppd);
49         if(!isset($this->ppdListHeader[$tmp2[0]])){
50           $this->ppdListHeader[$tmp2[0]]=0;
51         }
52         $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
53         $tmp3['link']   =preg_replace("/".str_replace("/","\/",$this->pathToPPD)."/i","",$file);
54         $tmp3['ppd']    =$ppd;
55         $this->ppdListHeader[$tmp2[0]]++;
56         $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
57       }
59       /* The user has already a valid PPD assigned
60        * Get some informations about this PPD
61        */
62       if(($ppdfile!= NULL)&&(strlen($ppdfile)>0)){
63         if(!file_exists($this->pathToPPD.$ppdfile)){
64           print_red(sprintf(_("Can't open '%s', ppd settings resetted."),$ppdfile));
65         }else{
66           $tmp2= split("\n", $this->ppdManager->loadDescription($this->pathToPPD.$ppdfile));
67           $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]);
68           $tmp3['link']   =$ppdfile;
69           $tmp3['ppd']    =$this->ppdManager->loadDescription($this->pathToPPD.$ppdfile);
70           $this->selectedPPD = $tmp3;
71         }
72       }
73     }
74   }
76   function getPrinterReload()
77   {
78     $tmp = $this->ppdManager->getPrinterList(true);
80     /* Sort all available files, and create header (Vendor index) */
81     foreach($tmp as $file=>$ppd){
82       $tmp2 = split("\n",$ppd);
83       if(!isset($this->ppdListHeader[$tmp2[0]])){
84         $this->ppdListHeader[$tmp2[0]]=0;
85       }
86       $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
87       $tmp3['link']   =$file;
88       $tmp3['ppd']    =$ppd;
89       $this->ppdListHeader[$tmp2[0]]++;
90       $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
91     }
93   }
95   function execute()
96   {
97     /* Call parent execute */
98     plugin::execute();
100     /* Fill templating stuff */
101     $smarty= get_smarty();
102     $display= "";
104     if((!is_dir($this->pathToPPD))||(empty($this->pathToPPD))){
105       print_red(sprintf(_("The specified PPD_PATH='%s' in your gosa.conf is invalid, can't read any ppd informations."),$this->pathToPPD));
106       $smarty->assign("ppdString", _("Can't get ppd informations."));
107       $smarty->assign("showOptions", "");
108     }else{
111       /* Is there a new PPD file uploaded ? */
112       if((isset($_FILES['NewPPDFile']))&&(isset($_POST['SubmitNewPPDFile']))){
113         $file = ($_FILES['NewPPDFile']);
114         if($file['size'] != 0 ){
115           $res  = @$this->ppdManager->loadDescription($file['tmp_name']);
116           $name = (trim(preg_replace("/-/","",$res)));
117           if(!empty($name)){
118             $vendor = trim(preg_replace("/-.*$/i","",$res));
119             $model  = trim(preg_replace("/".$vendor."/","",$name));
120             if(!is_dir($this->pathToPPD.$vendor)){
121               if(!(@mkdir($this->pathToPPD.$vendor))){
122                 print_red(sprintf(_("Can't create folder '%s' for the uploaded ppd file."),$vendor));
123               }
124             }
126             if(!isset($this->ppdList[$vendor])){
127               $this->ppdList[$vendor] = array();
128             }
130             if(is_dir($this->pathToPPD.$vendor)){  
131               $found = false;
132               foreach($this->ppdList[$vendor] as $key => $val){
133                 if(preg_match("/".$model.".*/i",$key)){
134                   $found = true;
135                   print_red(sprintf(_("There is already a ppd file for this kind of printer.")));
136                 }
137               }// Foreach
138               if(!$found){
139                 $ppdname = $vendor."/".$vendor."-".preg_replace("/^[^ ]/","",str_replace("-","",$model)).".ppd";
140                 $filename = $this->pathToPPD.$vendor."/".$vendor."-".preg_replace("/^[^ ]/","",str_replace("-","",$model)).".ppd";
141                 $fp = @fopen($filename,"w+");
142                 if(!$fp){
143                   print_red(sprintf(_("Can't save file '%s'."),$filename));
144                 }else{
145                   $str = file_get_contents($file['tmp_name']);
146                   fputs($fp,$str);
147                   @fclose($fp);
149                   $tmp2= split("\n", $res);
150                   $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]);
151                   $tmp3['link']   =$ppdname;
152                   $tmp3['ppd']    =$res;
154                   $this->selectedPPD = $tmp3;
155                   $this->getPrinterReload(); 
156                 }
157               }
158             }// If dir
159           }else{
160             print_red(_("Please specify a valid ppd file."));
161           }
162         }else{
163           print_red(_("Please specify a valid ppd file."));
164         }
166       }
169       /* Open a dialog that allow us to select different PPDs
170        */
171       if(isset($_POST['SelectPPD'])){
172         $this->dialog= new printerPPDSelectionDialog($this->config,$this->dn,$this->ppdList,$this->ppdListHeader,$this->selectedPPD);
173       }
175       /* The selection dialog fpr PPDs is canceled
176        */
177       if(isset($_POST['ClosePPDSelection'])){
178         unset($this->dialog);
179         $this->dialog=NULL;
180       }
182       /* A new PPDs was selected in the PPDs selection Dialog
183        * Perform a Check. If everything is fine, use the new PPD.
184        */
185       if(isset($_POST['SavePPDSelection'])){
186         if(!isset($_POST['PPDselection'])){
187           print_red(_("Please select a valid ppd."));
188         }else{
189           $this->selectedPPD['link'] = $_POST['PPDselection'];
190           $this->ppdConfig   = false;
191           unset($this->dialog);
192           $this->dialog=NULL;
193         }
194       }
196       /* Div Selection */ 
197       if((isset($_GET['act']))&&($_GET['act']=="use")){
198         $hit = false;
199         foreach($this->ppdList as $vendor => $ppds ){
200           foreach($ppds as $name => $ppd){
201             if($ppd['link']==$_GET['id']){
202               $hit = true;
203             }
204           }
205         }
206         if($hit == true){
207           $this->selectedPPD['link'] = str_replace($this->pathToPPD,"",$_GET['id']);
208           $this->ppdConfig   = false;
209         }
210         unset($this->dialog);
211         $this->dialog=NULL;
212         
213       }
215       /* if a dialog is open, print the dialog instead of this class
216        */
217       if($this->dialog!=NULL){
218         $display = $this->dialog->execute();
219         return($display);
220       }
222       /* Give smarty the information it needs */
223       $smarty->assign("ppdString" ,$this->getPPDInformation());
224       $tmp= $this->generateProperties();
225       if ($tmp == ""){
226         $smarty->assign("showOptions", 0);
227       } else {
228         $smarty->assign("showOptions", 1);
229         $smarty->assign("properties",$this->generateProperties());
230       }
231     } 
232     /* Print out template */
233     $display.= $smarty->fetch(get_template_path('printerPPDDialog.tpl', TRUE,dirname(__FILE__)));
234     return($display);
235   }
237   function check(){
238     /* Check the given data
239      */
240     $message=array();
241     return $message;
242   }
244   function save_object()
245   {
246     if(isset($_POST['PPDDisSubmitted'])){
247       if(is_array($this->ppdConfig)){
248         foreach($this->ppdConfig as $cat => $obj){
249           foreach($obj as $attr => $attributes){
250             if(isset($_POST[base64_encode($attributes['_name'])])){
251               $this->ppdConfig[$cat][$attr]['_default'] = $_POST[base64_encode($attributes['_name'])];
252             }
253           }
254         }
255       }
256     }
258   }
260   function save_ppd(){
261     if($this->ppdManager){
262       $this->ppdManager->saveProperties($this->pathToPPD.$this->selectedPPD['link'],$this->ppdConfig);
263     }
264   }
266   /* Save to LDAP */
267   function save()
268   {
269     /* return the selected PPD, and in future the selected options too */
270     return($this->selectedPPD['link']);
271   }
273   function getPPDInformation()
274   {
275     /* Get Information for a single PPD entry 
276      * This will be shown on top of template
277      */
278     $str = "none";
279     if(!empty($this->selectedPPD)){
280       $str = $this->ppdManager->loadDescription($this->pathToPPD."/".$this->selectedPPD['link']);
281     }
282     return($str) ; 
283   }
285   function generateProperties()
286   { 
287     /* In future there will be a schema parser that provide us all settings that can be made in the selected PPD file. 
288      * This function will generate a userfriendly post based form with this informations
289      */
291     /* Set Headline */
292     $str = "";
293     $feed= "";
295     $s_ppd = $this->pathToPPD.$this->selectedPPD['link'];
297     /* If ppd exists and is readable */
298     if((!empty($this->selectedPPD['link']))&&(file_exists($s_ppd))){
300       /* If there is no initial Configuration, load it */
301       if($this->ppdConfig == false){
302         $this->ppdConfig = $this->ppdManager->loadProperties($s_ppd);
303       }
305       /* Create a table */
306       $str .= "<div style='padding-left:30px;'><table>";
308       /* Input all data to the table */
309       foreach($this->ppdConfig as $cat => $obj){
311         /* Add new category */
312         $str .= "<tr><td colspan='2'>$feed";
313         if ($feed == ""){
314           $feed= "<br>";
315         }
316         $str .= "<b>"._("Section")." '".$cat."'&nbsp;</b><br>";
317         $str .= "</tr></td>";       
319         /* Add attributes of the current category */
320         foreach($obj as $attr => $settings){
323           /* Skip all entries beginning with _ */
324           if($attr[0] == "_") continue;  
326           /* Prepare data */
327           $values   = array();
328           $name     = $settings['_name'];
329           $default  = $settings['_default'];
330           $type     = $settings['_type'];
332           /* Add name to table */ 
333           $str .= "<tr><td style='padding-left:40px;'>\n";
334           $str .= $name."<br>\n";
335           $str .= "</td><td>\n";
337           /* Get all values */ 
338           foreach( $settings as $vname => $value){
339             if($vname[0] != "_"){
340               $values[$vname]= $value;
341             }
342           }
344           /* preparing Html output
345            * Supported types are PickOne/Boolean
346            */
348           /* If type is PickOne, create a select box */
349           if(($type == "PickOne")||(($type=="Boolean")&&(count($values)>1))){
351             $str  .=  "<select name='".base64_encode($name)."'>\n";
352             foreach($values as $value){
353               $selected = "";
354               if($value == $default){
355                 $selected = " selected ";
356               }
357               $str  .=  "<option value='".$value."' ".$selected.">".$value."</option>\n";
358             }
359             $str  .=  "</select>\n";
361           }elseif($type == "Boolean"){
363             /* If type is Boolean & no values are given */
364             $str  .=  "<select name='".base64_encode($name)."'>\n";
365             if($default == "False"){
366               $str  .=    "<option value='True' >"._("True")."</option>\n";
367               $str  .=    "<option value='False' selected>"._("False")."</option>\n";
368             }else{
369               $str  .=    "<option value='True' selected>"._("True")."</option>\n";
370               $str  .=    "<option value='False' >"._("False")."</option>\n";
371             }          
372             $str  .=  "</select>\n";
374           }else{
375             print_red(sprintf(_("Unsupported ppd type '%s' used for '%s' "),$type,$name));
376           }
377           $str .= "</td></tr>\n";
378         }
379       }
380       $str .= "</table></div>\n";
382     }
383     return($str);
384   }
386 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
387 ?>