Code

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