Code

Added prelimitary debconf processor
[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   }
24   
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   }
37   
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       $name= "";
58       $langcode= $this->language.".UTF-8";
59       $in_description= FALSE;
60       $got_local_description= FALSE;
61       $fh= fopen($filename, 'r');
63       while (!feof($fh)){
64         $line= fgets($fh, 1024);
66         /* Reset description flag */
67         if ($in_description && !preg_match("/^ /", $line)){
68           $in_description= FALSE;
69         }
70         
71         /* Template header */
72         if (preg_match("/^Template: /", $line)){
73           $name= trim(preg_replace("/^Template: (.*)$/", "\\1", $line));
74           $this->template[$name]= array();
75           $got_local_description= FALSE;
76           continue;
77         }
79         /* Get type */
80         if (preg_match("/^Type: /", $line)){
81           $type= trim(preg_replace("/^Type: (.*)$/", "\\1", $line));
82           $this->template[$name]['Type']= $type;
83           continue;
84         }
86         /* Get default */
87         if (preg_match("/^Default: /", $line)){
88           $default= trim(preg_replace("/^Default: (.*)$/", "\\1", $line));
89           $this->template[$name]['Default']= $default;
90           continue;
91         }
93         /* Get description */
94         if (!$got_local_description && preg_match("/^Description: /", $line)){
95           $description= trim(preg_replace("/^Description: (.*)$/", "\\1", $line));
96           $this->template[$name]['Topic']= $description;
97           $in_description= TRUE;
98           continue;
99         }
101         /* Fill description */
102         if (!$got_local_description && $in_description){
103           $description= preg_replace("/^ (.*)$/", "\\1", $line);
104           $this->template[$name]['Description'].= $description;
105           continue;
106         }
108         /* Get local description */
109         if (preg_match("/^Description-$langcode: /", $line)){
110           $description= trim(preg_replace("/^Description-$langcode: (.*)$/", "\\1", $line));
111           $this->template[$name]['Topic']= $description;
112           $in_description= TRUE;
113           $got_local_description= TRUE;
114           $this->template[$name]['Description']= "";
115           continue;
116         }
118         /* Fill local description */
119         if ($got_local_description && $in_description){
120           $description= preg_replace("/^ (.*)$/", "\\1", $line);
121           $this->template[$name]['Description'].= $description;
122           continue;
123         }
125         /* Get native choices */
126         if (preg_match("/^Choices: /", $line)){
127           $type= trim(preg_replace("/^Choices: (.*)$/", "\\1", $line));
128           $this->template[$name]['Choices']= $type;
129         }
131         /* Get local choices */
132         if (preg_match("/^Choices-$langcode: /", $line)){
133           $type= trim(preg_replace("/^Choices-$langcode: (.*)$/", "\\1", $line));
134           $this->template[$name]['Choices-local']= $type;
135         }
137       }
139       fclose($fh);
140       $this->has_template= TRUE;
141       return (TRUE);
142     }
144     $this->has_template= FALSE;
145     return (FALSE);
146   }
149   function has_template()
150   {
151     return ($has_template);
152   }
155   function get_dialog()
156   {
157     if ($this->has_template){
158       $result= "";
160       foreach ($this->template as $name => $entry){
162         $types= array("boolean" => "", "multiselect" => "", "note" => "",
163                       "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
164         
165         /* Check if type is available */
166         if (isset($types[$entry['Type']])){
168           /* Produce type specific output */
169           $fn= "render_".$entry['Type'];
170           $result.= $this->$fn($entry);
171           
172         } else {
173           php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
174         }
175       }
176       
177       return ($result);
178     } else {
179       return _("This package has no debconf options.");
180     }
181   }
184   function render_boolean($data)
185   {
186     $result=  "<table width='100%' border=1>";
187     $result.= "<tr><td valign='top'>";
188     $result.= "<b>".$data['Topic']."</b><br><br>";
189     $result.= $data['Description'];
190     $result.= "</td><td width='10%' valign='top'>O Yes<br>O No</td>";
191     $result.= "</table>";
193     return ($result);
194   }
197   function render_multiselect($data)
198   {
199     if (preg_match('/\$\{/', $data['Choices'])){
200       $choices= "Need to use some text...";
201     } else {
202       $choices= "";
203       foreach (split(", ", $data['Choices']) as $choice){
204         $choices.= "[ ] ".$choice."<br>";
205       }
206     }
207   
208     $result=  "<table width='100%' border=1>";
209     $result.= "<tr><td valign='top'>";
210     $result.= "<b>".$data['Topic']."</b><br><br>";
211     $result.= $data['Description'];
212     $result.= "</td><td width='30%' valign='top'>$choices</td>";
213     $result.= "</table>";
215     return ($result);
216   }
219   function render_note($data)
220   {
221     /* Ignore notes, they do not makes sense, since we don't get any
222        chance to test entered values... */
223     return ("");
224   }
227   function render_password($data)
228   {
229     $result=  "<table width='100%' border=1>";
230     $result.= "<tr><td valign='top'>";
231     $result.= "<b>".$data['Topic']."&nbsp;[......................]</b><br><br>";
232     $result.= $data['Description'];
233     $result.= "</td></table>";
235     return ($result);
236   }
239   function render_select($data)
240   {
241     if (preg_match('/\$\{/', $data['Choices'])){
242       $choices= "Need to use some text...";
243     } else {
244       $choices= "";
245       foreach (split(", ", $data['Choices']) as $choice){
246         $choices.= "[ ] ".$choice."<br>";
247       }
248     }
249   
250     $result=  "<table width='100%' border=1>";
251     $result.= "<tr><td valign='top'>";
252     $result.= "<b>".$data['Topic']."</b><br><br>";
253     $result.= $data['Description'];
254     $result.= "</td><td width='30%' valign='top'>$choices</td>";
255     $result.= "</table>";
257     return ($result);
258   }
261   function render_string($data)
262   {
263     $result=  "<table width='100%' border=1>";
264     $result.= "<tr><td valign='top'>";
265     $result.= "<b>".$data['Topic']."&nbsp;[......................]</b><br><br>";
266     $result.= $data['Description'];
267     $result.= "</td></table>";
269     return ($result);
270   }
273   function render_text($data)
274   {
275     /* Ignore text messages, they are normally used for status hints. */
276     return ("");
277   }
280   function render_title($data)
281   {
282     /* Ignore text messages, they are normally used for status hints. */
283     return ("");
284   }
287   function get_template_packages()
288   {
289   }
291   
292   function set_default($default)
293   {
294   }
299 # Example:
300 # $debconf= new debconf("xserver-xorg", "de");
301 # echo $debconf->get_dialog();
303 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
304 ?>