eb454a53f16c4f88d596d8a2286b4156a8b82e66
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 extends GOsaGuiElement {
28 /* Internal variable for color alternation */
29 private $colorAlternator= 0;
31 protected $headline;
32 protected $footer;
33 protected $entryFormat;
35 /* Dummy here ----> */
36 private $attributes= array('cn', '_icon', '_actions', 'dn');
37 /* <---- Dummy here */
39 protected $displayHeaderFlag= TRUE;
40 protected $displayFooterFlag= TRUE;
41 private $numberOfCols= 0;
43 /*!
44 \brief Container for objects
46 This variable stores the ObjectList object to be displayed.
47 */
48 protected $objects;
50 /*!
51 \brief Switch to handle multiselect or not
52 */
53 protected $multiselect;
55 /*! \brief ObjectListViewport constructor
57 The ObjectListViewport class renders/handles the ObjectList defined by $config.
59 \param config Config section that is used to configure this ObjectListViewport
60 */
61 public function __construct($config, $multiselect= TRUE, $displayHeader= TRUE, $displayFooter= FALSE){
63 /* Initialize from parents method */
64 parent::__construct();
66 /* Transfer initialization values */
67 $this->displayFooterFlag= $displayFooter;
68 $this->displayHeaderFlag= $displayHeader;
69 $this->multiselect= $multiselect;
71 /* Load list configuration from ConfigManager */
72 $cr= Registry::getInstance("ConfigManager");
73 $cr->setSection($config);
74 $this->parseHeadline($cr->getValue("headline"));
75 $this->footer= $cr->getValue("footer");
76 $this->entryFormat= $cr->getValue("entryFormat");
78 /* Load and instanciate classes, extract filter, icons, view hooks, etc. */
79 $this->objects= new ObjectList($config);
80 }
83 /*! \brief Processes post events
85 Processes all post events and acts as needed.
87 \return bool for if changes are present or not
88 */
89 protected function __process(){
91 /* FIXME: Dummy Action*/
92 $this->objects->reload();
94 /* TODO: process input */
95 return FALSE;
96 }
99 #/*! \brief Renders headline into a string
100 #
101 # Gets the headline description from the ObjectList object and renders it.
102 #
103 # \return HTML rendered headline
104 # */
105 #private function renderHeadline(){
106 # $tpl =" <tr>
107 # <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header_".$this->id."'>
108 # <table class='ObjectListViewport_Header_Table' id='ObjectListViewport_Header_Table_".$this->id."'>
109 # <tr>
110 # {content}
111 # </tr>
112 # </table>
113 # </td>
114 # </tr>";
115 # $buffer ="";
116 # foreach($this->headline as $key => $value){
117 # $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
118 # }
119 # return(preg_replace("/\{content\}/", $buffer,$tpl));
120 #}
123 #/*! \brief Renders footer into a string
124 #
125 # Gets the footer description from the ObjectList object and renders it.
126 #
127 # \return HTML rendered footer
128 # */
129 #private function renderFooter(){
130 # $buffer ="<tr>
131 # <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer_".$this->id."'>
132 # <table class='ObjectListViewport_Footer_Table'>
133 # <tr>
134 # <td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>
135 # </tr>
136 # </table>
137 # </td>
138 # </tr>";
139 # return $buffer;
140 #}
143 /* FIXME: replace this -> not our task */
144 private function getEntryIcon($entry,$alt = ""){
145 return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
146 }
149 /*! \brief Renders entries from the ObjectList iterator into a string
150 Gets the entry descriptions from the ObjectList object and renders them.
151 \return HTML rendered list entries
152 */
153 private function renderEntry($entry){
155 /* Copy template */
156 $buffer= $this->entryFormat;
158 /* Replace set of attributes */
159 foreach ($this->attributes as $attribute){
160 if (!isset($entry[$attribute])){
161 throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
162 } else {
164 if(preg_match("/_icon/i",$attribute)){
165 $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer);
166 }else{
167 $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
168 }
169 }
170 }
172 /* Execute optional filters */
173 preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
174 foreach ($matches as $match){
175 $filterName= preg_replace('/,.+$/', '', $match[1]);
176 $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
177 $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/',
178 $this->applyEntryFilter($filterName, $filterParameter),
179 $buffer);
180 }
182 #TODO: Make $buffer a proper HTML table output
183 $tmp = split("\|",trim($buffer,"|"));
185 /* Define entry template */
186 $tpl = "<td class='ObjectListViewport_Entry_Cell' style='{style_1}'>
187 <div style='{style_2}'>
188 {content}
189 </div>
190 </td>
191 ";
192 /* Template vriables to replace */
193 $attrs = array("/\{style_1\}/","/\{style_2\}/","/\{content\}/");
195 /* Append template for each given col */
196 $buffer ="";
197 for($i= 0; $i < $this->numberOfCols; $i++){
199 /* If current entry is the last to appen, then skip adding styles */
200 if($i == ($this->numberOfCols-1)){
201 $buffer.= preg_replace( $attrs,
202 array($this->headline[$i]['style'],"width:100%;overflow:hidden;",$tmp[$i]),$tpl);
203 }else{
204 $buffer.= preg_replace( $attrs,
205 array($this->headline[$i]['style'],"width:100%;overflow:hidden;".$this->headline[$i]['style'],$tmp[$i]),$tpl);
206 }
207 }
209 /* Add class depending on given id, to alternate background colors */
210 if($this->colorAlternator++ & 1){
211 $a = "class='ObjectListViewport_Entry_Row1'";
212 }else{
213 $a = "class='ObjectListViewport_Entry_Row2'";
214 }
216 return "<tr ".$a.">\n".$buffer."</tr>\n";
217 }
220 /*! \brief Applies filter to the given entry format string.
222 Instanciates the given ObjectListEntryFilter and calls the method.
224 \return rendered output
225 \sa ObjectListEntryFilter
226 */
227 private function applyEntryFilter($filterName, $string){
228 $className= "ObjectListEntryFilter_".$filterName;
229 $cl= new $className;
230 return $cl->filter("$string");
231 }
234 /*! \brief Renders complete ObjectList into a string
236 \return HTML rendered list
237 */
238 protected function __render() {
240 /* Apply current filter */
241 $entries = "";
242 $objects= new ObjectListFilterIterator($this->objects->getIterator());
243 foreach ($objects as $value){
244 $entries .= $this->renderEntry($value);
245 }
247 return "README ;-)\n";
249 #Fabian: _POST/_GET Variablen bitte mit $this->createVariable('name');
250 # erstellen.
251 # Damit kann das von der übergreifenden Funktion extrahiert werden
252 # und wir haben keinen doppelten Code dafür.
253 #
254 # Wurde z.B. eine Variable via $this->createVariable('cn') erzeugt und via
255 # smarty eingebunden, dann kann Sie nach einem _POST oder _GET via
256 # $this->getRequestVariable_cn() wieder abgefragt werden.
258 #$smarty = get_smarty();
259 #$smarty->assign("OLV_Entries",array("Doesn't work" => "Does not work."));//$objects);
260 #$smarty->assign("OLV_List_Id",$this->id);
262 #/* Footer variables */
263 #$smarty->assign("OLV_Footer_Enabled",$this->displayFooterFlag);
264 #$smarty->assign("OLV_Footer_Message",$this->footer);
265 #$smarty->assign("OLV_Num_Cols",$this->numberOfCols);
267 #/* Assign Headline values */
268 #$smarty->assign("OLV_Header_Enabled",$this->displayHeaderFlag);
269 #$smarty->assign("OLV_Header",$this->headline);
271 #return($smarty->fetch("ObjectListViewport.tpl"));
272 }
275 /*! \brief Parses the given headline format string
277 \return Array with cell properties (width, alignment,name)
278 */
279 private function parseHeadline($data)
280 {
281 /* Each cell definition is seperated by |
282 * split by and go through each definition
283 */
284 $this->headline= array();
285 $this->numberOfCols= 0;
286 $tmp= split("\|", trim($data, "|"));
287 $cell_formats= array();
289 foreach($tmp as $key => $dta){
291 $width= "";
292 $alignment= "";
293 $name= preg_replace("/\{[^\}]*+\}/", "", $dta);
294 $style= "";
296 /* Parse format string and detect width & alignment */
297 if(preg_match("/\{.*\}/", $dta)){
298 $format= preg_replace("/^[^\{]*+\{([^\}]*).*$/", "\\1", $dta);
300 /* Get aligment */
301 if(preg_match("/:/",$format)){
302 $al= preg_replace("/^[^:]*+:([a-z]*).*$/i", "\\1", $format);
304 if(preg_match("/T/i", $al)){
305 $alignment.= "top-";
306 $style.= "vertical-align: top;";
307 }
309 if(preg_match("/B/i", $al)){
310 $alignment.= "bottom-";
311 $style.= "vertical-align: bottom;";
312 }
314 if(preg_match("/R/i", $al)){
315 $alignment.= "right";
316 $style.= "text-align: right;";
317 }elseif(preg_match("/L/i", $al)){
318 $alignment.= "left";
319 $style.= "text-align: left;";
320 }elseif(preg_match("/C/i", $al) || preg_match("/M/i", $al) ){
321 $alignment.= "center";
322 $style.= "text-align: center;";
323 }
324 }
326 /* Get width */
327 $width = preg_replace("/^([^:]*).*$/","\\1", $format);
328 if(!empty($width)){
329 $style.= "width: ".$width.";";
330 }
331 }
333 $cell_formats[$key]= array("name" => $name, "width" => $width, "alignment" => $alignment, "style" => $style);
334 $this->numberOfCols++;
335 }
336 $this->headline= $cell_formats;
337 }
339 }
341 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
342 ?>