Code

9f3aec6e795145ea0b18d229af6b24231b71345d
[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   /* <---- Dummy here */
35   private $b_displayHeader    = TRUE;
36   private $b_displayFooter    = TRUE;
37   private $i_numberOfCols     = 0;
39   /*!
40     \brief Container for objects
42     This variable stores the ObjectList object to be displayed.
43    */
44         private $objects;
46   /*!
47     \brief Switch to handle multiselect or not
48    */
49         private $multiselect;
51   /*! \brief ID used to identify objects of same list */
52   private $id = "";
53   
54   /*! \brief ObjectListViewport constructor
56     The ObjectListViewport class renders/handles the ObjectList defined by $config.
58     \param config Config section that is used to configure this ObjectListViewport
59    */
60         public function __construct($config, $multiselect= TRUE){
62         $this->multiselect= $multiselect;
63     /* Dummy here */
64     $cr= Registry::getInstance("ConfigManager");
65     $cr->setSection($config);
66     $this->parseHeadline($cr->getValue("headline"));
67     $this->footer= $cr->getValue("footer");
68     $this->entryFormat= $cr->getValue("entryFormat");
70     /* Load and instanciate classes, extract filter, icons, view hooks, etc. */
71     $this->objects= new ObjectList($config);
73     /* generate an unique id */
74     $this->id = preg_replace("/[^0-9]/","",microtime());
75   }
77         /*! \brief Handles _POST / _GET events
79             Processes the list of registered plugins to do their eventHandler and adapt
80       internal objectlist according to this.
81          */
82   public function eventHandler(){
83     /* Reloads the list if things have changed interally */
84     $this->objects->reload();
85   }
88         /*! \brief Renders headline into a string
90             Gets the headline description from the ObjectList object and renders it.
92       \return HTML rendered headline
93          */
94   private function renderHeadline(){
95     $tpl =" <tr>
96               <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header".$this->id."'>
97                 <table class='ObjectListViewport_Header_Table' id='ObjectListViewport_Header_Table".$this->id."'>
98                   <tr>
99                     {content}
100                   </tr>
101                 </table>
102              </td>
103            </tr>";
104     $buffer ="";
105     foreach($this->headline as $key => $value){
106       $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
107     }
108     return(preg_replace("/\{content\}/", $buffer,$tpl));
109   }
112         /*! \brief Renders footer into a string
114             Gets the footer description from the ObjectList object and renders it.
116       \return HTML rendered footer
117          */
118   private function renderFooter(){
119     $buffer ="<tr>
120               <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer".$this->id."'>
121                 <table class='ObjectListViewport_Footer_Table'>
122                   <tr>
123                     <td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>
124                   </tr>
125                 </table>     
126               </td>
127            </tr>";
128     return $buffer;
129   }
132   private function getEntryIcon($entry,$alt = ""){
133     return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
134   }
137         /*! \brief Renders entries from the ObjectList iterator into a string
138             Gets the entry descriptions from the ObjectList object and renders them.
139       \return HTML rendered list entries
140          */
141   private function renderEntry($entry,$entry_id){
143     /* Copy template */
144     $buffer= $this->entryFormat;
146     /* Replace set of attributes */
147     foreach ($this->attributes as $attribute){
148       if (!isset($entry[$attribute])){
149         throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
150       } else {
152         if(preg_match("/_icon/i",$attribute)){
153           $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer); 
154         }else{
155           $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
156         }
157       }
158     }
160     /* Execute optional filters */
161     preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
162     foreach ($matches as $match){
163       $filterName= preg_replace('/,.+$/', '', $match[1]);
164       $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
165       $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/', $this->applyEntryFilter($filterName, $filterParameter), $buffer);
166     }
168     #TODO: Make $buffer a proper HTML table output
169     $tmp = split("\|",trim($buffer,"|"));  
171     /* define entry template */
172     $tpl = "<td class='ObjectListViewport_Entry_Cell' style='{style_1}'>
173               <div style='{style_2}'>
174                 {content}
175               </div>
176             </td>
177             ";
178     /* Template vriables to replace */
179     $attrs = array("/\{style_1\}/","/\{style_2\}/","/\{content\}/");
181     /* Append template for each given col */
182     $buffer ="";
183     for($i = 0 ; $i < $this->i_numberOfCols ; $i ++){
185       /* If current entry is the last to appen, then skip adding styles */
186       if($i == ($this->i_numberOfCols -1 )){
187         $buffer.= preg_replace( $attrs,
188             array($this->headline[$i]['style'],"width:100%;overflow:hidden;",$tmp[$i]),$tpl);
189       }else{
190         $buffer.= preg_replace( $attrs,
191             array($this->headline[$i]['style'],"width:100%;overflow:hidden;".$this->headline[$i]['style'],$tmp[$i]),$tpl);
192       }
193     }
195     if($entry_id % 2){
196       $a = "class='ObjectListViewport_Entry_Row1'";
197     }else{
198       $a = "class='ObjectListViewport_Entry_Row2'";
199     }
201     return "<tr ".$a.">".$buffer."</tr>\n";
202   }
205         /*! \brief Applies filter to the given entry format string.
207             Instanciates the given ObjectListEntryFilter and calls the method.
209       \return rendered output
210       \sa ObjectListEntryFilter
211          */
212   private function applyEntryFilter($filterName, $string){
213     $className= "ObjectListEntryFilter_".$filterName;
214     $cl= new $className;
215     return $cl->filter("$string");
216   }
219         /*! \brief Renders complete ObjectList into a string
221       \return HTML rendered list
222          */
223   public function render() {
225     $header = $footer = "";
226     if($this->b_displayHeader){
227       $header = $this->renderHeadline();
228     }
229     if($this->b_displayFooter){
230       $footer = $this->renderFooter();
231     }
233     /* Apply current filter */
234     $entries = "";
235     $objects= new ObjectListFilterIterator($this->objects->getIterator());
236     foreach ($objects as $key => $value){
237       $entries .= $this->renderEntry($value,$key);
238     }
240     /* Generate fixed headline */
241     $buffer = "
242     <table class='ObjectListViewport' id='ObjectListViewport".$this->id."' cellspacing=0 cellpadding=0>
243       <tr>
244         <td>
245               <table class='ObjectListViewport_Table' id='ObjectListViewport_Table".$this->id."' cellpadding=0 cellspacing=0 >
246                 ".$header."
247                 <tr>
248                   <td class='ObjectListViewport_TD_Entries' id='ObjectListViewport_TD_Entries".$this->id."'>
249                     <div class='ObjectListViewport_Entry_Cover' id='ObjectListViewport_Entry_Cover".$this->id."'> 
250                       <table class='ObjectListViewport_Entry_Table' id='ObjectListViewport_Entry_Table".$this->id."'>
251                         ".$entries."
252                       </table> 
253                     </div>
254                   </td>
255                 </tr>
256                 ".$footer."
257               </table>
259         </td>
260       </tr>
261     </table>
262 ";
264     return ($buffer);
265   }
268         /*! \brief Parses the given headline format string 
270       \return Array with cell properties (width, alignment,name)
271          */
272   private function parseHeadline($data)
273   {
274     /* Each cell definition is seperated by | 
275      *  split by and go through each definition
276      */
277     $this->headline = array();
278     $this->i_numberOfCols = 0;
279     $tmp = split("\|",trim($data,"|")); 
280     $cell_formats = array();
281     foreach($tmp as $key => $data){
283       $s_width    = "";
284       $s_alignment= "";
285       $s_name     = preg_replace("/\{[^\}]*+\}/","",$data);
286       $s_style    = "";
287     
288       /* Parse format string and detect width & alignment */
289       if(preg_match("/\{.*\}/",$data)){
290         $s_format=  preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
291     
292         /* Get aligment */
293         if(preg_match("/:/",$s_format)){
294           $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
296           if(preg_match("/T/i",$s_al)){
297             $s_alignment.= "top-"  ;
298             $s_style.= "vertical-align: top;";
299           }
300           if(preg_match("/B/i",$s_al)){
301             $s_alignment.= "bottom-"  ;
302             $s_style.= "vertical-align: bottom;";
303           }
304           if(preg_match("/R/i",$s_al)){
305             $s_alignment.= "right"  ;
306             $s_style.= "text-align: right;";
307           }elseif(preg_match("/L/i",$s_al)){
308             $s_alignment.= "left"  ;
309             $s_style.= "text-align: left;";
310           }elseif(preg_match("/C/i",$s_al) || preg_match("/M/i",$s_al) ){
311             $s_alignment.= "center"  ;
312             $s_style.= "text-align: center;";
313           }
314         }
316         /* Get width */
317         $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
318         if(!empty($s_width)){
319           $s_style.= "width: ".$s_width.";";
320         }
321       }
322       $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
323       $this->i_numberOfCols ++;
324     }
325     $this->headline = $cell_formats;
326   }
329   public function enableFooter($bool = TRUE){
330     $this->b_displayFooter = $bool;
331   }
332   public function enableHeader($bool = TRUE){
333     $this->b_displayHeader = $bool;
334   }
335   
338 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
339 ?>