Code

Updated class hirachy
[gosa.git] / include / class_ObjectListViewport.inc
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 }
15  
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   protected $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 ID used to identify objects of same list */
56   protected $id = "";
58   
59   /*! \brief ObjectListViewport constructor
61     The ObjectListViewport class renders/handles the ObjectList defined by $config.
63     \param config Config section that is used to configure this ObjectListViewport
64    */
65         public function __construct($config, $multiselect= TRUE, $displayHeader= TRUE, $displayFooter= FALSE){
67     /* Transfer initialization values */
68     $this->displayFooterFlag= $displayFooter;
69     $this->displayHeaderFlag= $displayHeader;
70     $this->multiselect= $multiselect;
72     /* Load list configuration from ConfigManager */
73     $cr= Registry::getInstance("ConfigManager");
74     $cr->setSection($config);
75     $this->parseHeadline($cr->getValue("headline"));
76     $this->footer= $cr->getValue("footer");
77     $this->entryFormat= $cr->getValue("entryFormat");
79     /* Load and instanciate classes, extract filter, icons, view hooks, etc. */
80     $this->objects= new ObjectList($config);
82     /* generate an unique id */
83     $this->id= uniqid();
84   }
87         /*! \brief Handles _POST / _GET events
89             Processes the list of registered plugins to do their eventHandler and adapt
90       internal objectlist according to this.
91          */
92   public function eventHandler(){
93     /* Reloads the list if things have changed interally */
94     $this->objects->reload();
95   }
98         /*! \brief Processes post events
100             Processes all post events and acts as needed.
102       \return bool for if changes are present or not
103          */
104   protected function __process(){
106     /* TODO: process input */
107     return FALSE;
108   }
111 #/*! \brief Renders headline into a string
113 #    Gets the headline description from the ObjectList object and renders it.
115 #    \return HTML rendered headline
116 # */
117 #private function renderHeadline(){
118 #  $tpl =" <tr>
119 #            <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header_".$this->id."'>
120 #              <table class='ObjectListViewport_Header_Table' id='ObjectListViewport_Header_Table_".$this->id."'>
121 #                <tr>
122 #                  {content}
123 #                </tr>
124 #              </table>
125 #           </td>
126 #         </tr>";
127 #  $buffer ="";
128 #  foreach($this->headline as $key => $value){
129 #    $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
130 #  }
131 #  return(preg_replace("/\{content\}/", $buffer,$tpl));
132 #}
135 #/*! \brief Renders footer into a string
137 #    Gets the footer description from the ObjectList object and renders it.
139 #    \return HTML rendered footer
140 # */
141 #private function renderFooter(){
142 #  $buffer ="<tr>
143 #            <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer_".$this->id."'>
144 #              <table class='ObjectListViewport_Footer_Table'>
145 #                <tr>
146 #                  <td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>
147 #                </tr>
148 #              </table>     
149 #            </td>
150 #         </tr>";
151 #  return $buffer;
152 #}
155   /* TODO: replace this -> not our task */
156   private function getEntryIcon($entry,$alt = ""){
157     return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
158   }
161         /*! \brief Renders entries from the ObjectList iterator into a string
162             Gets the entry descriptions from the ObjectList object and renders them.
163       \return HTML rendered list entries
164          */
165   private function renderEntry($entry){
167     /* Copy template */
168     $buffer= $this->entryFormat;
170     /* Replace set of attributes */
171     foreach ($this->attributes as $attribute){
172       if (!isset($entry[$attribute])){
173         throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
174       } else {
176         if(preg_match("/_icon/i",$attribute)){
177           $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer); 
178         }else{
179           $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
180         }
181       }
182     }
184     /* Execute optional filters */
185     preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
186     foreach ($matches as $match){
187       $filterName= preg_replace('/,.+$/', '', $match[1]);
188       $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
189       $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/', $this->applyEntryFilter($filterName, $filterParameter), $buffer);
190     }
192     #TODO: Make $buffer a proper HTML table output
193     $tmp = split("\|",trim($buffer,"|"));  
195     /* define entry template */
196     $tpl = "<td class='ObjectListViewport_Entry_Cell' style='{style_1}'>
197               <div style='{style_2}'>
198                 {content}
199               </div>
200             </td>
201             ";
202     /* Template vriables to replace */
203     $attrs = array("/\{style_1\}/","/\{style_2\}/","/\{content\}/");
205     /* Append template for each given col */
206     $buffer ="";
207     for($i= 0; $i < $this->numberOfCols; $i++){
209       /* If current entry is the last to appen, then skip adding styles */
210       if($i == ($this->numberOfCols-1)){
211         $buffer.= preg_replace( $attrs,
212             array($this->headline[$i]['style'],"width:100%;overflow:hidden;",$tmp[$i]),$tpl);
213       }else{
214         $buffer.= preg_replace( $attrs,
215             array($this->headline[$i]['style'],"width:100%;overflow:hidden;".$this->headline[$i]['style'],$tmp[$i]),$tpl);
216       }
217     }
219     /* Add class depending on given id, to alternate background colors */
220     if($this->colorAlternator++ & 1){
221       $a = "class='ObjectListViewport_Entry_Row1'";
222     }else{
223       $a = "class='ObjectListViewport_Entry_Row2'";
224     }
226     return "<tr ".$a.">\n".$buffer."</tr>\n";
227   }
230         /*! \brief Applies filter to the given entry format string.
232             Instanciates the given ObjectListEntryFilter and calls the method.
234       \return rendered output
235       \sa ObjectListEntryFilter
236          */
237   private function applyEntryFilter($filterName, $string){
238     $className= "ObjectListEntryFilter_".$filterName;
239     $cl= new $className;
240     return $cl->filter("$string");
241   }
244         /*! \brief Renders complete ObjectList into a string
246       \return HTML rendered list
247          */
248   protected function __render() {
250     /* Apply current filter */
251     $entries = "";
252     $objects= new ObjectListFilterIterator($this->objects->getIterator());
253     foreach ($objects as $value){
254       $entries .= $this->renderEntry($value);
255     }
257     $smarty = get_smarty();
258     $smarty->assign("OLV_Entries",array("Doesn't work" => "Does not work."));//$objects);
259     $smarty->assign("OLV_List_Id",$this->id);
261     /* Footer variables */
262     $smarty->assign("OLV_Footer_Enabled",$this->displayFooterFlag);
263     $smarty->assign("OLV_Footer_Message",$this->footer);
264     $smarty->assign("OLV_Num_Cols",$this->numberOfCols);
266     /* Assign Headline values */
267     $smarty->assign("OLV_Header_Enabled",$this->displayHeaderFlag);
268     $smarty->assign("OLV_Header",$this->headline);
269  
270     return($smarty->fetch("ObjectListViewport.tpl"));
271   }
274         /*! \brief Parses the given headline format string 
276       \return Array with cell properties (width, alignment,name)
277          */
278   private function parseHeadline($data)
279   {
280     /* Each cell definition is seperated by | 
281      *  split by and go through each definition
282      */
283     $this->headline= array();
284     $this->numberOfCols= 0;
285     $tmp= split("\|", trim($data, "|")); 
286     $cell_formats= array();
288     foreach($tmp as $key => $dta){
290       $width= "";
291       $alignment= "";
292       $name= preg_replace("/\{[^\}]*+\}/", "", $dta);
293       $style= "";
294     
295       /* Parse format string and detect width & alignment */
296       if(preg_match("/\{.*\}/", $dta)){
297         $format= preg_replace("/^[^\{]*+\{([^\}]*).*$/", "\\1", $dta);
298     
299         /* Get aligment */
300         if(preg_match("/:/",$format)){
301           $al= preg_replace("/^[^:]*+:([a-z]*).*$/i", "\\1", $format);
303           if(preg_match("/T/i", $al)){
304             $alignment.= "top-";
305             $style.= "vertical-align: top;";
306           }
307           if(preg_match("/B/i", $al)){
308             $alignment.= "bottom-";
309             $style.= "vertical-align: bottom;";
310           }
311           if(preg_match("/R/i", $al)){
312             $alignment.= "right";
313             $style.= "text-align: right;";
314           }elseif(preg_match("/L/i", $al)){
315             $alignment.= "left";
316             $style.= "text-align: left;";
317           }elseif(preg_match("/C/i", $al) || preg_match("/M/i", $al) ){
318             $alignment.= "center";
319             $style.= "text-align: center;";
320           }
321         }
323         /* Get width */
324         $width = preg_replace("/^([^:]*).*$/","\\1", $format);
325         if(!empty($width)){
326           $style.= "width: ".$width.";";
327         }
328       }
330       $cell_formats[$key]= array("name" => $name, "width" => $width, "alignment" => $alignment, "style" => $style);
331       $this->numberOfCols++;
332     }
333     $this->headline= $cell_formats;
334   }
338 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
339 ?>