Code

Apply fix for #4170
[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'] ="";
43         $this->template[$post_name]['Save'] = FALSE;
45         $got_local_description= FALSE;
46         continue;
47       }
49       /* Get type */
50       if (preg_match("/^Type: /", $line)){
51         $type= trim(preg_replace("/^Type: (.*)$/", "\\1", $line));
52         $this->template[$post_name]['Type']= $type;
53         continue;
54       }
56       /* Get default */
57       if (preg_match("/^Default: /", $line)){
58         $this->template[$post_name]['Default']= "";
59         $default= trim(preg_replace("/^Default: (.*)$/", "\\1", $line));
60         $this->template[$post_name]['Default']= $default;
61         continue;
62       }
64       /* Get description */
65       if (!$got_local_description && preg_match("/^Description: /i", $line)){
66         $description= trim(preg_replace("/^Description: (.*)$/i", "\\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: /i", $line)){
82         $description= trim(preg_replace("/^Description-$langcode: (.*)$/i", "\\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']])){
137         $checkbox_key = sprintf("save_%s", $entry['post_name']);
138         if (!isset($_POST[$checkbox_key])) {
139           $this->template[$post_name]['Save'] = FALSE;
140         }else {
141           $this->template[$post_name]['Save'] = TRUE;
142         }
144         /* special handling for arrays */
145         if(is_array($_POST[$entry['post_name']])){
146           $str = "";
147           foreach(get_post($entry['post_name']) as $val){
148             $str.= $val.", ";
149           }
150           $str = preg_replace("/\,\ $/","",$str);
151           $this->template[$post_name]['Default'] = $str;
152         }else{
153           $this->template[$post_name]['Default'] = get_post($entry['post_name']);
154         }
155       }
156     }
157     
158     foreach($this->template as $post_name => $entry){
159       if(isset($_POST["multi-".$entry['post_name']])){ 
160         $this->template[$post_name]['Default']= "";
161         foreach($_POST as $name => $value){
162           if (get_magic_quotes_gpc()) {
163               $value = stripcslashes($value);
164           }
165           if(preg_match("/".$entry['post_name']."-multi-/",$name)){
166             $this->template[$post_name]['Default'] .= $value.", ";
167           }
168         } 
169         $this->template[$post_name]['Default'] = preg_replace("/, $/","",$this->template[$post_name]['Default']);
170       }
171     }
172   }
175   /* This funtion sets the default value and the default save flag*/
176   function SetDefault($var,$val, $save_flag)
177   {
178     if ($this->loaded_template) {
179       foreach($this->template as $key => $tmp){
180         if($tmp['Name'] == $var ){
181           $this->template[$key]['Default'] = $val;
182           $this->template[$key]['Save'] = $save_flag;
183         }
184       }
185     }
186   }
189   /* Display all possible options in html*/
190   function get_dialog()
191   {
192     if ($this->loaded_template) {
193       $result= "<table summary=''>";
195       foreach ($this->template as $post_name => $entry){
197         $types= array("boolean" => "", "multiselect" => "", "note" => "",
198             "password" => "", "select" => "", "string" => "", "text" => "", "title" => "");
200         /* Check if type is available */
201         if ((isset($entry['Type']))&&(isset($types[$entry['Type']]))){
203           /* Produce type specific output */
204           $entry_type = $entry['Type'];
206           $fn= sprintf("render_%s", $entry_type);
208           $str = $this->$fn($entry);
209           if(!empty($str)){
210             $result.=$str."<tr><td colspan='3'><p class='seperator'>&nbsp;</p></td></tr>";
211           }
212         } else {
213           //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
214         }
215       }
217     
218       $result .= "<input type='hidden' post_name='check_post' value='1'></table>";
219       return ($result);
220     } else {
221       return _("This package has no debconf options.");
222     }
223   }
226   function render_checkbox($data) {
227     $postname = $data['post_name'];
228     if (isset($data['Save'])) {
229       $save = $data['Save'];
230     } else {
231       $save = 0;
232     }
234     $checked="";
235     if ($save) {
236       $checked = "checked";
237     }
238     
239     $template = "<img src='images/save.png'><br><input type=checkbox name='save_%s' value='%s' %s>";
240     return(sprintf($template, $postname, $save, $checked));
241   }
243   function render_boolean($data)
244   {
245     $post_name= $data['post_name'];
246     $result = "";
247     $template = "<input type='radio' name=\"%s\" value=\"%s\" %s onChange='javascript:document.getElementsByName(\"save_%s\")[0].checked = true;'>%s";
249     foreach(array("true","false") as $value){
250       if($data['Default'] == $value){
251         $result.= sprintf($template, $data['post_name'], $value, "checked",  $data['post_name'], _($value));
252       }else{
253         $result.= sprintf($template, $data['post_name'], $value, "", $data['post_name'], _($value));
254       }
255       $result.="<br>";
256     }
257     return ($this->render_entry($data, $result));
258   }
261   function render_multiselect($data)
262   {
263     $post_name= $data['post_name'];
264     $input = "";
265        
266     if (preg_match('/\$\{/', $data['Choices'])){
267         $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>';
268         return($this->render_string($data));
269     } else {
270         $choices= "";
271         foreach (split(", ", $data['Choices']) as $choice){
272             $choices[]= $choice;
273         }
274       
275         $input="<input type='hidden' name='multi-".$post_name."' value='1'>";
276         $template = "\n<input name='%s-multi-%s' type='checkbox' value=\"%s\" %s onChange='javascript:document.getElementsByName(\"save_%s\")[0].checked = true;'>%s";
278         $defs = split(", ",$data['Default']);
279         foreach($choices as $value){
280             if(in_array($value,$defs)){
281                 $input .= sprintf($template, $post_name, $value, htmlentities($value), "checked", $post_name, _($value));
282             }else{
283                 $input .= sprintf($template, $post_name, $value, $htmlentities($value), "", $post_name, _($value));
284             }
285         }
287     }    
288     return ($this->render_entry($data, $input));
292   function render_note($data)
293   {
294     /* Ignore notes, they do not makes sense, since we don't get any
295        chance to test entered values... */
296     return ("");
297   }
300   function render_password($data)
301   {
302     $result =  "";
303     $template = "&nbsp;<input type='text' name='%s' value=\"%s\" onChange='javascript:document.getElementsByName(\"save_%s\")[0].checked = true;'><br><br>";
304     $result .= sprintf($template, $data['post_name'], $data['Default'], $data['post_name']);
305     return ($this->render_entry($data, $result));
306   }
309   function render_select($data)
310   {
311     $post_name= $data['post_name'];
312     
313     $result = "";
314     if (preg_match('/\$\{/', $data['Choices'])){
315         return($this->render_multiselect($data));
316     } else {
317         $choices= "";
318         foreach (preg_split("/,\s?/", $data['Choices']) as $choice){
319             $choices[]= $choice;
320         }
322         $template = "\n<input type='radio' name='%s' value=\"%s\" %s onChange='javascript:document.getElementsByName(\"save_%s\")[0].checked = true;'>%s<br>";;
323         foreach($choices as $value){
324             if($data['Default'] == $value){
325                 $result.= sprintf($template, $post_name, htmlentities($value), "checked", $post_name, htmlentities($value));
326             }else{
327                 $result.= sprintf($template, $post_name, htmlentities($value), "", $post_name, htmlentities($value));
328             }
329         }
330     }
332     return ($this->render_entry($data, $result));
333   }
336   function render_string($data)
337   {
338     $template = "<input type='text' name='%s' value=\"%s\" style='width:300px' onChange='javascript:document.getElementsByName(\"save_%s\")[0].checked = true;'>";
339     $result=  sprintf($template, $data['post_name'], $data['Default'], $data['post_name']);
340     return ($this->render_entry($data, $result));
341   }
344   function render_text($data)
345   {
346     /* Ignore text messages, they are normally used for status hints. */
347     return ("");
348   }
351   function render_title($data)
352   {
353     /* Ignore text messages, they are normally used for status hints. */
354     return ("");
355   }
357   function render_entry($data, $input) {
358     $smarty = get_smarty();
359     $smarty->assign('chkbox', $this->render_checkbox($data));
360     $smarty->assign('topic', $data['Topic']);
361     $smarty->assign('description', $data['Description']);
362     $smarty->assign('input', $input);
363     return($smarty->fetch (get_template_path('debconfTemplate_entry.tpl', TRUE)));
364   }
368 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
369 ?>