Code

0482349e93bc54b2994750436cfef6aab57c5d1b
[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   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       if(!file_exists($ppdfile)){
53         print_red(sprintf(_("Can't open '%s', ppd settings resetted."),$ppdfile));
54       }else{
55         $tmp2= split("\n", $this->ppdManager->loadDescription($ppdfile));
56         $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]);
57         $tmp3['link']   =$ppdfile;
58         $tmp3['ppd']    =$this->ppdManager->loadDescription($ppdfile);
59         $this->selectedPPD = $tmp3;
60       }
61     }
62   }
64   function getPrinterReload()
65   {
66     $tmp = $this->ppdManager->getPrinterList(true);
68     /* Sort all available files, and create header (Vendor index) */
69     foreach($tmp as $file=>$ppd){
70       $tmp2 = split("\n",$ppd);
71       if(!isset($this->ppdListHeader[$tmp2[0]])){
72         $this->ppdListHeader[$tmp2[0]]=0;
73       }
74       $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2]);
75       $tmp3['link']   =$file;
76       $tmp3['ppd']    =$ppd;
77       $this->ppdListHeader[$tmp2[0]]++;
78       $this->ppdList[$tmp2[0]][preg_replace("/^ -/","",$tmp2[1]." - ".$tmp2[2])]=$tmp3;
79     }
81   }
83   function execute()
84   {
85         /* Call parent execute */
86         plugin::execute();
88     /* Fill templating stuff */
89     $smarty= get_smarty();
90     $display= "";
92     /* Is there a new PPD file uploaded ? */
93     if((isset($_FILES['NewPPDFile']))&&(isset($_POST['SubmitNewPPDFile']))){
94       $file = ($_FILES['NewPPDFile']);
95       if($file['size'] != 0 ){
96         $res  = @$this->ppdManager->loadDescription($file['tmp_name']);
97         $name = (trim(preg_replace("/-/","",$res)));
98         if(!empty($name)){
99           $vendor = trim(preg_replace("/-.*$/i","",$res));
100           $model  = trim(preg_replace("/".$vendor."/","",$name));
101           if(!is_dir('/var/spool/ppd/'.$vendor)){
102             if(!(@mkdir('/var/spool/ppd/'.$vendor))){
103               print_red(sprintf(_("Can't create folder '%s' for the uploaded ppd file."),$vendor));
104             }
105           }
107           if(!isset($this->ppdList[$vendor])){
108             $this->ppdList[$vendor] = array();
109           }
110   
111           if(is_dir('/var/spool/ppd/'.$vendor)){  
112             $found = false;
113             foreach($this->ppdList[$vendor] as $key => $val){
114               if(preg_match("/".$model.".*/i",$key)){
115                 $found = true;
116                 print_red(sprintf(_("There is already a ppd file for this kind of printer.")));
117               }
118             }// Foreach
119             if(!$found){
120               $filename = '/var/spool/ppd/'.$vendor."/".$vendor."-".preg_replace("/^[^ ]/","",str_replace("-","",$model)).".ppd";
121               $fp = @fopen($filename,"w+");
122               if(!$fp){
123                 print_red(sprintf(_("Can't save file '%s'."),$filename));
124               }else{
125                 $str = file_get_contents($file['tmp_name']);
126                 fputs($fp,$str);
127                 @fclose($fp);
128                
129                 $tmp2= split("\n", $res);
130                 $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]);
131                 $tmp3['link']   =$filename;
132                 $tmp3['ppd']    =$res;
133                 $this->selectedPPD = $tmp3;
134                 $this->getPrinterReload(); 
135               }
136             }
137           }// If dir
138         }else{
139           print_red(_("Please specify a valid ppd file."));
140         }
141       }else{
142         print_red(_("Please specify a valid ppd file."));
143       }
144   
145     }
148     /* Open a dialog that allow us to select different PPDs
149      */
150     if(isset($_POST['SelectPPD'])){
151       $this->dialog= new printerPPDSelectionDialog($this->config,$this->dn,$this->ppdList,$this->ppdListHeader,$this->selectedPPD);
152     }
154     /* The selection dialog fpr PPDs is canceled
155      */
156     if(isset($_POST['ClosePPDSelection'])){
157       unset($this->dialog);
158       $this->dialog=NULL;
159     }
160   
161     /* A new PPDs was selected in the PPDs selection Dialog
162      * Perform a Check. If everything is fine, use the new PPD.
163      */
164     if(isset($_POST['SavePPDSelection'])){
165       if(!isset($_POST['PPDselection'])){
166         print_red(_("Please select a valid ppd."));
167       }else{
168         $this->selectedPPD['link'] = $_POST['PPDselection'];
169         $this->ppdConfig   = false;
170         unset($this->dialog);
171         $this->dialog=NULL;
172       }
173     }
174  
175     /* if a dialog is open, print the dialog instead of this class
176      */
177     if($this->dialog!=NULL){
178       $display = $this->dialog->execute();
179       return($display);
180     }
182     /* Give smarty the information it needs */
183     $smarty->assign("ppdString" ,$this->getPPDInformation());
184     $tmp= $this->generateProperties();
185     if ($tmp == ""){
186       $smarty->assign("showOptions", 0);
187     } else {
188       $smarty->assign("showOptions", 1);
189       $smarty->assign("properties",$this->generateProperties());
190     }
191   
192     /* Print out template */
193     $display.= $smarty->fetch(get_template_path('printerPPDDialog.tpl', TRUE,dirname(__FILE__)));
194     return($display);
195   }
197   function check(){
198     /* Check the given data
199      */
200     $message=array();
201     return $message;
202   }
204   function save_object()
205   {
206     if(isset($_POST['PPDDisSubmitted'])){
207       if(is_array($this->ppdConfig)){
208         foreach($this->ppdConfig as $cat => $obj){
209           foreach($obj as $attr => $attributes){
210             if(isset($_POST[base64_encode($attributes['_name'])])){
211               $this->ppdConfig[$cat][$attr]['_default'] = $_POST[base64_encode($attributes['_name'])];
212             }
213           }
214         }
215       }
216     }
218   }
219   
220   function save_ppd(){
221     $this->ppdManager->saveProperties($this->selectedPPD['link'],$this->ppdConfig);
222   }
224   /* Save to LDAP */
225   function save()
226   {
227     /* return the selected PPD, and in future the selected options too */
228     return($this->selectedPPD['link']);
229   }
231   function getPPDInformation()
232   {
233     /* Get Information for a single PPD entry 
234      * This will be shown on top of template
235      */
236     $str = "none";
237     if(!empty($this->selectedPPD)){
238       $str = $this->selectedPPD['link'];
239       $str = $this->ppdManager->loadDescription($this->selectedPPD['link']);
240     }
241     return($str) ; 
242   }
244   function generateProperties()
245   { 
246     /* In future there will be a schema parser that provide us all settings that can be made in the selected PPD file. 
247      * This function will generate a userfriendly post based form with this informations
248      */
249   
250     /* Set Headline */
251     $str = "";
252     $feed= "";
254     /* If ppd exists and is readable */
255     if((!empty($this->selectedPPD['link']))&&(file_exists($this->selectedPPD['link']))){
256     
257       /* If there is no initial Configuration, load it */
258       if($this->ppdConfig == false){
259         $this->ppdConfig = $this->ppdManager->loadProperties($this->selectedPPD['link']);
260       }
262       /* Create a table */
263       $str .= "<div style='padding-left:30px;'><table>";
265       /* Input all data to the table */
266       foreach($this->ppdConfig as $cat => $obj){
267     
268         /* Add new category */
269         $str .= "<tr><td colspan='2'>$feed";
270         if ($feed == ""){
271           $feed= "<br>";
272         }
273         $str .= "<b>"._("Section")." '".$cat."'&nbsp;</b><br>";
274         $str .= "</tr></td>";       
276         /* Add attributes of the current category */
277         foreach($obj as $attr => $settings){
278   
279       
280           /* Skip all entries beginning with _ */
281           if($attr[0] == "_") continue;  
282           
283           /* Prepare data */
284           $values   = array();
285           $name     = $settings['_name'];
286           $default  = $settings['_default'];
287           $type     = $settings['_type'];
288  
289           /* Add name to table */ 
290           $str .= "<tr><td style='padding-left:40px;'>\n";
291           $str .= $name."<br>\n";
292           $str .= "</td><td>\n";
293    
294           /* Get all values */ 
295           foreach( $settings as $vname => $value){
296             if($vname[0] != "_"){
297               $values[$vname]= $value;
298             }
299           }
301           /* preparing Html output
302            * Supported types are PickOne/Boolean
303            */
305           /* If type is PickOne, create a select box */
306           if(($type == "PickOne")||(($type=="Boolean")&&(count($values)>1))){
308             $str  .=  "<select name='".base64_encode($name)."'>\n";
309             foreach($values as $value){
310               $selected = "";
311               if($value == $default){
312                 $selected = " selected ";
313               }
314               $str  .=  "<option value='".$value."' ".$selected.">".$value."</option>\n";
315             }
316             $str  .=  "</select>\n";
318           }elseif($type == "Boolean"){
320             /* If type is Boolean & no values are given */
321             $str  .=  "<select name='".base64_encode($name)."'>\n";
322             if($default == "False"){
323               $str  .=    "<option value='True' >"._("True")."</option>\n";
324               $str  .=    "<option value='False' selected>"._("False")."</option>\n";
325             }else{
326               $str  .=    "<option value='True' selected>"._("True")."</option>\n";
327               $str  .=    "<option value='False' >"._("False")."</option>\n";
328             }          
329             $str  .=  "</select>\n";
331           }else{
332             print_red(sprintf(_("Unsupported ppd type '%s' used for '%s' "),$type,$name));
333           }
334           $str .= "</td></tr>\n";
335         }
336       }
337       $str .= "</table></div>\n";
339     }
340     return($str);
341   }
343 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
344 ?>