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 }
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 implements GOsaGuiElement {
28 /* Internal variable for color alternation */
29 protected $colorAlternator= 0;
31 private $headline;
32 private $footer;
33 private $entryFormat;
35 /* Dummy here ----> */
36 private $attributes= array('cn', '_icon', '_actions', 'dn');
37 /* <---- Dummy here */
39 private $displayHeaderFlag= TRUE;
40 private $displayFooterFlag= TRUE;
41 private $numberOfCols= 0;
43 /*!
44 \brief Container for objects
46 This variable stores the ObjectList object to be displayed.
47 */
48 private $objects;
50 /*!
51 \brief Switch to handle multiselect or not
52 */
53 private $multiselect;
55 /*! \brief ID used to identify objects of same list */
56 private $id = "";
59 /*! \brief ObjectListViewport constructor
61 The ObjectListViewport class renders/handles the ObjectList defined by $config.
63 \param config Config section that is used to configure this ObjectListViewport
64 */
65 public function __construct($config, $multiselect= TRUE, $displayHeader= TRUE, $displayFooter= FALSE){
67 /* Transfer initialization values */
68 $this->displayFooterFlag= $displayFooter;
69 $this->displayHeaderFlag= $displayHeader;
70 $this->multiselect= $multiselect;
72 /* Load list configuration from ConfigManager */
73 $cr= Registry::getInstance("ConfigManager");
74 $cr->setSection($config);
75 $this->parseHeadline($cr->getValue("headline"));
76 $this->footer= $cr->getValue("footer");
77 $this->entryFormat= $cr->getValue("entryFormat");
79 /* Load and instanciate classes, extract filter, icons, view hooks, etc. */
80 $this->objects= new ObjectList($config);
82 /* generate an unique id */
83 $this->id= uniqid();
84 }
87 /*! \brief Handles _POST / _GET events
89 Processes the list of registered plugins to do their eventHandler and adapt
90 internal objectlist according to this.
91 */
92 public function eventHandler(){
93 /* Reloads the list if things have changed interally */
94 $this->objects->reload();
95 }
98 /*! \brief Processes post events
100 Processes all post events and acts as needed.
102 \return bool for if changes are present or not
103 */
104 public function process(){
106 /* TODO: process input */
107 return FALSE;
108 }
111 /*! \brief Renders headline into a string
113 Gets the headline description from the ObjectList object and renders it.
115 \return HTML rendered headline
116 */
117 private function renderHeadline(){
118 $tpl =" <tr>
119 <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header_".$this->id."'>
120 <table class='ObjectListViewport_Header_Table' id='ObjectListViewport_Header_Table_".$this->id."'>
121 <tr>
122 {content}
123 </tr>
124 </table>
125 </td>
126 </tr>";
127 $buffer ="";
128 foreach($this->headline as $key => $value){
129 $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
130 }
131 return(preg_replace("/\{content\}/", $buffer,$tpl));
132 }
135 /*! \brief Renders footer into a string
137 Gets the footer description from the ObjectList object and renders it.
139 \return HTML rendered footer
140 */
141 private function renderFooter(){
142 $buffer ="<tr>
143 <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer_".$this->id."'>
144 <table class='ObjectListViewport_Footer_Table'>
145 <tr>
146 <td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>
147 </tr>
148 </table>
149 </td>
150 </tr>";
151 return $buffer;
152 }
155 /* TODO: replace this -> not our task */
156 private function getEntryIcon($entry,$alt = ""){
157 return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
158 }
161 /*! \brief Renders entries from the ObjectList iterator into a string
162 Gets the entry descriptions from the ObjectList object and renders them.
163 \return HTML rendered list entries
164 */
165 private function renderEntry($entry){
167 /* Copy template */
168 $buffer= $this->entryFormat;
170 /* Replace set of attributes */
171 foreach ($this->attributes as $attribute){
172 if (!isset($entry[$attribute])){
173 throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
174 } else {
176 if(preg_match("/_icon/i",$attribute)){
177 $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer);
178 }else{
179 $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
180 }
181 }
182 }
184 /* Execute optional filters */
185 preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
186 foreach ($matches as $match){
187 $filterName= preg_replace('/,.+$/', '', $match[1]);
188 $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
189 $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/', $this->applyEntryFilter($filterName, $filterParameter), $buffer);
190 }
192 #TODO: Make $buffer a proper HTML table output
193 $tmp = split("\|",trim($buffer,"|"));
195 /* define entry template */
196 $tpl = "<td class='ObjectListViewport_Entry_Cell' style='{style_1}'>
197 <div style='{style_2}'>
198 {content}
199 </div>
200 </td>
201 ";
202 /* Template vriables to replace */
203 $attrs = array("/\{style_1\}/","/\{style_2\}/","/\{content\}/");
205 /* Append template for each given col */
206 $buffer ="";
207 for($i= 0; $i < $this->numberOfCols; $i++){
209 /* If current entry is the last to appen, then skip adding styles */
210 if($i == ($this->numberOfCols-1)){
211 $buffer.= preg_replace( $attrs,
212 array($this->headline[$i]['style'],"width:100%;overflow:hidden;",$tmp[$i]),$tpl);
213 }else{
214 $buffer.= preg_replace( $attrs,
215 array($this->headline[$i]['style'],"width:100%;overflow:hidden;".$this->headline[$i]['style'],$tmp[$i]),$tpl);
216 }
217 }
219 /* Add class depending on given id, to alternate background colors */
220 if($this->colorAlternator++ & 1){
221 $a = "class='ObjectListViewport_Entry_Row1'";
222 }else{
223 $a = "class='ObjectListViewport_Entry_Row2'";
224 }
226 return "<tr ".$a.">\n".$buffer."</tr>\n";
227 }
230 /*! \brief Applies filter to the given entry format string.
232 Instanciates the given ObjectListEntryFilter and calls the method.
234 \return rendered output
235 \sa ObjectListEntryFilter
236 */
237 private function applyEntryFilter($filterName, $string){
238 $className= "ObjectListEntryFilter_".$filterName;
239 $cl= new $className;
240 return $cl->filter("$string");
241 }
244 /*! \brief Renders complete ObjectList into a string
246 \return HTML rendered list
247 */
248 public function render() {
250 $header = $footer = "";
251 if($this->displayHeaderFlag){
252 $header = $this->renderHeadline();
253 }
254 if($this->displayFooterFlag){
255 $footer = $this->renderFooter();
256 }
258 /* Apply current filter */
259 $entries = "";
260 $objects= new ObjectListFilterIterator($this->objects->getIterator());
261 foreach ($objects as $value){
262 $entries .= $this->renderEntry($value);
263 }
265 /* Generate fixed headline */
266 $buffer = "
267 <table class='ObjectListViewport' id='ObjectListViewport_".$this->id."' cellspacing=0 cellpadding=0>
268 <tr>
269 <td>
270 <table class='ObjectListViewport_Table' id='ObjectListViewport_Table_".$this->id."' cellpadding=0 cellspacing=0 >
271 ".$header."
272 <tr>
273 <td class='ObjectListViewport_TD_Entries' id='ObjectListViewport_TD_Entries_".$this->id."'>
274 <div class='ObjectListViewport_Entry_Cover' id='ObjectListViewport_Entry_Cover_".$this->id."'>
275 <table class='ObjectListViewport_Entry_Table' id='ObjectListViewport_Entry_Table_".$this->id."'>
276 ".$entries."
277 </table>
278 </div>
279 </td>
280 </tr>
281 ".$footer."
282 </table>
284 </td>
285 </tr>
286 </table>
287 ";
289 return ($buffer);
290 }
293 /*! \brief Parses the given headline format string
295 \return Array with cell properties (width, alignment,name)
296 */
297 private function parseHeadline($data)
298 {
299 /* Each cell definition is seperated by |
300 * split by and go through each definition
301 */
302 $this->headline= array();
303 $this->numberOfCols= 0;
304 $tmp= split("\|", trim($data, "|"));
305 $cell_formats= array();
307 foreach($tmp as $key => $dta){
309 $width= "";
310 $alignment= "";
311 $name= preg_replace("/\{[^\}]*+\}/", "", $dta);
312 $style= "";
314 /* Parse format string and detect width & alignment */
315 if(preg_match("/\{.*\}/", $dta)){
316 $format= preg_replace("/^[^\{]*+\{([^\}]*).*$/", "\\1", $dta);
318 /* Get aligment */
319 if(preg_match("/:/",$format)){
320 $al= preg_replace("/^[^:]*+:([a-z]*).*$/i", "\\1", $format);
322 if(preg_match("/T/i", $al)){
323 $alignment.= "top-";
324 $style.= "vertical-align: top;";
325 }
326 if(preg_match("/B/i", $al)){
327 $alignment.= "bottom-";
328 $style.= "vertical-align: bottom;";
329 }
330 if(preg_match("/R/i", $al)){
331 $alignment.= "right";
332 $style.= "text-align: right;";
333 }elseif(preg_match("/L/i", $al)){
334 $alignment.= "left";
335 $style.= "text-align: left;";
336 }elseif(preg_match("/C/i", $al) || preg_match("/M/i", $al) ){
337 $alignment.= "center";
338 $style.= "text-align: center;";
339 }
340 }
342 /* Get width */
343 $width = preg_replace("/^([^:]*).*$/","\\1", $format);
344 if(!empty($width)){
345 $style.= "width: ".$width.";";
346 }
347 }
349 $cell_formats[$key]= array("name" => $name, "width" => $width, "alignment" => $alignment, "style" => $style);
350 $this->numberOfCols++;
351 }
352 $this->headline= $cell_formats;
353 }
355 }
357 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
358 ?>