Table.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class CI_Table {
00030
00031 var $rows = array();
00032 var $heading = array();
00033 var $auto_heading = TRUE;
00034 var $caption = NULL;
00035 var $template = NULL;
00036 var $newline = "\n";
00037 var $empty_cells = "";
00038
00039
00040 function CI_Table()
00041 {
00042 log_message('debug', "Table Class Initialized");
00043 }
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 function set_template($template)
00055 {
00056 if ( ! is_array($template))
00057 {
00058 return FALSE;
00059 }
00060
00061 $this->template = $template;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 function set_heading()
00076 {
00077 $args = func_get_args();
00078 $this->heading = (is_array($args[0])) ? $args[0] : $args;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 function make_columns($array = array(), $col_limit = 0)
00095 {
00096 if ( ! is_array($array) OR count($array) == 0)
00097 {
00098 return FALSE;
00099 }
00100
00101
00102
00103 $this->auto_heading = FALSE;
00104
00105 if ($col_limit == 0)
00106 {
00107 return $array;
00108 }
00109
00110 $new = array();
00111 while(count($array) > 0)
00112 {
00113 $temp = array_splice($array, 0, $col_limit);
00114
00115 if (count($temp) < $col_limit)
00116 {
00117 for ($i = count($temp); $i < $col_limit; $i++)
00118 {
00119 $temp[] = ' ';
00120 }
00121 }
00122
00123 $new[] = $temp;
00124 }
00125
00126 return $new;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 function set_empty($value)
00141 {
00142 $this->empty_cells = $value;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 function add_row()
00157 {
00158 $args = func_get_args();
00159 $this->rows[] = (is_array($args[0])) ? $args[0] : $args;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 function set_caption($caption)
00172 {
00173 $this->caption = $caption;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 function generate($table_data = NULL)
00186 {
00187
00188
00189 if ( ! is_null($table_data))
00190 {
00191 if (is_object($table_data))
00192 {
00193 $this->_set_from_object($table_data);
00194 }
00195 elseif (is_array($table_data))
00196 {
00197 $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
00198 $this->_set_from_array($table_data, $set_heading);
00199 }
00200 }
00201
00202
00203 if (count($this->heading) == 0 AND count($this->rows) == 0)
00204 {
00205 return 'Undefined table data';
00206 }
00207
00208
00209 $this->_compile_template();
00210
00211
00212
00213
00214 $out = $this->template['table_open'];
00215 $out .= $this->newline;
00216
00217
00218 if ($this->caption)
00219 {
00220 $out .= $this->newline;
00221 $out .= '<caption>' . $this->caption . '</caption>';
00222 $out .= $this->newline;
00223 }
00224
00225
00226 if (count($this->heading) > 0)
00227 {
00228 $out .= $this->template['heading_row_start'];
00229 $out .= $this->newline;
00230
00231 foreach($this->heading as $heading)
00232 {
00233 $out .= $this->template['heading_cell_start'];
00234 $out .= $heading;
00235 $out .= $this->template['heading_cell_end'];
00236 }
00237
00238 $out .= $this->template['heading_row_end'];
00239 $out .= $this->newline;
00240 }
00241
00242
00243 if (count($this->rows) > 0)
00244 {
00245 $i = 1;
00246 foreach($this->rows as $row)
00247 {
00248 if ( ! is_array($row))
00249 {
00250 break;
00251 }
00252
00253
00254 $name = (fmod($i++, 2)) ? '' : 'alt_';
00255
00256 $out .= $this->template['row_'.$name.'start'];
00257 $out .= $this->newline;
00258
00259 foreach($row as $cell)
00260 {
00261 $out .= $this->template['cell_'.$name.'start'];
00262
00263 if ($cell === "")
00264 {
00265 $out .= $this->empty_cells;
00266 }
00267 else
00268 {
00269 $out .= $cell;
00270 }
00271
00272 $out .= $this->template['cell_'.$name.'end'];
00273 }
00274
00275 $out .= $this->template['row_'.$name.'end'];
00276 $out .= $this->newline;
00277 }
00278 }
00279
00280 $out .= $this->template['table_close'];
00281
00282 return $out;
00283 }
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 function clear()
00294 {
00295 $this->rows = array();
00296 $this->heading = array();
00297 $this->auto_heading = TRUE;
00298 }
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 function _set_from_object($query)
00310 {
00311 if ( ! is_object($query))
00312 {
00313 return FALSE;
00314 }
00315
00316
00317 if (count($this->heading) == 0)
00318 {
00319 if ( ! method_exists($query, 'list_fields'))
00320 {
00321 return FALSE;
00322 }
00323
00324 $this->heading = $query->list_fields();
00325 }
00326
00327
00328
00329 if ($query->num_rows() > 0)
00330 {
00331 foreach ($query->result_array() as $row)
00332 {
00333 $this->rows[] = $row;
00334 }
00335 }
00336 }
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 function _set_from_array($data, $set_heading = TRUE)
00348 {
00349 if ( ! is_array($data) OR count($data) == 0)
00350 {
00351 return FALSE;
00352 }
00353
00354 $i = 0;
00355 foreach ($data as $row)
00356 {
00357 if ( ! is_array($row))
00358 {
00359 $this->rows[] = $data;
00360 break;
00361 }
00362
00363
00364 if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)
00365 {
00366 $this->heading = $row;
00367 }
00368 else
00369 {
00370 $this->rows[] = $row;
00371 }
00372
00373 $i++;
00374 }
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385 function _compile_template()
00386 {
00387 if ($this->template == NULL)
00388 {
00389 $this->template = $this->_default_template();
00390 return;
00391 }
00392
00393 $this->temp = $this->_default_template();
00394 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)
00395 {
00396 if ( ! isset($this->template[$val]))
00397 {
00398 $this->template[$val] = $this->temp[$val];
00399 }
00400 }
00401 }
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411 function _default_template()
00412 {
00413 return array (
00414 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
00415
00416 'heading_row_start' => '<tr>',
00417 'heading_row_end' => '</tr>',
00418 'heading_cell_start' => '<th>',
00419 'heading_cell_end' => '</th>',
00420
00421 'row_start' => '<tr>',
00422 'row_end' => '</tr>',
00423 'cell_start' => '<td>',
00424 'cell_end' => '</td>',
00425
00426 'row_alt_start' => '<tr>',
00427 'row_alt_end' => '</tr>',
00428 'cell_alt_start' => '<td>',
00429 'cell_alt_end' => '</td>',
00430
00431 'table_close' => '</table>'
00432 );
00433 }
00434
00435
00436 }
00437
00438
00439
00440