Code

Smarty Template for List generation
[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 $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 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 #/*! \brief Renders headline into a string
101 #    Gets the headline description from the ObjectList object and renders it.
103 #    \return HTML rendered headline
104 # */
105 #private function renderHeadline(){
106 #  $tpl =" <tr>
107 #            <td class='ObjectListViewport_TD_Header' id='ObjectListViewport_TD_Header_".$this->id."'>
108 #              <table class='ObjectListViewport_Header_Table' id='ObjectListViewport_Header_Table_".$this->id."'>
109 #                <tr>
110 #                  {content}
111 #                </tr>
112 #              </table>
113 #           </td>
114 #         </tr>";
115 #  $buffer ="";
116 #  foreach($this->headline as $key => $value){
117 #    $buffer .= "<td class='ObjectListViewport_Header_Cell' style='".$value['style']."'>".$value['name']."</td>\n";
118 #  }
119 #  return(preg_replace("/\{content\}/", $buffer,$tpl));
120 #}
123 #/*! \brief Renders footer into a string
125 #    Gets the footer description from the ObjectList object and renders it.
127 #    \return HTML rendered footer
128 # */
129 #private function renderFooter(){
130 #  $buffer ="<tr>
131 #            <td class='ObjectListViewport_TD_Footer' id='ObjectListViewport_TD_Footer_".$this->id."'>
132 #              <table class='ObjectListViewport_Footer_Table'>
133 #                <tr>
134 #                  <td class='ObjectListViewport_Footer_Cell' colspan='".count($this->headline)."'>".$this->footer."</td>
135 #                </tr>
136 #              </table>     
137 #            </td>
138 #         </tr>";
139 #  return $buffer;
140 #}
143   /* FIXME: replace this -> not our task */
144   private function getEntryIcon($entry,$alt = ""){
145     return("<img src='images/".$entry['_icon']."' alt='".$alt."' class='center'>");
146   }
149         /*! \brief Renders entries from the ObjectList iterator into a string
150             Gets the entry descriptions from the ObjectList object and renders them.
151       \return HTML rendered list entries
152          */
153   private function renderEntry($entry){
155     /* Copy template */
156     $buffer= $this->entryFormat;
158     /* Replace set of attributes */
159     foreach ($this->attributes as $attribute){
160       if (!isset($entry[$attribute])){
161         throw new ObjectListViewportException(sprintf(_("Can't locate attribute '%s' to replace in entry!"), $attribute));
162       } else {
164         if(preg_match("/_icon/i",$attribute)){
165           $buffer= preg_replace('/\{'.$attribute.'\}/', $this->getEntryIcon($entry),$buffer); 
166         }else{
167           $buffer= preg_replace('/\{'.$attribute.'\}/', $entry[$attribute],$buffer);
168         }
169       }
170     }
172     /* Execute optional filters */
173     preg_match_all ( '/\{_filter\(([^)]+)\)\}/', $buffer, $matches, PREG_SET_ORDER);
174     foreach ($matches as $match){
175       $filterName= preg_replace('/,.+$/', '', $match[1]);
176       $filterParameter= preg_replace('/^[^,]+,/', '', $match[1]);
177       $buffer= preg_replace('/\{_filter\('.normalizePreg($match[1]).'\)\}/',
178                             $this->applyEntryFilter($filterName, $filterParameter),
179                             $buffer);
180     }
182     #TODO: Make $buffer a proper HTML table output
183     $tmp = split("\|",trim($buffer,"|"));  
184     $cols = array();
186     for($i= 0; $i < $this->numberOfCols; $i++){
188       /* If current entry is the last to appen, then skip adding styles */
189       if($i == ($this->numberOfCols-1)){
190         $cols[$i]['style1'] = $this->headline[$i]['style'];
191         $cols[$i]['style2'] = "width:100%;overflow:hidden;";
192         $cols[$i]['value']   = $tmp[$i];
193       }else{
194         $cols[$i]['style1'] = $this->headline[$i]['style'];
195         $cols[$i]['style2'] = "width:100%;overflow:hidden;".$this->headline[$i]['style'];
196         $cols[$i]['value']   = $tmp[$i];
197       }
198     }
200     /* Add class depending on given id, to alternate background colors */
201     if($this->colorAlternator++ & 1){
202       $data['row']['class'] = "ObjectListViewport_Entry_Row1";
203     }else{
204       $data['row']['class'] = "ObjectListViewport_Entry_Row2";
205     }
207     $data['cols'] = $cols;
208     return($data);
209   }
212         /*! \brief Applies filter to the given entry format string.
214             Instanciates the given ObjectListEntryFilter and calls the method.
216       \return rendered output
217       \sa ObjectListEntryFilter
218          */
219   private function applyEntryFilter($filterName, $string){
220     $className= "ObjectListEntryFilter_".$filterName;
221     $cl= new $className;
222     return $cl->filter("$string");
223   }
226         /*! \brief Renders complete ObjectList into a string
228       \return HTML rendered list
229          */
230   protected function __render() {
232     /* Apply current filter */
233     $entries = "";
234     $objects= new ObjectListFilterIterator($this->objects->getIterator());
235     foreach ($objects as $value){
236       $entries[] = $this->renderEntry($value);
237     }
240     #Fabian: _POST/_GET Variablen bitte mit $this->createVariable('name');
241     #        erstellen.
242     #        Damit kann das von der übergreifenden Funktion extrahiert werden
243     #        und wir haben keinen doppelten Code dafür.
244     #        
245     #        Wurde z.B. eine Variable via $this->createVariable('cn') erzeugt und via
246     #        smarty eingebunden, dann kann Sie nach einem _POST oder _GET via
247     #        $this->getRequestVariable_cn() wieder abgefragt werden.
249     $smarty = get_smarty();
250     $smarty->assign("OLV_Entries",$entries);
251     $smarty->assign("OLV_List_Id",$this->id);
253     /* Footer variables */
254     $smarty->assign("OLV_Footer_Enabled",$this->displayFooterFlag);
255     $smarty->assign("OLV_Footer_Message",$this->footer);
256     $smarty->assign("OLV_Num_Cols",$this->numberOfCols);
258     /* Assign Headline values */
259     $smarty->assign("OLV_Header_Enabled",$this->displayHeaderFlag);
260     $smarty->assign("OLV_Header",$this->headline);
261  
262     return($smarty->fetch("ObjectListViewport.tpl"));
263   }
266         /*! \brief Parses the given headline format string 
268       \return Array with cell properties (width, alignment,name)
269          */
270   private function parseHeadline($data)
271   {
272     /* Each cell definition is seperated by | 
273      *  split by and go through each definition
274      */
275     $this->headline= array();
276     $this->numberOfCols= 0;
277     $tmp= split("\|", trim($data, "|")); 
278     $cell_formats= array();
280     foreach($tmp as $key => $dta){
282       $width= "";
283       $alignment= "";
284       $name= preg_replace("/\{[^\}]*+\}/", "", $dta);
285       $style= "";
286     
287       /* Parse format string and detect width & alignment */
288       if(preg_match("/\{.*\}/", $dta)){
289         $format= preg_replace("/^[^\{]*+\{([^\}]*).*$/", "\\1", $dta);
290     
291         /* Get aligment */
292         if(preg_match("/:/",$format)){
293           $al= preg_replace("/^[^:]*+:([a-z]*).*$/i", "\\1", $format);
295           if(preg_match("/T/i", $al)){
296             $alignment.= "top-";
297             $style.= "vertical-align: top;";
298           }
300           if(preg_match("/B/i", $al)){
301             $alignment.= "bottom-";
302             $style.= "vertical-align: bottom;";
303           }
305           if(preg_match("/R/i", $al)){
306             $alignment.= "right";
307             $style.= "text-align: right;";
308           }elseif(preg_match("/L/i", $al)){
309             $alignment.= "left";
310             $style.= "text-align: left;";
311           }elseif(preg_match("/C/i", $al) || preg_match("/M/i", $al) ){
312             $alignment.= "center";
313             $style.= "text-align: center;";
314           }
315         }
317         /* Get width */
318         $width = preg_replace("/^([^:]*).*$/","\\1", $format);
319         if(!empty($width)){
320           $style.= "width: ".$width.";";
321         }
322       }
324       $cell_formats[$key]= array("name" => $name, "width" => $width, "alignment" => $alignment, "style" => $style);
325       $this->numberOfCols++;
326     }
327     $this->headline= $cell_formats;
328   }
332 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
333 ?>