Code

Modified test1 to use smarty
[gosa.git] / include / class_ObjectListViewport.inc
index 6f070214b3fbb88ffac2a47a7265c7b95c90f96c..477fd41321dc98b6470c9df43fec309c602fcb8d 100644 (file)
@@ -23,16 +23,23 @@ class ObjectListViewportException extends Exception {
 
     \sa ObjectList
  */
-class ObjectListViewport {
+class ObjectListViewport implements GOsaGuiElement {
+
+  /* Internal variable for color alternation */
+  protected $colorAlternator= 0;
 
-  /* Dummy here ----> */
   private $headline;
   private $footer;
   private $entryFormat;
-  private $attributes= array('cn', '_icon', '_actions', 'dn');
 
+  /* Dummy here ----> */
+  private $attributes= array('cn', '_icon', '_actions', 'dn');
   /* <---- Dummy here */
 
+  private $displayHeaderFlag= TRUE;
+  private $displayFooterFlag= TRUE;
+  private $numberOfCols= 0;
+
   /*!
     \brief Container for objects
 
@@ -45,27 +52,38 @@ class ObjectListViewport {
    */
        private $multiselect;
 
+  /*! \brief ID used to identify objects of same list */
+  private $id = "";
 
+  
   /*! \brief ObjectListViewport constructor
 
     The ObjectListViewport class renders/handles the ObjectList defined by $config.
 
     \param config Config section that is used to configure this ObjectListViewport
    */
-       public function __construct($config, $multiselect= TRUE){
+       public function __construct($config, $multiselect= TRUE, $displayHeader= TRUE, $displayFooter= FALSE){
 
-       $this->multiselect= $multiselect;
-    /* Dummy here */
+    /* Transfer initialization values */
+    $this->displayFooterFlag= $displayFooter;
+    $this->displayHeaderFlag= $displayHeader;
+    $this->multiselect= $multiselect;
+
+    /* Load list configuration from ConfigManager */
     $cr= Registry::getInstance("ConfigManager");
     $cr->setSection($config);
-    $this->headline= $this->parseHeadline($cr->getValue("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);
+
+    /* generate an unique id */
+    $this->id= uniqid();
   }
 
+
        /*! \brief Handles _POST / _GET events
 
            Processes the list of registered plugins to do their eventHandler and adapt
@@ -77,37 +95,64 @@ class ObjectListViewport {
   }
 
 
-       /*! \brief Renders headline into a string
+       /*! \brief Processes post events
 
-           Gets the headline description from the ObjectList object and renders it.
+           Processes all post events and acts as needed.
 
-      \return HTML rendered headline
+      \return bool for if changes are present or not
         */
-  private function renderHeadline(){
-    $buffer ="<tr>\n";
-    foreach($this->headline as $key => $value){
-      $buffer .= "<td style='".$value['style']."'>".$value['name']."</td>\n";
-    }
-    $buffer.="</tr>\n";
-    return $buffer;
-  }
+  public function process(){
 
-
-       /*! \brief Renders footer into a string
-
-           Gets the footer description from the ObjectList object and renders it.
-
-      \return HTML rendered footer
-        */
-  private function renderFooter(){
-
-    $buffer ="<tr>\n";
-    $buffer .= "<td colspan='".count($this->headline)."'>".$this->footer."</td>\n";
-    $buffer.="</tr>\n";
-    return $buffer;
+    /* TODO: process input */
+    return FALSE;
   }
 
 
+#/*! \brief Renders headline into a string
+#
+#    Gets the headline description from the ObjectList object and renders it.
+#
+#    \return HTML rendered headline
+# */
+#private function renderHeadline(){
+#  $tpl =" <tr>
+#            <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header_".$this->id."'>
+#              <table class='ObjectListViewport_Header_Table' id='ObjectListViewport_Header_Table_".$this->id."'>
+#                <tr>
+#                  {content}
+#                </tr>
+#              </table>
+#           </td>
+#         </tr>";
+#  $buffer ="";
+#  foreach($this->headline as $key => $value){
+#    $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
+#  }
+#  return(preg_replace("/\{content\}/", $buffer,$tpl));
+#}
+
+
+#/*! \brief Renders footer into a string
+#
+#    Gets the footer description from the ObjectList object and renders it.
+#
+#    \return HTML rendered footer
+# */
+#private function renderFooter(){
+#  $buffer ="<tr>
+#            <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer_".$this->id."'>
+#              <table class='ObjectListViewport_Footer_Table'>
+#                <tr>
+#                  <td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>
+#                </tr>
+#              </table>     
+#            </td>
+#         </tr>";
+#  return $buffer;
+#}
+
+
+  /* TODO: replace this -> not our task */
   private function getEntryIcon($entry,$alt = ""){
     return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
   }
@@ -145,16 +190,40 @@ class ObjectListViewport {
     }
 
     #TODO: Make $buffer a proper HTML table output
-
     $tmp = split("\|",trim($buffer,"|"));  
 
-    $buffer ="<tr>\n";
-    foreach($tmp as $key => $value){
-      $buffer .= "<td class='ObjectListViewportEntry' style='".$this->headline[$key]['style']."'>".$value."</td>\n";
+    /* define entry template */
+    $tpl = "<td class='ObjectListViewport_Entry_Cell' style='{style_1}'>
+              <div style='{style_2}'>
+                {content}
+              </div>
+            </td>
+            ";
+    /* Template vriables to replace */
+    $attrs = array("/\{style_1\}/","/\{style_2\}/","/\{content\}/");
+
+    /* Append template for each given col */
+    $buffer ="";
+    for($i= 0; $i < $this->numberOfCols; $i++){
+
+      /* If current entry is the last to appen, then skip adding styles */
+      if($i == ($this->numberOfCols-1)){
+        $buffer.= preg_replace( $attrs,
+            array($this->headline[$i]['style'],"width:100%;overflow:hidden;",$tmp[$i]),$tpl);
+      }else{
+        $buffer.= preg_replace( $attrs,
+            array($this->headline[$i]['style'],"width:100%;overflow:hidden;".$this->headline[$i]['style'],$tmp[$i]),$tpl);
+      }
+    }
+
+    /* Add class depending on given id, to alternate background colors */
+    if($this->colorAlternator++ & 1){
+      $a = "class='ObjectListViewport_Entry_Row1'";
+    }else{
+      $a = "class='ObjectListViewport_Entry_Row2'";
     }
-    $buffer.="</tr>\n";
 
-    return $buffer."\n";
+    return "<tr ".$a.">\n".$buffer."</tr>\n";
   }
 
 
@@ -178,21 +247,32 @@ class ObjectListViewport {
         */
   public function render() {
 
-    /* Generate fixed headline */
-    $buffer= $this->renderHeadline();
-
     /* Apply current filter */
+    $entries = "";
     $objects= new ObjectListFilterIterator($this->objects->getIterator());
-    
     foreach ($objects as $value){
-      $buffer.= $this->renderEntry($value);
+      $entries .= $this->renderEntry($value);
     }
 
-    /* Generate footer */
-    $buffer.= $this->renderFooter();
+    $smarty = get_smarty();
+    $smarty->assign("OLV_Entries",$entries);
+    $smarty->assign("OLV_List_Id",$this->id);
+
+    /* Footer variables */
+    $smarty->assign("OLV_Footer_Enabled",$this->displayFooterFlag);
+    $smarty->assign("OLV_Footer_Message",$this->footer);
+    $smarty->assign("OLV_Num_Cols",$this->numberOfCols);
 
+    /* Assign Headline values */
+    $smarty->assign("OLV_Header_Enabled",$this->displayHeaderFlag);
+    $smarty->assign("OLV_Header",$this->headline);
+    $buffer ="";
+    foreach($this->headline as $key => $value){
+      $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
+    }
 
-    return ("<table class='ObjectListViewportTable'>".$buffer."</table>");
+    return($smarty->fetch("ObjectListViewport.tpl"));
   }
 
 
@@ -205,57 +285,59 @@ class ObjectListViewport {
     /* 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    = "";
+    $this->headline= array();
+    $this->numberOfCols= 0;
+    $tmp= split("\|", trim($data, "|")); 
+    $cell_formats= array();
+
+    foreach($tmp as $key => $dta){
+
+      $width= "";
+      $alignment= "";
+      $name= preg_replace("/\{[^\}]*+\}/", "", $dta);
+      $style= "";
     
       /* Parse format string and detect width & alignment */
-      if(preg_match("/\{.*\}/",$data)){
-        $s_format=  preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
+      if(preg_match("/\{.*\}/", $dta)){
+        $format= preg_replace("/^[^\{]*+\{([^\}]*).*$/", "\\1", $dta);
     
         /* Get aligment */
-        if(preg_match("/:/",$s_format)){
-          $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
+        if(preg_match("/:/",$format)){
+          $al= preg_replace("/^[^:]*+:([a-z]*).*$/i", "\\1", $format);
 
-          if(preg_match("/T/i",$s_al)){
-            $s_alignment.= "top-"  ;
-            $s_style.= "vertical-align: top;";
+          if(preg_match("/T/i", $al)){
+            $alignment.= "top-";
+            $style.= "vertical-align: top;";
           }
-          if(preg_match("/B/i",$s_al)){
-            $s_alignment.= "bottom-"  ;
-            $s_style.= "vertical-align: bottom;";
+          if(preg_match("/B/i", $al)){
+            $alignment.= "bottom-";
+            $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;";
+          if(preg_match("/R/i", $al)){
+            $alignment.= "right";
+            $style.= "text-align: right;";
+          }elseif(preg_match("/L/i", $al)){
+            $alignment.= "left";
+            $style.= "text-align: left;";
+          }elseif(preg_match("/C/i", $al) || preg_match("/M/i", $al) ){
+            $alignment.= "center";
+            $style.= "text-align: center;";
           }
         }
 
         /* Get width */
-        $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
-        if(!empty($s_width)){
-          $s_style = "width: ".$s_width.";";
+        $width = preg_replace("/^([^:]*).*$/","\\1", $format);
+        if(!empty($width)){
+          $style.= "width: ".$width.";";
         }
-        
-        $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
       }
+
+      $cell_formats[$key]= array("name" => $name, "width" => $width, "alignment" => $alignment, "style" => $style);
+      $this->numberOfCols++;
     }
-    return($cell_formats);
+    $this->headline= $cell_formats;
   }
 
-
-  
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: