959ae306775a8f651b15b2394f9a5690e15a9272
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.="<td style='width:12px;'> </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(empty($this->headline[$key]['width']) || preg_match("/\%/",$this->headline[$key]['width'])){
160 $buffer .= "<td nowrap class='ObjectListViewport_Entry_Cell' style='".$this->headline[$key]['style']."'>".
161 "<div style='overflow:hidden;width:100%;'>".
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 /* Generate fixed headline */
198 $buffer= $this->renderHeadline();
200 /* Apply current filter */
201 $objects= new ObjectListFilterIterator($this->objects->getIterator());
203 $buffer.="<div style='overflow:auto' id='ObjectListViewport_Entry_Cover'>";
204 $buffer.="<table class='ObjectListViewport_Entry_Table'>\n";
205 foreach ($objects as $value){
206 $buffer.= $this->renderEntry($value);
207 }
208 $buffer.="</table>\n";
209 $buffer.="</div>";
211 /* Generate footer */
212 $buffer.= $this->renderFooter();
214 return ($buffer);
215 }
218 /*! \brief Parses the given headline format string
220 \return Array with cell properties (width, alignment,name)
221 */
222 private function parseHeadline($data)
223 {
224 /* Each cell definition is seperated by |
225 * split by and go through each definition
226 */
227 $tmp = split("\|",trim($data,"|"));
228 $cell_formats = array();
229 foreach($tmp as $key => $data){
231 $s_width = "";
232 $s_alignment= "";
233 $s_name = preg_replace("/\{[^\}]*+\}/","",$data);
234 $s_style = "";
236 /* Parse format string and detect width & alignment */
237 if(preg_match("/\{.*\}/",$data)){
238 $s_format= preg_replace("/^[^\{]*+\{([^\}]*).*$/","\\1",$data);
240 /* Get aligment */
241 if(preg_match("/:/",$s_format)){
242 $s_al = preg_replace("/^[^:]*+:([a-z]*).*$/i","\\1",$s_format);
244 if(preg_match("/T/i",$s_al)){
245 $s_alignment.= "top-" ;
246 $s_style.= "vertical-align: top;";
247 }
248 if(preg_match("/B/i",$s_al)){
249 $s_alignment.= "bottom-" ;
250 $s_style.= "vertical-align: bottom;";
251 }
252 if(preg_match("/R/i",$s_al)){
253 $s_alignment.= "right" ;
254 $s_style.= "text-align: right;";
255 }elseif(preg_match("/L/i",$s_al)){
256 $s_alignment.= "left" ;
257 $s_style.= "text-align: left;";
258 }elseif(preg_match("/C/i",$s_al) || preg_match("/M/i",$s_al) ){
259 $s_alignment.= "center" ;
260 $s_style.= "text-align: center;";
261 }
262 }
264 /* Get width */
265 $s_width = preg_replace("/^([^:]*).*$/","\\1",$s_format);
266 if(!empty($s_width)){
267 $s_style = "width: ".$s_width.";";
268 }
270 $cell_formats[$key] = array("name" => $s_name, "width" => $s_width, "alignment" => $s_alignment,"style" => $s_style);
271 }
272 }
273 return($cell_formats);
274 }
278 }
280 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
281 ?>