Code

Added DivSelect to
[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                   print_a($tmp3);
156                   $this->selectedPPD = $tmp3;
157                   $this->getPrinterReload(); 
158                 }
159               }
160             }// If dir
161           }else{
162             print_red(_("Please specify a valid ppd file."));
163           }
164         }else{
165           print_red(_("Please specify a valid ppd file."));
166         }
168       }
171       /* Open a dialog that allow us to select different PPDs
172        */
173       if(isset($_POST['SelectPPD'])){
174         $this->dialog= new printerPPDSelectionDialog($this->config,$this->dn,$this->ppdList,$this->ppdListHeader,$this->selectedPPD);
175       }
177       /* The selection dialog fpr PPDs is canceled
178        */
179       if(isset($_POST['ClosePPDSelection'])){
180         unset($this->dialog);
181         $this->dialog=NULL;
182       }
184       /* A new PPDs was selected in the PPDs selection Dialog
185        * Perform a Check. If everything is fine, use the new PPD.
186        */
187       if(isset($_POST['SavePPDSelection'])){
188         if(!isset($_POST['PPDselection'])){
189           print_red(_("Please select a valid ppd."));
190         }else{
191           $this->selectedPPD['link'] = $_POST['PPDselection'];
192           $this->ppdConfig   = false;
193           unset($this->dialog);
194           $this->dialog=NULL;
195         }
196       }
198       /* Div Selection */ 
199       if((isset($_GET['act']))&&($_GET['act']=="use")){
200         $hit = false;
201         foreach($this->ppdList as $vendor => $ppds ){
202           foreach($ppds as $name => $ppd){
203             if($ppd['link']==$_GET['id']){
204               $hit = true;
205             }
206           }
207         } 
208         $this->selectedPPD['link'] = $_GET['id'];
209         $this->ppdConfig   = false;
210         unset($this->dialog);
211         $this->dialog=NULL;
212       }
214       /* if a dialog is open, print the dialog instead of this class
215        */
216       if($this->dialog!=NULL){
217         $display = $this->dialog->execute();
218         return($display);
219       }
221       /* Give smarty the information it needs */
222       $smarty->assign("ppdString" ,$this->getPPDInformation());
223       $tmp= $this->generateProperties();
224       if ($tmp == ""){
225         $smarty->assign("showOptions", 0);
226       } else {
227         $smarty->assign("showOptions", 1);
228         $smarty->assign("properties",$this->generateProperties());
229       }
230     } 
231     /* Print out template */
232     $display.= $smarty->fetch(get_template_path('printerPPDDialog.tpl', TRUE,dirname(__FILE__)));
233     return($display);
234   }
236   function check(){
237     /* Check the given data
238      */
239     $message=array();
240     return $message;
241   }
243   function save_object()
244   {
245     if(isset($_POST['PPDDisSubmitted'])){
246       if(is_array($this->ppdConfig)){
247         foreach($this->ppdConfig as $cat => $obj){
248           foreach($obj as $attr => $attributes){
249             if(isset($_POST[base64_encode($attributes['_name'])])){
250               $this->ppdConfig[$cat][$attr]['_default'] = $_POST[base64_encode($attributes['_name'])];
251             }
252           }
253         }
254       }
255     }
257   }
259   function save_ppd(){
260     $this->ppdManager->saveProperties($this->pathToPPD.$this->selectedPPD['link'],$this->ppdConfig);
261   }
263   /* Save to LDAP */
264   function save()
265   {
266     /* return the selected PPD, and in future the selected options too */
267     return($this->selectedPPD['link']);
268   }
270   function getPPDInformation()
271   {
272     /* Get Information for a single PPD entry 
273      * This will be shown on top of template
274      */
275     $str = "none";
276     if(!empty($this->selectedPPD)){
277       $str = $this->ppdManager->loadDescription($this->pathToPPD.$this->selectedPPD['link']);
278     }
279     return($str) ; 
280   }
282   function generateProperties()
283   { 
284     /* In future there will be a schema parser that provide us all settings that can be made in the selected PPD file. 
285      * This function will generate a userfriendly post based form with this informations
286      */
288     /* Set Headline */
289     $str = "";
290     $feed= "";
292     $s_ppd = $this->pathToPPD.$this->selectedPPD['link'];
294     /* If ppd exists and is readable */
295     if((!empty($this->selectedPPD['link']))&&(file_exists($s_ppd))){
297       /* If there is no initial Configuration, load it */
298       if($this->ppdConfig == false){
299         $this->ppdConfig = $this->ppdManager->loadProperties($s_ppd);
300       }
302       /* Create a table */
303       $str .= "<div style='padding-left:30px;'><table>";
305       /* Input all data to the table */
306       foreach($this->ppdConfig as $cat => $obj){
308         /* Add new category */
309         $str .= "<tr><td colspan='2'>$feed";
310         if ($feed == ""){
311           $feed= "<br>";
312         }
313         $str .= "<b>"._("Section")." '".$cat."'&nbsp;</b><br>";
314         $str .= "</tr></td>";       
316         /* Add attributes of the current category */
317         foreach($obj as $attr => $settings){
320           /* Skip all entries beginning with _ */
321           if($attr[0] == "_") continue;  
323           /* Prepare data */
324           $values   = array();
325           $name     = $settings['_name'];
326           $default  = $settings['_default'];
327           $type     = $settings['_type'];
329           /* Add name to table */ 
330           $str .= "<tr><td style='padding-left:40px;'>\n";
331           $str .= $name."<br>\n";
332           $str .= "</td><td>\n";
334           /* Get all values */ 
335           foreach( $settings as $vname => $value){
336             if($vname[0] != "_"){
337               $values[$vname]= $value;
338             }
339           }
341           /* preparing Html output
342            * Supported types are PickOne/Boolean
343            */
345           /* If type is PickOne, create a select box */
346           if(($type == "PickOne")||(($type=="Boolean")&&(count($values)>1))){
348             $str  .=  "<select name='".base64_encode($name)."'>\n";
349             foreach($values as $value){
350               $selected = "";
351               if($value == $default){
352                 $selected = " selected ";
353               }
354               $str  .=  "<option value='".$value."' ".$selected.">".$value."</option>\n";
355             }
356             $str  .=  "</select>\n";
358           }elseif($type == "Boolean"){
360             /* If type is Boolean & no values are given */
361             $str  .=  "<select name='".base64_encode($name)."'>\n";
362             if($default == "False"){
363               $str  .=    "<option value='True' >"._("True")."</option>\n";
364               $str  .=    "<option value='False' selected>"._("False")."</option>\n";
365             }else{
366               $str  .=    "<option value='True' selected>"._("True")."</option>\n";
367               $str  .=    "<option value='False' >"._("False")."</option>\n";
368             }          
369             $str  .=  "</select>\n";
371           }else{
372             print_red(sprintf(_("Unsupported ppd type '%s' used for '%s' "),$type,$name));
373           }
374           $str .= "</td></tr>\n";
375         }
376       }
377       $str .= "</table></div>\n";
379     }
380     return($str);
381   }
383 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
384 ?>