Code

2cc44af49d46a7c7bb0a7857e717c623c051cb74
[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;
49   /*! \brief ObjectListViewport constructor
51     The ObjectListViewport class renders/handles the ObjectList defined by $config.
53     \param config Config section that is used to configure this ObjectListViewport
54    */
55         public function __construct($config, $multiselect= TRUE){
57         $this->multiselect= $multiselect;
58     /* Dummy here */
59     $cr= Registry::getInstance("ConfigManager");
60     $cr->setSection($config);
61     $this->headline= $this->parseHeadline($cr->getValue("headline"));
62     $this->footer= $cr->getValue("footer");
63     $this->entryFormat= $cr->getValue("entryFormat");
65     /* Load and instanciate classes, extract filter, icons, view hooks, etc. */
66     $this->objects= new ObjectList($config);
67   }
69         /*! \brief Handles _POST / _GET events
71             Processes the list of registered plugins to do their eventHandler and adapt
72       internal objectlist according to this.
73          */
74   public function eventHandler(){
75     /* Reloads the list if things have changed interally */
76     $this->objects->reload();
77   }
80         /*! \brief Renders headline into a string
82             Gets the headline description from the ObjectList object and renders it.
84       \return HTML rendered headline
85          */
86   private function renderHeadline(){
87     $buffer ="<table class='ObjectListViewport_Header_Table'>\n";
88     $buffer.="<tr>\n";
89     foreach($this->headline as $key => $value){
90       $buffer .= "<td  style='".$value['style']."'>".$value['name']."</td>\n";
91     }
92     $buffer.="<td style='width:13px;'>&nbsp;</td>";
93     $buffer.="</tr>\n";
94     $buffer.="</table>\n";
95     return $buffer;
96   }
99         /*! \brief Renders footer into a string
101             Gets the footer description from the ObjectList object and renders it.
103       \return HTML rendered footer
104          */
105   private function renderFooter(){
106     $buffer ="<table class='ObjectListViewport_Footer_Table'>\n"; 
107     $buffer.="<tr>\n";
108     $buffer.= "<td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>\n";
109     $buffer.="</tr>\n";
110     $buffer.="</table>\n";
111     return $buffer;
112   }
115   private function getEntryIcon($entry,$alt = ""){
116     return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
117   }
120         /*! \brief Renders entries from the ObjectList iterator into a string
121             Gets the entry descriptions from the ObjectList object and renders them.
122       \return HTML rendered list entries
123          */
124   private function renderEntry($entry){
126     /* Copy template */
127     $buffer= $this->entryFormat;
129     /* Replace set of attributes */
130     foreach ($this->attributes as $attribute){
131       if (!isset($entry[$attribute])){
132         throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
133       } else {
135         if(preg_match("/_icon/i",$attribute)){
136           $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer); 
137         }else{
138           $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
139         }
140       }
141     }
143     /* Execute optional filters */
144     preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
145     foreach ($matches as $match){
146       $filterName= preg_replace('/,.+$/', '', $match[1]);
147       $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
148       $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/', $this->applyEntryFilter($filterName, $filterParameter), $buffer);
149     }
151     #TODO: Make $buffer a proper HTML table output
153     $tmp = split("\|",trim($buffer,"|"));  
156     $buffer="<tr>\n";
157     foreach($tmp as $key => $value){
159       if(1 || empty($this->headline[$key]['width']) || preg_match("/\%/",$this->headline[$key]['width'])){
160         $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
161           "<div style='overflow:hidden;".$this->headline[$key]['style']."'>".
162             $value.
163           "</div>".
164           "</td>\n";
165       }else{
166         $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
167           $value.
168           "</td>\n";
169       }
170     }
171     $buffer.="</tr>\n";
173     return $buffer."\n";
174   }
177         /*! \brief Applies filter to the given entry format string.
179             Instanciates the given ObjectListEntryFilter and calls the method.
181       \return rendered output
182       \sa ObjectListEntryFilter
183          */
184   private function applyEntryFilter($filterName, $string){
185     $className= "ObjectListEntryFilter_".$filterName;
186     $cl= new $className;
187     return $cl->filter("$string");
188   }
191         /*! \brief Renders complete ObjectList into a string
193       \return HTML rendered list
194          */
195   public function render() {
197     $header = $this->renderHeadline();
198     $footer = $this->renderFooter();
200     /* Apply current filter */
201     $entries = "";
202     $objects= new ObjectListFilterIterator($this->objects->getIterator());
203     foreach ($objects as $value){
204       $entries .= $this->renderEntry($value);
205     }
207     
209     /* Generate fixed headline */
210     $buffer = "
211     <table class='ObjectListViewport' id='ObjectListViewport' cellspacing=o cellpadding=0
212           style=' width:100%;height:100%;; 
213                   border-collapse:collapse;'>
214       <tr>
215         <td>
216               <table class='ObjectListViewport_Table' id='ObjectListViewport_Table' cellpadding=0 cellspacing=0 >
217                 <tr>
218                   <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header'>
219                     ".$header."
220                   </td>
221                 </tr>
222                 <tr>
223                   <td class='ObjectListViewport_TD_Entries' id='ObjectListViewport_TD_Entries'>
224                     <div style='overflow:auto' id='ObjectListViewport_Entry_Cover'> 
225                       <table class='ObjectListViewport_Entry_Table' id='ObjectListViewport_Entry_Table'>
226                         ".$entries."
227                       </table> 
228                     </div>
229                   </td>
230                 </tr>
231                 <tr>
232                   <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer'>
233                     ".$footer."
234                   </td>
235                 </tr>
236               </table>
238         </td>
239       </tr>
240     </table>
241 ";
243     return ($buffer);
244   }
247         /*! \brief Parses the given headline format string 
249       \return Array with cell properties (width, alignment,name)
250          */
251   private function parseHeadline($data)
252   {
253     /* Each cell definition is seperated by | 
254      *  split by and go through each definition
255      */
256     $tmp = split("\|",trim($data,"|"));  
257     $cell_formats = array();
258     foreach($tmp as $key => $data){
260       $s_width    = "";
261       $s_alignment= "";
262       $s_name     = preg_replace("/\{[^\}]*+\}/","",$data);
263       $s_style    = "";
264     
265       /* Parse format string and detect width & alignment */
266       if(preg_match("/\{.*\}/",$data)){
267         $s_format=  preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
268     
269         /* Get aligment */
270         if(preg_match("/:/",$s_format)){
271           $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
273           if(preg_match("/T/i",$s_al)){
274             $s_alignment.= "top-"  ;
275             $s_style.= "vertical-align: top;";
276           }
277           if(preg_match("/B/i",$s_al)){
278             $s_alignment.= "bottom-"  ;
279             $s_style.= "vertical-align: bottom;";
280           }
281           if(preg_match("/R/i",$s_al)){
282             $s_alignment.= "right"  ;
283             $s_style.= "text-align: right;";
284           }elseif(preg_match("/L/i",$s_al)){
285             $s_alignment.= "left"  ;
286             $s_style.= "text-align: left;";
287           }elseif(preg_match("/C/i",$s_al) || preg_match("/M/i",$s_al) ){
288             $s_alignment.= "center"  ;
289             $s_style.= "text-align: center;";
290           }
291         }
293         /* Get width */
294         $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
295         if(!empty($s_width)){
296           $s_style = "width: ".$s_width.";";
297         }
298         
299         $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
300       }
301     }
302     return($cell_formats);
303   }
306   
309 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
310 ?>