Code

2e9fca6b30ed59dea9958207e5274ef074f77d56
[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($_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'] = $_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) {
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           $entry_type = $entry['Type'];
195           $fn= sprintf("render_%s", $entry_type);
197           $str = $this->$fn($entry);
198           if(!empty($str)){
199             $result.=$str."<tr><td colspan='2'><p class='seperator'>&nbsp;</p></td></tr>";
200           }
201         } else {
202           //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
203         }
204       }
206     
207       $result .= "<input type='hidden' post_name='check_post' value='1'></table>";
208       return ($result);
209     } else {
210       return _("This package has no debconf options.");
211     }
212   }
215   function render_boolean($data)
216   {
217     $post_name= $data['post_name'];
218     $result="
219       <tr>
220       <td valign='top' style='width:100%'>
221       <h2>".$data['Topic']."</h2>".$data['Description']."
222       </td>
223       <td style=\"white-space:nowrap; vertical-align:top; border-left: 1px solid rgb(160, 160, 160);\">";
225     foreach(array("true","false") as $value){
226       if($data['Default'] == $value){
227         $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' checked>"._($value);
228       }else{
229         $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' >"._($value);
230       }
231       $result.="<br>";
232     }
234     $result.= "
235       </td>
236       </tr>
237       ";
239     return ($result);
240   }
243   function render_multiselect($data)
244   {
245     $post_name= $data['post_name'];
246     if (preg_match('/\$\{/', $data['Choices'])){
247       $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>';
248       $result= $this->render_string($data);
249     } else {
250       $choices= "";
251       foreach (split(", ", $data['Choices']) as $choice){
252         $choices[]= $choice;
253       }
256       $result="
257         <tr>
258         <td valign='top'>
259         <h2>".$data['Topic']."</h2>".$data['Description']."
260         </td>
261         <td valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
262           <input type='hidden' name='multi-".$post_name."' value='1'>
263         ";
264         
265       $defs = split(", ",$data['Default']);
266       foreach($choices as $value){
267         if(in_array($value,$defs)){
268           $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."' checked>".$value."<br>";
269         }else{
270           $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."'>".$value."<br>";
271         }
272       }
274     $result .=    "</td>
275         </tr>
276         ";
277     }    
279     return ($result);
280   }
283   function render_note($data)
284   {
285     /* Ignore notes, they do not makes sense, since we don't get any
286        chance to test entered values... */
287     return ("");
288   }
291   function render_password($data)
292   {
293     $result=  "";
294     $result.= "<tr><td valign='top'>";
295     $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>";
296     $result.= $data['Description'];
297     $result.= "</td>";
299     return ($result);
300   }
303   function render_select($data)
304   {
305     $post_name= $data['post_name'];
307     if (preg_match('/\$\{/', $data['Choices'])){
308       $result = $this->render_multiselect($data);
309     } else {
310       $choices= "";
311       foreach (split(", ", $data['Choices']) as $choice){
312         $choices[]= $choice;
313       }
316       $result="
317         
318         <tr>
319         <td valign='top'>
320         <h2>".$data['Topic']."</h2>".$data['Description']."
321         </td>
322         <td  valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
323         ";
325       foreach($choices as $value){
326         if($data['Default'] == $value){
327           $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."' checked >".htmlentities($value)."<br>";
328         }else{
329           $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."'>".htmlentities($value)."<br>";
330         }
331       }
333       $result.= "
334         
335         </td>
336         </tr>
337         ";
338     }
340     return ($result);
341   }
344   function render_string($data)
345   {
346     $result=  "
347                 <tr>
348                   <td valign='top'>
349                     <h2>".$data['Topic']."</h2>".$data['Description']."
350                   </td>
351                   <td  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\" valign='top'>
352                     <input type='text' name='".$data['post_name']."' value='".$data['Default']."' style='width:300px;'>
353                   </td>
354                 </tr>
355               ";
357     return ($result);
358   }
361   function render_text($data)
362   {
363     /* Ignore text messages, they are normally used for status hints. */
364     return ("");
365   }
368   function render_title($data)
369   {
370     /* Ignore text messages, they are normally used for status hints. */
371     return ("");
372   }
377 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
378 ?>