Code

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