admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.3.1 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * HTML Table Generating Class |
| 20 | * |
| 21 | * Lets you create tables manually or from database result objects, or arrays. |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @category HTML Tables |
| 26 | * @author Rick Ellis |
| 27 | * @link http://www.codeigniter.com/user_guide/libraries/uri.html |
| 28 | */ |
| 29 | class CI_Table { |
| 30 | |
| 31 | var $rows = array(); |
| 32 | var $heading = array(); |
| 33 | var $template = NULL; |
| 34 | var $newline = "\n"; |
| 35 | |
| 36 | |
| 37 | function CI_Table() |
| 38 | { |
| 39 | log_message('debug', "Table Class Initialized"); |
| 40 | } |
| 41 | |
| 42 | // -------------------------------------------------------------------- |
| 43 | |
| 44 | /** |
| 45 | * Set the template |
| 46 | * |
| 47 | * @access public |
| 48 | * @param array |
| 49 | * @return void |
| 50 | */ |
| 51 | function set_template($template) |
| 52 | { |
| 53 | if ( ! is_array()) |
| 54 | { |
| 55 | return FALSE; |
| 56 | } |
| 57 | |
| 58 | $this->template = $template; |
| 59 | } |
| 60 | |
| 61 | // -------------------------------------------------------------------- |
| 62 | |
| 63 | /** |
admin | a1931ad | 2006-10-09 04:17:38 +0000 | [diff] [blame] | 64 | * Set the table heading |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 65 | * |
| 66 | * Can be passed as an array or discreet params |
| 67 | * |
| 68 | * @access public |
| 69 | * @param mixed |
| 70 | * @return void |
| 71 | */ |
admin | a1931ad | 2006-10-09 04:17:38 +0000 | [diff] [blame] | 72 | function set_heading() |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 73 | { |
| 74 | $args = func_get_args(); |
| 75 | $this->heading = (is_array($args[0])) ? $args[0] : $args; |
| 76 | } |
| 77 | |
| 78 | // -------------------------------------------------------------------- |
| 79 | |
| 80 | /** |
| 81 | * Add a table row |
| 82 | * |
| 83 | * Can be passed as an array or discreet params |
| 84 | * |
| 85 | * @access public |
| 86 | * @param mixed |
| 87 | * @return void |
| 88 | */ |
| 89 | function add_row() |
| 90 | { |
| 91 | $args = func_get_args(); |
| 92 | $this->rows[] = (is_array($args[0])) ? $args[0] : $args; |
| 93 | } |
| 94 | |
| 95 | // -------------------------------------------------------------------- |
| 96 | |
| 97 | /** |
| 98 | * Generate the table |
| 99 | * |
| 100 | * @access public |
| 101 | * @param mixed |
| 102 | * @return string |
| 103 | */ |
| 104 | function generate($table_data = NULL) |
| 105 | { |
| 106 | // The table data can optionally be passed to this function |
| 107 | // either as a database result object or an array |
| 108 | if ( ! is_null($table_data)) |
| 109 | { |
| 110 | if (is_object($table_data)) |
| 111 | { |
| 112 | $this->_set_from_object($table_data); |
| 113 | } |
| 114 | elseif (is_array($table_data)) |
| 115 | { |
| 116 | $this->_set_from_array($table_data); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Is there anything to display? No? Smite them! |
| 121 | if (count($this->heading) == 0 AND count($this->rows) == 0) |
| 122 | { |
| 123 | return 'Undefined table data'; |
| 124 | } |
| 125 | |
| 126 | // Compile and validate the templata date |
| 127 | $this->_compile_template(); |
| 128 | |
| 129 | |
| 130 | // Build the table! |
| 131 | |
| 132 | $out = $this->template['table_open']; |
| 133 | $out .= $this->newline; |
| 134 | |
| 135 | // Is there a table heading to display? |
| 136 | if (count($this->heading) > 0) |
| 137 | { |
| 138 | $out .= $this->template['heading_row_start']; |
| 139 | $out .= $this->newline; |
| 140 | |
| 141 | foreach($this->heading as $heading) |
| 142 | { |
| 143 | $out .= $this->template['heading_cell_start']; |
| 144 | $out .= $heading; |
| 145 | $out .= $this->template['heading_cell_end']; |
| 146 | } |
| 147 | |
| 148 | $out .= $this->template['heading_row_end']; |
| 149 | $out .= $this->newline; |
| 150 | } |
| 151 | |
| 152 | // Build the table rows |
| 153 | if (count($this->rows) > 0) |
| 154 | { |
| 155 | $i = 1; |
| 156 | foreach($this->rows as $row) |
| 157 | { |
| 158 | if ( ! is_array($row)) |
| 159 | { |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | // We use modulus to alternate the row colors |
| 164 | $alt = (fmod($i++, 2)) ? '' : 'alt_'; |
| 165 | |
| 166 | $out .= $this->template['row_'.$alt.'start']; |
| 167 | $out .= $this->newline; |
| 168 | |
| 169 | foreach($row as $cells) |
| 170 | { |
| 171 | $out .= $this->template['cell_'.$alt.'start']; |
| 172 | $out .= $cells; |
| 173 | $out .= $this->template['cell_'.$alt.'end']; |
| 174 | } |
| 175 | |
| 176 | $out .= $this->template['row_'.$alt.'end']; |
| 177 | $out .= $this->newline; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | $out .= $this->template['table_close']; |
| 182 | |
| 183 | return $out; |
| 184 | } |
| 185 | |
| 186 | // -------------------------------------------------------------------- |
| 187 | |
| 188 | /** |
| 189 | * Set table data from a database result object |
| 190 | * |
| 191 | * @access public |
| 192 | * @param object |
| 193 | * @return void |
| 194 | */ |
| 195 | function _set_from_object($query) |
| 196 | { |
| 197 | if ( ! is_object($query)) |
| 198 | { |
| 199 | return FALSE; |
| 200 | } |
| 201 | |
| 202 | // First generate the headings from the table column names |
| 203 | if (count($this->heading) == 0) |
| 204 | { |
| 205 | if ( ! method_exists($query, 'field_names')) |
| 206 | { |
| 207 | return FALSE; |
| 208 | } |
| 209 | |
| 210 | $this->heading = $query->field_names(); |
| 211 | } |
| 212 | |
| 213 | // Next blast through the result array and build out the rows |
| 214 | foreach ($query->result_array() as $row) |
| 215 | { |
| 216 | $this->rows[] = $row; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // -------------------------------------------------------------------- |
| 221 | |
| 222 | /** |
| 223 | * Set table data from an array |
| 224 | * |
| 225 | * @access public |
| 226 | * @param array |
| 227 | * @return void |
| 228 | */ |
| 229 | function _set_from_array($data) |
| 230 | { |
| 231 | if ( ! is_array($data) OR count($data) == 0) |
| 232 | { |
| 233 | return FALSE; |
| 234 | } |
| 235 | |
| 236 | $i = 0; |
| 237 | foreach ($data as $row) |
| 238 | { |
| 239 | if ( ! is_array($row)) |
| 240 | { |
| 241 | $this->rows[] = $data; |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | // If a heading hasn't already been set we'll use the first row of the array as the heading |
| 246 | if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0) |
| 247 | { |
| 248 | $this->heading = $row; |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | $this->rows[] = $row; |
| 253 | } |
| 254 | |
| 255 | $i++; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // -------------------------------------------------------------------- |
| 260 | |
| 261 | /** |
| 262 | * Compile Template |
| 263 | * |
| 264 | * @access private |
| 265 | * @return void |
| 266 | */ |
| 267 | function _compile_template() |
| 268 | { |
| 269 | if ($this->template == NULL) |
| 270 | { |
| 271 | $this->template = $this->_default_template(); |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | $this->temp = $this->_default_template(); |
| 276 | 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) |
| 277 | { |
| 278 | if ( ! isset($this->template[$val])) |
| 279 | { |
| 280 | $this->template[$val] = $this->temp[$val]; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // -------------------------------------------------------------------- |
| 286 | |
| 287 | /** |
| 288 | * Default Template |
| 289 | * |
| 290 | * @access private |
| 291 | * @return void |
| 292 | */ |
| 293 | function _default_template() |
| 294 | { |
| 295 | return array ( |
| 296 | 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', |
| 297 | |
| 298 | 'heading_row_start' => '<tr>', |
| 299 | 'heading_row_end' => '</tr>', |
| 300 | 'heading_cell_start' => '<th>', |
| 301 | 'heading_cell_end' => '</th>', |
| 302 | |
| 303 | 'row_start' => '<tr>', |
| 304 | 'row_end' => '</tr>', |
| 305 | 'cell_start' => '<td>', |
| 306 | 'cell_end' => '</td>', |
| 307 | |
| 308 | 'row_alt_start' => '<tr>', |
| 309 | 'row_alt_end' => '</tr>', |
| 310 | 'cell_alt_start' => '<td>', |
| 311 | 'cell_alt_end' => '</td>', |
| 312 | |
| 313 | 'table_close' => '</table>' |
| 314 | ); |
| 315 | } |
admin | a1931ad | 2006-10-09 04:17:38 +0000 | [diff] [blame] | 316 | |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 317 | |
| 318 | } |
| 319 | |
| 320 | ?> |