| [ Index ] |
PHP Cross Reference of Quick Form DataBase |
[Summary view] [Print] [Text view]
1 <?php 2 if (!defined('QFDB_SECURITY')) { 3 die("You can't access this file directly..."); 4 } 5 6 7 8 class qfdbobj_report { 9 10 // 0.4 11 static public function obj_struct(&$qfdb) 12 { 13 $qfdb->m['object'] = 'report'; // string (fixed) 14 $qfdb->m['status'] = '12345'; // string 15 $qfdb->m['if'] = ''; // string 16 $qfdb->m['alias'] = ''; // string 17 18 $qfdb->m['query'] = '';// string 19 $qfdb->m['cols_rows'] = 'cols'; // string 20 $qfdb->m['cols_format'] = ''; // string 21 $qfdb->m['class_line_1'] = $qfdb->get_css_value('qfdb_report_line_1'); // string 22 $qfdb->m['class_line_2'] = $qfdb->get_css_value('qfdb_report_line_2'); // string 23 $qfdb->m['row_width_left'] = '30%'; // string 24 $qfdb->m['row_width_right'] = '70%'; // string 25 $qfdb->m['paginate_inpage'] = '10'; // int 26 $qfdb->m['paginate_style'] = '';// string 27 $qfdb->m['paginate_class'] = $qfdb->get_css_value('qfdb_report_paginate'); // string 28 $qfdb->m['paginate_class_top'] = $qfdb->get_css_value('qfdb_report_paginate_top'); // string 29 $qfdb->m['paginate_class_bottom'] = $qfdb->get_css_value('qfdb_report_paginate_bottom'); // string 30 $qfdb->m['paginate_others'] = '';// string 31 $qfdb->m['paginate_left'] = '['; // string 32 $qfdb->m['paginate_right'] = ']'; // string 33 $qfdb->m['paginate_print'] = 'bottom'; // string 34 $qfdb->m['paginate_url'] = ''; // string 35 $qfdb->m['paginate_noresult'] = ''; // string 36 $qfdb->m['edit'] = 'Editar'; // string 37 $qfdb->m['edit_left'] = '['; // string 38 $qfdb->m['edit_url'] = ''; // string 39 $qfdb->m['edit_right'] = ']'; // string 40 $qfdb->m['edit_class'] = $qfdb->get_css_value('qfdb_report_edit'); // string 41 $qfdb->m['edit_others'] = ''; // string 42 $qfdb->m['table_thead'] = ''; // string 43 $qfdb->m['table_thead_class'] = $qfdb->get_css_value('qfdb_report_thead'); // string 44 $qfdb->m['table_thead_others'] = ''; // string 45 $qfdb->m['table_tfoot'] = ''; // string 46 $qfdb->m['table_border'] = ''; // string 47 $qfdb->m['table_cellpadding'] = ''; // string 48 $qfdb->m['table_cellspacing'] = ''; // string 49 $qfdb->m['table_class'] = $qfdb->get_css_value('qfdb_report_table'); // string 50 $qfdb->m['table_style'] = ''; // string 51 $qfdb->m['table_others'] = ''; // string 52 53 $qfdb->m['object_properties'] = count($qfdb->m) +2; // int 54 } // end func 55 56 // 0.3 57 static public function obj_html(&$qfdb) 58 { 59 // vars 60 $report['format'] = array(); 61 $report['query'] = false; 62 $report['format_ncols'] = 0; 63 $report['paginate_npage'] = 0; 64 $report['paginate_query'] = ''; 65 $report['paginate'] = ''; 66 $report['paginate_nresults'] = false; // DB object, need $qdfb->db_fetchrow() to use it 67 $report['db_result'] = false; 68 $report['db_elements'] = 0; 69 $report['db_row'] = false; 70 if (isset($_GET['report_page'])) { 71 $report['paginate_page'] = intval($_GET['report_page']); 72 } elseif(isset($_POST['report_page'])) { 73 $report['paginate_page'] = intval($_POST['report_page']); 74 } else { 75 $report['paginate_page'] = 0; 76 } 77 if ($report['paginate_page'] < 0) { 78 $report['paginate_page'] = 0; 79 } 80 81 // verify paginate_url 82 $qfdb->m['paginate_url'] = self::report_paginate_url($qfdb->m['paginate_url']); 83 84 // verify query and get sum of elements 85 $report_db_result = $qfdb->sql_query($qfdb->m['query']); 86 if ($report_db_result) { 87 // verify sum of pages pages (in paginate) 88 if ($qfdb->m['paginate_inpage'] && $qfdb->m['paginate_print'] != 'none') { 89 self::report_make_paginate_query($qfdb, $report); 90 } 91 } else { 92 $qfdb->add_critical_error(QFDBOBJ_REPORT_INVALID_QUERY); 93 } 94 95 // verify format 96 self::report_make_format($qfdb, $report); 97 98 99 // make paginate 100 if ($qfdb->get_ok()) { 101 if ($qfdb->m['paginate_print'] != 'none') { 102 $report['paginate'] = self::report_make_paginate($qfdb, $report); 103 } 104 } 105 106 // make report query and return DB Resources 107 self::report_make_sql_query($qfdb, $report); 108 109 // PRINT paginate -> TOP 110 if (/*$report['db_elements']*/ $report['paginate_npage'] > 1 && ($qfdb->m['paginate_print'] == 'both' || $qfdb->m['paginate_print'] == 'top')) { 111 $aux = ($qfdb->m['paginate_class_top'])? ' qfdb_report_paginate_top' : ''; 112 $qfdb->add_output('html', str_replace('____PAGINATE_TOP_BOTTOM____', $aux, $report['paginate'])); 113 } 114 115 // report THEAD 116 self::report_make_table_thead($qfdb, $report); 117 118 // report TFOOT 119 self::report_make_table_tfoot($qfdb, $report); 120 121 122 // report TBODY 123 self::report_make_table_tbody($qfdb, $report); 124 125 // PRINT paginate -> BOTTOM 126 if (/*$report['db_elements']*/ $report['paginate_npage'] > 1 && ($qfdb->m['paginate_print'] == 'both' || $qfdb->m['paginate_print'] == 'bottom')) { 127 $aux = ($qfdb->m['paginate_class_bottom'])? ' qfdb_report_paginate_bottom' : ''; 128 $qfdb->add_output('html', str_replace('____PAGINATE_TOP_BOTTOM____', $aux, $report['paginate'])); 129 } 130 } // end func 131 132 133 134 135 // 0.3 136 static public function obj_db(&$qfdb) 137 { 138 self::obj_html($qfdb); 139 } // end func 140 141 142 // 0.3 143 static public function obj_debug(&$qfdb) 144 { 145 } // end func 146 147 148 // 0.4 149 static public function obj_fixed(&$qfdb) 150 { 151 // cols_rows 152 if ($qfdb->m['cols_rows'] != 'rows') { 153 $qfdb->m['cols_rows'] = 'cols'; 154 } 155 156 // paginate_inpage 157 $qfdb->m['paginate_inpage'] = intval($qfdb->m['paginate_inpage']); 158 if ($qfdb->m['paginate_inpage'] < 0) { 159 $qfdb->m['paginate_inpage'] = 10; 160 } 161 162 // paginate_print 163 $paginate_inpage = array('top', 'bottom', 'both', 'none'); 164 if (!in_array($qfdb->m['paginate_print'], $paginate_inpage)) { 165 $qfdb->m['paginate_print'] = 'bottom'; 166 } 167 168 // query 169 if (!$qfdb->m['query']) { 170 $qfdb->add_critical_error(QFDBOBJ_REPORT_EMPTY_QUERY); 171 } 172 } // end func 173 174 175 176 177 static private function report_paginate_url($url) 178 { 179 if ($url) { 180 if (strpos($url, '?') !== false) { 181 if (substr($url, -1) == '&') { 182 $url .= 'report_page='; 183 } else { 184 $url .= '&report_page='; 185 } 186 } else { 187 $url .= '?report_page='; 188 } 189 } else { 190 $url = '?report_page='; 191 } 192 193 return $url; 194 } // end func 195 196 197 // 0.4 198 static private function report_make_paginate_query(&$qfdb, &$report) 199 { 200 $query = $qfdb->strip_tab_nl($qfdb->m['query']); 201 202 //$report['paginate_query'] = substr($query ,0, strpos($query, ' ')) . ' COUNT(*) AS paginate_nresults ' . substr($query, strpos(strtolower($query), 'from')); 203 $report['paginate_query'] = 'SELECT COUNT(*) AS `paginate_nresults` ' . substr($query, strpos(strtolower($query), ' from ')); 204 205 // verify if exists LIMIT -> SQL command 206 if (eregi('[ ]+limit[ ]*', $query)) { 207 $qfdb->add_warning(QFDBOBJ_REPORT_QUERY_WITH_LIMIT); 208 } 209 $paginate_nresults = $qfdb->sql_query2array($report['paginate_query']); 210 211 $report['paginate_nresults'] = intval($paginate_nresults[0]); 212 213 $report['paginate_npage'] = ceil($report['paginate_nresults'] / $qfdb->m['paginate_inpage']); 214 } // end func 215 216 217 // 0.4 218 static private function report_make_format(&$qfdb, &$report) 219 { 220 $error = false; 221 222 if ($qfdb->m['cols_rows'] == 'cols') { 223 $lines = explode('&&&', $qfdb->m['cols_format']); 224 if (is_array($lines)) { 225 foreach ($lines as $kline => $vline) { 226 $lines = trim($vline); 227 if (!strlen($lines)) { 228 continue; 229 } 230 $cols = explode(':::', $vline); 231 if (is_array($cols)) { 232 foreach ($cols as $kcols => $vcols) { 233 $vcols = trim($vcols); 234 $report['format'][$kline][$kcols] = $vcols; 235 } 236 237 $countc = count($report['format'][$kline]); 238 if ($countc != 6) { 239 // fixed cols 240 for ($i=$countc; $i<6 ; $i++) { 241 $report['format'][$kline][$i] = ''; 242 } 243 } 244 $report['format'][$kline][2] = -10; //intval($report['format'][$kline][$i][2]); 245 $report['format'][$kline][2] = ($report['format'][$kline][2] < 0) ? 0 : $report['format'][$kline][2]; 246 } else { 247 $error = true; 248 } 249 } 250 } else { 251 $error = true; 252 } 253 } 254 255 if ($error) { 256 $qfdb->add_critical_error(QFDBOBJ_REPORT_INVALID_COLS_FORMAT); 257 $report['format'] = array(); 258 $report['format_ncols'] = 0; 259 } else { 260 $report['format_ncols'] = count($report['format']); 261 } 262 } // end func 263 264 265 266 static private function report_make_paginate(&$qfdb, &$report) 267 { 268 269 $paginate = "\n\n<div"; 270 if ($qfdb->m['paginate_class']) { 271 $paginate .= ' class="'.$qfdb->m['paginate_class'].'____PAGINATE_TOP_BOTTOM____'.'"'; 272 } 273 if ($qfdb->m['paginate_style']) { 274 $paginate .= ' style="'.$qfdb->m['paginate_style'].'"'; 275 } 276 277 if ($qfdb->m['paginate_others']) { 278 $paginate .= ' '.$qfdb->m['paginate_others']; 279 } 280 $paginate .= "> \n"; 281 for ($i=0; $i< $report['paginate_npage']; $i++) { 282 if ($i == $report['paginate_page']) { 283 $paginate .= ' '.$qfdb->m['paginate_left'].'<b>'.($i+1).'</b>'.$qfdb->m['paginate_right']. "\n"; 284 } else { 285 $paginate .= ' '.$qfdb->m['paginate_left'].'<a href="'.$qfdb->m['paginate_url'].$i.'">'. ($i+1).'</a>'.$qfdb->m['paginate_right'] . "\n"; 286 } 287 } 288 289 $paginate .= "</div>\n\n"; 290 291 return $paginate; 292 293 } // end func 294 295 296 297 // 0.4 298 static private function report_make_sql_query(&$qfdb, &$report) 299 { 300 $report['query'] = $qfdb->m['query']; 301 302 // set LIMIT in SQL query 303 if ($qfdb->m['paginate_inpage'] > 0) { 304 $report['query'] .= ' LIMIT '.($qfdb->m['paginate_inpage']*$report['paginate_page']).', '.$qfdb->m['paginate_inpage']; 305 } 306 307 if ($report['db_result'] = $qfdb->sql_query($report['query'])) { 308 $report['db_elements'] = $qfdb->sql_numrows(); 309 } else { 310 $qfdb->add_critical_error(QFDBOBJ_REPORT_WRONG_QUERY); 311 } 312 } // end func 313 314 // 0.4 315 static private function report_make_table_thead(&$qfdb, &$report) 316 { 317 $qfdb->add_output('html', "\n\n".'<table border="'.intval($qfdb->m['table_border']).'" cellpadding="'. 318 intval($qfdb->m['table_cellpadding']).'" cellspacing="'.intval($qfdb->m['table_cellspacing']).'"'); 319 320 if ($qfdb->m['table_class']) { 321 $qfdb->add_output('html', ' class="'.$qfdb->m['table_class'].'"'); 322 } 323 if ($qfdb->m['table_style']) { 324 $qfdb->add_output('html', ' style="'.$qfdb->m['table_style'].'"'); 325 } 326 if ($qfdb->m['table_others']) { 327 $qfdb->add_output('html', ' '.$qfdb->m['table_others']); 328 } 329 $qfdb->add_output('html', '>'."\n\n<thead>\n"); 330 331 // HEAD (THEAD) 332 if ($qfdb->m['table_thead']) { 333 $qfdb->add_output('html', "<tr>\n <td colspan=\"{$report['format_ncols']}\">\n {$qfdb->m['table_thead']}\n </td>\n</tr>\n"); 334 } else { 335 $qfdb->add_output('html', "<tr>\n"); 336 337 $thead = ''; 338 if ($qfdb->m['table_thead_class']) { 339 $thead .= ' class="'.$qfdb->m['table_thead_class'].'"'; 340 } 341 if ($qfdb->m['table_thead_others']) { 342 $thead .= ' '.$qfdb->m['table_thead_others']; 343 } 344 345 for ($i=0; $i<$report['format_ncols']; $i++) { 346 $content = (trim($report['format'][$i][4])) ? $report['format'][$i][4] : ' '; 347 $qfdb->add_output('html', " <td{$thead}>\n {$content}\n </td>\n"); 348 } 349 350 $qfdb->add_output('html', "</tr>\n"); 351 } 352 353 $qfdb->add_output('html', "</thead>\n\n"); 354 } // end func 355 356 357 358 // 0.4 359 static private function report_make_table_tbody(&$qfdb, &$report) 360 { 361 $qfdb->add_output('html', "\n<tbody>\n"); 362 363 $aux = 0; 364 while ($report['db_row'] = $qfdb->sql_fetchrow( $report['db_result'] )) { 365 $tr_class = ($aux%2) ? $qfdb->m['class_line_2'] : $qfdb->m['class_line_1']; 366 $qfdb->add_output('html', "<tr class=\"{$tr_class}\">\n"); 367 $aux++; 368 369 for ($i=0; $i<$report['format_ncols']; $i++) { 370 if (!$qfdb->get_ok()) { 371 return; 372 } 373 374 $tbody = ''; 375 if ($report['format'][$i][1]) { 376 $tbody .= ' style="width: '.$report['format'][$i][1].';"'; 377 } 378 if ($report['format'][$i][3]) { 379 $tbody .= ' class="'.$report['format'][$i][3].'"'; 380 } 381 382 // make content of cell 383 $content = self::report_make_tbody_content($qfdb, $report, $i); 384 $qfdb->add_output('html', " <td{$tbody}>\n $content\n </td>\n"); 385 } 386 $qfdb->add_output('html', "</tr>\n\n"); 387 } 388 389 if (!$aux) { 390 $qfdb->add_output('html', "<tr>\n <td colspan=\"{$report['format_ncols']}\" style=\"text-align: center;\">\n {$qfdb->m['paginate_noresult']}\n </td>\n</tr>\n\n"); 391 } 392 393 $qfdb->add_output('html', "\n</tbody>\n\n</table>\n\n"); 394 } // end func 395 396 397 398 399 // 0.4 400 static private function report_make_tbody_content(&$qfdb, &$report, $i) 401 { 402 if ($report['format'][$i][5] == '__EDIT__') { // EDIT 403 $report['format'][$i][2] = 0; // fixed it 404 405 if (!$report['format'][$i][0]) { 406 $qfdb->add_critical_error(QFDBOBJ_REPORT_EDIT_WITHOUT_DBFIELD); 407 return; 408 } 409 410 // verify if db field exist 411 if (isset($report['db_row'][ $report['format'][$i][0] ])) { 412 static $error_edit = false; 413 if (!ereg('__ID__', $qfdb->m['edit_url']) && !$error_edit) { 414 $qfdb->add_warning(QFDBOBJ_REPORT_EDITURL_WITHOUT_ID); 415 $error_edit = true; 416 $url = $qfdb->m['edit_url']; 417 } else { 418 $url = str_replace('__ID__', $report['db_row'][ $report['format'][$i][0] ] , $qfdb->m['edit_url']); 419 } 420 421 // make content (__EDIT__) 422 $content = $qfdb->m['edit_left'].'<a href="'.$url.'"'; 423 if ($qfdb->m['edit_class']) { 424 $content .= ' class="'.$qfdb->m['edit_class'].'"'; 425 } 426 if ($qfdb->m['edit_others']) { 427 $content .= ' '.$qfdb->m['edit_others']; 428 } 429 $content .= '>'.$qfdb->m['edit'].'</a>'.$qfdb->m['edit_right']; 430 431 } else { 432 $qfdb->add_critical_error(QFDBOBJ_REPORT_WRONG_DBFIELD_EDIT . $report['format'][$i][0]); 433 return; 434 } 435 } elseif ($report['format'][$i][5]) { // if function is set to generate content of table cell 436 $aux = ereg_replace('[^0-9a-zA-Z _]', '', trim($report['format'][$i][5])); 437 438 // remove duplicate white space in var 439 $count = 0; 440 while ($count != strlen($aux)) { 441 $count = strlen($aux); 442 $aux = str_replace(' ', ' ', $aux); 443 } 444 445 $aux = explode(' ', $aux); 446 447 if ($aux[0] == 'array') { // if array is used 448 eval('global $'.$aux[1] . ';'); 449 $auxvar = $$aux[1]; 450 if (is_array($auxvar)) { 451 return $auxvar[ $report['db_row'][ $report['format'][$i][0] ] ]; 452 } else { 453 $qfdb->add_critical_error( sprintf(QFDBOBJ_REPORT_UNDEFINED_ARRAY, $aux[1], $report['format'][$i][5]) ); 454 return; 455 } 456 } else { // if a function is used 457 $args = array(); 458 459 foreach ($aux as $value) { 460 if (!$value) { 461 continue; 462 } 463 if (!isset($func)) { 464 $func = $value; 465 continue; 466 } 467 468 // verify if db field exist 469 if (isset($report['db_row'][$value])) { 470 $args[] = $report['db_row'][$value]; 471 } else { 472 $qfdb->add_critical_error(QFDBOBJ_REPORT_WRONG_DBFIELD_ARGS . $value); 473 return; 474 } 475 } 476 477 478 if (function_exists($func)) { 479 $content = $func($args); 480 } else { 481 $qfdb->add_critical_error(QFDBOBJ_REPORT_FUNTION_NOT_DEFINED . $func); 482 return; 483 } 484 } 485 486 } else { // if not is definided neither array nor function then a value returned of query is used 487 if (isset($report['db_row'][ $report['format'][$i][0] ])) { 488 $content = $report['db_row'][ $report['format'][$i][0] ]; 489 } else { 490 $qfdb->add_critical_error(QFDBOBJ_REPORT_WRONG_DBFIELD . $report['format'][$i][0]); 491 $content = ''; 492 } 493 } 494 495 // if need truncate content string 496 if ($report['format'][$i][2]) { 497 $content = substr($content, 0, $report['format'][$i][2]); 498 } 499 500 if (!trim($content)) { 501 $content = ' '; 502 } 503 504 return $content; 505 } // end func 506 507 508 509 510 // 0.4 511 static private function report_make_table_tfoot(&$qfdb, &$report) 512 { 513 $qfdb->add_output('html', "\n<tfoot><tr>{$qfdb->m['table_tfoot']}\n</tr></tfoot>\n\n"); 514 515 } // end func 516 517 } // end class
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri Nov 14 17:48:13 2008 | Cross-referenced by PHPXref 0.7 |