7ff0e655f0cddf1043281a7e5b36c679f436aae9
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_Header_Table'>\n";
88 $buffer.="<tr>\n";
89 foreach($this->headline as $key => $value){
90 $buffer .= "<td style='".$value['style']."'>".$value['name']."</td>\n";
91 }
92 $buffer.="<td style='width:13px;'> </td>";
93 $buffer.="</tr>\n";
94 $buffer.="</table>\n";
95 return $buffer;
96 }
99 /*! \brief Renders footer into a string
101 Gets the footer description from the ObjectList object and renders it.
103 \return HTML rendered footer
104 */
105 private function renderFooter(){
106 $buffer ="<table class='ObjectListViewport_Footer_Table'>\n";
107 $buffer.="<tr>\n";
108 $buffer.= "<td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>\n";
109 $buffer.="</tr>\n";
110 $buffer.="</table>\n";
111 return $buffer;
112 }
115 private function getEntryIcon($entry,$alt = ""){
116 return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
117 }
120 /*! \brief Renders entries from the ObjectList iterator into a string
121 Gets the entry descriptions from the ObjectList object and renders them.
122 \return HTML rendered list entries
123 */
124 private function renderEntry($entry){
126 /* Copy template */
127 $buffer= $this->entryFormat;
129 /* Replace set of attributes */
130 foreach ($this->attributes as $attribute){
131 if (!isset($entry[$attribute])){
132 throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
133 } else {
135 if(preg_match("/_icon/i",$attribute)){
136 $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer);
137 }else{
138 $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
139 }
140 }
141 }
143 /* Execute optional filters */
144 preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
145 foreach ($matches as $match){
146 $filterName= preg_replace('/,.+$/', '', $match[1]);
147 $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
148 $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/', $this->applyEntryFilter($filterName, $filterParameter), $buffer);
149 }
151 #TODO: Make $buffer a proper HTML table output
153 $tmp = split("\|",trim($buffer,"|"));
156 $buffer="<tr>\n";
157 foreach($tmp as $key => $value){
159 if(1 || empty($this->headline[$key]['width']) || preg_match("/\%/",$this->headline[$key]['width'])){
160 $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
161 "<div style='overflow:hidden;".$this->headline[$key]['style']."'>".
162 $value.
163 "</div>".
164 "</td>\n";
165 }else{
166 $buffer .= "<td class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
167 $value.
168 "</td>\n";
169 }
170 }
171 $buffer.="</tr>\n";
173 return $buffer."\n";
174 }
177 /*! \brief Applies filter to the given entry format string.
179 Instanciates the given ObjectListEntryFilter and calls the method.
181 \return rendered output
182 \sa ObjectListEntryFilter
183 */
184 private function applyEntryFilter($filterName, $string){
185 $className= "ObjectListEntryFilter_".$filterName;
186 $cl= new $className;
187 return $cl->filter("$string");
188 }
191 /*! \brief Renders complete ObjectList into a string
193 \return HTML rendered list
194 */
195 public function render() {
197 $header = $this->renderHeadline();
198 $footer = $this->renderFooter();
200 /* Apply current filter */
201 $entries = "";
202 $objects= new ObjectListFilterIterator($this->objects->getIterator());
203 foreach ($objects as $value){
204 $entries .= $this->renderEntry($value);
205 }
209 /* Generate fixed headline */
210 $buffer = "
211 <table class='ObjectListViewport' id='ObjectListViewport' cellspacing=o cellpadding=0>
212 <tr>
213 <td>
214 <table class='ObjectListViewport_Table' id='ObjectListViewport_Table' cellpadding=0 cellspacing=0 >
215 <tr>
216 <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header'>
217 ".$header."
218 </td>
219 </tr>
220 <tr>
221 <td class='ObjectListViewport_TD_Entries' id='ObjectListViewport_TD_Entries'>
222 <div style='overflow:auto' id='ObjectListViewport_Entry_Cover'>
223 <table class='ObjectListViewport_Entry_Table' id='ObjectListViewport_Entry_Table'>
224 ".$entries."
225 </table>
226 </div>
227 </td>
228 </tr>
229 <tr>
230 <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer'>
231 ".$footer."
232 </td>
233 </tr>
234 </table>
236 </td>
237 </tr>
238 </table>
239 ";
241 return ($buffer);
242 }
245 /*! \brief Parses the given headline format string
247 \return Array with cell properties (width, alignment,name)
248 */
249 private function parseHeadline($data)
250 {
251 /* Each cell definition is seperated by |
252 * split by and go through each definition
253 */
254 $tmp = split("\|",trim($data,"|"));
255 $cell_formats = array();
256 foreach($tmp as $key => $data){
258 $s_width = "";
259 $s_alignment= "";
260 $s_name = preg_replace("/\{[^\}]*+\}/","",$data);
261 $s_style = "";
263 /* Parse format string and detect width & alignment */
264 if(preg_match("/\{.*\}/",$data)){
265 $s_format= preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
267 /* Get aligment */
268 if(preg_match("/:/",$s_format)){
269 $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
271 if(preg_match("/T/i",$s_al)){
272 $s_alignment.= "top-" ;
273 $s_style.= "vertical-align: top;";
274 }
275 if(preg_match("/B/i",$s_al)){
276 $s_alignment.= "bottom-" ;
277 $s_style.= "vertical-align: bottom;";
278 }
279 if(preg_match("/R/i",$s_al)){
280 $s_alignment.= "right" ;
281 $s_style.= "text-align: right;";
282 }elseif(preg_match("/L/i",$s_al)){
283 $s_alignment.= "left" ;
284 $s_style.= "text-align: left;";
285 }elseif(preg_match("/C/i",$s_al) || preg_match("/M/i",$s_al) ){
286 $s_alignment.= "center" ;
287 $s_style.= "text-align: center;";
288 }
289 }
291 /* Get width */
292 $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
293 if(!empty($s_width)){
294 $s_style = "width: ".$s_width.";";
295 }
297 $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
298 }
299 }
300 return($cell_formats);
301 }
305 }
307 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
308 ?>