Code

1b4c5a39633a788f0332102f7eb8e8cfa0cacb11
[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       $tmp2= split("\n", $this->ppdManager->loadDescription($ppdfile));
53       $tmp3['name']   =preg_replace("/^ -/","",$tmp2[1]);
54       $tmp3['link']   =$ppdfile;
55       $tmp3['ppd']    =$this->ppdManager->loadDescription($ppdfile);
56       $this->selectedPPD = $tmp3;
57    }
59  
60   }
62   function execute()
63   {
64     /* Fill templating stuff */
65     $smarty= get_smarty();
66     $display= "";
69     /* Open a dialog that allow us to select different PPDs
70      */
71     if(isset($_POST['SelectPPD'])){
72       $this->dialog= new printerPPDSelectionDialog($this->config,$this->dn,$this->ppdList,$this->ppdListHeader,$this->selectedPPD);
73     }
75     /* The selection dialog fpr PPDs is canceled
76      */
77     if(isset($_POST['ClosePPDSelection'])){
78       unset($this->dialog);
79       $this->dialog=NULL;
80     }
81   
82     /* A new PPDs was selected in the PPDs selection Dialog
83      * Perform a Check. If everything is fine, use the new PPD.
84      */
85     if(isset($_POST['SavePPDSelection'])){
86       if(count($this->dialog->check())>0){
87         foreach($this->dialog->check() as $msg){
88           print_red($msg);
89         }
90       }else{
91         $this->selectedPPD = $this->dialog->save();
92         $this->ppdConfig = false;
93         unset($this->dialog);
94         $this->dialog=NULL;
95       }
96     }
97   
98     /* if a dialog is open, print the dialog instead of this class
99      */
100     if($this->dialog!=NULL){
101       $display = $this->dialog->execute();
102       return($display);
103     }
105     /* Give smarty the information it needs */
106     $smarty->assign("ppdString" ,$this->getPPDInformation());
107     $smarty->assign("properties",$this->generateProperties());
108   
109     /* Print out template */
110     $display.= $smarty->fetch(get_template_path('printerPPDDialog.tpl', TRUE,dirname(__FILE__)));
111     return($display);
112   }
114   function check(){
115     /* Check the given data
116      */
117     $message=array();
118     return $message;
119   }
121   function save_object()
122   {
123     if(isset($_POST['PPDDisSubmitted'])){
124       foreach($this->ppdConfig as $cat => $obj){
125         foreach($obj as $attr => $attributes){
126           if(isset($_POST[base64_encode($attributes['_name'])])){
127             $this->ppdConfig[$cat][$attr]['_default'] = $_POST[base64_encode($attributes['_name'])];
128           }
129         }
130       }
131     }
133   }
135   /* Save to LDAP */
136   function save()
137   {
138     /* return the selected PPD, and in future the selected options too */
139     $this->ppdManager->saveProperties($this->selectedPPD['link'],$this->ppdConfig);
140     return($this->selectedPPD['link']);
141   }
143   function getPPDInformation()
144   {
145     /* Get Information for a single PPD entry 
146      * This will be shown on top of template
147      */
148     $str = "none";
149     if(!empty($this->selectedPPD)){
150       $str = $this->selectedPPD['link'];
151       $str = $this->ppdManager->loadDescription($this->selectedPPD['link']);
152     }
153     return($str) ; 
154   }
156   function generateProperties()
157   { 
158     /* In future there will be a schema parser that provide us all settings that can be made in the selected PPD file. 
159      * This function will generate a userfriendly post based form with this informations
160      */
161   
162     $HeadLine  = "<h2>"._("Options")."&nbsp;:</h2>";
164     /* Set Headline */
165     $str = $HeadLine;
167     /* If ppd exists and is readable */
168     if((!empty($this->selectedPPD['link']))&&(file_exists($this->selectedPPD['link']))){
169     
170       /* If there is no initial Configuration, load it */
171       if($this->ppdConfig == false){
172         $this->ppdConfig = $this->ppdManager->loadProperties($this->selectedPPD['link']);
173       }
175       /* Create a table */
176       $str .= "<div style='padding-left:10px;'><table>";
178       /* Input all data to the table */
179       foreach($this->ppdConfig as $cat => $obj){
180     
181         /* Add new category */
182         $str .= "<tr><td colspan='2'><p class='seperator'>&nbsp;</p><br>";
183         $str .= "<b>".$cat."</b><br>";
184         $str .= "</tr></td>";       
186         /* Add attributes of the current category */
187         foreach($obj as $attr => $settings){
188         
189           /* Skip all entries beginning with _ */
190           if($attr[0] == "_") continue;  
191           
192           /* Prepare data */
193           $values   = array();
194           $name     = $settings['_name'];
195           $default  = $settings['_default'];
196           $type     = $settings['_type'];
197  
198           /* Add name to table */ 
199           $str .= "<tr><td style='padding-left:10px;'>\n";
200           $str .= $name."<br>\n";
201           $str .= "</td><td>\n";
202    
203           /* Get all values */ 
204           foreach( $settings as $vname => $value){
205             if($vname[0] != "_"){
206               $values[$vname]= $vname;
207             }
208           }
210           /* preparing Html output
211            * Supported types are PickOne/Boolean
212            */
214           /* If type is PickOne, create a select box */
215           if(($type == "PickOne")||(($type=="Boolean")&&(count($values)>1))){
217             $str  .=  "<select name='".base64_encode($name)."'>\n";
218             foreach($values as $value){
219               $selected = "";
220               if($value == $default){
221                 $selected = " selected ";
222               }
223               $str  .=  "<option value='".$value."' ".$selected.">".$value."</option>\n";
224             }
225             $str  .=  "</select>\n";
227           }elseif($type == "Boolean"){
229             /* If type is Boolean & no values are given */
230             $str  .=  "<select name='".base64_encode($name)."'>\n";
231             if($default == "False"){
232               $str  .=    "<option value='True' >"._("True")."</option>\n";
233               $str  .=    "<option value='False' selected>"._("False")."</option>\n";
234             }else{
235               $str  .=    "<option value='True' selected>"._("True")."</option>\n";
236               $str  .=    "<option value='False' >"._("False")."</option>\n";
237             }          
238             $str  .=  "</select>\n";
240           }else{
241             print_red(sprintf(_("Unsupported PPD type '%s' used for '%s' "),$type,$name));
242           }
243           $str .= "</td></tr>\n";
244         }
245       }
246       $str .= "</table></div>\n";
248     }
249     return($str);
250   }
252 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
253 ?>