Code

Branch for testing
[gosa.git] / include / class_debconfTemplate.inc
index 6e96574c645e3da7c1302d6f2e92d558235fd1db..d781321ec8daba9f701e18fa561ff5738a966e41 100644 (file)
@@ -4,8 +4,8 @@ class debconf
 {
   var $package= "";
   var $language= "";
-  var $has_template= FALSE;
-  var $template_directory= "/var/lib/dpkg/info";
+  var $loaded_template= FALSE;
+  var $template_directory= "";
   var $template= array();
 
 
@@ -19,7 +19,6 @@ class debconf
   function set_package($package)
   {
     $this->package= $package;
-    return ($this->load());
   }
 
 
@@ -43,24 +42,30 @@ class debconf
 
   function load()
   {
-    /* Reject requests, if parameters are not set */
-    if ($this->package == "" || $this->template_directory == ""){
-      return (FALSE);
-    }
-
-    /* Try to load package based template file */
-    $filename= preg_replace("/\/+/", "/", $this->template_directory."/".$this->package.".templates");
-    if (is_file($filename) && is_readable($filename)){
+    if( TRUE === $this->has_template() ) {
+    
+      /* Try to load package based template file */
       $this->template= array();
 
       /* Read template array */
-      $post_name= 0;
-      $langcode= $this->language.".UTF-8";
-      $in_description= FALSE;
-      $got_local_description= FALSE;
+      $post_name             = 0;
+      $langcode              = $this->language.".UTF-8";
+      $in_description        = FALSE;
+      $got_local_description = FALSE;
+
+      /* get filename */
+      $filename= preg_replace("/\/+/", "/", $this->template_directory."/".$this->package.".templates");
+
+      /* Check if file is readable */
+      if (!is_file($filename) || !is_readable($filename)){
+        return(FALSE);
+      }
+
+      /* Open file and read content line by line */
       $fh= fopen($filename, 'r');
 
-      while (!feof($fh)){
+      /* While the file handle is valid && there is still data to read -> parse configuration file */
+      while ($fh && !feof($fh)){
         $line= fgets($fh, 1024);
 
         /* Reset description flag */
@@ -71,9 +76,7 @@ class debconf
         /* Template header */
         if (preg_match("/^Template: /", $line)){
           $post_name ++; 
-  
           $name= trim(preg_replace("/^Template: (.*)$/", "\\1", $line));
-          
           $this->template[$post_name]['Name'] = $name;
           $this->template[$post_name]['Default'] ="";
          
@@ -145,7 +148,7 @@ class debconf
       }
 
       fclose($fh);
-      $this->has_template= TRUE;
+      $this->loaded_template= TRUE;
       
       $tmp= array();
       foreach($this->template as $post_name => $template){
@@ -157,21 +160,32 @@ class debconf
       return (TRUE);
     }
 
-    $this->has_template= FALSE;
+    $this->loaded_template= FALSE;
     return (FALSE);
   }
 
 
   function has_template()
   {
-    return ($this->has_template);
+    /* Reject requests, if parameters are not set */
+    if ($this->package == "" || $this->template_directory == ""){
+      return (FALSE);
+    }
+    $filename= preg_replace("/\/+/", "/", $this->template_directory."/".$this->package.".templates");
+    return (is_file($filename) && is_readable($filename));
   }
 
 
+  /* Check if some fields are posted */
   function PostCheck()
   {
+    /* Walk through all template variables */
     foreach($this->template as $post_name => $entry){
+
+      /* Check if this var is set*/
       if(isset($_POST[$entry['post_name']])){
+
+        /* special handling for arrays */
         if(is_array($_POST[$entry['post_name']])){
           $str = "";
           foreach($_POST[$entry['post_name']] as $val){
@@ -184,12 +198,27 @@ class debconf
         }
       }
     }
-  
+    
+    foreach($this->template as $post_name => $entry){
+      if(isset($_POST["multi-".$entry['post_name']])){ 
+        $this->template[$post_name]['Default']= "";
+        foreach($_POST as $name => $value){
+          if(preg_match("/".$entry['post_name']."-multi-/",$name)){
+            $this->template[$post_name]['Default'] .= $value.", ";
+          }
+        } 
+        $this->template[$post_name]['Default'] = preg_replace("/, $/","",$this->template[$post_name]['Default']);
+      }
+    }
+
+
   }
 
+
+  /* This funtion sets the defualt value */
   function SetDefault($var,$val)
   {
-    if($this->has_template){
+    if ($this->loaded_template) {
       foreach($this->template as $key => $tmp){
         if($tmp['Name'] == $var ){
           $this->template[$key]['Default'] = $val;
@@ -198,10 +227,12 @@ class debconf
     }
   }
 
+
+  /* Display all possible options in html*/
   function get_dialog()
   {
-    if ($this->has_template){
-      $result= "";
+    if ($this->loaded_template) {
+      $result= "<table summary=''>";
 
       foreach ($this->template as $post_name => $entry){
 
@@ -213,13 +244,17 @@ class debconf
 
           /* Produce type specific output */
           $fn= "render_".$entry['Type'];
-          $result.= $this->$fn($entry);
+          $str = $this->$fn($entry);
+          if(!empty($str)){
+            $result.=$str."<tr><td colspan='2'><p class='seperator'>&nbsp;</p></td></tr>";
+          }
         } else {
           //php_error(E_WARNING, "An unknown type has been specified in the debconf template. Please fix.");
         }
       }
 
-      $result .= "<input type='hidden' post_name='check_post' value='1'>";
+    
+      $result .= "<input type='hidden' post_name='check_post' value='1'></table>";
       return ($result);
     } else {
       return _("This package has no debconf options.");
@@ -232,31 +267,26 @@ class debconf
 
     $post_name= $data['post_name'];
     $result="
-      <table width='100%' border=0>
       <tr>
-      <td valign='top'>
-      <b>".$data['Topic']."</b>
+      <td valign='top' style='width:100%'>
+      <h2>".$data['Topic']."</h2>".$data['Description']."
       </td>
-      <td width='10%' valign='top'>";
+      <td style=\"white-space:nowrap; vertical-align:top; border-left: 1px solid rgb(160, 160, 160);\">";
 
     foreach(array("true","false") as $value){
       if($data['Default'] == $value){
-        $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' checked>".$value."</option>";
+        $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' checked>"._($value);
       }else{
-        $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' >".$value."</option>";
+        $result.="<input type='radio' name='".$data['post_name']."' value='".$value."' >"._($value);
       }
+      $result.="<br>";
     }
 
     $result.= "
       </td>
       </tr>
-      <tr>
-      <td colspan=2>".$data['Description']."
-      </td>
-      </tr>
-      </table>";
+      ";
 
-          $result.= "<p class='seperator'>&nbsp;</p>";
     return ($result);
   }
 
@@ -265,44 +295,36 @@ class debconf
   {
     $post_name= $data['post_name'];
     if (preg_match('/\$\{/', $data['Choices'])){
-      $choices= array("Need to use some text...");
+      $result= $this->render_string($data);
     } else {
       $choices= "";
       foreach (split(", ", $data['Choices']) as $choice){
         $choices[]= $choice;
       }
-    }
 
 
-    $result="
-      <table width='100%' border=0>
-      <tr>
-      <td valign='top'>
-      <b>".$data['Topic']."</b>
-      </td>
-      <td width='10%' valign='top'>
-      <select name='".$post_name."[]'  multiple size=5>";
-
-    $defs = split(", ",$data['Default']);
-  
-    foreach($choices as $value){
-      if(in_array($value,$defs)){
-        $result.="\n<option value='".htmlentities($value)."' selected>".htmlentities($value)."</option>";
-      }else{
-        $result.="\n<option value='".htmlentities($value)."'>".htmlentities($value)."</option>";
+      $result="
+        <tr>
+        <td valign='top'>
+        <h2>".$data['Topic']."</h2>".$data['Description']."
+        </td>
+        <td valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
+          <input type='hidden' name='multi-".$post_name."' value='1'>
+        ";
+        
+      $defs = split(", ",$data['Default']);
+      foreach($choices as $value){
+        if(in_array($value,$defs)){
+          $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."' checked>".$value."<br>";
+        }else{
+          $result.="\n<input name='".$post_name."-multi-".$value."' type='checkbox' value='".htmlentities($value)."'>".$value."<br>";
+        }
       }
-    }
 
-    $result.= "
-      </select>
-      </td>
-      </tr>
-      <tr>
-      <td colspan=2>".$data['Description']."
-      </td>
-      </tr>
-      </table>";
-          $result.= "<p class='seperator'>&nbsp;</p>";
+    $result .=    "</td>
+        </tr>
+        ";
+    }    
 
     return ($result);
   }
@@ -318,12 +340,11 @@ class debconf
 
   function render_password($data)
   {
-    $result=  "<table width='100%' border=0>";
+    $result=  "";
     $result.= "<tr><td valign='top'>";
-    $result.= "<b>".$data['Topic']."&nbsp;<input type='text' name='".$data['post_name']."' value='".$data['Default']."'></b><br><br>";
+    $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>";
     $result.= $data['Description'];
-    $result.= "</td></table>";
-          $result.= "<p class='seperator'>&nbsp;</p>";
+    $result.= "</td>";
 
     return ($result);
   }
@@ -344,32 +365,27 @@ class debconf
 
 
     $result="
-      <table width='100%' border=0>
+      
       <tr>
       <td valign='top'>
-      <b>".$data['Topic']."</b>
+      <h2>".$data['Topic']."</h2>".$data['Description']."
       </td>
-      <td width='10%' valign='top'>
-      <select name='".$post_name."'>";
+      <td  valign='top'  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\">
+      ";
 
     foreach($choices as $value){
       if($data['Default'] == $value){
-        $result.="\n<option value='".htmlentities($value)."' selected>".htmlentities($value)."</option>";
+        $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."' checked >".htmlentities($value)."<br>";
       }else{
-        $result.="\n<option value='".htmlentities($value)."'>".htmlentities($value)."</option>";
+        $result.="\n<input type='radio' name='".$post_name."' value='".htmlentities($value)."'>".htmlentities($value)."<br>";
       }
     }
 
     $result.= "
-      </select>
-      </td>
-      </tr>
-      <tr>
-      <td colspan=2>".$data['Description']."
+      
       </td>
       </tr>
-      </table>";
-          $result.= "<p class='seperator'>&nbsp;</p>";
+      ";
 
     return ($result);
   }
@@ -377,22 +393,16 @@ class debconf
 
   function render_string($data)
   {
-    $result=  "<table width='100%' border=0>
+    $result=  "
                 <tr>
                   <td valign='top'>
-                    <b>".$data['Topic']."</b>&nbsp;
+                    <h2>".$data['Topic']."</h2>".$data['Description']."
                   </td>
-                  <td width='10%'>
-                    <input type='text' name='".$data['post_name']."' value='".$data['Default']."'>
+                  <td  style=\"white-space:nowrap; border-left: 1px solid rgb(160, 160, 160);\" valign='top'>
+                    <input type='text' name='".$data['post_name']."' value='".$data['Default']."' style='width:300px;'>
                   </td>
                 </tr>
-                <tr>
-                  <td colspan='2'>
-                      ".$data['Description']."
-                  </td>
-                </tr>
-              </td></table>";
-          $result.= "<p class='seperator'>&nbsp;</p>";
+              ";
 
     return ($result);
   }
@@ -411,22 +421,8 @@ class debconf
     return ("");
   }
 
-
-  function get_template_packages()
-  {
-  }
-
-
-  function set_default($default)
-  {
-  }
-
-
 }
 
-# Example:
-#$debconf= new debconf("libnss-ldap", "de");
-#echo $debconf->get_dialog();
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>