Code

56dadcb21f6d77c96aeba49c5c61ea7e08a8032d
[gosa.git] / trunk / gosa-plugins / fai / admin / fai / class_debconfTemplate.inc
1 <?php
3 class debconf
4 {
5   var $package= "";
6   var $language= "";
7   var $loaded_template= FALSE;
8   var $template= array();
11   function debconf($language= "")
12   {
13     $this->set_language($language);
14   }
16   function set_language($language)
17   {
18     $this->language= $language;
19   }
21   function load_from_string($str)
22   {
23     $lines                 = split("\n",$str);
24     $this->template        = array();
25     $post_name             = 0;
26     $langcode              = $this->language.".UTF-8";
27     $in_description        = FALSE;
28     $got_local_description = FALSE;
30     foreach($lines as $line){
32       /* Reset description flag */
33       if ($in_description && !preg_match("/^ /", $line)){
34         $in_description= FALSE;
35       }
37       /* Template header */
38       if (preg_match("/^Template: /", $line)){
39         $post_name ++; 
40         $name= trim(preg_replace("/^Template: (.*)$/", "\\1", $line));
41         $this->template[$post_name]['Name'] = $name;
42         $this->template[$post_name]['Default'] ="";
44         $got_local_description= FALSE;
45         continue;
46       }
48       /* Get type */
49       if (preg_match("/^Type: /", $line)){
50         $type= trim(preg_replace("/^Type: (.*)$/", "\\1", $line));
51         $this->template[$post_name]['Type']= $type;
52         continue;
53       }
55       /* Get default */
56       if (preg_match("/^Default: /", $line)){
57         $this->template[$post_name]['Default']= "";
58         $default= trim(preg_replace("/^Default: (.*)$/", "\\1", $line));
59         $this->template[$post_name]['Default']= $default;
60         continue;
61       }
63       /* Get description */
64       if (!$got_local_description && preg_match("/^Description: /i", $line)){
65         $description= trim(preg_replace("/^Description: (.*)$/i", "\\1", $line));
66         $this->template[$post_name]['Topic']= $description;
67         $this->template[$post_name]['Description']= "";
68         $in_description= TRUE;
69         continue;
70       }
72       /* Fill description */
73       if (!$got_local_description && $in_description){
74         $description= preg_replace("/^ (.*)$/", "\\1", $line);
75         $this->template[$post_name]['Description'].= $description;
76         continue;
77       }
79       /* Get local description */
80       if (preg_match("/^Description-$langcode: /i", $line)){
81         $description= trim(preg_replace("/^Description-$langcode: (.*)$/i", "\\1", $line));
82         $this->template[$post_name]['Topic']= $description;
83         $in_description= TRUE;
84         $got_local_description= TRUE;
85         $this->template[$post_name]['Description']= "";
86         continue;
87       }
89       /* Fill local description */
90       if ($got_local_description && $in_description){
91         $description= preg_replace("/^ (.*)$/", "\\1", $line);
92         $this->template[$post_name]['Description'].= $description;
93         continue;
94       }
96       /* Get native choices */
97       if (preg_match("/^Choices: /", $line)){
98         $type= trim(preg_replace("/^Choices: (.*)$/", "\\1", $line));
99         $this->template[$post_name]['Choices']= $type;
100       }
102       /* Get local choices */
103       if (preg_match("/^Choices-$langcode: /", $line)){
104         $type= trim(preg_replace("/^Choices-$langcode: (.*)$/", "\\1", $line));
105         $this->template[$post_name]['Choices-local']= $type;
106       }
108     }
110     $this->loaded_template= TRUE;
112     $tmp= array();
113     foreach($this->template as $post_name => $template){
114       $template['post_name'] = "post_".$post_name;
115       $tmp[] = $template;
116     }
117     $this->template = $tmp;
119     return (TRUE);
120   }
122   function has_template()
123   {
124     return(count($this->template) != FALSE);
125   }
128   /* Check if some fields are posted */
129   function PostCheck()
130   {
131     /* Walk through all template variables */
132     foreach($this->template as $post_name => $entry){
134       /* Check if this var is set*/
135       if(isset($_POST[$entry['post_name']])){
137         /* special handling for arrays */
138         if(is_array($_POST[$entry['post_name']])){
139           $str = "";
140           foreach(get_post($entry['post_name']) as $val){
141             $str.= $val.", ";
142           }
143           $str = preg_replace("/\,\ $/","",$str);
144           $this->template[$post_name]['Default'] = $str;
145         }else{
146           $this->template[$post_name]['Default'] = get_post($entry['post_name']);
147         }
148       }
149     }
150     
151     foreach($this->template as $post_name => $entry){
152       if(isset($_POST["multi-".$entry['post_name']])){ 
153         $this->template[$post_name]['Default']= "";
154         foreach($_POST as $name => $value){
155           if (get_magic_quotes_gpc()) {
156               $value = stripcslashes($value);
157           }
158           if(preg_match("/".$entry['post_name']."-multi-/",$name)){
159             $this->template[$post_name]['Default'] .= $value.", ";
160           }
161         } 
162         $this->template[$post_name]['Default'] = preg_replace("/, $/","",$this->template[$post_name]['Default']);
163       }
164     }
165   }
168   /* This funtion sets the defualt value */
169   function SetDefault($var,$val)
170   {
171     if ($this->loaded_template) {
172       foreach($this->template as $key => $tmp){
173         if($tmp['Name'] == $var ){
174           $this->template[$key]['Default'] = $val;
175         }
176       }
177     }
178   }
181   /* Display all possible options in html*/
182   function get_dialog()
183   {
184     if ($this->loaded_template) {
185       $result= "<table summary=''>";
187       foreach ($this->template as $post_name => $entry){
189         $types= array("boolean" => "", "multiselect" => "", "note" => "",
190             "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
192         /* Check if type is available */
193         if ((isset($entry['Type']))&&(isset($types[$entry['Type']]))){
195           /* Produce type specific output */
196           $entry_type = $entry['Type'];
198           $fn= sprintf("render_%s", $entry_type);
200           $str = $this->$fn($entry);
201           if(!empty($str)){
202             $result.=$str."<tr><td colspan='2'><p class='seperator'>&nbsp;</p></td></tr>";
203           }
204         } else {
205           //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
206         }
207       }
209     
210       $result .= "<input type='hidden' post_name='check_post' value='1'></table>";
211       return ($result);
212     } else {
213       return _("This package has no debconf options.");
214     }
215   }
218   function render_boolean($data)
219   {
220     $post_name= $data['post_name'];
221     $result="
222       <tr>
223       <td valign='top' style='width:100%'>
224       <h2>".$data['Topic']."</h2>".$data['Description']."
225       </td>
226       <td style=\"white-space:nowrap; vertical-align:top; border-left: 1px solid rgb(160, 160, 160);\">";
228     foreach(array("true","false") as $value){
229       if($data['Default'] == $value){
230         $result.="<input type='radio' name='".$data['post_name']."' value=\"".$value."\" checked>"._($value);
231       }else{
232         $result.="<input type='radio' name='".$data['post_name']."' value=\"".$value."\" >"._($value);
233       }
234       $result.="<br>";
235     }
237     $result.= "
238       </td>
239       </tr>
240       ";
242     return ($result);
243   }
246   function render_multiselect($data)
247   {
248     $post_name= $data['post_name'];
249     if (preg_match('/\$\{/', $data['Choices'])){
250       $data['Description'] .= '<br><br><b>' . _('This debconf question is dynamically generated during package installation and requires choosing between specific options which cannot be presented here. The entered text needs to be one of the valid choices in order to take effect.') . '</b>';
251       $result= $this->render_string($data);
252     } else {
253       $choices= "";
254       foreach (split(", ", $data['Choices']) as $choice){
255         $choices[]= $choice;
256       }
259       $result="
260         <tr>
261         <td valign='top'>
262         <h2>".$data['Topic']."</h2>".$data['Description']."
263         </td>
264         <td valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
265           <input type='hidden' name='multi-".$post_name."' value='1'>
266         ";
267         
268       $defs = split(", ",$data['Default']);
269       foreach($choices as $value){
270         if(in_array($value,$defs)){
271           $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value=\"".htmlentities($value)."\" checked>".$value."<br>";
272         }else{
273           $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value=\"".htmlentities($value)."\">".$value."<br>";
274         }
275       }
277     $result .=    "</td>
278         </tr>
279         ";
280     }    
282     return ($result);
283   }
286   function render_note($data)
287   {
288     /* Ignore notes, they do not makes sense, since we don't get any
289        chance to test entered values... */
290     return ("");
291   }
294   function render_password($data)
295   {
296     $result=  "";
297     $result.= "<tr><td valign='top'>";
298     $result.= "<h2>".$data['Topic']."</h2>".$data['Description']."</td><td style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">&nbsp;<input type='text' name='".$data['post_name']."' value=\"".$data['Default']."\"></b><br><br>";
299     $result.= $data['Description'];
300     $result.= "</td>";
302     return ($result);
303   }
306   function render_select($data)
307   {
308     $post_name= $data['post_name'];
310     if (preg_match('/\$\{/', $data['Choices'])){
311       $result = $this->render_multiselect($data);
312     } else {
313       $choices= "";
314       foreach (split(", ", $data['Choices']) as $choice){
315         $choices[]= $choice;
316       }
319       $result="
320         
321         <tr>
322         <td valign='top'>
323         <h2>".$data['Topic']."</h2>".$data['Description']."
324         </td>
325         <td  valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
326         ";
328       foreach($choices as $value){
329         if($data['Default'] == $value){
330           $result.="\n<input type='radio' name='".$post_name."' value=\"".htmlentities($value)."\" checked >".htmlentities($value)."<br>";
331         }else{
332           $result.="\n<input type='radio' name='".$post_name."' value=\"".htmlentities($value)."\">".htmlentities($value)."<br>";
333         }
334       }
336       $result.= "
337         
338         </td>
339         </tr>
340         ";
341     }
343     return ($result);
344   }
347   function render_string($data)
348   {
349     $result=  "
350                 <tr>
351                   <td valign='top'>
352                     <h2>".$data['Topic']."</h2>".$data['Description']."
353                   </td>
354                   <td  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\" valign='top'>
355                     <input type='text' name='".$data['post_name']."' value=\"".$data['Default']."\" style='width:300px;'>
356                   </td>
357                 </tr>
358               ";
360     return ($result);
361   }
364   function render_text($data)
365   {
366     /* Ignore text messages, they are normally used for status hints. */
367     return ("");
368   }
371   function render_title($data)
372   {
373     /* Ignore text messages, they are normally used for status hints. */
374     return ("");
375   }
380 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
381 ?>