Code

Updated viewport
[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   /* <---- Dummy here */
35   /*!
36     \brief Container for objects
38     This variable stores the ObjectList object to be displayed.
39    */
40         private $objects;
42   /*!
43     \brief Switch to handle multiselect or not
44    */
45         private $multiselect;
48   /*! \brief ObjectListViewport constructor
50     The ObjectListViewport class renders/handles the ObjectList defined by $config.
52     \param config Config section that is used to configure this ObjectListViewport
53    */
54         public function __construct($config, $multiselect= TRUE){
56         $this->multiselect= $multiselect;
57     /* Dummy here */
58     $cr= Registry::getInstance("ConfigManager");
59     $cr->setSection($config);
60     $this->headline= $this->parseHeadline($cr->getValue("headline"));
61     $this->footer= $cr->getValue("footer");
62     $this->entryFormat= $cr->getValue("entryFormat");
64     /* Load and instanciate classes, extract filter, icons, view hooks, etc. */
65     $this->objects= new ObjectList($config);
66   }
68         /*! \brief Handles _POST / _GET events
70             Processes the list of registered plugins to do their eventHandler and adapt
71       internal objectlist according to this.
72          */
73   public function eventHandler(){
74     /* Reloads the list if things have changed interally */
75     $this->objects->reload();
76   }
79         /*! \brief Renders headline into a string
81             Gets the headline description from the ObjectList object and renders it.
83       \return HTML rendered headline
84          */
85   private function renderHeadline(){
86     # Dummy implementation. Use pre-defined headline.
87     $buffer= $this->headline."\n";
89     #TODO: Make $buffer a proper HTML table output
90     return $buffer;
91   }
94         /*! \brief Renders footer into a string
96             Gets the footer description from the ObjectList object and renders it.
98       \return HTML rendered footer
99          */
100   private function renderFooter(){
101     # Dummy implementation. Use pre-defined footer.
102     $buffer= "|".$this->footer."|";
104     #TODO: Make $buffer a proper HTML table output
105     return $buffer."|\n";
106   }
109         /*! \brief Renders entries from the ObjectList iterator into a string
111             Gets the entry descriptions from the ObjectList object and renders them.
113       \return HTML rendered list entries
114          */
115   private function renderEntry($entry){
117     /* Copy template */
118     $buffer= $this->entryFormat;
120     $tmp = split("\|",trim($this->entryFormat,"|"));  
122     $buffer ="<tr>\n";
123     foreach($tmp as $key => $value){
124       $buffer .= "<td style='".$this->headline[$key]['style']."'>".$value."</td>\n";
125     }
126     $buffer.="</tr>\n";
128     /* Replace set of attributes */
129     foreach ($this->attributes as $attribute){
130       if (!isset($entry[$attribute])){
131         throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
132       } else {
133         $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
134       }
135     }
137     /* Execute optional filters */
138     preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
139     foreach ($matches as $match){
140       $filterName= preg_replace('/,.+$/', '', $match[1]);
141       $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
142       $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/', $this->applyEntryFilter($filterName, $filterParameter), $buffer);
143     }
145     #TODO: Make $buffer a proper HTML table output
147     return $buffer."\n";
148   }
151         /*! \brief Applies filter to the given entry format string.
153             Instanciates the given ObjectListEntryFilter and calls the method.
155       \return rendered output
156       \sa ObjectListEntryFilter
157          */
158   private function applyEntryFilter($filterName, $string){
159     $className= "ObjectListEntryFilter_".$filterName;
160     $cl= new $className;
161     return $cl->filter("$string");
162   }
165         /*! \brief Renders complete ObjectList into a string
167       \return HTML rendered list
168          */
169   public function render() {
171     /* Generate fixed headline */
172     $buffer= $this->renderHeadline();
174     /* Apply current filter */
175     $objects= new ObjectListFilterIterator($this->objects->getIterator());
176     foreach ($objects as $value){
177       $buffer.= $this->renderEntry($value);
178     }
180     /* Generate footer */
181     $buffer.= $this->renderFooter();
183     return ("<table border=1 style='width:100%'>".$buffer."</table>");
184   }
187         /*! \brief Parses the given headline format string 
189       \return Array with cell properties (width, alignment,name)
190          */
191   private function parseHeadline($data)
192   {
193     /* Each cell definition is seperated by | 
194      *  split by and go through each definition
195      */
196     $tmp = split("\|",trim($data,"|"));  
197     $cell_formats = array();
198     foreach($tmp as $key => $data){
200       $s_width    = "";
201       $s_alignment= "";
202       $s_name     = preg_replace("/\{[^\}]*+\}/","",$data);
203       $s_style    = "height:40px;";
204     
205       /* Parse format string and detect width & alignment */
206       if(preg_match("/\{.*\}/",$data)){
207         $s_format=  preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
208     
209         /* Get aligment */
210         if(preg_match("/:/",$s_format)){
211           $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
213           if(preg_match("/T/i",$s_al)){
214             $s_alignment.= "top-"  ;
215             $s_style.= "vertical-align: top;";
216           }
217           if(preg_match("/B/i",$s_al)){
218             $s_alignment.= "bottom-"  ;
219             $s_style.= "vertical-align: bottom;";
220           }
221           if(preg_match("/R/i",$s_al)){
222             $s_alignment.= "right"  ;
223             $s_style.= "text-align: right;";
224           }elseif(preg_match("/L/i",$s_al)){
225             $s_alignment.= "left"  ;
226             $s_style.= "text-align: left;";
227           }elseif(preg_match("/C/i",$s_al) || preg_match("/M/i",$s_al) ){
228             $s_alignment.= "center"  ;
229             $s_style.= "text-align: center;";
230           }
231         }
233         /* Get width */
234         $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
235         if(!empty($s_width)){
236           $s_style = "width: ".$s_width.";";
237         }
238         
239         $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
240       }
241     }
242     return($cell_formats);
243   }
246 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
247 ?>