Code

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