Code

Layout fixes
[gosa.git] / include / class_debconfTemplate.inc
1 <?php
3 class debconf
4 {
5   var $package= "";
6   var $language= "";
7   var $has_template= FALSE;
8   var $template_directory= "/var/lib/dpkg/info";
9   var $template= array();
12   function debconf($package= "", $language= "")
13   {
14     $this->set_language($language);
15     $this->set_package($package);
16   }
19   function set_package($package)
20   {
21     $this->package= $package;
22     return ($this->load());
23   }
26   function set_template_directory($directory)
27   {
28     if (is_dir($directory) && is_readable($directory)){
29       $this->template_directory = $directory;
30       return TRUE;
31     }
33     $this->template_directory= "";
34     return FALSE;
35   }
38   function set_language($language)
39   {
40     $this->language= $language;
41   }
44   function load()
45   {
46     /* Reject requests, if parameters are not set */
47     if ($this->package == "" || $this->template_directory == ""){
48       return (FALSE);
49     }
51     /* Try to load package based template file */
52     $filename= preg_replace("/\/+/", "/", $this->template_directory."/".$this->package.".templates");
53     if (is_file($filename) && is_readable($filename)){
54       $this->template= array();
56       /* Read template array */
57       $post_name              = 0;
58       $langcode             = $this->language.".UTF-8";
59       $in_description       = FALSE;
60       $got_local_description= FALSE;
62       $fh= fopen($filename, 'r');
64       while (!feof($fh)){
65         $line= fgets($fh, 1024);
67         /* Reset description flag */
68         if ($in_description && !preg_match("/^ /", $line)){
69           $in_description= FALSE;
70         }
72         /* Template header */
73         if (preg_match("/^Template: /", $line)){
74           $post_name ++; 
75           $name= trim(preg_replace("/^Template: (.*)$/", "\\1", $line));
76           $this->template[$post_name]['Name'] = $name;
77           $this->template[$post_name]['Default'] ="";
78          
79           $got_local_description= FALSE;
80           continue;
81         }
83         /* Get type */
84         if (preg_match("/^Type: /", $line)){
85           $type= trim(preg_replace("/^Type: (.*)$/", "\\1", $line));
86           $this->template[$post_name]['Type']= $type;
87           continue;
88         }
90         /* Get default */
91         if (preg_match("/^Default: /", $line)){
92           $this->template[$post_name]['Default']= "";
93           $default= trim(preg_replace("/^Default: (.*)$/", "\\1", $line));
94           $this->template[$post_name]['Default']= $default;
95           continue;
96         }
98         /* Get description */
99         if (!$got_local_description && preg_match("/^Description: /", $line)){
100           $this->template[$post_name]['Description']= "";
101           $description= trim(preg_replace("/^Description: (.*)$/", "\\1", $line));
102           $this->template[$post_name]['Topic']= $description;
103           $this->template[$post_name]['Description']= "";
104           $in_description= TRUE;
105           continue;
106         }
108         /* Fill description */
109         if (!$got_local_description && $in_description){
110           $description= preg_replace("/^ (.*)$/", "\\1", $line);
111           $this->template[$post_name]['Description'].= $description;
112           continue;
113         }
115         /* Get local description */
116         if (preg_match("/^Description-$langcode: /", $line)){
117           $description= trim(preg_replace("/^Description-$langcode: (.*)$/", "\\1", $line));
118           $this->template[$post_name]['Topic']= $description;
119           $in_description= TRUE;
120           $got_local_description= TRUE;
121           $this->template[$post_name]['Description']= "";
122           continue;
123         }
125         /* Fill local description */
126         if ($got_local_description && $in_description){
127           $description= preg_replace("/^ (.*)$/", "\\1", $line);
128           $this->template[$post_name]['Description'].= $description;
129           continue;
130         }
132         /* Get native choices */
133         if (preg_match("/^Choices: /", $line)){
134           $type= trim(preg_replace("/^Choices: (.*)$/", "\\1", $line));
135           $this->template[$post_name]['Choices']= $type;
136         }
138         /* Get local choices */
139         if (preg_match("/^Choices-$langcode: /", $line)){
140           $type= trim(preg_replace("/^Choices-$langcode: (.*)$/", "\\1", $line));
141           $this->template[$post_name]['Choices-local']= $type;
142         }
144       }
146       fclose($fh);
147       $this->has_template= TRUE;
148       
149       $tmp= array();
150       foreach($this->template as $post_name => $template){
151         $template['post_name'] = "post_".$post_name;
152         $tmp[] = $template;
153       }
154       $this->template = $tmp;
156       return (TRUE);
157     }
159     $this->has_template= FALSE;
160     return (FALSE);
161   }
164   function has_template()
165   {
166     return ($this->has_template);
167   }
170   /* Check if some fields are posted */
171   function PostCheck()
172   {
173     /* Walk through all template variables */
174     foreach($this->template as $post_name => $entry){
175     
176       /* Check if this var is set*/
177       if(isset($_POST[$entry['post_name']])){
178   
179         /* special handling for arrays */
180         if(is_array($_POST[$entry['post_name']])){
181           $str = "";
182           foreach($_POST[$entry['post_name']] as $val){
183             $str.= $val.", ";
184           }
185           $str = preg_replace("/\,\ $/","",$str);
186           $this->template[$post_name]['Default'] = $str;
187         }else{
188           $this->template[$post_name]['Default'] = $_POST[$entry['post_name']];
189         }
190       }
191     }
192   
193   }
196   /* This funtion sets the defualt value */
197   function SetDefault($var,$val)
198   {
199     if($this->has_template){
200       foreach($this->template as $key => $tmp){
201         if($tmp['Name'] == $var ){
202           $this->template[$key]['Default'] = $val;
203         }
204       }
205     }
206   }
209   /* Display all possible options in html*/
210   function get_dialog()
211   {
212     if ($this->has_template){
213       $result= "";
215       foreach ($this->template as $post_name => $entry){
217         $types= array("boolean" => "", "multiselect" => "", "note" => "",
218             "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
220         /* Check if type is available */
221         if ((isset($entry['Type']))&&(isset($types[$entry['Type']]))){
223           /* Produce type specific output */
224           $fn= "render_".$entry['Type'];
225           $result.= $this->$fn($entry);
226         } else {
227           //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
228         }
229       }
231       $result .= "<input type='hidden' post_name='check_post' value='1'>";
232       return ($result);
233     } else {
234       return _("This package has no debconf options.");
235     }
236   }
239   function render_boolean($data)
240   {
242     $post_name= $data['post_name'];
243     $result="
244       <table width='100%' border=0>
245       <tr>
246       <td valign='top'>
247       <b>".$data['Topic']."</b>
248       </td>
249       <td width='10%' valign='top'>";
251     foreach(array("true","false") as $value){
252       if($data['Default'] == $value){
253         $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' checked>".$value."</option>";
254       }else{
255         $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' >".$value."</option>";
256       }
257     }
259     $result.= "
260       </td>
261       </tr>
262       <tr>
263       <td colspan=2>".$data['Description']."
264       </td>
265       </tr>
266       </table>";
268           $result.= "<p class='seperator'>&nbsp;</p>";
269     return ($result);
270   }
273   function render_multiselect($data)
274   {
275     $post_name= $data['post_name'];
276     if (preg_match('/\$\{/', $data['Choices'])){
277       $choices= array("Need to use some text...");
278     } else {
279       $choices= "";
280       foreach (split(", ", $data['Choices']) as $choice){
281         $choices[]= $choice;
282       }
283     }
286     $result="
287       <table width='100%' border=0>
288       <tr>
289       <td valign='top'>
290       <b>".$data['Topic']."</b>
291       </td>
292       <td width='10%' valign='top'>
293       <select name='".$post_name."[]'  multiple size=5>";
295     $defs = split(", ",$data['Default']);
296   
297     foreach($choices as $value){
298       if(in_array($value,$defs)){
299         $result.="\n<option value='".htmlentities($value)."' selected>".htmlentities($value)."</option>";
300       }else{
301         $result.="\n<option value='".htmlentities($value)."'>".htmlentities($value)."</option>";
302       }
303     }
305     $result.= "
306       </select>
307       </td>
308       </tr>
309       <tr>
310       <td colspan=2>".$data['Description']."
311       </td>
312       </tr>
313       </table>";
314           $result.= "<p class='seperator'>&nbsp;</p>";
316     return ($result);
317   }
320   function render_note($data)
321   {
322     /* Ignore notes, they do not makes sense, since we don't get any
323        chance to test entered values... */
324     return ("");
325   }
328   function render_password($data)
329   {
330     $result=  "<table width='100%' border=0>";
331     $result.= "<tr><td valign='top'>";
332     $result.= "<b>".$data['Topic']."&nbsp;<input type='text' name='".$data['post_name']."' value='".$data['Default']."'></b><br><br>";
333     $result.= $data['Description'];
334     $result.= "</td></table>";
335           $result.= "<p class='seperator'>&nbsp;</p>";
337     return ($result);
338   }
341   function render_select($data)
342   {
343     $post_name= $data['post_name'];
345     if (preg_match('/\$\{/', $data['Choices'])){
346       $choices= array("Need to use some text...");
347     } else {
348       $choices= "";
349       foreach (split(", ", $data['Choices']) as $choice){
350         $choices[]= $choice;
351       }
352     }
355     $result="
356       <table width='100%' border=0>
357       <tr>
358       <td valign='top'>
359       <b>".$data['Topic']."</b>
360       </td>
361       <td width='10%' valign='top'>
362       <select name='".$post_name."'>";
364     foreach($choices as $value){
365       if($data['Default'] == $value){
366         $result.="\n<option value='".htmlentities($value)."' selected>".htmlentities($value)."</option>";
367       }else{
368         $result.="\n<option value='".htmlentities($value)."'>".htmlentities($value)."</option>";
369       }
370     }
372     $result.= "
373       </select>
374       </td>
375       </tr>
376       <tr>
377       <td colspan=2>".$data['Description']."
378       </td>
379       </tr>
380       </table>";
381           $result.= "<p class='seperator'>&nbsp;</p>";
383     return ($result);
384   }
387   function render_string($data)
388   {
389     $result=  "<table width='100%' border=0>
390                 <tr>
391                   <td valign='top'>
392                     <b>".$data['Topic']."</b>&nbsp;
393                   </td>
394                   <td width='10%'>
395                     <input type='text' name='".$data['post_name']."' value='".$data['Default']."'>
396                   </td>
397                 </tr>
398                 <tr>
399                   <td colspan='2'>
400                       ".$data['Description']."
401                   </td>
402                 </tr>
403               </td></table>";
404           $result.= "<p class='seperator'>&nbsp;</p>";
406     return ($result);
407   }
410   function render_text($data)
411   {
412     /* Ignore text messages, they are normally used for status hints. */
413     return ("");
414   }
417   function render_title($data)
418   {
419     /* Ignore text messages, they are normally used for status hints. */
420     return ("");
421   }
425 # Example:
426 #$debconf= new debconf("libnss-ldap", "de");
427 #echo $debconf->get_dialog();
429 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
430 ?>