| 542 | | $other_class_name = Inflector::classify($other_table_name); |
| | 557 | |
| | 558 | # Use any passed-in parameters |
| | 559 | if (!is_null($parameters)) { |
| | 560 | if(@array_key_exists("conditions", $parameters)) |
| | 561 | $additional_conditions = $parameters['conditions']; |
| | 562 | elseif($parameters[0] != "") |
| | 563 | $additional_conditions = $parameters[0]; |
| | 564 | |
| | 565 | if(@array_key_exists("order", $parameters)) |
| | 566 | $orderings = $parameters['order']; |
| | 567 | elseif($parameters[1] != "") |
| | 568 | $orderings = $parameters[1]; |
| | 569 | |
| | 570 | if(@array_key_exists("limit", $parameters)) |
| | 571 | $limit = $parameters['limit']; |
| | 572 | elseif($parameters[2] != "") |
| | 573 | $limit = $parameters[2]; |
| | 574 | |
| | 575 | if(@array_key_exists("joins", $parameters)) |
| | 576 | $additional_joins = $parameters['joins']; |
| | 577 | elseif($parameters[3] != "") |
| | 578 | $additional_joins = $parameters[3]; |
| | 579 | |
| | 580 | if(!empty($additional_conditions)) |
| | 581 | $conditions .= " AND (" . $additional_conditions . ")"; |
| | 582 | if(!empty($additional_joins)) |
| | 583 | $joins .= " " . $additional_joins; |
| | 584 | |
| | 585 | if(@array_key_exists("class_name", $parameters)) { |
| | 586 | $other_object_name = $parameters['class_name']; |
| | 587 | } |
| | 588 | if(@array_key_exists("join_table", $parameters)) { |
| | 589 | $join_table = $parameters['join_table']; |
| | 590 | } |
| | 591 | } |
| | 592 | if(!is_null($other_object_name)) { |
| | 593 | $other_class_name = Inflector::camelize($other_object_name); |
| | 594 | } else { |
| | 595 | $other_class_name = Inflector::classify($other_table_name); |
| | 596 | } |
| 554 | | $joins = "LEFT JOIN {$join_table} ON {$other_table_name}.id = {$other_foreign_key}"; |
| 555 | | |
| 556 | | # Use any passed-in parameters |
| 557 | | if (!is_null($parameters)) { |
| 558 | | if(@array_key_exists("conditions", $parameters)) |
| 559 | | $additional_conditions = $parameters['conditions']; |
| 560 | | elseif($parameters[0] != "") |
| 561 | | $additional_conditions = $parameters[0]; |
| 562 | | |
| 563 | | if(@array_key_exists("orderings", $parameters)) |
| 564 | | $orderings = $parameters['orderings']; |
| 565 | | elseif($parameters[1] != "") |
| 566 | | $orderings = $parameters[1]; |
| 567 | | |
| 568 | | if(@array_key_exists("limit", $parameters)) |
| 569 | | $limit = $parameters['limit']; |
| 570 | | elseif($parameters[2] != "") |
| 571 | | $limit = $parameters[2]; |
| 572 | | |
| 573 | | if(@array_key_exists("joins", $parameters)) |
| 574 | | $additional_joins = $parameters['joins']; |
| 575 | | elseif($parameters[3] != "") |
| 576 | | $additional_joins = $parameters[3]; |
| 577 | | |
| 578 | | if (!empty($additional_conditions)) |
| 579 | | $conditions .= " AND (" . $additional_conditions . ")"; |
| 580 | | if (!empty($additional_joins)) |
| 581 | | $joins .= " " . $additional_joins; |
| 582 | | } |
| | 610 | $joins .= "LEFT JOIN {$join_table} ON {$other_table_name}.id = {$other_foreign_key}"; |
| 597 | | # Prepare the class name and primary key, e.g. if |
| 598 | | # customers has_many contacts, then we'll need a Contact |
| 599 | | # object, and the customer_id field name. |
| 600 | | if(@array_key_exists("foreign_key", $parameters)) |
| 601 | | $foreign_key = $parameters['foreign_key']; |
| 602 | | else |
| 603 | | $foreign_key = Inflector::singularize($this->table_name)."_id"; |
| 604 | | |
| 605 | | $other_class_name = Inflector::classify($other_table_name); |
| 606 | | $conditions = "{$foreign_key}=$this->id"; |
| | 667 | |
| | 668 | if(@array_key_exists("foreign_key", $parameters)) { |
| | 669 | $foreign_key = $parameters['foreign_key']; |
| | 670 | } else { |
| | 671 | $foreign_key = Inflector::singularize($this->table_name)."_id"; |
| | 672 | } |
| | 673 | if(count($other_class_object->primary_keys) == 1) { |
| | 674 | $id = $other_class_object->primary_keys[0]; |
| | 675 | } else { |
| | 676 | $id = "id"; |
| | 677 | } |
| | 678 | $id_value = $this->$id; |
| | 679 | if(is_numeric($id_value)) { |
| | 680 | $conditions = "$foreign_key={$id_value}"; |
| | 681 | } else { |
| | 682 | $conditions = "$foreign_key='{$id_value}'"; |
| | 683 | } |
| | 684 | |
| 654 | | # Prepare the class name and primary key, e.g. if |
| 655 | | # customers has_many contacts, then we'll need a Contact |
| 656 | | # object, and the customer_id field name. |
| 657 | | $other_ = Inflector::camelize($other_object_name); |
| 658 | | if(@array_key_exists("foreign_key", $parameters)) |
| | 698 | if(@array_key_exists("class_name", $parameters)) { |
| | 699 | $other_object_name = $parameters['class_name']; |
| | 700 | } |
| | 701 | $other_class_name = Inflector::camelize($other_object_name); |
| | 702 | |
| | 703 | # Instantiate an object to access find_all |
| | 704 | $other_class_object = new $other_class_name(); |
| | 705 | |
| | 706 | if(@array_key_exists("foreign_key", $parameters)) { |
| 694 | | |
| 695 | | $conditions = "id='".$this->$foreign_key."'"; |
| 696 | | # Instantiate an object to access find_all |
| 697 | | $results = new $other_class_name(); |
| | 756 | } |
| | 757 | |
| | 758 | if(count($other_class_object->primary_keys) == 1) { |
| | 759 | $id = $other_class_object->primary_keys[0]; |
| | 760 | } else { |
| | 761 | $id = "id"; |
| | 762 | } |
| | 763 | $foreign_key_value = $this->$foreign_key; |
| | 764 | if(is_numeric($foreign_key_value)) { |
| | 765 | $conditions = "$id = $foreign_key_value"; |
| | 766 | } else { |
| | 767 | $conditions = "$id = '$foreign_key_value'"; |
| | 768 | } |
| | 769 | |
| 980 | | // Is output to be generated in pages? |
| 981 | | if (is_array($limit)) { |
| 982 | | |
| 983 | | // Yes, get next page number and rows per page from argument |
| 984 | | list($this->page, $this->rows_per_page) = $limit; |
| 985 | | if($this->page <= 0) { |
| 986 | | $this->page = 1; |
| 987 | | } |
| 988 | | # Default for rows_per_page: |
| 989 | | if ($this->rows_per_page == null) { |
| 990 | | $this->rows_per_page = $this->rows_per_page_default; |
| 991 | | } |
| 992 | | # Set the LIMIT string segment for the SQL in the find_all |
| 993 | | $this->offset = ($this->page - 1) * $this->rows_per_page; |
| 994 | | # mysql 3.23 doesn't support OFFSET |
| 995 | | //$limit = "$rows_per_page OFFSET $offset"; |
| 996 | | $limit = $this->offset.", ".$this->rows_per_page; |
| 997 | | |
| 998 | | // Remember that we're generating output in pages |
| 999 | | // so $limit needs to be added to the query |
| 1000 | | $set_pages = true; |
| 1001 | | } |
| 1002 | | |
| 1003 | | // Test source of SQL for query |
| | 1054 | # Test source of SQL for query |
| 1029 | | // If limit specified, divide into pages |
| 1030 | | if(!is_null($limit)) { |
| 1031 | | // FIXME: Isn't the second test redundant? |
| 1032 | | if($set_pages) { |
| 1033 | | |
| 1034 | | // Send query to database |
| 1035 | | //echo "query: $sql\n"; |
| 1036 | | if($this->is_error($rs = $this->query($sql))) { |
| 1037 | | |
| 1038 | | // Error returned, throw error exception |
| 1039 | | $this->raise($rs->getMessage()); |
| 1040 | | // Execution doesn't return here |
| 1041 | | } else { |
| 1042 | | |
| 1043 | | # Set number of total pages in result set |
| 1044 | | # without the LIMIT |
| 1045 | | if($count = $rs->numRows()) |
| 1046 | | $this->pages = ( |
| 1047 | | ($count % $this->rows_per_page) == 0) |
| 1048 | | ? $count / $this->rows_per_page |
| 1049 | | : floor($count / $this->rows_per_page) + 1; |
| 1050 | | } |
| | 1087 | # If limit specified, divide into pages |
| | 1088 | if(!is_null($limit) || !is_null($offset) || !is_null($per_page)) { |
| | 1089 | |
| | 1090 | # Is output to be generated in pages? |
| | 1091 | if(is_array($limit)) { |
| | 1092 | # Yes, get next page number and rows per page from argument |
| | 1093 | list($this->page, $this->rows_per_page) = $limit; |
| | 1094 | } else { |
| | 1095 | $this->rows_per_page = $limit; |
| 1052 | | $sql .= "LIMIT $limit"; |
| 1053 | | } |
| 1054 | | } |
| 1055 | | |
| 1056 | | //echo "ActiveRecord::find_all() - sql: $sql\n<br>"; |
| 1057 | | //echo "query: $sql\n"; |
| | 1097 | if(!is_null($per_page)) { |
| | 1098 | $this->rows_per_page = $per_page; |
| | 1099 | } |
| | 1100 | # Default for rows_per_page: |
| | 1101 | if ($this->rows_per_page <= 0) { |
| | 1102 | $this->rows_per_page = $this->rows_per_page_default; |
| | 1103 | } |
| | 1104 | |
| | 1105 | $this->page = $_REQUEST['page']; |
| | 1106 | if($this->page <= 0) { |
| | 1107 | $this->page = 1; |
| | 1108 | } |
| | 1109 | |
| | 1110 | # Set the LIMIT string segment for the SQL |
| | 1111 | if(!$offset) { |
| | 1112 | $offset = ($this->page - 1) * $this->rows_per_page; |
| | 1113 | } |
| | 1114 | |
| | 1115 | $sql .= "LIMIT $this->rows_per_page OFFSET $offset"; |
| | 1116 | # $sql .= "LIMIT $offset, $this->rows_per_page"; |
| | 1117 | |
| | 1118 | # Send query to database |
| | 1119 | # Set number of total pages in result set |
| | 1120 | # without the LIMIT |
| | 1121 | if($count = $this->count_all("*", $conditions, $joins)) { |
| | 1122 | $this->pagination_count = $count; |
| | 1123 | $this->pages = ( |
| | 1124 | ($count % $this->rows_per_page) == 0) |
| | 1125 | ? $count / $this->rows_per_page |
| | 1126 | : floor($count / $this->rows_per_page) + 1; |
| | 1127 | } |
| | 1128 | } |
| | 1129 | } |
| | 1130 | |
| | 1131 | # echo "ActiveRecord::find_all() - sql: $sql\n<br>"; |
| | 1132 | # echo "query: $sql\n"; |
| 1970 | | $this->table_name = Inflector::tableize(get_class($this)); |
| 1971 | | } |
| | 2044 | $class_name = $this->get_class_name(); |
| | 2045 | $this->table_name = Inflector::tableize($class_name); |
| | 2046 | } |
| | 2047 | } |
| | 2048 | |
| | 2049 | /** |
| | 2050 | * Get class name of child object |
| | 2051 | * |
| | 2052 | * this will return the manually set name or get_class($this) |
| | 2053 | * @return string child class name |
| | 2054 | */ |
| | 2055 | private function get_class_name() { |
| | 2056 | if(!is_null($this->class_name)) { |
| | 2057 | $class_name = $this->class_name; |
| | 2058 | } else { |
| | 2059 | $class_name = get_class($this); |
| | 2060 | } |
| | 2061 | return $class_name; |
| 2374 | | /** |
| 2375 | | * Paging html functions |
| 2376 | | * @todo Document this API |
| 2377 | | */ |
| 2378 | | function limit_select($controller =null, $additional_query = null) { |
| 2379 | | if($this->pages > 0) { |
| 2380 | | $html = " |
| 2381 | | <select name=\"per_page\" onChange=\"document.location = '?$this->paging_extra_params&per_page=' + this.options[this.selectedIndex].value;\"> |
| 2382 | | <option value=\"$this->rows_per_page\" selected>per page:</option> |
| 2383 | | <option value=10>10</option> |
| 2384 | | <option value=20>20</option> |
| 2385 | | <option value=50>50</option> |
| 2386 | | <option value=100>100</option> |
| 2387 | | <option value=999999999>ALL</option> |
| 2388 | | </select> |
| 2389 | | "; |
| 2390 | | } |
| 2391 | | return $html; |
| 2392 | | } |
| 2393 | | |
| 2394 | | /** |
| 2395 | | * @todo Document this API |
| 2396 | | * |
| 2397 | | * @return string HTML to link to previous and next pages |
| 2398 | | * @uses $display |
| 2399 | | * @uses $page |
| 2400 | | * @uses $pages |
| 2401 | | * @uses $paging_extra_params |
| 2402 | | * @uses rows_per_page |
| 2403 | | */ |
| 2404 | | function page_list(){ |
| 2405 | | $page_list = ""; |
| 2406 | | |
| 2407 | | /* Print the first and previous page links if necessary */ |
| 2408 | | if(($this->page != 1) && ($this->page)) |
| 2409 | | $page_list .= "<a href=\"?$this->paging_extra_params&page=1&per_page=$this->rows_per_page\" class=\"page_list\" title=\"First Page\"><<</a> "; |
| 2410 | | |
| 2411 | | if(($this->page-1) > 0) |
| 2412 | | $page_list .= "<a href=\"?$this->paging_extra_params&page=".($this->page-1)."&per_page=$this->rows_per_page\" class=\"page_list\" title=\"Previous Page\"><</a> "; |
| 2413 | | |
| 2414 | | if($this->pages < $this->display) |
| 2415 | | $this->display = $this->pages; |
| 2416 | | |
| 2417 | | if($this->page == $this->pages) { |
| 2418 | | if($this->pages - $this->display == 0) |
| 2419 | | $start = 1; |
| 2420 | | else |
| 2421 | | $start = $this->pages - $this->display; |
| 2422 | | $max = $this->pages; |
| 2423 | | } else { |
| 2424 | | if($this->page >= $this->display) { |
| 2425 | | $start = $this->page - ($this->display / 2); |
| 2426 | | $max = $this->page + (($this->display / 2)-1); |
| 2427 | | } else { |
| 2428 | | $start = 1; |
| 2429 | | $max = $this->display; |
| 2430 | | } |
| 2431 | | } |
| 2432 | | |
| 2433 | | if($max >= $this->pages) |
| 2434 | | $max = $this->pages; |
| 2435 | | |
| 2436 | | /* Print the numeric page list; make the current page unlinked and bold */ |
| 2437 | | if($max != 1) { |
| 2438 | | for ($i=$start; $i<=$max; $i++) { |
| 2439 | | if ($i == $this->page) |
| 2440 | | $page_list .= "<span class=\"pageList\"><b>".$i."</b></span>"; |
| 2441 | | else |
| 2442 | | $page_list .= "<a href=\"?$this->paging_extra_params&page=$i&per_page=$this->rows_per_page\" class=\"page_list\" title=\"Page ".$i."\">".$i."</a>"; |
| 2443 | | |
| 2444 | | $page_list .= " "; |
| 2445 | | } |
| 2446 | | } |
| 2447 | | |
| 2448 | | /* Print the Next and Last page links if necessary */ |
| 2449 | | if(($this->page+1) <= $this->pages) |
| 2450 | | $page_list .= "<a href=\"?$this->paging_extra_params&page=".($this->page+1)."&per_page=$this->rows_per_page\" class=\"page_list\" title=\"Next Page\">></a> "; |
| 2451 | | |
| 2452 | | if(($this->page != $this->pages) && ($this->pages != 0)) |
| 2453 | | $page_list .= "<a href=\"?$this->paging_extra_params&page=".$this->pages."&per_page=$this->rows_per_page\" class=\"page_list\" title=\"Last Page\">>></a> "; |
| 2454 | | |
| 2455 | | $page_list .= "\n"; |
| 2456 | | |
| 2457 | | //error_log("Page list=[$page_list]"); |
| 2458 | | return $page_list; |
| 2459 | | } |
| 2460 | | |