Code

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