Code

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