Provide better error reporting and error checking when updating a network
[www-register-wizard.git] / libraries / Table.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.3.1\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * HTML Table Generating Class\r
20  *\r
21  * Lets you create tables manually or from database result objects, or arrays.\r
22  *\r
23  * @package             CodeIgniter\r
24  * @subpackage  Libraries\r
25  * @category    HTML Tables\r
26  * @author              ExpressionEngine Dev Team\r
27  * @link                http://codeigniter.com/user_guide/libraries/uri.html\r
28  */\r
29 class CI_Table {\r
30 \r
31         var $rows                               = array();\r
32         var $heading                    = array();\r
33         var $auto_heading               = TRUE; \r
34         var $caption                    = NULL; \r
35         var $template                   = NULL;\r
36         var $newline                    = "\n";\r
37         var $empty_cells                = "";\r
38         \r
39         \r
40         function CI_Table()\r
41         {\r
42                 log_message('debug', "Table Class Initialized");\r
43         }\r
44 \r
45         // --------------------------------------------------------------------\r
46 \r
47         /**\r
48          * Set the template\r
49          *\r
50          * @access      public\r
51          * @param       array\r
52          * @return      void\r
53          */\r
54         function set_template($template)\r
55         {\r
56                 if ( ! is_array($template))\r
57                 {\r
58                         return FALSE;\r
59                 }\r
60         \r
61                 $this->template = $template;\r
62         }\r
63 \r
64         // --------------------------------------------------------------------\r
65 \r
66         /**\r
67          * Set the table heading\r
68          *\r
69          * Can be passed as an array or discreet params\r
70          *\r
71          * @access      public\r
72          * @param       mixed\r
73          * @return      void\r
74          */\r
75         function set_heading()\r
76         {\r
77                 $args = func_get_args();\r
78                 $this->heading = (is_array($args[0])) ? $args[0] : $args;\r
79         }\r
80 \r
81         // --------------------------------------------------------------------\r
82 \r
83         /**\r
84          * Set columns.  Takes a one-dimensional array as input and creates\r
85          * a multi-dimensional array with a depth equal to the number of\r
86          * columns.  This allows a single array with many elements to  be\r
87          * displayed in a table that has a fixed column count.\r
88          *\r
89          * @access      public\r
90          * @param       array\r
91          * @param       int\r
92          * @return      void\r
93          */\r
94         function make_columns($array = array(), $col_limit = 0)\r
95         {\r
96                 if ( ! is_array($array) OR count($array) == 0)\r
97                 {\r
98                         return FALSE;\r
99                 }\r
100                 \r
101                 // Turn off the auto-heading feature since it's doubtful we \r
102                 // will want headings from a one-dimensional array\r
103                 $this->auto_heading = FALSE;\r
104                 \r
105                 if ($col_limit == 0)\r
106                 {\r
107                         return $array;\r
108                 }\r
109         \r
110                 $new = array();\r
111                 while(count($array) > 0)\r
112                 {       \r
113                         $temp = array_splice($array, 0, $col_limit);\r
114                         \r
115                         if (count($temp) < $col_limit)\r
116                         {\r
117                                 for ($i = count($temp); $i < $col_limit; $i++)\r
118                                 {\r
119                                         $temp[] = '&nbsp;';\r
120                                 }\r
121                         }\r
122                         \r
123                         $new[] = $temp;\r
124                 }\r
125                 \r
126                 return $new;\r
127         }\r
128 \r
129         // --------------------------------------------------------------------\r
130 \r
131         /**\r
132          * Set "empty" cells\r
133          *\r
134          * Can be passed as an array or discreet params\r
135          *\r
136          * @access      public\r
137          * @param       mixed\r
138          * @return      void\r
139          */\r
140         function set_empty($value)\r
141         {\r
142                 $this->empty_cells = $value;\r
143         }\r
144         \r
145         // --------------------------------------------------------------------\r
146 \r
147         /**\r
148          * Add a table row\r
149          *\r
150          * Can be passed as an array or discreet params\r
151          *\r
152          * @access      public\r
153          * @param       mixed\r
154          * @return      void\r
155          */\r
156         function add_row()\r
157         {\r
158                 $args = func_get_args();\r
159                 $this->rows[] = (is_array($args[0])) ? $args[0] : $args;\r
160         }\r
161 \r
162         // --------------------------------------------------------------------\r
163 \r
164         /**\r
165          * Add a table caption\r
166          *\r
167          * @access      public\r
168          * @param       string\r
169          * @return      void\r
170          */\r
171         function set_caption($caption)\r
172         {\r
173                 $this->caption = $caption;\r
174         }       \r
175 \r
176         // --------------------------------------------------------------------\r
177 \r
178         /**\r
179          * Generate the table\r
180          *\r
181          * @access      public\r
182          * @param       mixed\r
183          * @return      string\r
184          */\r
185         function generate($table_data = NULL)\r
186         {\r
187                 // The table data can optionally be passed to this function\r
188                 // either as a database result object or an array\r
189                 if ( ! is_null($table_data))\r
190                 {\r
191                         if (is_object($table_data))\r
192                         {\r
193                                 $this->_set_from_object($table_data);\r
194                         }\r
195                         elseif (is_array($table_data))\r
196                         {\r
197                                 $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;\r
198                                 $this->_set_from_array($table_data, $set_heading);\r
199                         }\r
200                 }\r
201         \r
202                 // Is there anything to display?  No?  Smite them!\r
203                 if (count($this->heading) == 0 AND count($this->rows) == 0)\r
204                 {\r
205                         return 'Undefined table data';\r
206                 }\r
207         \r
208                 // Compile and validate the template date\r
209                 $this->_compile_template();\r
210         \r
211         \r
212                 // Build the table!\r
213                 \r
214                 $out = $this->template['table_open'];\r
215                 $out .= $this->newline;         \r
216 \r
217                 // Add any caption here\r
218                 if ($this->caption)\r
219                 {\r
220                         $out .= $this->newline;\r
221                         $out .= '<caption>' . $this->caption . '</caption>';\r
222                         $out .= $this->newline;\r
223                 }\r
224 \r
225                 // Is there a table heading to display?\r
226                 if (count($this->heading) > 0)\r
227                 {\r
228                         $out .= $this->template['heading_row_start'];\r
229                         $out .= $this->newline;         \r
230 \r
231                         foreach($this->heading as $heading)\r
232                         {\r
233                                 $out .= $this->template['heading_cell_start'];\r
234                                 $out .= $heading;\r
235                                 $out .= $this->template['heading_cell_end'];\r
236                         }\r
237 \r
238                         $out .= $this->template['heading_row_end'];\r
239                         $out .= $this->newline;                         \r
240                 }\r
241 \r
242                 // Build the table rows\r
243                 if (count($this->rows) > 0)\r
244                 {\r
245                         $i = 1;\r
246                         foreach($this->rows as $row)\r
247                         {\r
248                                 if ( ! is_array($row))\r
249                                 {\r
250                                         break;\r
251                                 }\r
252                         \r
253                                 // We use modulus to alternate the row colors\r
254                                 $name = (fmod($i++, 2)) ? '' : 'alt_';\r
255                         \r
256                                 $out .= $this->template['row_'.$name.'start'];\r
257                                 $out .= $this->newline;         \r
258         \r
259                                 foreach($row as $cell)\r
260                                 {\r
261                                         $out .= $this->template['cell_'.$name.'start'];\r
262                                         \r
263                                         if ($cell === "")\r
264                                         {\r
265                                                 $out .= $this->empty_cells;\r
266                                         }\r
267                                         else\r
268                                         {\r
269                                                 $out .= $cell;\r
270                                         }\r
271                                         \r
272                                         $out .= $this->template['cell_'.$name.'end'];\r
273                                 }\r
274         \r
275                                 $out .= $this->template['row_'.$name.'end'];\r
276                                 $out .= $this->newline; \r
277                         }\r
278                 }\r
279 \r
280                 $out .= $this->template['table_close'];\r
281         \r
282                 return $out;\r
283         }\r
284         \r
285         // --------------------------------------------------------------------\r
286 \r
287         /**\r
288          * Clears the table arrays.  Useful if multiple tables are being generated\r
289          *\r
290          * @access      public\r
291          * @return      void\r
292          */\r
293         function clear()\r
294         {\r
295                 $this->rows                             = array();\r
296                 $this->heading                  = array();\r
297                 $this->auto_heading             = TRUE; \r
298         }\r
299         \r
300         // --------------------------------------------------------------------\r
301 \r
302         /**\r
303          * Set table data from a database result object\r
304          *\r
305          * @access      public\r
306          * @param       object\r
307          * @return      void\r
308          */\r
309         function _set_from_object($query)\r
310         {\r
311                 if ( ! is_object($query))\r
312                 {\r
313                         return FALSE;\r
314                 }\r
315                 \r
316                 // First generate the headings from the table column names\r
317                 if (count($this->heading) == 0)\r
318                 {\r
319                         if ( ! method_exists($query, 'list_fields'))\r
320                         {\r
321                                 return FALSE;\r
322                         }\r
323                         \r
324                         $this->heading = $query->list_fields();\r
325                 }\r
326                                 \r
327                 // Next blast through the result array and build out the rows\r
328                 \r
329                 if ($query->num_rows() > 0)\r
330                 {\r
331                         foreach ($query->result_array() as $row)\r
332                         {\r
333                                 $this->rows[] = $row;\r
334                         }\r
335                 }\r
336         }\r
337 \r
338         // --------------------------------------------------------------------\r
339 \r
340         /**\r
341          * Set table data from an array\r
342          *\r
343          * @access      public\r
344          * @param       array\r
345          * @return      void\r
346          */\r
347         function _set_from_array($data, $set_heading = TRUE)\r
348         {\r
349                 if ( ! is_array($data) OR count($data) == 0)\r
350                 {\r
351                         return FALSE;\r
352                 }\r
353                 \r
354                 $i = 0;\r
355                 foreach ($data as $row)\r
356                 {\r
357                         if ( ! is_array($row))\r
358                         {\r
359                                 $this->rows[] = $data;\r
360                                 break;\r
361                         }\r
362                                                 \r
363                         // If a heading hasn't already been set we'll use the first row of the array as the heading\r
364                         if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)\r
365                         {\r
366                                 $this->heading = $row;\r
367                         }\r
368                         else\r
369                         {\r
370                                 $this->rows[] = $row;\r
371                         }\r
372                         \r
373                         $i++;\r
374                 }\r
375         }\r
376 \r
377         // --------------------------------------------------------------------\r
378 \r
379         /**\r
380          * Compile Template\r
381          *\r
382          * @access      private\r
383          * @return      void\r
384          */\r
385         function _compile_template()\r
386         {       \r
387                 if ($this->template == NULL)\r
388                 {\r
389                         $this->template = $this->_default_template();\r
390                         return;\r
391                 }\r
392                 \r
393                 $this->temp = $this->_default_template();\r
394                 foreach (array('table_open','heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', 'row_start', 'row_end', 'cell_start', 'cell_end', 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', 'table_close') as $val)\r
395                 {\r
396                         if ( ! isset($this->template[$val]))\r
397                         {\r
398                                 $this->template[$val] = $this->temp[$val];\r
399                         }\r
400                 }       \r
401         }\r
402         \r
403         // --------------------------------------------------------------------\r
404 \r
405         /**\r
406          * Default Template\r
407          *\r
408          * @access      private\r
409          * @return      void\r
410          */\r
411         function _default_template()\r
412         {\r
413                 return  array (\r
414                                                 'table_open'                    => '<table border="0" cellpadding="4" cellspacing="0">',\r
415 \r
416                                                 'heading_row_start'     => '<tr>',\r
417                                                 'heading_row_end'               => '</tr>',\r
418                                                 'heading_cell_start'    => '<th>',\r
419                                                 'heading_cell_end'              => '</th>',\r
420 \r
421                                                 'row_start'                     => '<tr>',\r
422                                                 'row_end'                               => '</tr>',\r
423                                                 'cell_start'                    => '<td>',\r
424                                                 'cell_end'                              => '</td>',\r
425 \r
426                                                 'row_alt_start'                 => '<tr>',\r
427                                                 'row_alt_end'                   => '</tr>',\r
428                                                 'cell_alt_start'                => '<td>',\r
429                                                 'cell_alt_end'                  => '</td>',\r
430 \r
431                                                 'table_close'                   => '</table>'\r
432                                         );      \r
433         }\r
434         \r
435 \r
436 }\r
437 \r
438 \r
439 /* End of file Table.php */\r
440 /* Location: ./system/libraries/Table.php */