Code

list base image migration for gosa-plugins
[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                 = 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: /", $line)){
65         $this->template[$post_name]['Description']= "";
66         $description= trim(preg_replace("/^Description: (.*)$/", "\\1", $line));
67         $this->template[$post_name]['Topic']= $description;
68         $this->template[$post_name]['Description']= "";
69         $in_description= TRUE;
70         continue;
71       }
73       /* Fill description */
74       if (!$got_local_description && $in_description){
75         $description= preg_replace("/^ (.*)$/", "\\1", $line);
76         $this->template[$post_name]['Description'].= $description;
77         continue;
78       }
80       /* Get local description */
81       if (preg_match("/^Description-$langcode: /", $line)){
82         $description= trim(preg_replace("/^Description-$langcode: (.*)$/", "\\1", $line));
83         $this->template[$post_name]['Topic']= $description;
84         $in_description= TRUE;
85         $got_local_description= TRUE;
86         $this->template[$post_name]['Description']= "";
87         continue;
88       }
90       /* Fill local description */
91       if ($got_local_description && $in_description){
92         $description= preg_replace("/^ (.*)$/", "\\1", $line);
93         $this->template[$post_name]['Description'].= $description;
94         continue;
95       }
97       /* Get native choices */
98       if (preg_match("/^Choices: /", $line)){
99         $type= trim(preg_replace("/^Choices: (.*)$/", "\\1", $line));
100         $this->template[$post_name]['Choices']= $type;
101       }
103       /* Get local choices */
104       if (preg_match("/^Choices-$langcode: /", $line)){
105         $type= trim(preg_replace("/^Choices-$langcode: (.*)$/", "\\1", $line));
106         $this->template[$post_name]['Choices-local']= $type;
107       }
109     }
111     $this->loaded_template= TRUE;
113     $tmp= array();
114     foreach($this->template as $post_name => $template){
115       $template['post_name'] = "post_".$post_name;
116       $tmp[] = $template;
117     }
118     $this->template = $tmp;
120     return (TRUE);
121   }
123   function has_template()
124   {
125     return(count($this->template) != FALSE);
126   }
129   /* Check if some fields are posted */
130   function PostCheck()
131   {
132     /* Walk through all template variables */
133     foreach($this->template as $post_name => $entry){
135       /* Check if this var is set*/
136       if(isset($_POST[$entry['post_name']])){
138         /* special handling for arrays */
139         if(is_array($_POST[$entry['post_name']])){
140           $str = "";
141           foreach($_POST[$entry['post_name']] as $val){
142             $str.= $val.", ";
143           }
144           $str = preg_replace("/\,\ $/","",$str);
145           $this->template[$post_name]['Default'] = $str;
146         }else{
147           $this->template[$post_name]['Default'] = $_POST[$entry['post_name']];
148         }
149       }
150     }
151     
152     foreach($this->template as $post_name => $entry){
153       if(isset($_POST["multi-".$entry['post_name']])){ 
154         $this->template[$post_name]['Default']= "";
155         foreach($_POST as $name => $value){
156           if(preg_match("/".$entry['post_name']."-multi-/",$name)){
157             $this->template[$post_name]['Default'] .= $value.", ";
158           }
159         } 
160         $this->template[$post_name]['Default'] = preg_replace("/, $/","",$this->template[$post_name]['Default']);
161       }
162     }
163   }
166   /* This funtion sets the defualt value */
167   function SetDefault($var,$val)
168   {
169     if ($this->loaded_template) {
170       foreach($this->template as $key => $tmp){
171         if($tmp['Name'] == $var ){
172           $this->template[$key]['Default'] = $val;
173         }
174       }
175     }
176   }
179   /* Display all possible options in html*/
180   function get_dialog()
181   {
182     if ($this->loaded_template) {
183       $result= "<table summary=''>";
185       foreach ($this->template as $post_name => $entry){
187         $types= array("boolean" => "", "multiselect" => "", "note" => "",
188             "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
190         /* Check if type is available */
191         if ((isset($entry['Type']))&&(isset($types[$entry['Type']]))){
193           /* Produce type specific output */
194           $fn= "render_".$entry['Type'];
195           $str = $this->$fn($entry);
196           if(!empty($str)){
197             $result.=$str."<tr><td colspan='2'><p class='seperator'>&nbsp;</p></td></tr>";
198           }
199         } else {
200           //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
201         }
202       }
204     
205       $result .= "<input type='hidden' post_name='check_post' value='1'></table>";
206       return ($result);
207     } else {
208       return _("This package has no debconf options.");
209     }
210   }
213   function render_boolean($data)
214   {
216     $post_name= $data['post_name'];
217     $result="
218       <tr>
219       <td valign='top' style='width:100%'>
220       <h2>".$data['Topic']."</h2>".$data['Description']."
221       </td>
222       <td style=\"white-space:nowrap; vertical-align:top; border-left: 1px solid rgb(160, 160, 160);\">";
224     foreach(array("true","false") as $value){
225       if($data['Default'] == $value){
226         $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' checked>"._($value);
227       }else{
228         $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' >"._($value);
229       }
230       $result.="<br>";
231     }
233     $result.= "
234       </td>
235       </tr>
236       ";
238     return ($result);
239   }
242   function render_multiselect($data)
243   {
244     $post_name= $data['post_name'];
245     if (preg_match('/\$\{/', $data['Choices'])){
246       $result= $this->render_string($data);
247     } else {
248       $choices= "";
249       foreach (split(", ", $data['Choices']) as $choice){
250         $choices[]= $choice;
251       }
254       $result="
255         <tr>
256         <td valign='top'>
257         <h2>".$data['Topic']."</h2>".$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 = split(", ",$data['Default']);
264       foreach($choices as $value){
265         if(in_array($value,$defs)){
266           $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."' checked>".$value."<br>";
267         }else{
268           $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($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.= "<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>";
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       $choices= array("Need to use some text...");
307     } else {
308       $choices= "";
309       foreach (split(", ", $data['Choices']) as $choice){
310         $choices[]= $choice;
311       }
312     }
315     $result="
316       
317       <tr>
318       <td valign='top'>
319       <h2>".$data['Topic']."</h2>".$data['Description']."
320       </td>
321       <td  valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
322       ";
324     foreach($choices as $value){
325       if($data['Default'] == $value){
326         $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."' checked >".htmlentities($value)."<br>";
327       }else{
328         $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."'>".htmlentities($value)."<br>";
329       }
330     }
332     $result.= "
333       
334       </td>
335       </tr>
336       ";
338     return ($result);
339   }
342   function render_string($data)
343   {
344     $result=  "
345                 <tr>
346                   <td valign='top'>
347                     <h2>".$data['Topic']."</h2>".$data['Description']."
348                   </td>
349                   <td  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\" valign='top'>
350                     <input type='text' name='".$data['post_name']."' value='".$data['Default']."' style='width:300px;'>
351                   </td>
352                 </tr>
353               ";
355     return ($result);
356   }
359   function render_text($data)
360   {
361     /* Ignore text messages, they are normally used for status hints. */
362     return ("");
363   }
366   function render_title($data)
367   {
368     /* Ignore text messages, they are normally used for status hints. */
369     return ("");
370   }
375 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
376 ?>