477fd41321dc98b6470c9df43fec309c602fcb8d
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
112 #
113 # Gets the headline description from the ObjectList object and renders it.
114 #
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
136 #
137 # Gets the footer description from the ObjectList object and renders it.
138 #
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 /* Apply current filter */
251 $entries = "";
252 $objects= new ObjectListFilterIterator($this->objects->getIterator());
253 foreach ($objects as $value){
254 $entries .= $this->renderEntry($value);
255 }
257 $smarty = get_smarty();
258 $smarty->assign("OLV_Entries",$entries);
259 $smarty->assign("OLV_List_Id",$this->id);
261 /* Footer variables */
262 $smarty->assign("OLV_Footer_Enabled",$this->displayFooterFlag);
263 $smarty->assign("OLV_Footer_Message",$this->footer);
264 $smarty->assign("OLV_Num_Cols",$this->numberOfCols);
266 /* Assign Headline values */
267 $smarty->assign("OLV_Header_Enabled",$this->displayHeaderFlag);
268 $smarty->assign("OLV_Header",$this->headline);
270 $buffer ="";
271 foreach($this->headline as $key => $value){
272 $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
273 }
275 return($smarty->fetch("ObjectListViewport.tpl"));
276 }
279 /*! \brief Parses the given headline format string
281 \return Array with cell properties (width, alignment,name)
282 */
283 private function parseHeadline($data)
284 {
285 /* Each cell definition is seperated by |
286 * split by and go through each definition
287 */
288 $this->headline= array();
289 $this->numberOfCols= 0;
290 $tmp= split("\|", trim($data, "|"));
291 $cell_formats= array();
293 foreach($tmp as $key => $dta){
295 $width= "";
296 $alignment= "";
297 $name= preg_replace("/\{[^\}]*+\}/", "", $dta);
298 $style= "";
300 /* Parse format string and detect width & alignment */
301 if(preg_match("/\{.*\}/", $dta)){
302 $format= preg_replace("/^[^\{]*+\{([^\}]*).*$/", "\\1", $dta);
304 /* Get aligment */
305 if(preg_match("/:/",$format)){
306 $al= preg_replace("/^[^:]*+:([a-z]*).*$/i", "\\1", $format);
308 if(preg_match("/T/i", $al)){
309 $alignment.= "top-";
310 $style.= "vertical-align: top;";
311 }
312 if(preg_match("/B/i", $al)){
313 $alignment.= "bottom-";
314 $style.= "vertical-align: bottom;";
315 }
316 if(preg_match("/R/i", $al)){
317 $alignment.= "right";
318 $style.= "text-align: right;";
319 }elseif(preg_match("/L/i", $al)){
320 $alignment.= "left";
321 $style.= "text-align: left;";
322 }elseif(preg_match("/C/i", $al) || preg_match("/M/i", $al) ){
323 $alignment.= "center";
324 $style.= "text-align: center;";
325 }
326 }
328 /* Get width */
329 $width = preg_replace("/^([^:]*).*$/","\\1", $format);
330 if(!empty($width)){
331 $style.= "width: ".$width.";";
332 }
333 }
335 $cell_formats[$key]= array("name" => $name, "width" => $width, "alignment" => $alignment, "style" => $style);
336 $this->numberOfCols++;
337 }
338 $this->headline= $cell_formats;
339 }
341 }
343 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
344 ?>