Code

3ec96e99538c13fc62179b7a0e776ea28d2db68d
[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     /* Add class depending on given id, to alternate background colors */
199     if($this->colorAlternator++ & 1){
200       $a = "class='ObjectListViewport_Entry_Row1'";
201     }else{
202       $a = "class='ObjectListViewport_Entry_Row2'";
203     }
205     return "<tr ".$a.">\n".$buffer."</tr>\n";
206   }
209         /*! \brief Applies filter to the given entry format string.
211             Instanciates the given ObjectListEntryFilter and calls the method.
213       \return rendered output
214       \sa ObjectListEntryFilter
215          */
216   private function applyEntryFilter($filterName, $string){
217     $className= "ObjectListEntryFilter_".$filterName;
218     $cl= new $className;
219     return $cl->filter("$string");
220   }
223         /*! \brief Renders complete ObjectList into a string
225       \return HTML rendered list
226          */
227   public function render() {
229     $header = $footer = "";
230     if($this->displayHeaderFlag){
231       $header = $this->renderHeadline();
232     }
233     if($this->displayFooterFlag){
234       $footer = $this->renderFooter();
235     }
237     /* Apply current filter */
238     $entries = "";
239     $objects= new ObjectListFilterIterator($this->objects->getIterator());
240     foreach ($objects as $value){
241       $entries .= $this->renderEntry($value);
242     }
244     /* Generate fixed headline */
245     $buffer = "
246     <table class='ObjectListViewport' id='ObjectListViewport".$this->id."' cellspacing=0 cellpadding=0>
247       <tr>
248         <td>
249               <table class='ObjectListViewport_Table' id='ObjectListViewport_Table".$this->id."' cellpadding=0 cellspacing=0 >
250                 ".$header."
251                 <tr>
252                   <td class='ObjectListViewport_TD_Entries' id='ObjectListViewport_TD_Entries".$this->id."'>
253                     <div class='ObjectListViewport_Entry_Cover' id='ObjectListViewport_Entry_Cover".$this->id."'> 
254                       <table class='ObjectListViewport_Entry_Table' id='ObjectListViewport_Entry_Table".$this->id."'>
255                         ".$entries."
256                       </table> 
257                     </div>
258                   </td>
259                 </tr>
260                 ".$footer."
261               </table>
263         </td>
264       </tr>
265     </table>
266 ";
268     return ($buffer);
269   }
272         /*! \brief Parses the given headline format string 
274       \return Array with cell properties (width, alignment,name)
275          */
276   private function parseHeadline($data)
277   {
278     /* Each cell definition is seperated by | 
279      *  split by and go through each definition
280      */
281     $this->headline= array();
282     $this->numberOfCols= 0;
283     $tmp= split("\|", trim($data, "|")); 
284     $cell_formats= array();
286     foreach($tmp as $key => $dta){
288       $width= "";
289       $alignment= "";
290       $name= preg_replace("/\{[^\}]*+\}/", "", $dta);
291       $style= "";
292     
293       /* Parse format string and detect width & alignment */
294       if(preg_match("/\{.*\}/", $dta)){
295         $format= preg_replace("/^[^\{]*+\{([^\}]*).*$/", "\\1", $dta);
296     
297         /* Get aligment */
298         if(preg_match("/:/",$format)){
299           $al= preg_replace("/^[^:]*+:([a-z]*).*$/i", "\\1", $format);
301           if(preg_match("/T/i", $al)){
302             $alignment.= "top-";
303             $style.= "vertical-align: top;";
304           }
305           if(preg_match("/B/i", $al)){
306             $alignment.= "bottom-";
307             $style.= "vertical-align: bottom;";
308           }
309           if(preg_match("/R/i", $al)){
310             $alignment.= "right";
311             $style.= "text-align: right;";
312           }elseif(preg_match("/L/i", $al)){
313             $alignment.= "left";
314             $style.= "text-align: left;";
315           }elseif(preg_match("/C/i", $al) || preg_match("/M/i", $al) ){
316             $alignment.= "center";
317             $style.= "text-align: center;";
318           }
319         }
321         /* Get width */
322         $width = preg_replace("/^([^:]*).*$/","\\1", $format);
323         if(!empty($width)){
324           $style.= "width: ".$width.";";
325         }
326       }
328       $cell_formats[$key]= array("name" => $name, "width" => $width, "alignment" => $alignment, "style" => $style);
329       $this->numberOfCols++;
330     }
331     $this->headline= $cell_formats;
332   }
335   public function enableFooter($bool= TRUE){
336     $this->displayFooterFlag= $bool;
337   }
340   public function enableHeader($bool= TRUE){
341     $this->displayHeaderFlag= $bool;
342   }
343   
346 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
347 ?>