Code

6f070214b3fbb88ffac2a47a7265c7b95c90f96c
[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 ="<tr>\n";
88     foreach($this->headline as $key => $value){
89       $buffer .= "<td style='".$value['style']."'>".$value['name']."</td>\n";
90     }
91     $buffer.="</tr>\n";
92     return $buffer;
93   }
96         /*! \brief Renders footer into a string
98             Gets the footer description from the ObjectList object and renders it.
100       \return HTML rendered footer
101          */
102   private function renderFooter(){
104     $buffer ="<tr>\n";
105     $buffer .= "<td colspan='".count($this->headline)."'>".$this->footer."</td>\n";
106     $buffer.="</tr>\n";
107     return $buffer;
108   }
111   private function getEntryIcon($entry,$alt = ""){
112     return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
113   }
116         /*! \brief Renders entries from the ObjectList iterator into a string
117             Gets the entry descriptions from the ObjectList object and renders them.
118       \return HTML rendered list entries
119          */
120   private function renderEntry($entry){
122     /* Copy template */
123     $buffer= $this->entryFormat;
125     /* Replace set of attributes */
126     foreach ($this->attributes as $attribute){
127       if (!isset($entry[$attribute])){
128         throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
129       } else {
131         if(preg_match("/_icon/i",$attribute)){
132           $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer); 
133         }else{
134           $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
135         }
136       }
137     }
139     /* Execute optional filters */
140     preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
141     foreach ($matches as $match){
142       $filterName= preg_replace('/,.+$/', '', $match[1]);
143       $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
144       $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/', $this->applyEntryFilter($filterName, $filterParameter), $buffer);
145     }
147     #TODO: Make $buffer a proper HTML table output
149     $tmp = split("\|",trim($buffer,"|"));  
151     $buffer ="<tr>\n";
152     foreach($tmp as $key => $value){
153       $buffer .= "<td class='ObjectListViewportEntry' style='".$this->headline[$key]['style']."'>".$value."</td>\n";
154     }
155     $buffer.="</tr>\n";
157     return $buffer."\n";
158   }
161         /*! \brief Applies filter to the given entry format string.
163             Instanciates the given ObjectListEntryFilter and calls the method.
165       \return rendered output
166       \sa ObjectListEntryFilter
167          */
168   private function applyEntryFilter($filterName, $string){
169     $className= "ObjectListEntryFilter_".$filterName;
170     $cl= new $className;
171     return $cl->filter("$string");
172   }
175         /*! \brief Renders complete ObjectList into a string
177       \return HTML rendered list
178          */
179   public function render() {
181     /* Generate fixed headline */
182     $buffer= $this->renderHeadline();
184     /* Apply current filter */
185     $objects= new ObjectListFilterIterator($this->objects->getIterator());
186     
187     foreach ($objects as $value){
188       $buffer.= $this->renderEntry($value);
189     }
191     /* Generate footer */
192     $buffer.= $this->renderFooter();
195     return ("<table class='ObjectListViewportTable'>".$buffer."</table>");
196   }
199         /*! \brief Parses the given headline format string 
201       \return Array with cell properties (width, alignment,name)
202          */
203   private function parseHeadline($data)
204   {
205     /* Each cell definition is seperated by | 
206      *  split by and go through each definition
207      */
208     $tmp = split("\|",trim($data,"|"));  
209     $cell_formats = array();
210     foreach($tmp as $key => $data){
212       $s_width    = "";
213       $s_alignment= "";
214       $s_name     = preg_replace("/\{[^\}]*+\}/","",$data);
215       $s_style    = "";
216     
217       /* Parse format string and detect width & alignment */
218       if(preg_match("/\{.*\}/",$data)){
219         $s_format=  preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
220     
221         /* Get aligment */
222         if(preg_match("/:/",$s_format)){
223           $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
225           if(preg_match("/T/i",$s_al)){
226             $s_alignment.= "top-"  ;
227             $s_style.= "vertical-align: top;";
228           }
229           if(preg_match("/B/i",$s_al)){
230             $s_alignment.= "bottom-"  ;
231             $s_style.= "vertical-align: bottom;";
232           }
233           if(preg_match("/R/i",$s_al)){
234             $s_alignment.= "right"  ;
235             $s_style.= "text-align: right;";
236           }elseif(preg_match("/L/i",$s_al)){
237             $s_alignment.= "left"  ;
238             $s_style.= "text-align: left;";
239           }elseif(preg_match("/C/i",$s_al) || preg_match("/M/i",$s_al) ){
240             $s_alignment.= "center"  ;
241             $s_style.= "text-align: center;";
242           }
243         }
245         /* Get width */
246         $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
247         if(!empty($s_width)){
248           $s_style = "width: ".$s_width.";";
249         }
250         
251         $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
252       }
253     }
254     return($cell_formats);
255   }
258   
261 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
262 ?>