4bf3f5bc89ef52c94cd63fda5580df9f84bc26cb
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 $numberOfColumns= 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 entries from the ObjectList iterator into a string
100 Gets the entry descriptions from the ObjectList object and renders them.
101 \return HTML rendered list entries
102 */
103 private function renderEntry($entry){
105 /* Copy template */
106 $buffer= $this->entryFormat;
108 /* Replace set of attributes */
109 foreach ($this->attributes as $attribute){
110 if (!isset($entry[$attribute])){
111 throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
112 } else {
113 $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
114 }
115 }
117 /* Execute optional filters */
118 preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
119 foreach ($matches as $match){
120 $filterName= preg_replace('/,.+$/', '', $match[1]);
121 $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
122 $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/',
123 $this->applyEntryFilter($filterName, $filterParameter),
124 $buffer);
125 }
127 #TODO: Make $buffer a proper HTML table output
128 $tmp = split("\|",trim($buffer,"|"));
129 $cols = array();
131 for($i= 0; $i < $this->numberOfColumns; $i++){
133 /* If current entry is the last to appen, then skip adding styles */
134 if($i == ($this->numberOfColumns-1)){
135 $cols[$i]['style1'] = $this->columnInformation[$i]['style'];
136 $cols[$i]['style2'] = "width:100%;overflow:hidden;";
137 $cols[$i]['value'] = $tmp[$i];
138 }else{
139 $cols[$i]['style1'] = $this->columnInformation[$i]['style'];
140 $cols[$i]['style2'] = "width:100%;overflow:hidden;".$this->columnInformation[$i]['style'];
141 $cols[$i]['value'] = $tmp[$i];
142 }
143 }
145 /* Add class depending on given id, to alternate background colors */
146 if($this->colorAlternator++ & 1){
147 $data['row']['class'] = "ObjectListViewport_Entry_Row1";
148 }else{
149 $data['row']['class'] = "ObjectListViewport_Entry_Row2";
150 }
152 $data['cols'] = $cols;
153 return($data);
154 }
157 /*! \brief Applies filter to the given entry format string.
159 Instanciates the given ObjectListEntryFilter and calls the method.
161 \return rendered output
162 \sa ObjectListEntryFilter
163 */
164 private function applyEntryFilter($filterName, $string){
165 $className= "ObjectListEntryFilter_".$filterName;
166 $cl= new $className;
167 return $cl->filter("$string");
168 }
171 /*! \brief Renders complete ObjectList into a string
173 \return HTML rendered list
174 */
175 protected function __render() {
177 /* Apply current filter */
178 $entries = "";
179 $objects= new ObjectListFilterIterator($this->objects->getIterator());
180 foreach ($objects as $key => $value){
181 $entries[$key] = $this->renderEntry($value);
182 $this->createVariable("OLV_Entry_".$key);
183 }
185 #Fabian: _POST/_GET Variablen bitte mit $this->createVariable('name');
186 # erstellen.
187 # Damit kann das von der übergreifenden Funktion extrahiert werden
188 # und wir haben keinen doppelten Code dafür.
189 #
190 # Wurde z.B. eine Variable via $this->createVariable('cn') erzeugt und via
191 # smarty eingebunden, dann kann Sie nach einem _POST oder _GET via
192 # $this->getRequestVariable_cn() wieder abgefragt werden.
194 $smarty = get_smarty();
195 $smarty->assign("OLV_Entries",$entries);
196 $smarty->assign("OLV_List_Id",$this->id);
197 $smarty->assign("OLV_Multiselect_Enabled",$this->multiselect);
199 /* Footer variables */
200 $smarty->assign("OLV_Footer_Enabled",$this->displayFooterFlag);
201 $smarty->assign("OLV_Footer_Message",$this->footer);
202 $smarty->assign("OLV_Num_Cols",$this->numberOfColumns);
204 /* Assign Headline values */
205 $smarty->assign("OLV_Header_Enabled",$this->displayHeaderFlag);
206 $smarty->assign("OLV_Header",$this->columnInformation);
208 return($smarty->fetch("ObjectListViewport.tpl"));
209 }
212 /*! \brief Parses the given headline format string
214 \return Array with cell properties (width, alignment,name)
215 */
216 private function parseHeadline($data)
217 {
218 /* Each cell definition is seperated by |
219 * split by and go through each definition
220 */
221 $this->columnInformation= array();
222 $this->numberOfColumns= 0;
223 $tmp= split("\|", trim($data, "|"));
224 $cell_formats= array();
226 foreach($tmp as $key => $dta){
228 $width= "";
229 $alignment= "";
230 $name= preg_replace("/\{[^\}]*+\}/", "", $dta);
231 $style= "";
233 /* Parse format string and detect width & alignment */
234 if(preg_match("/\{.*\}/", $dta)){
235 $format= preg_replace("/^[^\{]*+\{([^\}]*).*$/", "\\1", $dta);
237 /* Get aligment */
238 if(preg_match("/:/",$format)){
239 $al= preg_replace("/^[^:]*+:([a-z]*).*$/i", "\\1", $format);
241 if(preg_match("/T/i", $al)){
242 $alignment.= "top-";
243 $style.= "vertical-align: top;";
244 }
246 if(preg_match("/B/i", $al)){
247 $alignment.= "bottom-";
248 $style.= "vertical-align: bottom;";
249 }
251 if(preg_match("/R/i", $al)){
252 $alignment.= "right";
253 $style.= "text-align: right;";
254 }elseif(preg_match("/L/i", $al)){
255 $alignment.= "left";
256 $style.= "text-align: left;";
257 }elseif(preg_match("/C/i", $al) || preg_match("/M/i", $al) ){
258 $alignment.= "center";
259 $style.= "text-align: center;";
260 }
261 }
263 /* Get width */
264 $width = preg_replace("/^([^:]*).*$/","\\1", $format);
265 if(!empty($width)){
266 $style.= "width: ".$width.";";
267 }
268 }
270 $cell_formats[$key]= array("name" => $name, "width" => $width, "alignment" => $alignment, "style" => $style);
271 $this->numberOfColumns++;
272 }
273 $this->columnInformation= $cell_formats;
274 }
276 }
278 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
279 ?>