Code

Removed debug content
[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     $buffer ="<table class='ObjectListViewport_Header_Table' id='ObjectListViewport_Header_Table".$this->id."'>\n";
96     $buffer.="<tr>\n";
97     foreach($this->headline as $key => $value){
98       $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
99     }
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     for($i = 0 ; $i < $this->i_numberOfCols ; $i ++){
172   
173       $value = $tmp[$i];
174       if($i == ($this->i_numberOfCols -1 )){
176        $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$i]['style']."'>".
177        "<div style='width:100%;overflow:hidden;'>".
178             $value.
179           "</div>".
180           "</td>\n";
181       }else{
182        $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$i]['style']."'>".
183        "<div style='width:100%;overflow:hidden;".$this->headline[$i]['style']."'>".
184             $value.
185           "</div>".
186           "</td>\n";
187       }
188     }
191     $buffer.="</tr>\n";
193     return $buffer."\n";
194   }
197         /*! \brief Applies filter to the given entry format string.
199             Instanciates the given ObjectListEntryFilter and calls the method.
201       \return rendered output
202       \sa ObjectListEntryFilter
203          */
204   private function applyEntryFilter($filterName, $string){
205     $className= "ObjectListEntryFilter_".$filterName;
206     $cl= new $className;
207     return $cl->filter("$string");
208   }
211         /*! \brief Renders complete ObjectList into a string
213       \return HTML rendered list
214          */
215   public function render() {
217     $header = $footer = "";
218     if($this->b_displayHeader){
219       $header ="<tr>
220                   <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header".$this->id."'>
221                     ".$this->renderHeadline()."
222                   </td>
223                 </tr>";
224     }
225     if($this->b_displayFooter){
226       $footer ="<tr>
227                   <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer".$this->id."'>
228                     ".$this->renderFooter()."
229                   </td>
230                 </tr>";
231     }
233     /* Apply current filter */
234     $entries = "";
235     $objects= new ObjectListFilterIterator($this->objects->getIterator());
236     foreach ($objects as $value){
237       $entries .= $this->renderEntry($value);
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 ?>