Code

1968cd1899b94b83365af6190cb37df6f9d2c393
[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  style='".$value['style']."'>".$value['name']."</td>\n";
96     }
97     $buffer.="<td style='width:13px;'>&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){
164       if(1 || empty($this->headline[$key]['width']) || preg_match("/\%/",$this->headline[$key]['width'])){
165         $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
166           "<div style='overflow:hidden;".$this->headline[$key]['style']."'>".
167             $value.
168           "</div>".
169           "</td>\n";
170       }else{
171         $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
172           $value.
173           "</td>\n";
174       }
175     }
176     $buffer.="</tr>\n";
178     return $buffer."\n";
179   }
182         /*! \brief Applies filter to the given entry format string.
184             Instanciates the given ObjectListEntryFilter and calls the method.
186       \return rendered output
187       \sa ObjectListEntryFilter
188          */
189   private function applyEntryFilter($filterName, $string){
190     $className= "ObjectListEntryFilter_".$filterName;
191     $cl= new $className;
192     return $cl->filter("$string");
193   }
196         /*! \brief Renders complete ObjectList into a string
198       \return HTML rendered list
199          */
200   public function render() {
202     $header = $this->renderHeadline();
203     $footer = $this->renderFooter();
205     /* Apply current filter */
206     $entries = "";
207     $objects= new ObjectListFilterIterator($this->objects->getIterator());
208     foreach ($objects as $value){
209       $entries .= $this->renderEntry($value);
210     }
212     /* Generate fixed headline */
213     $buffer = "
214     <table class='ObjectListViewport' id='ObjectListViewport".$this->id."' cellspacing=o cellpadding=0>
215       <tr>
216         <td>
217               <table class='ObjectListViewport_Table' id='ObjectListViewport_Table".$this->id."' cellpadding=0 cellspacing=0 >
218                 <tr>
219                   <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header".$this->id."'>
220                     ".$header."
221                   </td>
222                 </tr>
223                 <tr>
224                   <td class='ObjectListViewport_TD_Entries' id='ObjectListViewport_TD_Entries".$this->id."'>
225                     <div class='ObjectListViewport_Entry_Cover' id='ObjectListViewport_Entry_Cover".$this->id."'> 
226                       <table class='ObjectListViewport_Entry_Table' id='ObjectListViewport_Entry_Table".$this->id."'>
227                         ".$entries."
228                       </table> 
229                     </div>
230                   </td>
231                 </tr>
232                 <tr>
233                   <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer".$this->id."'>
234                     ".$footer."
235                   </td>
236                 </tr>
237               </table>
239         </td>
240       </tr>
241     </table>
242 ";
244     return ($buffer);
245   }
248         /*! \brief Parses the given headline format string 
250       \return Array with cell properties (width, alignment,name)
251          */
252   private function parseHeadline($data)
253   {
254     /* Each cell definition is seperated by | 
255      *  split by and go through each definition
256      */
257     $tmp = split("\|",trim($data,"|"));  
258     $cell_formats = array();
259     foreach($tmp as $key => $data){
261       $s_width    = "";
262       $s_alignment= "";
263       $s_name     = preg_replace("/\{[^\}]*+\}/","",$data);
264       $s_style    = "";
265     
266       /* Parse format string and detect width & alignment */
267       if(preg_match("/\{.*\}/",$data)){
268         $s_format=  preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
269     
270         /* Get aligment */
271         if(preg_match("/:/",$s_format)){
272           $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
274           if(preg_match("/T/i",$s_al)){
275             $s_alignment.= "top-"  ;
276             $s_style.= "vertical-align: top;";
277           }
278           if(preg_match("/B/i",$s_al)){
279             $s_alignment.= "bottom-"  ;
280             $s_style.= "vertical-align: bottom;";
281           }
282           if(preg_match("/R/i",$s_al)){
283             $s_alignment.= "right"  ;
284             $s_style.= "text-align: right;";
285           }elseif(preg_match("/L/i",$s_al)){
286             $s_alignment.= "left"  ;
287             $s_style.= "text-align: left;";
288           }elseif(preg_match("/C/i",$s_al) || preg_match("/M/i",$s_al) ){
289             $s_alignment.= "center"  ;
290             $s_style.= "text-align: center;";
291           }
292         }
294         /* Get width */
295         $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
296         if(!empty($s_width)){
297           $s_style = "width: ".$s_width.";";
298         }
299         
300         $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
301       }
302     }
303     return($cell_formats);
304   }
307   
310 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
311 ?>