Code

Some changes
[gosa.git] / include / class_ObjectListViewport.inc
index e7ed7911e2cce8d63cf8eb38faafed981b17790b..7d51f7b8b5f74390aa364b526a8fd5c95bae221b 100644 (file)
@@ -25,6 +25,14 @@ class ObjectListViewportException extends Exception {
  */
 class ObjectListViewport {
 
+  /* Dummy here ----> */
+  private $headline;
+  private $footer;
+  private $entryFormat;
+  private $attributes= array('cn', '_icon', '_actions', 'dn');
+
+  /* <---- Dummy here */
+
   /*!
     \brief Container for objects
 
@@ -32,6 +40,11 @@ class ObjectListViewport {
    */
        private $objects;
 
+  /*!
+    \brief Switch to handle multiselect or not
+   */
+       private $multiselect;
+
 
   /*! \brief ObjectListViewport constructor
 
@@ -39,7 +52,15 @@ class ObjectListViewport {
 
     \param config Config section that is used to configure this ObjectListViewport
    */
-       public function __construct($config){
+       public function __construct($config, $multiselect= TRUE){
+
+       $this->multiselect= $multiselect;
+    /* Dummy here */
+    $cr= Registry::getInstance("ConfigManager");
+    $cr->setSection($config);
+    $this->headline= $this->parseHeadline($cr->getValue("headline"));
+    $this->footer= $cr->getValue("footer");
+    $this->entryFormat= $cr->getValue("entryFormat");
 
     /* Load and instanciate classes, extract filter, icons, view hooks, etc. */
     $this->objects= new ObjectList($config);
@@ -63,7 +84,15 @@ class ObjectListViewport {
       \return HTML rendered headline
         */
   private function renderHeadline(){
-    return $this->objects->getHeadline()."\n";
+    $buffer ="<table class='ObjectListViewport_Headline_Table'>\n";
+    $buffer.="<tr>\n";
+    foreach($this->headline as $key => $value){
+      $buffer .= "<td class='ObjectListViewport_Headline_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
+    }
+    $buffer.="<td style='width:12px;'>&nbsp;</td>";
+    $buffer.="</tr>\n";
+    $buffer.="</table>\n";
+    return $buffer;
   }
 
 
@@ -74,24 +103,88 @@ class ObjectListViewport {
       \return HTML rendered footer
         */
   private function renderFooter(){
-    return $this->objects->getFooter()."\n";
+    $buffer ="<table class='ObjectListViewport_Footer_Table'>\n"; 
+    $buffer.="<tr>\n";
+    $buffer.= "<td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>\n";
+    $buffer.="</tr>\n";
+    $buffer.="</table>\n";
+    return $buffer;
   }
 
 
-       /*! \brief Renders entries from the ObjectList iterator into a string
+  private function getEntryIcon($entry,$alt = ""){
+    return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
+  }
 
-           Gets the entry descriptions from the ObjectList object and renders them.
 
+       /*! \brief Renders entries from the ObjectList iterator into a string
+           Gets the entry descriptions from the ObjectList object and renders them.
       \return HTML rendered list entries
         */
   private function renderEntry($entry){
-    $buffer= "|";
-    foreach ($entry as $column){
-      $buffer.= "$column|";
+
+    /* Copy template */
+    $buffer= $this->entryFormat;
+
+    /* Replace set of attributes */
+    foreach ($this->attributes as $attribute){
+      if (!isset($entry[$attribute])){
+        throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
+      } else {
+
+        if(preg_match("/_icon/i",$attribute)){
+          $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer); 
+        }else{
+          $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
+        }
+      }
     }
-    $buffer.= "\n";
 
-    return $buffer;
+    /* Execute optional filters */
+    preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
+    foreach ($matches as $match){
+      $filterName= preg_replace('/,.+$/', '', $match[1]);
+      $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
+      $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/', $this->applyEntryFilter($filterName, $filterParameter), $buffer);
+    }
+
+    #TODO: Make $buffer a proper HTML table output
+
+    $tmp = split("\|",trim($buffer,"|"));  
+
+
+    $buffer="<tr>\n";
+    foreach($tmp as $key => $value){
+
+      if(empty($this->headline[$key]['width']) || preg_match("/\%/",$this->headline[$key]['width'])){
+        $buffer .= "<td nowrap class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
+          "<div style='overflow:hidden;width:100%;'>".
+          $value.
+          "</div>".
+          "</td>\n";
+      }else{
+        $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
+          $value.
+          "</td>\n";
+      }
+    }
+    $buffer.="</tr>\n";
+
+    return $buffer."\n";
+  }
+
+
+       /*! \brief Applies filter to the given entry format string.
+
+           Instanciates the given ObjectListEntryFilter and calls the method.
+
+      \return rendered output
+      \sa ObjectListEntryFilter
+        */
+  private function applyEntryFilter($filterName, $string){
+    $className= "ObjectListEntryFilter_".$filterName;
+    $cl= new $className;
+    return $cl->filter("$string");
   }
 
 
@@ -100,13 +193,20 @@ class ObjectListViewport {
       \return HTML rendered list
         */
   public function render() {
+
     /* Generate fixed headline */
     $buffer= $this->renderHeadline();
 
-    /* Generate scrollable contents */
-    foreach ($this->objects as $value){
-      $buffer.= $this->renderEntry($value);
+    /* Apply current filter */
+    $objects= new ObjectListFilterIterator($this->objects->getIterator());
+    
+    $buffer.="<table><tr><td>
+              <table class='ObjectListViewport_Entry_Table'>\n"; 
+    foreach ($objects as $value){
+      $buffer.= "<br>";//$this->renderEntry($value);
     }
+    $buffer.="</table>
+              </td></tr></table>\n"; 
 
     /* Generate footer */
     $buffer.= $this->renderFooter();
@@ -114,6 +214,67 @@ class ObjectListViewport {
     return ($buffer);
   }
 
+
+       /*! \brief Parses the given headline format string 
+
+      \return Array with cell properties (width, alignment,name)
+        */
+  private function parseHeadline($data)
+  {
+    /* Each cell definition is seperated by | 
+     *  split by and go through each definition
+     */
+    $tmp = split("\|",trim($data,"|"));  
+    $cell_formats = array();
+    foreach($tmp as $key => $data){
+
+      $s_width    = "";
+      $s_alignment= "";
+      $s_name     = preg_replace("/\{[^\}]*+\}/","",$data);
+      $s_style    = "";
+    
+      /* Parse format string and detect width & alignment */
+      if(preg_match("/\{.*\}/",$data)){
+        $s_format=  preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
+    
+        /* Get aligment */
+        if(preg_match("/:/",$s_format)){
+          $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
+
+          if(preg_match("/T/i",$s_al)){
+            $s_alignment.= "top-"  ;
+            $s_style.= "vertical-align: top;";
+          }
+          if(preg_match("/B/i",$s_al)){
+            $s_alignment.= "bottom-"  ;
+            $s_style.= "vertical-align: bottom;";
+          }
+          if(preg_match("/R/i",$s_al)){
+            $s_alignment.= "right"  ;
+            $s_style.= "text-align: right;";
+          }elseif(preg_match("/L/i",$s_al)){
+            $s_alignment.= "left"  ;
+            $s_style.= "text-align: left;";
+          }elseif(preg_match("/C/i",$s_al) || preg_match("/M/i",$s_al) ){
+            $s_alignment.= "center"  ;
+            $s_style.= "text-align: center;";
+          }
+        }
+
+        /* Get width */
+        $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
+        if(!empty($s_width)){
+          $s_style = "width: ".$s_width.";";
+        }
+        
+        $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
+      }
+    }
+    return($cell_formats);
+  }
+
+
+  
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: