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