Code

Allow disabling of footer and header part
[gosa.git] / include / class_ObjectListViewport.inc
1 <?php
3 /*! \brief   Exception implementation for ObjectListViewport
4     \author  Cajus Pollmeier <pollmeier@gonicus.de>
5     \version 1.00
6     \date    2007/11/02
8     This class handles the exceptions occuring in ObjectListViewport.
9  */
10 class ObjectListViewportException extends Exception {
11         public function __construct($message, $code = 0) {
12                 parent::__construct($message, $code);
13         }
14 }
15  
17 /*! \brief   Implementation for ObjectListViewport
18     \author  Cajus Pollmeier <pollmeier@gonicus.de>
19     \version 1.00
20     \date    2007/11/02
22     This class handles painting of ObjectList objects.
24     \sa ObjectList
25  */
26 class ObjectListViewport {
28   /* Dummy here ----> */
29   private $headline;
30   private $footer;
31   private $entryFormat;
32   private $attributes= array('cn', '_icon', '_actions', 'dn');
33   
34   private $b_displayHeader  = TRUE;
35   private $b_displayFooter    = TRUE;
36   /* <---- Dummy here */
38   /*!
39     \brief Container for objects
41     This variable stores the ObjectList object to be displayed.
42    */
43         private $objects;
45   /*!
46     \brief Switch to handle multiselect or not
47    */
48         private $multiselect;
50   /*! \brief ID used to identify objects of same list */
51   private $id = "";
52   
53   /*! \brief ObjectListViewport constructor
55     The ObjectListViewport class renders/handles the ObjectList defined by $config.
57     \param config Config section that is used to configure this ObjectListViewport
58    */
59         public function __construct($config, $multiselect= TRUE){
61         $this->multiselect= $multiselect;
62     /* Dummy here */
63     $cr= Registry::getInstance("ConfigManager");
64     $cr->setSection($config);
65     $this->headline= $this->parseHeadline($cr->getValue("headline"));
66     $this->footer= $cr->getValue("footer");
67     $this->entryFormat= $cr->getValue("entryFormat");
69     /* Load and instanciate classes, extract filter, icons, view hooks, etc. */
70     $this->objects= new ObjectList($config);
72     /* generate an unique id */
73     $this->id = preg_replace("/[^0-9]/","",microtime());
74   }
76         /*! \brief Handles _POST / _GET events
78             Processes the list of registered plugins to do their eventHandler and adapt
79       internal objectlist according to this.
80          */
81   public function eventHandler(){
82     /* Reloads the list if things have changed interally */
83     $this->objects->reload();
84   }
87         /*! \brief Renders headline into a string
89             Gets the headline description from the ObjectList object and renders it.
91       \return HTML rendered headline
92          */
93   private function renderHeadline(){
94     $buffer ="<table class='ObjectListViewport_Header_Table'>\n";
95     $buffer.="<tr>\n";
96     foreach($this->headline as $key => $value){
97       $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
98     }
99     $buffer.="<td class='ObjectListViewport_Header_Cell' style='border-right:0px;width:14px;'>&nbsp;</td>";
100     $buffer.="</tr>\n";
101     $buffer.="</table>\n";
102     return $buffer;
103   }
106         /*! \brief Renders footer into a string
108             Gets the footer description from the ObjectList object and renders it.
110       \return HTML rendered footer
111          */
112   private function renderFooter(){
113     $buffer ="<table class='ObjectListViewport_Footer_Table'>\n"; 
114     $buffer.="<tr>\n";
115     $buffer.= "<td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>\n";
116     $buffer.="</tr>\n";
117     $buffer.="</table>\n";
118     return $buffer;
119   }
122   private function getEntryIcon($entry,$alt = ""){
123     return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
124   }
127         /*! \brief Renders entries from the ObjectList iterator into a string
128             Gets the entry descriptions from the ObjectList object and renders them.
129       \return HTML rendered list entries
130          */
131   private function renderEntry($entry){
133     /* Copy template */
134     $buffer= $this->entryFormat;
136     /* Replace set of attributes */
137     foreach ($this->attributes as $attribute){
138       if (!isset($entry[$attribute])){
139         throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
140       } else {
142         if(preg_match("/_icon/i",$attribute)){
143           $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer); 
144         }else{
145           $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
146         }
147       }
148     }
150     /* Execute optional filters */
151     preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
152     foreach ($matches as $match){
153       $filterName= preg_replace('/,.+$/', '', $match[1]);
154       $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
155       $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/', $this->applyEntryFilter($filterName, $filterParameter), $buffer);
156     }
158     #TODO: Make $buffer a proper HTML table output
160     $tmp = split("\|",trim($buffer,"|"));  
163     $buffer="<tr>\n";
164     foreach($tmp as $key => $value){
165         $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
166           "<div style='width:100%;overflow:hidden;".$this->headline[$key]['style']."'>".
167             $value.
168           "</div>".
169           "</td>\n";
170     }
171     $buffer.="</tr>\n";
173     return $buffer."\n";
174   }
177         /*! \brief Applies filter to the given entry format string.
179             Instanciates the given ObjectListEntryFilter and calls the method.
181       \return rendered output
182       \sa ObjectListEntryFilter
183          */
184   private function applyEntryFilter($filterName, $string){
185     $className= "ObjectListEntryFilter_".$filterName;
186     $cl= new $className;
187     return $cl->filter("$string");
188   }
191         /*! \brief Renders complete ObjectList into a string
193       \return HTML rendered list
194          */
195   public function render() {
197     $header = $footer = "";
198     if($this->b_displayHeader){
199       $header ="<tr>
200                   <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header".$this->id."'>
201                     ".$this->renderHeadline()."
202                   </td>
203                 </tr>";
204     }
205     if($this->b_displayFooter){
206       $footer ="<tr>
207                   <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer".$this->id."'>
208                     ".$this->renderFooter()."
209                   </td>
210                 </tr>";
211     }
213     /* Apply current filter */
214     $entries = "";
215     $objects= new ObjectListFilterIterator($this->objects->getIterator());
216     foreach ($objects as $value){
217       $entries .= $this->renderEntry($value);
218     }
220     /* Generate fixed headline */
221     $buffer = "
222     <table class='ObjectListViewport' id='ObjectListViewport".$this->id."' cellspacing=0 cellpadding=0>
223       <tr>
224         <td>
225               <table class='ObjectListViewport_Table' id='ObjectListViewport_Table".$this->id."' cellpadding=0 cellspacing=0 >
226                 ".$header."
227                 <tr>
228                   <td class='ObjectListViewport_TD_Entries' id='ObjectListViewport_TD_Entries".$this->id."'>
229                     <div class='ObjectListViewport_Entry_Cover' id='ObjectListViewport_Entry_Cover".$this->id."'> 
230                       <table class='ObjectListViewport_Entry_Table' id='ObjectListViewport_Entry_Table".$this->id."'>
231                         ".$entries."
232                       </table> 
233                     </div>
234                   </td>
235                 </tr>
236                 ".$footer."
237               </table>
239         </td>
240       </tr>
241     </table>
242 ";
244     return ($buffer);
245   }
248         /*! \brief Parses the given headline format string 
250       \return Array with cell properties (width, alignment,name)
251          */
252   private function parseHeadline($data)
253   {
254     /* Each cell definition is seperated by | 
255      *  split by and go through each definition
256      */
257     $tmp = split("\|",trim($data,"|")); 
258     $cell_formats = array();
259     foreach($tmp as $key => $data){
261       $s_width    = "";
262       $s_alignment= "";
263       $s_name     = preg_replace("/\{[^\}]*+\}/","",$data);
264       $s_style    = "";
265     
266       /* Parse format string and detect width & alignment */
267       if(preg_match("/\{.*\}/",$data)){
268         $s_format=  preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
269     
270         /* Get aligment */
271         if(preg_match("/:/",$s_format)){
272           $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
274           if(preg_match("/T/i",$s_al)){
275             $s_alignment.= "top-"  ;
276             $s_style.= "vertical-align: top;";
277           }
278           if(preg_match("/B/i",$s_al)){
279             $s_alignment.= "bottom-"  ;
280             $s_style.= "vertical-align: bottom;";
281           }
282           if(preg_match("/R/i",$s_al)){
283             $s_alignment.= "right"  ;
284             $s_style.= "text-align: right;";
285           }elseif(preg_match("/L/i",$s_al)){
286             $s_alignment.= "left"  ;
287             $s_style.= "text-align: left;";
288           }elseif(preg_match("/C/i",$s_al) || preg_match("/M/i",$s_al) ){
289             $s_alignment.= "center"  ;
290             $s_style.= "text-align: center;";
291           }
292         }
294         /* Get width */
295         $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
296         if(!empty($s_width)){
297           $s_style.= "width: ".$s_width.";";
298         }
299       }
300       $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
301     }
302     return($cell_formats);
303   }
306   public function enableFooter($bool = TRUE){
307     $this->b_displayFooter = $bool;
308   }
309   public function enableHeader($bool = TRUE){
310     $this->b_displayHeader = $bool;
311   }
312   
315 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
316 ?>