Code

Backport from trunk
[gosa.git] / 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                 = explode("\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($_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(preg_match("/".$entry['post_name']."-multi-/",$name)){
156             $this->template[$post_name]['Default'] .= $value.", ";
157           }
158         } 
159         $this->template[$post_name]['Default'] = preg_replace("/, $/","",$this->template[$post_name]['Default']);
160       }
161     }
162   }
165   /* This funtion sets the defualt value */
166   function SetDefault($var,$val)
167   {
168     if ($this->loaded_template) {
169       foreach($this->template as $key => $tmp){
170         if($tmp['Name'] == $var ){
171           $this->template[$key]['Default'] = $val;
172         }
173       }
174     }
175   }
178   /* Display all possible options in html*/
179   function get_dialog()
180   {
181     if ($this->loaded_template && count($this->template)) {
182       $result= "<table summary=''>";
184       foreach ($this->template as $post_name => $entry){
186         $types= array("boolean" => "", "multiselect" => "", "note" => "",
187             "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
189         /* Check if type is available */
190         if ((isset($entry['Type']))&&(isset($types[$entry['Type']]))){
192           /* Produce type specific output */
193           $fn= "render_".$entry['Type'];
194           $str = $this->$fn($entry);
195           if(!empty($str)){
196             $result.=$str."<tr><td colspan='2'><hr></td></tr>";
197           }
198         } else {
199           //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
200         }
201       }
202       $result.="</table>";
204     
205       $result .= "<input type='hidden' post_name='check_post' value='1'>";
206       return ($result);
207     } else {
208       return _("This package has no debconf options.");
209     }
210   }
213   function render_boolean($data)
214   {
215     $post_name= $data['post_name'];
216     $result="
217       <tr>
218       <td valign='top' style='width:100%'>
219       <h3>".$data['Topic']."</h3>".$data['Description']."
220       </td>
221       <td style=\"white-space:nowrap; vertical-align:top; border-left: 1px solid rgb(160, 160, 160);\">";
223     foreach(array("true","false") as $value){
224       if($data['Default'] == $value){
225         $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' checked>"._($value);
226       }else{
227         $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' >"._($value);
228       }
229       $result.="<br>";
230     }
232     $result.= "
233       </td>
234       </tr>
235       ";
237     return ($result);
238   }
241   function render_multiselect($data)
242   {
243     $post_name= $data['post_name'];
244     if (preg_match('/\$\{/', $data['Choices'])){
245        $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>';
246       $result= $this->render_string($data);
247     } else {
248       $choices= "";
249       foreach (explode(", ", $data['Choices']) as $choice){
250         $choices[]= $choice;
251       }
254       $result="
255         <tr>
256         <td valign='top'>
257         <h3>".$data['Topic']."</h3>".$data['Description']."
258         </td>
259         <td valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
260           <input type='hidden' name='multi-".$post_name."' value='1'>
261         ";
262         
263       $defs = explode(", ",$data['Default']);
264       foreach($choices as $value){
265         if(in_array_strict($value,$defs)){
266           $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".set_post($value)."' checked>".$value."<br>";
267         }else{
268           $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".set_post($value)."'>".$value."<br>";
269         }
270       }
272     $result .=    "</td>
273         </tr>
274         ";
275     }    
277     return ($result);
278   }
281   function render_note($data)
282   {
283     /* Ignore notes, they do not makes sense, since we don't get any
284        chance to test entered values... */
285     return ("");
286   }
289   function render_password($data)
290   {
291     $result=  "";
292     $result.= "<tr><td valign='top'>";
293     $result.= "<h3>".$data['Topic']."</h3>".$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>";
294     $result.= $data['Description'];
295     $result.= "</td>";
297     return ($result);
298   }
301   function render_select($data)
302   {
303     $post_name= $data['post_name'];
305     if (preg_match('/\$\{/', $data['Choices'])){
306       $result = $this->render_multiselect($data);
307     } else {
308       $choices= "";
309       foreach (explode(", ", $data['Choices']) as $choice){
310         $choices[]= $choice;
311       }
313       $result="
314         
315         <tr>
316         <td valign='top'>
317         <h3>".$data['Topic']."</h3>".$data['Description']."
318         </td>
319         <td  valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
320         ";
322       foreach($choices as $value){
323         if($data['Default'] == $value){
324           $result.="\n<input type='radio' name='".$post_name."' value='".set_post($value)."' checked >".set_post($value)."<br>";
325         }else{
326           $result.="\n<input type='radio' name='".$post_name."' value='".set_post($value)."'>".set_post($value)."<br>";
327         }
328       }
330       $result.= "
331         
332         </td>
333         </tr>
334         ";
335     }
337     return ($result);
338   }
341   function render_string($data)
342   {
343     $result=  "
344                 <tr>
345                   <td valign='top'>
346                     <h3>".$data['Topic']."</h3>".$data['Description']."
347                   </td>
348                   <td  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\" valign='top'>
349                     <input type='text' name='".$data['post_name']."' value='".$data['Default']."' style='width:300px;'>
350                   </td>
351                 </tr>
352               ";
354     return ($result);
355   }
358   function render_text($data)
359   {
360     /* Ignore text messages, they are normally used for status hints. */
361     return ("");
362   }
365   function render_title($data)
366   {
367     /* Ignore text messages, they are normally used for status hints. */
368     return ("");
369   }
374 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
375 ?>