Code

Added styles from currently used list.
[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');
34   /* <---- Dummy here */
36   /*!
37     \brief Container for objects
39     This variable stores the ObjectList object to be displayed.
40    */
41         private $objects;
43   /*!
44     \brief Switch to handle multiselect or not
45    */
46         private $multiselect;
48   /*! \brief ID used to identify objects of same list */
49   private $id = "";
50   
51   /*! \brief ObjectListViewport constructor
53     The ObjectListViewport class renders/handles the ObjectList defined by $config.
55     \param config Config section that is used to configure this ObjectListViewport
56    */
57         public function __construct($config, $multiselect= TRUE){
59         $this->multiselect= $multiselect;
60     /* Dummy here */
61     $cr= Registry::getInstance("ConfigManager");
62     $cr->setSection($config);
63     $this->headline= $this->parseHeadline($cr->getValue("headline"));
64     $this->footer= $cr->getValue("footer");
65     $this->entryFormat= $cr->getValue("entryFormat");
67     /* Load and instanciate classes, extract filter, icons, view hooks, etc. */
68     $this->objects= new ObjectList($config);
70     /* generate an unique id */
71     $this->id = preg_replace("/[^0-9]/","",microtime());
72   }
74         /*! \brief Handles _POST / _GET events
76             Processes the list of registered plugins to do their eventHandler and adapt
77       internal objectlist according to this.
78          */
79   public function eventHandler(){
80     /* Reloads the list if things have changed interally */
81     $this->objects->reload();
82   }
85         /*! \brief Renders headline into a string
87             Gets the headline description from the ObjectList object and renders it.
89       \return HTML rendered headline
90          */
91   private function renderHeadline(){
92     $buffer ="<table class='ObjectListViewport_Header_Table'>\n";
93     $buffer.="<tr>\n";
94     foreach($this->headline as $key => $value){
95       $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
96     }
97     $buffer.="<td class='ObjectListViewport_Header_Cell' style='border-right:0px;width:14px;'>&nbsp;</td>";
98     $buffer.="</tr>\n";
99     $buffer.="</table>\n";
100     return $buffer;
101   }
104         /*! \brief Renders footer into a string
106             Gets the footer description from the ObjectList object and renders it.
108       \return HTML rendered footer
109          */
110   private function renderFooter(){
111     $buffer ="<table class='ObjectListViewport_Footer_Table'>\n"; 
112     $buffer.="<tr>\n";
113     $buffer.= "<td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>\n";
114     $buffer.="</tr>\n";
115     $buffer.="</table>\n";
116     return $buffer;
117   }
120   private function getEntryIcon($entry,$alt = ""){
121     return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
122   }
125         /*! \brief Renders entries from the ObjectList iterator into a string
126             Gets the entry descriptions from the ObjectList object and renders them.
127       \return HTML rendered list entries
128          */
129   private function renderEntry($entry){
131     /* Copy template */
132     $buffer= $this->entryFormat;
134     /* Replace set of attributes */
135     foreach ($this->attributes as $attribute){
136       if (!isset($entry[$attribute])){
137         throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
138       } else {
140         if(preg_match("/_icon/i",$attribute)){
141           $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer); 
142         }else{
143           $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
144         }
145       }
146     }
148     /* Execute optional filters */
149     preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
150     foreach ($matches as $match){
151       $filterName= preg_replace('/,.+$/', '', $match[1]);
152       $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
153       $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/', $this->applyEntryFilter($filterName, $filterParameter), $buffer);
154     }
156     #TODO: Make $buffer a proper HTML table output
158     $tmp = split("\|",trim($buffer,"|"));  
161     $buffer="<tr>\n";
162     foreach($tmp as $key => $value){
163         $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
164           "<div style='width:100%;overflow:hidden;".$this->headline[$key]['style']."'>".
165             $value.
166           "</div>".
167           "</td>\n";
168     }
169     $buffer.="</tr>\n";
171     return $buffer."\n";
172   }
175         /*! \brief Applies filter to the given entry format string.
177             Instanciates the given ObjectListEntryFilter and calls the method.
179       \return rendered output
180       \sa ObjectListEntryFilter
181          */
182   private function applyEntryFilter($filterName, $string){
183     $className= "ObjectListEntryFilter_".$filterName;
184     $cl= new $className;
185     return $cl->filter("$string");
186   }
189         /*! \brief Renders complete ObjectList into a string
191       \return HTML rendered list
192          */
193   public function render() {
195     $header = $this->renderHeadline();
196     $footer = $this->renderFooter();
198     /* Apply current filter */
199     $entries = "";
200     $objects= new ObjectListFilterIterator($this->objects->getIterator());
201     foreach ($objects as $value){
202       $entries .= $this->renderEntry($value);
203     }
205     /* Generate fixed headline */
206     $buffer = "
207     <table class='ObjectListViewport' id='ObjectListViewport".$this->id."' cellspacing=0 cellpadding=0>
208       <tr>
209         <td>
210               <table class='ObjectListViewport_Table' id='ObjectListViewport_Table".$this->id."' cellpadding=0 cellspacing=0 >
211                 <tr>
212                   <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header".$this->id."'>
213                     ".$header."
214                   </td>
215                 </tr>
216                 <tr>
217                   <td class='ObjectListViewport_TD_Entries' id='ObjectListViewport_TD_Entries".$this->id."'>
218                     <div class='ObjectListViewport_Entry_Cover' id='ObjectListViewport_Entry_Cover".$this->id."'> 
219                       <table class='ObjectListViewport_Entry_Table' id='ObjectListViewport_Entry_Table".$this->id."'>
220                         ".$entries."
221                       </table> 
222                     </div>
223                   </td>
224                 </tr>
225                 <tr>
226                   <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer".$this->id."'>
227                     ".$footer."
228                   </td>
229                 </tr>
230               </table>
232         </td>
233       </tr>
234     </table>
235 ";
237     return ($buffer);
238   }
241         /*! \brief Parses the given headline format string 
243       \return Array with cell properties (width, alignment,name)
244          */
245   private function parseHeadline($data)
246   {
247     /* Each cell definition is seperated by | 
248      *  split by and go through each definition
249      */
250     $tmp = split("\|",trim($data,"|")); 
251     $cell_formats = array();
252     foreach($tmp as $key => $data){
254       $s_width    = "";
255       $s_alignment= "";
256       $s_name     = preg_replace("/\{[^\}]*+\}/","",$data);
257       $s_style    = "";
258     
259       /* Parse format string and detect width & alignment */
260       if(preg_match("/\{.*\}/",$data)){
261         $s_format=  preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
262     
263         /* Get aligment */
264         if(preg_match("/:/",$s_format)){
265           $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
267           if(preg_match("/T/i",$s_al)){
268             $s_alignment.= "top-"  ;
269             $s_style.= "vertical-align: top;";
270           }
271           if(preg_match("/B/i",$s_al)){
272             $s_alignment.= "bottom-"  ;
273             $s_style.= "vertical-align: bottom;";
274           }
275           if(preg_match("/R/i",$s_al)){
276             $s_alignment.= "right"  ;
277             $s_style.= "text-align: right;";
278           }elseif(preg_match("/L/i",$s_al)){
279             $s_alignment.= "left"  ;
280             $s_style.= "text-align: left;";
281           }elseif(preg_match("/C/i",$s_al) || preg_match("/M/i",$s_al) ){
282             $s_alignment.= "center"  ;
283             $s_style.= "text-align: center;";
284           }
285         }
287         /* Get width */
288         $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
289         if(!empty($s_width)){
290           $s_style.= "width: ".$s_width.";";
291         }
292       }
293       $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
294     }
295     return($cell_formats);
296   }
299   
302 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
303 ?>