| 644 | | } else { |
| 645 | | return array(); |
| 646 | | } |
| 647 | | } |
| 648 | | } |
| 649 | | |
| 650 | | try { |
| 651 | | if(method_exists($this, $method)){ |
| 652 | | call_user_func_array(array($this, $method), $args); |
| 653 | | } else { |
| 654 | | if($has_relation){ |
| 655 | | throw new ActiveRecordException("No existe el modelo '$model' para relacionar con ActiveRecord::{$this->source}"); |
| 656 | | } else { |
| 657 | | throw new ActiveRecordException("No existe el método '$method' en ActiveRecord::".get_class($this)); |
| 658 | | } |
| 659 | | } |
| 660 | | } |
| 661 | | catch(Exception $e){ |
| 662 | | $this->exceptions($e); |
| 663 | | } |
| 664 | | return $this->$method($args); |
| 665 | | |
| 666 | | } |
| 667 | | |
| 668 | | /** |
| 669 | | * Se conecta a la base de datos y descarga los meta-datos si es necesario |
| 670 | | * |
| 671 | | * @param boolean $new_connection |
| 672 | | */ |
| 673 | | private function _connect($new_connection=false){ |
| 674 | | if(!is_object($this->db)||$new_connection){ |
| 675 | | if($this->mode) { |
| 676 | | $this->db = DbBase::raw_connect($new_connection, "mode: {$this->mode}"); |
| 677 | | } else { |
| 678 | | $this->db = DbBase::raw_connect($new_connection); |
| 679 | | } |
| 680 | | } |
| 681 | | $this->db->debug = $this->debug; |
| 682 | | $this->db->logger = $this->logger; |
| 683 | | $this->dump(); |
| 684 | | } |
| 685 | | |
| 686 | | /** |
| 687 | | * Cargar los metadatos de la tabla |
| 688 | | * |
| 689 | | */ |
| 690 | | public function dump_model(){ |
| 691 | | $this->_connect(); |
| 692 | | } |
| 693 | | |
| 694 | | /** |
| 695 | | * Verifica si la tabla definida en $this->source existe |
| 696 | | * en la base de datos y la vuelca en dump_info |
| 697 | | * |
| 698 | | * @return boolean |
| 699 | | */ |
| 700 | | protected function dump(){ |
| 701 | | if($this->_dumped){ |
| 702 | | return false; |
| 703 | | } |
| 704 | | if($this->source) { |
| 705 | | $this->source = str_replace(";", "", strtolower($this->source)); |
| 706 | | } else { |
| 707 | | $this->_model_name(); |
| 708 | | if(!$this->source){ |
| 709 | | return false; |
| 710 | | } |
| 711 | | } |
| 712 | | $table = $this->source; |
| 713 | | $schema = $this->schema; |
| 714 | | if(!count(ActiveRecord::get_meta_data($this->source))){ |
| 715 | | $this->_dumped = true; |
| 716 | | if($this->db->table_exists($table, $schema)){ |
| 717 | | $this->_dump_info($table, $schema); |
| 718 | | } else { |
| 719 | | throw new ActiveRecordException("No existe la tabla '$table' en la base de datos"); |
| 720 | | return false; |
| 721 | | } |
| 722 | | if(!count($this->primary_key)){ |
| 723 | | if(!$this->is_view){ |
| 724 | | throw new ActiveRecordException("No se ha definido una llave primaria para la tabla '$table' esto imposibilita crear el ActiveRecord para esta entidad"); |
| 725 | | return false; |
| 726 | | } |
| 727 | | } |
| 728 | | } else { |
| 729 | | if(!$this->is_dumped()){ |
| 730 | | $this->_dumped = true; |
| 731 | | $this->_dump_info($table, $schema); |
| 732 | | } |
| 733 | | } |
| 734 | | return true; |
| 735 | | } |
| 736 | | |
| 737 | | /** |
| 738 | | * Vuelca la información de la tabla $table en la base de datos |
| 739 | | * para armar los atributos y meta-data del ActiveRecord |
| 740 | | * |
| 741 | | * @param string $table |
| 742 | | * @return boolean |
| 743 | | */ |
| 744 | | private function _dump_info($table, $schema=''){ |
| 745 | | $this->_dump_lock = true; |
| 746 | | if(!count(ActiveRecord::get_meta_data($table))){ |
| 747 | | $meta_data = $this->db->describe_table($table, $schema); |
| 748 | | if($meta_data){ |
| 749 | | ActiveRecord::set_meta_data($table, $meta_data); |
| 750 | | } |
| 751 | | } |
| 752 | | foreach(ActiveRecord::get_meta_data($table) as $field){ |
| 753 | | $this->fields[] = $field['Field']; |
| 754 | | if($field['Key']=='PRI'){ |
| 755 | | $this->primary_key[] = $field['Field']; |
| 756 | | } else $this->non_primary[] = $field['Field']; |
| 757 | | /** |
| 758 | | * Si se indica que no puede ser nulo, pero se indica un |
| 759 | | * valor por defecto, entonces no se incluye en la lista, ya que |
| 760 | | * al colocar un valor por defecto, el campo nunca sera nulo |
| 761 | | **/ |
| 762 | | if($field['Null']=='NO' && !(isset($field['Default']) && $field['Default'])){ |
| 763 | | $this->not_null[] = $field['Field']; |
| 764 | | } |
| 765 | | if(isset($field['Default']) && $field['Default']) { |
| 766 | | $this->_with_default[] = $field['Field']; |
| 767 | | } |
| 768 | | if($field['Type']){ |
| 769 | | $this->_data_type[$field['Field']] = strtolower($field['Type']); |
| 770 | | } |
| 771 | | if(substr($field['Field'], strlen($field['Field'])-3, 3)=='_at'){ |
| 772 | | $this->_at[] = $field['Field']; |
| 773 | | } |
| 774 | | if(substr($field['Field'], strlen($field['Field'])-3, 3)=='_in'){ |
| 775 | | $this->_in[] = $field['Field']; |
| 776 | | } |
| 777 | | } |
| 778 | | $this->attributes_names = $this->fields; |
| 779 | | $this->_dump_lock = false; |
| 780 | | return true; |
| 781 | | } |
| 782 | | |
| 783 | | /** |
| 784 | | * Commit a Transaction |
| 785 | | * |
| 786 | | * @return success |
| 787 | | */ |
| 788 | | public function commit(){ |
| 789 | | $this->_connect(); |
| 790 | | return $this->db->commit(); |
| 791 | | } |
| 792 | | |
| 793 | | /** |
| 794 | | * Rollback a Transaction |
| 795 | | * |
| 796 | | * @return success |
| 797 | | */ |
| 798 | | public function rollback(){ |
| 799 | | $this->_connect(); |
| 800 | | return $this->db->rollback(); |
| 801 | | } |
| 802 | | |
| 803 | | /** |
| 804 | | * Start a transaction in RDBM |
| 805 | | * |
| 806 | | * @return success |
| 807 | | */ |
| 808 | | public function begin(){ |
| 809 | | $this->_connect(true); |
| 810 | | return $this->db->begin(); |
| 811 | | } |
| 812 | | |
| 813 | | /** |
| 814 | | * Find all records in this table using a SQL Statement |
| 815 | | * |
| 816 | | * @param string $sqlQuery |
| 817 | | * @return ActiveRecord Cursor |
| 818 | | */ |
| 819 | | public function find_all_by_sql($sqlQuery){ |
| 820 | | $this->_connect(); |
| 821 | | $results = array(); |
| 822 | | foreach($this->db->fetch_all($sqlQuery) as $result){ |
| 823 | | $results[] = $this->dump_result($result); |
| 824 | | } |
| 825 | | return $results; |
| 826 | | } |
| 827 | | /** |
| 828 | | * Find a record in this table using a SQL Statement |
| 829 | | * |
| 830 | | * @param string $sqlQuery |
| 831 | | * @return ActiveRecord Cursor |
| 832 | | */ |
| 833 | | public function find_by_sql($sqlQuery){ |
| 834 | | $this->_connect(); |
| 835 | | $row = $this->db->fetch_one($sqlQuery); |
| 836 | | if($row!==false){ |
| 837 | | $this->dump_result_self($row); |
| 838 | | return $this->dump_result($row); |
| 839 | | } else { |
| 840 | | return false; |
| 841 | | } |
| 842 | | } |
| 843 | | |
| 844 | | /** |
| 845 | | * Execute a SQL Statement directly |
| 846 | | * |
| 847 | | * @param string $sqlQuery |
| 848 | | * @return int affected |
| 849 | | */ |
| 850 | | public function sql($sqlQuery){ |
| 851 | | $this->_connect(); |
| 852 | | return $this->db->query($sqlQuery); |
| 853 | | } |
| 854 | | |
| 855 | | /** |
| 856 | | * Return Fist Record |
| 857 | | * |
| 858 | | * @param mixed $what |
| 859 | | * @param boolean $debug |
| 860 | | * |
| 861 | | * Recibe los mismos parametros que find |
| 862 | | * |
| 863 | | * @return ActiveRecord Cursor |
| 864 | | */ |
| 865 | | public function find_first($what=''){ |
| 866 | | $this->_connect(); |
| 867 | | $what = get_params(func_get_args()); |
| 868 | | $select = "SELECT "; |
| 869 | | if(isset($what['columns'])){ |
| 870 | | $select.= ActiveRecord::sql_sanizite($what['columns']); |
| 871 | | } elseif(isset($what['distinct'])) { |
| 872 | | $select.='DISTINCT '; |
| 873 | | $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields); |
| 874 | | } else { |
| 875 | | $select.= join(",", $this->fields); |
| 876 | | } |
| 877 | | if($this->schema){ |
| 878 | | $select.= " FROM {$this->schema}.{$this->source}"; |
| 879 | | } else { |
| 880 | | $select.= " FROM {$this->source}"; |
| 881 | | } |
| 882 | | |
| 883 | | $what['limit'] = 1; |
| 884 | | $select.= $this->convert_params_to_sql($what); |
| 885 | | $resp = false; |
| 886 | | try { |
| 887 | | |
| 888 | | $result = $this->db->fetch_one($select); |
| 889 | | if($result){ |
| 890 | | $this->dump_result_self($result); |
| 891 | | $resp = $this->dump_result($result); |
| 892 | | } |
| 893 | | } |
| 894 | | catch(Exception $e){ |
| 895 | | $this->exceptions($e); |
| 896 | | } |
| 897 | | return $resp; |
| 898 | | } |
| 899 | | |
| 900 | | /** |
| 901 | | * Find data on Relational Map table |
| 902 | | * |
| 903 | | * @param string $what |
| 904 | | * @return ActiveRecord Cursor |
| 905 | | * |
| 906 | | * columns: columnas a utilizar |
| 907 | | * conditions : condiciones de busqueda en WHERE |
| 908 | | * join: inclusion inner join o outer join |
| 909 | | * group : campo para grupo en GROUP BY |
| 910 | | * having : condicion para el grupo |
| 911 | | * order : campo para criterio de ordenamiento ORDER BY |
| 912 | | * distinct: campos para hacer select distinct |
| 913 | | */ |
| 914 | | public function find($what=''){ |
| 915 | | $this->_connect(); |
| 916 | | $what = get_params(func_get_args()); |
| 917 | | $select = "SELECT "; |
| 918 | | if(isset($what['columns'])){ |
| 919 | | $select.= $what['columns'] ? ActiveRecord::sql_sanizite($what['columns']) : join(",", $this->fields); |
| 920 | | } elseif(isset($what['distinct'])) { |
| 921 | | $select.='DISTINCT '; |
| 922 | | $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields); |
| 923 | | } else { |
| 924 | | $select.= join(",", $this->fields); |
| 925 | | } |
| 926 | | if($this->schema){ |
| 927 | | $select.= " FROM {$this->schema}.{$this->source}"; |
| 928 | | } else { |
| 929 | | $select.= " FROM {$this->source}"; |
| 930 | | } |
| 931 | | $select.= $this->convert_params_to_sql($what); |
| 932 | | |
| 933 | | $results = array(); |
| 934 | | $all_results = $this->db->in_query($select); |
| 935 | | foreach($all_results AS $result){ |
| 936 | | $results[] = $this->dump_result($result); |
| 937 | | } |
| 938 | | $this->count = count($results); |
| 939 | | |
| 940 | | if(isset($what[0]) && is_numeric($what[0])){ |
| 941 | | if(!isset($results[0])){ |
| 942 | | $this->count = 0; |
| 943 | | return false; |
| 944 | | } else { |
| 945 | | $this->dump_result_self($all_results[0]); |
| 946 | | $this->count = 1; |
| 947 | | return $results[0]; |
| 948 | | } |
| 949 | | } else { |
| 950 | | $this->count = count($results); |
| 951 | | return $results; |
| 952 | | } |
| 953 | | } |
| 954 | | |
| 955 | | /* |
| 956 | | * Arma una consulta SQL con el parametro $what, as�: |
| 957 | | * $what = get_params(func_get_args()); |
| 958 | | * $select = "SELECT * FROM Clientes"; |
| 959 | | * $select.= $this->convert_params_to_sql($what); |
| 960 | | * |
| 961 | | * @param string $what |
| 962 | | * @return string |
| 963 | | */ |
| 964 | | public function convert_params_to_sql($what = ''){ |
| 965 | | $select = ""; |
| 966 | | if(is_array($what)){ |
| 967 | | if(!isset($what['conditions'])) { |
| 968 | | if(!isset($this->primary_key[0]) && (isset($this->id) || $this->is_view)){ |
| 969 | | $this->primary_key[0] = "id"; |
| 970 | | } |
| 971 | | ActiveRecord::sql_item_sanizite($this->primary_key[0]); |
| 972 | | if(isset($what[0])){ |
| 973 | | if(is_numeric($what[0])){ |
| 974 | | $what['conditions'] = "{$this->primary_key[0]} = {$this->db->add_quotes($what[0])}"; |
| 975 | | } else { |
| 976 | | if($what[0]==''){ |
| 977 | | $what['conditions'] = "{$this->primary_key[0]} = ''"; |
| 978 | | } else { |
| 979 | | $what['conditions'] = $what[0]; |
| 980 | | } |
| 981 | | } |
| 982 | | } |
| 983 | | } |
| 984 | | |
| 985 | | if(isset($what['join']) && $what['join']){ |
| 986 | | $select.=" {$what['join']}"; |
| 987 | | } |
| 988 | | if(isset($what['conditions']) && $what['conditions']){ |
| 989 | | $select.=" WHERE {$what['conditions']}"; |
| 990 | | } |
| 991 | | if(isset($what['group']) && $what['group']){ |
| 992 | | $select.=" GROUP BY {$what['group']}"; |
| 993 | | } |
| 994 | | if(isset($what['having']) && $what['having']){ |
| 995 | | $select.=" HAVING {$what['having']}"; |
| 996 | | } |
| 997 | | if(isset($what['order'])&&$what['order']) { |
| 998 | | ActiveRecord::sql_sanizite($what['order']); |
| 999 | | $select.=" ORDER BY {$what['order']}"; |
| 1000 | | } |
| 1001 | | |
| 1002 | | $limit_args = array($select); |
| 1003 | | if(isset($what['limit'])) { |
| 1004 | | array_push($limit_args, "limit: $what[limit]"); |
| 1005 | | } |
| 1006 | | if(isset($what['offset'])) { |
| 1007 | | array_push($limit_args, "offset: $what[offset]"); |
| 1008 | | } |
| 1009 | | if(count($limit_args)>1) { |
| 1010 | | $select = call_user_func_array(array($this,'limit'), $limit_args); |
| 1011 | | } |
| 1012 | | |
| 1013 | | } else { |
| 1014 | | if(strlen($what)){ |
| 1015 | | if(is_numeric($what)){ |
| 1016 | | $select.= "WHERE {$this->primary_key[0]} = '$what'"; |
| 1017 | | } else { |
| 1018 | | $select.= "WHERE $what"; |
| 1019 | | } |
| 1020 | | } |
| 1021 | | } |
| 1022 | | return $select; |
| 1023 | | } |
| 1024 | | |
| 1025 | | /* |
| 1026 | | * Devuelve una clausula LIMIT adecuada al RDBMS empleado |
| 1027 | | * |
| 1028 | | * limit: maxima cantidad de elementos a mostrar |
| 1029 | | * offset: desde que elemento se comienza a mostrar |
| 1030 | | * |
| 1031 | | * @param string $sql consulta select |
| 1032 | | * @return String clausula LIMIT adecuada al RDBMS empleado |
| 1033 | | */ |
| 1034 | | public function limit($sql){ |
| 1035 | | $args = func_get_args(); |
| 1036 | | return call_user_func_array(array($this->db, 'limit'), $args); |
| 1037 | | } |
| 1038 | | |
| 1039 | | /** |
| 1040 | | * Ejecuta un SELECT DISTINCT |
| 1041 | | * @param string $what |
| 1042 | | * @return array |
| 1043 | | * |
| 1044 | | * Soporta parametros iguales a find |
| 1045 | | **/ |
| 1046 | | public function distinct($what=''){ |
| 1047 | | $this->_connect(); |
| 1048 | | $what = get_params(func_get_args()); |
| 1049 | | |
| 1050 | | if($this->schema){ |
| 1051 | | $table = $this->schema.".".$this->source; |
| 1052 | | } else { |
| 1053 | | $table = $this->source; |
| 1054 | | } |
| 1055 | | |
| 1056 | | if(!isset($what['columns'])){ |
| 1057 | | $what['columns'] = $what['0']; |
| 1058 | | } else { |
| 1059 | | if(!$what['columns']) { |
| 1060 | | $what['columns'] = $what['0']; |
| 1061 | | } |
| 1062 | | } |
| 1063 | | $what['columns'] = ActiveRecord::sql_sanizite($what['columns']); |
| 1064 | | |
| 1065 | | $select = "SELECT DISTINCT {$what['columns']} FROM $table "; |
| 1066 | | |
| 1067 | | /** |
| 1068 | | * Se elimina el de indice cero ya que por defecto convert_params_to_sql lo considera como una condicion en WHERE |
| 1069 | | */ |
| 1070 | | unset($what[0]); |
| 1071 | | $select.= $this->convert_params_to_sql($what); |
| 1072 | | |
| 1073 | | $results = array(); |
| 1074 | | foreach($this->db->fetch_all($select) as $result){ |
| 1075 | | $results[] = $result[0]; |
| 1076 | | } |
| 1077 | | return $results; |
| 1078 | | } |
| 1079 | | |
| 1080 | | /** |
| 1081 | | * Ejecuta una consulta en el RDBM directamente |
| 1082 | | * |
| 1083 | | * @param string $sql |
| 1084 | | * @return resource |
| 1085 | | */ |
| 1086 | | public function select_one($sql){ |
| 1087 | | $this->_connect(); |
| 1088 | | if(substr(ltrim($sql), 0, 7)!="SELECT") { |
| 1089 | | $sql = "SELECT ".$sql; |
| 1090 | | } |
| 1091 | | $num = $this->db->fetch_one($sql); |
| 1092 | | return $num[0]; |
| 1093 | | } |
| 1094 | | |
| 1095 | | static public function static_select_one($sql){ |
| 1096 | | $db = db::raw_connect(); |
| 1097 | | if(substr(ltrim($sql), 0, 7)!="SELECT") { |
| 1098 | | $sql = "SELECT ".$sql; |
| 1099 | | } |
| 1100 | | $num = $db->fetch_one($sql); |
| 1101 | | return $num[0]; |
| 1102 | | } |
| 1103 | | |
| 1104 | | /** |
| 1105 | | * Realiza un conteo de filas |
| 1106 | | * |
| 1107 | | * @param string $what |
| 1108 | | * @return integer |
| 1109 | | */ |
| 1110 | | public function count($what=''){ |
| 1111 | | $this->_connect(); |
| 1112 | | $what = get_params(func_get_args()); |
| 1113 | | |
| 1114 | | if($this->schema){ |
| 1115 | | $table = "{$this->schema}.{$this->source}"; |
| 1116 | | } else { |
| 1117 | | $table = $this->source; |
| 1118 | | } |
| 1119 | | |
| 1120 | | if(isset($what['distinct'])&&$what['distinct']) { |
| 1121 | | if(isset($what['group']) || isset($what['order'])) { |
| 1122 | | $select = "SELECT COUNT(*) FROM (SELECT DISTINCT {$what['distinct']} FROM $table " ; |
| 1123 | | $select.=$this->convert_params_to_sql($what); |
| 1124 | | $select.=') AS t '; |
| 1125 | | } else { |
| 1126 | | $select = "SELECT COUNT(DISTINCT {$what['distinct']}) FROM $table " ; |
| 1127 | | $select.=$this->convert_params_to_sql($what); |
| 1128 | | } |
| 1129 | | } else { |
| 1130 | | $select = "SELECT COUNT(*) FROM $table " ; |
| 1131 | | $select.=$this->convert_params_to_sql($what); |
| 1132 | | } |
| 1133 | | |
| 1134 | | $num = $this->db->fetch_one($select); |
| 1135 | | return $num[0]; |
| 1136 | | } |
| 1137 | | |
| 1138 | | /** |
| 1139 | | * Realiza un promedio sobre el campo $what |
| 1140 | | * |
| 1141 | | * @param string $what |
| 1142 | | * @return array |
| 1143 | | */ |
| 1144 | | public function average($what=''){ |
| 1145 | | $this->_connect(); |
| 1146 | | $what = get_params(func_get_args()); |
| 1147 | | if(isset($what['column'])) { |
| 1148 | | if(!$what['column']){ |
| 1149 | | $what['column'] = $what[0]; |
| 1150 | | } |
| 1151 | | } else { |
| 1152 | | $what['column'] = $what[0]; |
| 1153 | | } |
| 1154 | | unset($what[0]); |
| 1155 | | |
| 1156 | | ActiveRecord::sql_item_sanizite($what['column']); |
| 1157 | | if($this->schema){ |
| 1158 | | $table = "{$this->schema}.{$this->source}"; |
| 1159 | | } else { |
| 1160 | | $table = $this->source; |
| 1161 | | } |
| 1162 | | $select = "SELECT AVG({$what['column']}) FROM $table " ; |
| 1163 | | $select.=$this->convert_params_to_sql($what); |
| 1164 | | |
| 1165 | | $num = $this->db->fetch_one($select); |
| 1166 | | return $num[0]; |
| 1167 | | } |
| 1168 | | |
| 1169 | | public function sum($what=''){ |
| 1170 | | $this->_connect(); |
| 1171 | | $what = get_params(func_get_args()); |
| 1172 | | if(isset($what['column'])) { |
| 1173 | | if(!$what['column']){ |
| 1174 | | $what['column'] = $what[0]; |
| 1175 | | } |
| 1176 | | } else { |
| 1177 | | $what['column'] = $what[0]; |
| 1178 | | } |
| 1179 | | unset($what[0]); |
| 1180 | | |
| 1181 | | ActiveRecord::sql_item_sanizite($what['column']); |
| 1182 | | if($this->schema){ |
| 1183 | | $table = "{$this->schema}.{$this->source}"; |
| 1184 | | } else { |
| 1185 | | $table = $this->source; |
| 1186 | | } |
| 1187 | | $select = "SELECT SUM({$what['column']}) FROM $table " ; |
| 1188 | | $select.=$this->convert_params_to_sql($what); |
| 1189 | | |
| 1190 | | $num = $this->db->fetch_one($select); |
| 1191 | | return $num[0]; |
| 1192 | | } |
| 1193 | | |
| 1194 | | /** |
| 1195 | | * Busca el valor maximo para el campo $what |
| 1196 | | * |
| 1197 | | * @param string $what |
| 1198 | | * @return mixed |
| 1199 | | */ |
| 1200 | | public function maximum($what=''){ |
| 1201 | | $this->_connect(); |
| 1202 | | $what = get_params(func_get_args()); |
| 1203 | | if(isset($what['column'])) { |
| 1204 | | if(!$what['column']){ |
| 1205 | | $what['column'] = $what[0]; |
| 1206 | | } |
| 1207 | | } else { |
| 1208 | | $what['column'] = $what[0]; |
| 1209 | | } |
| 1210 | | unset($what[0]); |
| 1211 | | |
| 1212 | | ActiveRecord::sql_item_sanizite($what['column']); |
| 1213 | | if($this->schema){ |
| 1214 | | $table = "{$this->schema}.{$this->source}"; |
| 1215 | | } else { |
| 1216 | | $table = $this->source; |
| 1217 | | } |
| 1218 | | $select = "SELECT MAX({$what['column']}) FROM $table " ; |
| 1219 | | $select.=$this->convert_params_to_sql($what); |
| 1220 | | |
| 1221 | | $num = $this->db->fetch_one($select); |
| 1222 | | return $num[0]; |
| 1223 | | } |
| 1224 | | |
| 1225 | | /** |
| 1226 | | * Busca el valor minimo para el campo $what |
| 1227 | | * |
| 1228 | | * @param string $what |
| 1229 | | * @return mixed |
| 1230 | | */ |
| 1231 | | public function minimum($what=''){ |
| 1232 | | $this->_connect(); |
| 1233 | | $what = get_params(func_get_args()); |
| 1234 | | if(isset($what['column'])) { |
| 1235 | | if(!$what['column']){ |
| 1236 | | $what['column'] = $what[0]; |
| 1237 | | } |
| 1238 | | } else { |
| 1239 | | $what['column'] = $what[0]; |
| 1240 | | } |
| 1241 | | unset($what[0]); |
| 1242 | | |
| 1243 | | ActiveRecord::sql_item_sanizite($what['column']); |
| 1244 | | if($this->schema){ |
| 1245 | | $table = "{$this->schema}.{$this->source}"; |
| 1246 | | } else { |
| 1247 | | $table = $this->source; |
| 1248 | | } |
| 1249 | | $select = "SELECT MIN({$what['column']}) FROM $table " ; |
| 1250 | | $select.=$this->convert_params_to_sql($what); |
| 1251 | | |
| 1252 | | $num = $this->db->fetch_one($select); |
| 1253 | | return $num[0]; |
| 1254 | | } |
| 1255 | | |
| 1256 | | /** |
| 1257 | | * Realiza un conteo directo mediante $sql |
| 1258 | | * |
| 1259 | | * @param string $sqlQuery |
| 1260 | | * @return mixed |
| 1261 | | */ |
| 1262 | | public function count_by_sql($sqlQuery){ |
| 1263 | | $this->_connect(); |
| 1264 | | $num = $this->db->fetch_one($sqlQuery); |
| 1265 | | return $num[0]; |
| 1266 | | } |
| 1267 | | |
| 1268 | | /** |
| 1269 | | * Iguala los valores de un resultado de la base de datos |
| 1270 | | * en un nuevo objeto con sus correspondientes |
| 1271 | | * atributos de la clase |
| 1272 | | * |
| 1273 | | * @param array $result |
| 1274 | | * @return ActiveRecord |
| 1275 | | */ |
| 1276 | | function dump_result($result){ |
| 1277 | | $this->_connect(); |
| 1278 | | $obj = clone $this; |
| 1279 | | /** |
| 1280 | | * Consulta si la clase es padre de otra y crea el tipo de dato correcto |
| 1281 | | */ |
| 1282 | | if(isset($result['type'])){ |
| 1283 | | if(in_array($result['type'], $this->parent_of)){ |
| 1284 | | if(class_exists($result['type'])){ |
| 1285 | | $obj = new $result['type']; |
| 1286 | | unset($result['type']); |
| 1287 | | } |
| 1288 | | } |
| 1289 | | } |
| 1290 | | $this->_dump_lock = true; |
| 1291 | | if(is_array($result)){ |
| 1292 | | foreach($result as $k => $r){ |
| 1293 | | if(!is_numeric($k)){ |
| 1294 | | $obj->$k = stripslashes($r); |
| 1295 | | } |
| 1296 | | } |
| 1297 | | } |
| 1298 | | $this->_dump_lock = false; |
| 1299 | | return $obj; |
| 1300 | | } |
| 1301 | | |
| 1302 | | /** |
| 1303 | | * Iguala los valores de un resultado de la base de datos |
| 1304 | | * con sus correspondientes atributos de la clase |
| 1305 | | * |
| 1306 | | * @param array $result |
| 1307 | | * @return ActiveRecord |
| 1308 | | */ |
| 1309 | | public function dump_result_self($result){ |
| 1310 | | $this->_connect(); |
| 1311 | | $this->_dump_lock = true; |
| 1312 | | if(is_array($result)){ |
| 1313 | | foreach($result as $k => $r){ |
| 1314 | | if(!is_numeric($k)){ |
| 1315 | | $this->$k = stripslashes($r); |
| 1316 | | } |
| 1317 | | } |
| 1318 | | } |
| 1319 | | $this->_dump_lock = false; |
| 1320 | | } |
| 1321 | | |
| 1322 | | /** |
| 1323 | | * Create a new Row using values from $_REQUEST |
| 1324 | | * |
| 1325 | | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
| 1326 | | * @return boolean success |
| 1327 | | */ |
| 1328 | | public function create_from_request($form=''){ |
| 1329 | | if($form) { |
| 1330 | | return $this->create($_REQUEST[$form]); |
| 1331 | | } else { |
| 1332 | | return $this->create($_REQUEST); |
| 1333 | | } |
| 1334 | | } |
| 1335 | | |
| 1336 | | /** |
| 1337 | | * Saves a new Row using values from $_REQUEST |
| 1338 | | * |
| 1339 | | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
| 1340 | | * @return boolean success |
| 1341 | | */ |
| 1342 | | public function save_from_request($form=''){ |
| 1343 | | if($form) { |
| 1344 | | return $this->save($_REQUEST[$form]); |
| 1345 | | } else { |
| 1346 | | return $this->save($_REQUEST); |
| 1347 | | } |
| 1348 | | } |
| 1349 | | |
| 1350 | | |
| 1351 | | /** |
| 1352 | | * Updates a Row using values from $_REQUEST |
| 1353 | | * |
| 1354 | | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
| 1355 | | * @return boolean success |
| 1356 | | */ |
| 1357 | | public function update_from_request($form=''){ |
| 1358 | | if($form) { |
| 1359 | | return $this->update($_REQUEST[$form]); |
| 1360 | | } else { |
| 1361 | | return $this->update($_REQUEST); |
| 1362 | | } |
| 1363 | | } |
| 1364 | | |
| 1365 | | /** |
| 1366 | | * Creates a new Row in map table |
| 1367 | | * |
| 1368 | | * @param mixed $values |
| 1369 | | * @return success boolean |
| 1370 | | */ |
| 1371 | | public function create(){ |
| 1372 | | $this->_connect(); |
| 1373 | | |
| 1374 | | if(func_num_args()>0){ |
| 1375 | | $params = get_params(func_get_args()); |
| 1376 | | $values = (isset($params[0]) && is_array($params[0])) ? $params[0]: $params; |
| 1377 | | foreach($this->fields as $field) { |
| 1378 | | if(isset($values[$field])) { |
| 1379 | | $this->$field = $values[$field]; |
| 1380 | | } |
| 1381 | | } |
| 1382 | | } |
| 1383 | | if($this->primary_key[0]=='id'){ |
| 1384 | | $this->id = null; |
| 1385 | | } |
| 1386 | | |
| 1387 | | return $this->save(); |
| 1388 | | } |
| 1389 | | |
| 1390 | | /** |
| 1391 | | * Consulta si un determinado registro existe o no |
| 1392 | | * en la entidad de la base de datos |
| 1393 | | * |
| 1394 | | * @return boolean |
| 1395 | | */ |
| 1396 | | function exists($where_pk=''){ |
| 1397 | | $this->_connect(); |
| 1398 | | if($this->schema){ |
| 1399 | | $table = "{$this->schema}.{$this->source}"; |
| 1400 | | } else { |
| 1401 | | $table = $this->source; |
| 1402 | | } |
| 1403 | | if(!$where_pk){ |
| 1404 | | $where_pk = array(); |
| 1405 | | foreach($this->primary_key as $key){ |
| 1406 | | if($this->$key){ |
| 1407 | | $where_pk[] = " $key = '{$this->$key}'"; |
| 1408 | | } |
| 1409 | | } |
| 1410 | | if(count($where_pk)){ |
| 1411 | | $this->_where_pk = join(" AND ", $where_pk); |
| 1412 | | } else { |
| 1413 | | return 0; |
| 1414 | | } |
| 1415 | | $query = "SELECT COUNT(*) FROM $table WHERE {$this->_where_pk}"; |
| 1416 | | } else { |
| 1417 | | if(is_numeric($where_pk)){ |
| 1418 | | $query = "SELECT(*) FROM $table WHERE id = '$where_pk'"; |
| 1419 | | } else { |
| 1420 | | $query = "SELECT COUNT(*) FROM $table WHERE $where_pk"; |
| 1421 | | } |
| 1422 | | } |
| 1423 | | $num = $this->db->fetch_one($query); |
| 1424 | | return $num[0]; |
| 1425 | | } |
| 1426 | | |
| 1427 | | /** |
| 1428 | | * Saves Information on the ActiveRecord Properties |
| 1429 | | * @param array $values array de valores a cargar |
| 1430 | | * @return boolean success |
| 1431 | | */ |
| 1432 | | public function save(){ |
| 1433 | | $this->_connect(); |
| 1434 | | |
| 1435 | | if(func_num_args()>0){ |
| 1436 | | $params = get_params(func_get_args()); |
| 1437 | | $values = (isset($params[0]) && is_array($params[0])) ? $params[0]: $params; |
| 1438 | | foreach($this->fields as $field) { |
| 1439 | | if(isset($values[$field])) { |
| 1440 | | $this->$field = $values[$field]; |
| 1441 | | } |
| 1442 | | } |
| 1443 | | } |
| 1444 | | |
| 1445 | | $ex = $this->exists(); |
| 1446 | | |
| 1447 | | if($this->schema){ |
| 1448 | | $table = $this->schema.".".$this->source; |
| 1449 | | } else { |
| 1450 | | $table = $this->source; |
| 1451 | | } |
| 1452 | | |
| 1453 | | #Run Validation Callbacks Before |
| 1454 | | if(method_exists($this, 'before_validation')){ |
| 1455 | | if($this->before_validation()=='cancel') { |
| 1456 | | return false; |
| 1457 | | } |
| 1458 | | } else { |
| 1459 | | if(isset($this->before_validation)){ |
| 1460 | | $method = $this->before_validation; |
| 1461 | | if($this->$method()=='cancel') { |
| 1462 | | return false; |
| 1463 | | } |
| 1464 | | } |
| 1465 | | } |
| 1466 | | if(!$ex&&method_exists($this, "before_validation_on_create")){ |
| 1467 | | if($this->before_validation_on_create()=='cancel') { |
| 1468 | | return false; |
| 1469 | | } |
| 1470 | | } else { |
| 1471 | | if(isset($this->before_validation_on_create)){ |
| 1472 | | $method = $this->before_validation_on_create; |
| 1473 | | if($this->$method()=='cancel') { |
| 1474 | | return false; |
| 1475 | | } |
| 1476 | | } |
| 1477 | | } |
| 1478 | | if($ex&&method_exists($this, "before_validation_on_update")){ |
| 1479 | | if($this->before_validation_on_update()=='cancel') { |
| 1480 | | return false; |
| 1481 | | } |
| 1482 | | } else { |
| 1483 | | if(isset($this->before_validation_on_update)){ |
| 1484 | | $method = $this->before_validation_on_update; |
| 1485 | | if($this->$method()=='cancel') { |
| 1486 | | return false; |
| 1487 | | } |
| 1488 | | } |
| 1489 | | } |
| 1490 | | |
| 1491 | | /** |
| 1492 | | * Recordamos que aqui no aparecen los que tienen valores por defecto, |
| 1493 | | * pero sin embargo se debe estar pendiente de validar en las otras verificaciones |
| 1494 | | * los campos nulos, ya que en estas si el campo es nulo, realmente se refiere a un campo que |
| 1495 | | * debe tomar el valor por defecto |
| 1496 | | **/ |
| 1497 | | $e = false; |
| 1498 | | for($i=0;$i<=count($this->not_null)-1;$i++){ |
| 1499 | | $f = $this->not_null[$i]; |
| 1500 | | if(isset($this->$f) && (is_null($this->$f)||$this->$f=='')){ |
| 1501 | | if(!$ex&&$f=='id'){ |
| 1502 | | continue; |
| 1503 | | } |
| 1504 | | if(!$ex&&in_array($f, $this->_at)){ |
| 1505 | | continue; |
| 1506 | | } |
| 1507 | | if($ex&&in_array($f, $this->_in)){ |
| 1508 | | continue; |
| 1509 | | } |
| 1510 | | Flash::error("Error: El campo $f no puede ser nulo"); |
| 1511 | | $e = true; |
| 1512 | | } |
| 1513 | | } |
| 1514 | | if($e){ |
| 1515 | | return false; |
| 1516 | | } |
| 1517 | | |
| 1518 | | /** |
| 1519 | | * Validacion validates_presence |
| 1520 | | **/ |
| 1521 | | $e = false; |
| 1522 | | foreach($this->_validates_presence as $f=>$opt){ |
| 1523 | | $f = $this->not_null[$i]; |
| 1524 | | if(isset($this->$f) && (is_null($this->$f)||$this->$f=='')){ |
| 1525 | | if(!$ex&&$f=='id'){ |
| 1526 | | continue; |
| 1527 | | } |
| 1528 | | if(!$ex&&in_array($f, $this->_at)){ |
| 1529 | | continue; |
| 1530 | | } |
| 1531 | | if($ex&&in_array($f, $this->_in)){ |
| 1532 | | continue; |
| 1533 | | } |
| 1534 | | |
| 1535 | | if(isset($opt['message'])) { |
| 1536 | | Flash::error($opt['message']); |
| 1537 | | } else { |
| 1538 | | $field = isset($opt['field']) ? $opt['field'] : $f; |
| 1539 | | Flash::error("Error: El campo $field no puede ser nulo"); |
| 1540 | | } |
| 1541 | | $e = true; |
| 1542 | | } |
| 1543 | | } |
| 1544 | | if($e){ |
| 1545 | | return false; |
| 1546 | | } |
| 1547 | | |
| 1548 | | /** |
| 1549 | | * Validacion validates_length |
| 1550 | | **/ |
| 1551 | | $e = false; |
| 1552 | | foreach($this->_validates_length as $f => $opt){ |
| 1553 | | if(isset($this->$f) && !is_null($this->$f) && $this->$f!='') { |
| 1554 | | $field = isset($opt['field']) ? $opt['field'] : $f; |
| 1555 | | |
| 1556 | | if(isset($opt['in'])){ |
| 1557 | | $in = explode(":", $opt['in']); |
| 1558 | | if(is_numeric($in[0])&&is_numeric($in[1])){ |
| 1559 | | $opt['minimum'] = $in[0]; |
| 1560 | | $opt['maximum'] = $in[1]; |
| 1561 | | } |
| 1562 | | } |
| 1563 | | if(isset($opt['minimum']) && is_numeric($opt['minimum'])){ |
| 1564 | | $n = $opt['minimum']; |
| 1565 | | if(strlen($this->$f)<$n){ |
| 1566 | | if(!isset($opt['too_short'])){ |
| 1567 | | Flash::error("Error: El campo $field debe tener como mínimo $n caracteres"); |
| 1568 | | $e = true; |
| 1569 | | } else { |
| 1570 | | Flash::error($opt['too_short']); |
| 1571 | | $e = true; |
| 1572 | | } |
| 1573 | | } |
| 1574 | | } |
| 1575 | | if(isset($opt['maximum']) && is_numeric($opt['maximum'])){ |
| 1576 | | $n = $opt['maximum']; |
| 1577 | | if(strlen($this->$f)>$n){ |
| 1578 | | if(!isset($opt['too_long'])){ |
| 1579 | | Flash::error("Error: El campo $field debe tener como máximo $n caracteres"); |
| 1580 | | $e = true; |
| 1581 | | } else { |
| 1582 | | Flash::error($opt['too_long']); |
| 1583 | | $e = true; |
| 1584 | | } |
| 1585 | | } |
| 1586 | | } |
| 1587 | | } |
| 1588 | | } |
| 1589 | | if($e){ |
| 1590 | | return false; |
| 1591 | | } |
| 1592 | | |
| 1593 | | /** |
| 1594 | | * Validacion validates_inclusion |
| 1595 | | **/ |
| 1596 | | $e = false; |
| 1597 | | foreach($this->_validates_inclusion as $finc => $list){ |
| 1598 | | if(isset($this->$finc) && !is_null($this->$finc) && $this->$finc!='') { |
| 1599 | | if(!is_array($list[1]) && $this->$finc!=$list[1]){ |
| 1600 | | if(isset($list['message'])) { |
| 1601 | | Flash::error($list['message']); |
| 1602 | | } else { |
| 1603 | | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
| 1604 | | Flash::error("$field debe tener un valor entre ({$list[1]})"); |
| 1605 | | } |
| 1606 | | $e = true; |
| 1607 | | } else { |
| 1608 | | if(!in_array($this->$finc, $list[1])){ |
| 1609 | | if(isset($list['message'])) { |
| 1610 | | Flash::error($list['message']); |
| 1611 | | } else { |
| 1612 | | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
| 1613 | | Flash::error("$field debe tener un valor entre (".join(",", $list[1]).")"); |
| 1614 | | } |
| 1615 | | $e = true; |
| 1616 | | } |
| 1617 | | } |
| 1618 | | } |
| 1619 | | } |
| 1620 | | if($e){ |
| 1621 | | return false; |
| 1622 | | } |
| 1623 | | |
| 1624 | | /** |
| 1625 | | * Validacion validates_exclusion |
| 1626 | | **/ |
| 1627 | | $e = false; |
| 1628 | | foreach($this->_validates_exclusion as $finc => $list){ |
| 1629 | | if(isset($this->$finc) && !is_null($this->$finc) && $this->$finc!='') { |
| 1630 | | if(!is_array($list[1]) && $this->$finc==$list[1]){ |
| 1631 | | if(isset($list['message'])) { |
| 1632 | | Flash::error($list['message']); |
| 1633 | | } else { |
| 1634 | | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
| 1635 | | Flash::error("$field no debe tener un valor entre ({$list[1]})"); |
| 1636 | | } |
| 1637 | | $e = true; |
| 1638 | | } else { |
| 1639 | | if(in_array($this->$finc, $list[1])){ |
| 1640 | | if(isset($list['message'])) { |
| 1641 | | Flash::error($list['message']); |
| 1642 | | } else { |
| 1643 | | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
| 1644 | | Flash::error("$field no debe tener un valor entre (".join(",", $list[1]).")"); |
| 1645 | | } |
| 1646 | | $e = true; |
| 1647 | | } |
| 1648 | | } |
| 1649 | | } |
| 1650 | | } |
| 1651 | | if($e){ |
| 1652 | | return false; |
| 1653 | | } |
| 1654 | | |
| 1655 | | /** |
| 1656 | | * Validacion validates_numericality |
| 1657 | | **/ |
| 1658 | | $e = false; |
| 1659 | | foreach($this->_validates_numericality as $fnum=>$opt){ |
| 1660 | | if(isset($this->$fnum) && !is_null($this->$fnum) && $this->$fnum!='') { |
| 1661 | | if(!is_numeric($this->$fnum)){ |
| 1662 | | if(isset($opt['message'])) { |
| 1663 | | Flash::error($opt['message']); |
| 1664 | | } else { |
| 1665 | | $field = isset($opt['field']) ? $opt['field'] : ucwords($fnum); |
| 1666 | | Flash::error("$field debe tener un valor numérico"); |
| 1667 | | } |
| 1668 | | $e = true; |
| 1669 | | } |
| 1670 | | } |
| 1671 | | } |
| 1672 | | if($e){ |
| 1673 | | return false; |
| 1674 | | } |
| 1675 | | |
| 1676 | | /** |
| 1677 | | * Validacion validates_format |
| 1678 | | **/ |
| 1679 | | $e = false; |
| 1680 | | foreach($this->_validates_format as $fkey => $opt){ |
| 1681 | | if(isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey!='') { |
| 1682 | | if(!ereg($opt[1], $this->$fkey)){ |
| 1683 | | if(isset($opt['message'])) { |
| 1684 | | Flash::error($opt['message']); |
| 1685 | | } else { |
| 1686 | | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
| 1687 | | Flash::error("Formato erroneo para $field"); |
| 1688 | | } |
| 1689 | | $e = true; |
| 1690 | | } |
| 1691 | | } |
| 1692 | | } |
| 1693 | | if($e){ |
| 1694 | | return false; |
| 1695 | | } |
| 1696 | | |
| 1697 | | /** |
| 1698 | | * Validacion validates_date |
| 1699 | | **/ |
| 1700 | | $e = false; |
| 1701 | | foreach($this->_validates_date as $fkey=>$opt){ |
| 1702 | | if(isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey!='') { |
| 1703 | | if(!ereg("^[0-9]{4}[-/](0[1-9]|1[12])[-/](0[1-9]|[12][0-9]|3[01])$", $this->$fkey, $regs)){ |
| 1704 | | if(isset($opt['message'])) { |
| 1705 | | Flash::error($opt['message']); |
| 1706 | | } else { |
| 1707 | | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
| 1708 | | Flash::error("Formato de fecha ({$this->$fkey}) erroneo para $field"); |
| 1709 | | } |
| 1710 | | $e = true; |
| 1711 | | } |
| 1712 | | } |
| 1713 | | } |
| 1714 | | if($e){ |
| 1715 | | return false; |
| 1716 | | } |
| 1717 | | |
| 1718 | | /** |
| 1719 | | * Validacion validates_email |
| 1720 | | **/ |
| 1721 | | $e = false; |
| 1722 | | foreach($this->_validates_email as $fkey){ |
| 1723 | | if(isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey!='') { |
| 1724 | | if(!ereg("^[a-zA-Z0-9_\.\+]+@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*$", $this->$fkey, $regs)){ |
| 1725 | | if(isset($opt['message'])) { |
| 1726 | | Flash::error($opt['message']); |
| 1727 | | } else { |
| 1728 | | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
| 1729 | | Flash::error("Formato de e-mail erroneo en el campo $field"); |
| 1730 | | } |
| 1731 | | $e = true; |
| 1732 | | } |
| 1733 | | } |
| 1734 | | } |
| 1735 | | if($e){ |
| 1736 | | return false; |
| 1737 | | } |
| 1738 | | |
| 1739 | | /** |
| 1740 | | * Validacion validates_uniqueness |
| 1741 | | **/ |
| 1742 | | $e = false; |
| 1743 | | foreach($this->_validates_uniqueness as $fkey => $opt){ |
| 1744 | | ActiveRecord::sql_item_sanizite($fkey); |
| 1745 | | if(isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey!='') { |
| 1746 | | if($ex){ |
| 1747 | | $copy = clone $this; |
| 1748 | | $copy->find_first("$fkey = {$this->db->add_quotes($this->$fkey)}"); |
| 1749 | | $number = $this->db->fetch_one("SELECT COUNT(*) FROM $table WHERE $fkey = {$this->db->add_quotes($this->$fkey)} and $fkey <> {$this->db->add_quotes($copy->$fkey)}"); |
| 1750 | | } else { |
| 1751 | | $number = $this->db->fetch_one("SELECT COUNT(*) FROM $table WHERE $fkey = {$this->db->add_quotes($this->$fkey)}"); |
| 1752 | | } |
| 1753 | | if((int) $number[0]){ |
| 1754 | | if(isset($opt['message'])) { |
| 1755 | | Flash::error($opt['message']); |
| 1756 | | } else { |
| 1757 | | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
| 1758 | | Flash::error("El valor '{$this->$fkey}' ya existe para el campo $field"); |
| 1759 | | } |
| 1760 | | $e = true; |
| 1761 | | } |
| 1762 | | } |
| 1763 | | } |
| 1764 | | if($e){ |
| 1765 | | return false; |
| 1766 | | } |
| 1767 | | |
| 1768 | | |
| 1769 | | #Run Validation Callbacks After |
| 1770 | | if(!$ex&&method_exists($this, "after_validation_on_create")){ |
| 1771 | | if($this->after_validation_on_create()=='cancel') { |
| 1772 | | return false; |
| 1773 | | } |
| 1774 | | } else { |
| 1775 | | if(isset($this->after_validation_on_create)){ |
| 1776 | | $method = $this->after_validation_on_create; |
| 1777 | | if($this->$method()=='cancel') { |
| 1778 | | return false; |
| 1779 | | } |
| 1780 | | } |
| 1781 | | } |
| 1782 | | if($ex&&method_exists($this, "after_validation_on_update")){ |
| 1783 | | if($this->after_validation_on_update()=='cancel') { |
| 1784 | | return false; |
| 1785 | | } |
| 1786 | | } else { |
| 1787 | | if(isset($this->after_validation_on_update)){ |
| 1788 | | $method = $this->after_validation_on_update; |
| 1789 | | if($this->$method()=='cancel') return false; |
| 1790 | | } |
| 1791 | | } |
| 1792 | | if(method_exists($this, 'after_validation')){ |
| 1793 | | if($this->after_validation()=='cancel') { |
| 1794 | | return false; |
| 1795 | | } |
| 1796 | | } else { |
| 1797 | | if(isset($this->after_validation)){ |
| 1798 | | $method = $this->after_validation; |
| 1799 | | if($this->$method()=='cancel') { |
| 1800 | | return false; |
| 1801 | | } |
| 1802 | | } |
| 1803 | | } |
| 1804 | | # Run Before Callbacks |
| 1805 | | if(method_exists($this, "before_save")){ |
| 1806 | | if($this->before_save()=='cancel') { |
| 1807 | | return false; |
| 1808 | | } |
| 1809 | | } else { |
| 1810 | | if(isset($this->before_save)){ |
| 1811 | | $method = $this->before_save; |
| 1812 | | if($this->$method()=='cancel') { |
| 1813 | | return false; |
| 1814 | | } |
| 1815 | | } |
| 1816 | | } |
| 1817 | | if($ex&&method_exists($this, "before_update")){ |
| 1818 | | if($this->before_update()=='cancel') { |
| 1819 | | return false; |
| 1820 | | } |
| 1821 | | } else { |
| 1822 | | if(isset($this->before_update)){ |
| 1823 | | $method = $this->before_update; |
| 1824 | | if($this->$method()=='cancel') { |
| 1825 | | return false; |
| 1826 | | } |
| 1827 | | } |
| 1828 | | } |
| 1829 | | if(!$ex&&method_exists($this, "before_create")){ |
| 1830 | | if($this->before_create()=='cancel') { |
| 1831 | | return false; |
| 1832 | | } |
| 1833 | | } else { |
| 1834 | | if(isset($this->before_create)){ |
| 1835 | | $method = $this->before_create; |
| 1836 | | if($this->$method()=='cancel') { |
| 1837 | | return false; |
| 1838 | | } |
| 1839 | | } |
| 1840 | | } |
| 1841 | | |
| 1842 | | $environment = Config::read("environment.ini"); |
| 1843 | | $config = $environment->{$this->get_mode()}; |
| 1844 | | if($ex){ |
| 1845 | | $fields = array(); |
| 1846 | | $values = array(); |
| 1847 | | foreach($this->non_primary as $np){ |
| 1848 | | $np = ActiveRecord::sql_item_sanizite($np); |
| 1849 | | if(in_array($np, $this->_in)){ |
| 1850 | | if($config->database->type=='oracle'){ |
| 1851 | | $this->$np = date("Y-m-d"); |
| 1852 | | } else { |
| 1853 | | $this->$np = date("Y-m-d G:i:s"); |
| 1854 | | } |
| 1855 | | } |
| 1856 | | |
| 1857 | | if(isset($this->$np)) { |
| 1858 | | $fields[] = $np; |
| 1859 | | if(is_null($this->$np) || $this->$np==''){ |
| 1860 | | $values[] = "NULL"; |
| 1861 | | } elseif(substr($this->$np, 0, 1)=="%"){ |
| 1862 | | $values[] = str_replace("%", "", $this->$np); |
| 1863 | | } else { |
| 1864 | | /** |
| 1865 | | * Se debe especificar el formato de fecha en Oracle |
| 1866 | | */ |
| 1867 | | if($this->_data_type[$np]=='date'&&$config->database->type=='oracle'){ |
| 1868 | | $values[] = "TO_DATE(".$this->db->add_quotes($this->$np).", 'YYYY-MM-DD')"; |
| 1869 | | } else { |
| 1870 | | $values[] = $this->db->add_quotes($this->$np); |
| 1871 | | } |
| 1872 | | } |
| 1873 | | } |
| 1874 | | } |
| 1875 | | $val = $this->db->update($table, $fields, $values, $this->_where_pk); |
| 1876 | | } else { |
| 1877 | | $fields = array(); |
| 1878 | | $values = array(); |
| 1879 | | foreach($this->fields as $field){ |
| 1880 | | if($field!='id'&&!$this->id){ |
| 1881 | | if(in_array($field, $this->_at)){ |
| 1882 | | if($config->database->type=='oracle'){ |
| 1883 | | $this->$field = date("Y-m-d"); |
| 1884 | | } else { |
| 1885 | | $this->$field = date("Y-m-d G:i:s"); |
| 1886 | | } |
| 1887 | | } |
| 1888 | | |
| 1889 | | if(in_array($field, $this->_in)){ |
| 1890 | | unset($this->$field); |
| 1891 | | } |
| 1892 | | |
| 1893 | | $use_default = in_array($field, $this->_with_default) && isset($this->$field) && (is_null($this->$field) || $this->$field==''); |
| 1894 | | if(isset($this->$field) && !$use_default) { |
| 1895 | | $fields[] = ActiveRecord::sql_sanizite($field); |
| 1896 | | if(substr($this->$field, 0, 1)=="%"){ |
| 1897 | | $values[] = str_replace("%", "", $this->$field); |
| 1898 | | } else { |
| 1899 | | if($this->is_a_numeric_type($field)||$this->$field==null){ |
| 1900 | | $values[] = addslashes($this->$field!==''&&$this->$field!==null ? $this->$field : "NULL"); |
| 1901 | | } else { |
| 1902 | | if($this->_data_type[$field]=='date'&&$config->database->type=='oracle'){ |
| 1903 | | /** |
| 1904 | | * Se debe especificar el formato de fecha en Oracle |
| 1905 | | */ |
| 1906 | | $values[] = "TO_DATE(".$this->db->add_quotes($this->$field).", 'YYYY-MM-DD')"; |
| 1907 | | } else { |
| 1908 | | if(!is_null($this->$field) && $this->$field!=''){ |
| 1909 | | $values[] = $this->db->add_quotes($this->$field); |
| 1910 | | } else { |
| 1911 | | $values[] = "NULL"; |
| 1912 | | } |
| 1913 | | } |
| 1914 | | } |
| 1915 | | } |
| 1916 | | } |
| 1917 | | } else { |
| 1918 | | /** |
| 1919 | | * Campos autonumericos en Oracle deben utilizar una sequencia auxiliar |
| 1920 | | */ |
| 1921 | | if($config->database->type=='oracle'){ |
| 1922 | | if(!$this->id){ |
| 1923 | | $fields[] = "id"; |
| 1924 | | $values[] = $this->source."_id_seq.NEXTVAL"; |
| 1925 | | } |
| 1926 | | } |
| 1927 | | if($config->database->type=='informix'){ |
| 1928 | | if(!$this->id){ |
| 1929 | | $fields[] = "id"; |
| 1930 | | $values[] = 0; |
| 1931 | | } |
| 1932 | | } |
| 1933 | | } |
| 1934 | | } |
| 1935 | | $val = $this->db->insert($table, $values, $fields); |
| 1936 | | } |
| 1937 | | |
| 1938 | | if(!isset($config->database->pdo)&&$config->database->type=='oracle'){ |
| 1939 | | $this->commit(); |
| 1940 | | } |
| 1941 | | |
| 1942 | | if(!$ex){ |
| 1943 | | $this->db->logger = true; |
| 1944 | | $m = $this->db->last_insert_id($table, $this->primary_key[0]); |
| 1945 | | $this->find_first($m); |
| 1946 | | } |
| 1947 | | |
| 1948 | | if($val){ |
| 1949 | | if($ex&&method_exists($this, "after_update")){ |
| 1950 | | if($this->after_update()=='cancel') { |
| 1951 | | return false; |
| 1952 | | } |
| 1953 | | } else { |
| 1954 | | if(isset($this->after_update)){ |
| 1955 | | $method = $this->after_update; |
| 1956 | | if($this->$method()=='cancel') { |
| 1957 | | return false; |
| 1958 | | } |
| 1959 | | } |
| 1960 | | } |
| 1961 | | if(!$ex&&method_exists($this, "after_create")){ |
| 1962 | | if($this->after_create()=='cancel') { |
| 1963 | | return false; |
| 1964 | | } |
| 1965 | | } else { |
| 1966 | | if(isset($this->after_create)){ |
| 1967 | | $method = $this->after_create; |
| 1968 | | if($this->$method()=='cancel') { |
| 1969 | | return false; |
| 1970 | | } |
| 1971 | | } |
| 1972 | | } |
| 1973 | | if(method_exists($this, "after_save")){ |
| 1974 | | if($this->after_save()=='cancel') { |
| 1975 | | return false; |
| 1976 | | } |
| 1977 | | } else { |
| 1978 | | if(isset($this->after_save)){ |
| 1979 | | $method = $this->after_save; |
| 1980 | | if($this->$method()=='cancel') { |
| 1981 | | return false; |
| 1982 | | } |
| 1983 | | } |
| 1984 | | } |
| 1985 | | return $val; |
| 1986 | | } else { |
| 1987 | | return false; |
| 1988 | | } |
| 1989 | | } |
| 1990 | | |
| 1991 | | /** |
| 1992 | | * Find All data in the Relational Table |
| 1993 | | * |
| 1994 | | * @param string $field |
| 1995 | | * @param string $value |
| 1996 | | * @return ActiveRecod Cursor |
| 1997 | | */ |
| 1998 | | function find_all_by($field, $value){ |
| 1999 | | ActiveRecord::sql_item_sanizite($field); |
| 2000 | | return $this->find("conditions: $field = {$this->db->add_quotes($value)}"); |
| 2001 | | } |
| 2002 | | |
| 2003 | | /** |
| 2004 | | * Updates Data in the Relational Table |
| 2005 | | * |
| 2006 | | * @param mixed $values |
| 2007 | | * @return boolean sucess |
| 2008 | | */ |
| 2009 | | function update(){ |
| 2010 | | $this->_connect(); |
| 2011 | | if(func_num_args()>0){ |
| 2012 | | $params = get_params(func_get_args()); |
| 2013 | | $values = (isset($params[0]) && is_array($params[0])) ? $params[0]: $params; |
| 2014 | | foreach($this->fields as $field) { |
| 2015 | | if(isset($values[$field])) { |
| 2016 | | $this->$field = $values[$field]; |
| 2017 | | } |
| 2018 | | } |
| 2019 | | } |
| 2020 | | |
| 2021 | | if($this->exists()){ |
| 2022 | | return $this->save(); |
| 2023 | | } else { |
| 2024 | | Flash::error('No se puede actualizar porque el registro no existe'); |
| 2025 | | return false; |
| 2026 | | } |
| 2027 | | } |
| 2028 | | |
| 2029 | | /** |
| 2030 | | * Deletes data from Relational Map Table |
| 2031 | | * |
| 2032 | | * @param mixed $what |
| 2033 | | */ |
| 2034 | | function delete($what=''){ |
| 2035 | | $this->_connect(); |
| 2036 | | if(func_num_args()>1){ |
| 2037 | | $what = get_params(func_get_args()); |
| 2038 | | } |
| 2039 | | if($this->schema){ |
| 2040 | | $table = $this->schema.".".$this->source; |
| 2041 | | } else { |
| 2042 | | $table = $this->source; |
| 2043 | | } |
| 2044 | | $conditions = ""; |
| 2045 | | if(is_array($what)){ |
| 2046 | | if($what["conditions"]) { |
| 2047 | | $conditions = $what["conditions"]; |
| 2048 | | } |
| 2049 | | } else { |
| 2050 | | if(is_numeric($what)){ |
| 2051 | | ActiveRecord::sql_sanizite($this->primary_key[0]); |
| 2052 | | $conditions = "{$this->primary_key[0]} = '$what'"; |
| 2053 | | } else{ |
| 2054 | | if($what){ |
| 2055 | | $conditions = $what; |
| 2056 | | } else { |
| 2057 | | ActiveRecord::sql_sanizite($this->primary_key[0]); |
| 2058 | | $conditions = "{$this->primary_key[0]} = '{$this->{$this->primary_key[0]}}'"; |
| 2059 | | } |
| 2060 | | } |
| 2061 | | } |
| 2062 | | if(method_exists($this, "before_delete")){ |
| 2063 | | if($this->id) { |
| 2064 | | $this->find($this->id); |
| 2065 | | } |
| 2066 | | if($this->before_delete()=='cancel') { |
| 2067 | | return false; |
| 2068 | | } |
| 2069 | | } else { |
| 2070 | | if(isset($this->before_delete)){ |
| 2071 | | if($this->id) { |
| 2072 | | $this->find($this->id); |
| 2073 | | } |
| 2074 | | $method = $this->before_delete; |
| 2075 | | if($this->$method()=='cancel') { |
| 2076 | | return false; |
| 2077 | | } |
| 2078 | | } |
| 2079 | | } |
| 2080 | | $val = $this->db->delete($table, $conditions); |
| 2081 | | if($val){ |
| 2082 | | if(method_exists($this, "after_delete")){ |
| 2083 | | if($this->after_delete()=='cancel') { |
| 2084 | | return false; |
| 2085 | | } |
| 2086 | | } else { |
| 2087 | | if(isset($this->after_delete)){ |
| 2088 | | $method = $this->after_delete; |
| 2089 | | if($this->$method()=='cancel') { |
| 2090 | | return false; |
| 2091 | | } |
| 2092 | | } |
| 2093 | | } |
| 2094 | | } |
| 2095 | | return $val; |
| 2096 | | } |
| 2097 | | |
| 2098 | | /** |
| 2099 | | * Actualiza todos los atributos de la entidad |
| 2100 | | * $Clientes->update_all("estado='A', fecha='2005-02-02'", "id>100"); |
| 2101 | | * $Clientes->update_all("estado='A', fecha='2005-02-02'", "id>100", "limit: 10"); |
| 2102 | | * |
| 2103 | | * @param string $values |
| 2104 | | */ |
| 2105 | | function update_all($values){ |
| 2106 | | $this->_connect(); |
| 2107 | | $params = array(); |
| 2108 | | if($this->schema){ |
| 2109 | | $table = $this->schema.".".$this->source; |
| 2110 | | } else { |
| 2111 | | $table = $this->source; |
| 2112 | | } |
| 2113 | | if(func_num_args()>1){ |
| 2114 | | $params = get_params(func_get_args()); |
| 2115 | | } |
| 2116 | | if(!isset($params['conditions'])||!$params['conditions']){ |
| 2117 | | if(isset($params[1])){ |
| 2118 | | $params['conditions'] = $params[1]; |
| 2119 | | } else { |
| 2120 | | $params['conditions'] = ""; |
| 2121 | | } |
| 2122 | | } |
| 2123 | | if($params['conditions']){ |
| 2124 | | $params['conditions'] = " WHERE ".$params['conditions']; |
| 2125 | | } |
| 2126 | | $sql = "UPDATE $table SET $values {$params['conditions']}"; |
| 2127 | | |
| 2128 | | $limit_args = array($sql); |
| 2129 | | if(isset($params['limit'])) { |
| 2130 | | array_push($limit_args, "limit: $params[limit]"); |
| 2131 | | } |
| 2132 | | if(isset($params['offset'])) { |
| 2133 | | array_push($limit_args, "offset: $params[offset]"); |
| 2134 | | } |
| 2135 | | if(count($limit_args)>1) { |
| 2136 | | $sql = call_user_func_array(array($this,'limit'), $limit_args); |
| 2137 | | } |
| 2138 | | |
| 2139 | | $environment = Config::read("environment.ini"); |
| 2140 | | $config = $environment->{$this->get_mode()}; |
| 2141 | | if(!isset($config->database->pdo)||!$config->database->pdo){ |
| 2142 | | if($config->database->type=="informix"){ |
| 2143 | | $this->db->set_return_rows(false); |
| 2144 | | } |
| 2145 | | } |
| 2146 | | return $this->db->query($sql); |
| 2147 | | } |
| 2148 | | |
| 2149 | | /** |
| 2150 | | * Delete All data from Relational Map Table |
| 2151 | | * |
| 2152 | | * @param string $conditions |
| 2153 | | * @return boolean |
| 2154 | | */ |
| 2155 | | function delete_all($conditions=''){ |
| 2156 | | $this->_connect(); |
| 2157 | | $limit = ""; |
| 2158 | | if($this->schema){ |
| 2159 | | $table = $this->schema.".".$this->source; |
| 2160 | | } else { |
| 2161 | | $table = $this->source; |
| 2162 | | } |
| 2163 | | if(func_num_args()>1){ |
| 2164 | | $params = get_params(func_get_args()); |
| 2165 | | |
| 2166 | | $limit_args = array($select); |
| 2167 | | if(isset($params['limit'])) { |
| 2168 | | array_push($limit_args, "limit: $params[limit]"); |
| 2169 | | } |
| 2170 | | if(isset($params['offset'])) { |
| 2171 | | array_push($limit_args, "offset: $params[offset]"); |
| 2172 | | } |
| 2173 | | if(count($limit_args)>1) { |
| 2174 | | $select = call_user_func_array(array($this,'limit'), $limit_args); |
| 2175 | | } |
| 2176 | | } |
| 2177 | | return $this->db->delete($table, $conditions); |
| 2178 | | } |
| 2179 | | |
| 2180 | | /** |
| 2181 | | * ********************************************************************************* |
| 2182 | | * Metodos de Debug |
| 2183 | | * ********************************************************************************* |
| 2184 | | */ |
| 2185 | | |
| 2186 | | /** |
| 2187 | | * Imprime una version humana de los valores de los campos |
| 2188 | | * del modelo en una sola linea |
| 2189 | | * |
| 2190 | | */ |
| 2191 | | public function inspect(){ |
| 2192 | | $this->_connect(); |
| 2193 | | $inspect = array(); |
| 2194 | | foreach($this->fields as $field){ |
| 2195 | | if(!is_array($field)){ |
| 2196 | | $inspect[] = "$field: {$this->$field}"; |
| 2197 | | } |
| 2198 | | } |
| 2199 | | return join(", ", $inspect); |
| 2200 | | } |
| 2201 | | |
| 2202 | | /** |
| 2203 | | * ********************************************************************************* |
| 2204 | | * Metodos de Validacion |
| 2205 | | * ********************************************************************************* |
| 2206 | | */ |
| 2207 | | |
| 2208 | | /** |
| 2209 | | * Validate that Attributes cannot have a NULL value |
| 2210 | | */ |
| 2211 | | protected function validates_presence_of(){ |
| 2212 | | $this->_connect(); |
| 2213 | | |
| 2214 | | $params = get_params(func_get_args()); |
| 2215 | | if(is_array($params[0])) { |
| 2216 | | $params = $params[0]; |
| 2217 | | } |
| 2218 | | |
| 2219 | | $fields = array(); |
| 2220 | | for($i=0; isset($params[$i]); $i++) { |
| 2221 | | $fields[] = $params[$i]; |
| 2222 | | unset($params[$i]); |
| 2223 | | $i++; |
| 2224 | | } |
| 2225 | | |
| 2226 | | foreach($fields as $p){ |
| 2227 | | if(!in_array($p, $this->fields)&&!isset($this->$p)){ |
| 2228 | | throw new ActiveRecordException('No se puede validar presencia de "'.$p.'" |
| 2229 | | en el modelo '.$this->source.' porque no existe el atributo</u><br> |
| | 579 | } else { |
| | 580 | return array(); |
| | 581 | } |
| | 582 | } |
| | 583 | } |
| | 584 | try { |
| | 585 | if (method_exists($this, $method)) { |
| | 586 | call_user_func_array(array($this, $method), $args); |
| | 587 | } else { |
| | 588 | if ($has_relation) { |
| | 589 | throw new ActiveRecordException("No existe el modelo '$model' para relacionar con ActiveRecord::{$this->source}"); |
| | 590 | } else { |
| | 591 | throw new ActiveRecordException("No existe el método '$method' en ActiveRecord::" . get_class($this)); |
| | 592 | } |
| | 593 | } |
| | 594 | } |
| | 595 | catch(Exception $e) { |
| | 596 | $this->exceptions($e); |
| | 597 | } |
| | 598 | return $this->$method($args); |
| | 599 | } |
| | 600 | /** |
| | 601 | * Se conecta a la base de datos y descarga los meta-datos si es necesario |
| | 602 | * |
| | 603 | * @param boolean $new_connection |
| | 604 | */ |
| | 605 | private function _connect($new_connection = false) { |
| | 606 | if (!is_object($this->db) || $new_connection) { |
| | 607 | if ($this->mode) { |
| | 608 | $this->db = DbBase::raw_connect($new_connection, "mode: {$this->mode}"); |
| | 609 | } else { |
| | 610 | $this->db = DbBase::raw_connect($new_connection); |
| | 611 | } |
| | 612 | } |
| | 613 | $this->db->debug = $this->debug; |
| | 614 | $this->db->logger = $this->logger; |
| | 615 | $this->dump(); |
| | 616 | } |
| | 617 | /** |
| | 618 | * Cargar los metadatos de la tabla |
| | 619 | * |
| | 620 | */ |
| | 621 | public function dump_model() { |
| | 622 | $this->_connect(); |
| | 623 | } |
| | 624 | /** |
| | 625 | * Verifica si la tabla definida en $this->source existe |
| | 626 | * en la base de datos y la vuelca en dump_info |
| | 627 | * |
| | 628 | * @return boolean |
| | 629 | */ |
| | 630 | protected function dump() { |
| | 631 | if ($this->_dumped) { |
| | 632 | return false; |
| | 633 | } |
| | 634 | if ($this->source) { |
| | 635 | $this->source = str_replace(";", "", strtolower($this->source)); |
| | 636 | } else { |
| | 637 | $this->_model_name(); |
| | 638 | if (!$this->source) { |
| | 639 | return false; |
| | 640 | } |
| | 641 | } |
| | 642 | $table = $this->source; |
| | 643 | $schema = $this->schema; |
| | 644 | if (!count(ActiveRecord::get_meta_data($this->source))) { |
| | 645 | $this->_dumped = true; |
| | 646 | if ($this->db->table_exists($table, $schema)) { |
| | 647 | $this->_dump_info($table, $schema); |
| | 648 | } else { |
| | 649 | throw new ActiveRecordException("No existe la tabla '$table' en la base de datos"); |
| | 650 | return false; |
| | 651 | } |
| | 652 | if (!count($this->primary_key)) { |
| | 653 | if (!$this->is_view) { |
| | 654 | throw new ActiveRecordException("No se ha definido una llave primaria para la tabla '$table' esto imposibilita crear el ActiveRecord para esta entidad"); |
| | 655 | return false; |
| | 656 | } |
| | 657 | } |
| | 658 | } else { |
| | 659 | if (!$this->is_dumped()) { |
| | 660 | $this->_dumped = true; |
| | 661 | $this->_dump_info($table, $schema); |
| | 662 | } |
| | 663 | } |
| | 664 | return true; |
| | 665 | } |
| | 666 | /** |
| | 667 | * Vuelca la información de la tabla $table en la base de datos |
| | 668 | * para armar los atributos y meta-data del ActiveRecord |
| | 669 | * |
| | 670 | * @param string $table |
| | 671 | * @return boolean |
| | 672 | */ |
| | 673 | private function _dump_info($table, $schema = '') { |
| | 674 | $this->_dump_lock = true; |
| | 675 | if (!count(ActiveRecord::get_meta_data($table))) { |
| | 676 | $meta_data = $this->db->describe_table($table, $schema); |
| | 677 | if ($meta_data) { |
| | 678 | ActiveRecord::set_meta_data($table, $meta_data); |
| | 679 | } |
| | 680 | } |
| | 681 | foreach(ActiveRecord::get_meta_data($table) as $field) { |
| | 682 | $this->fields[] = $field['Field']; |
| | 683 | if ($field['Key'] == 'PRI') { |
| | 684 | $this->primary_key[] = $field['Field']; |
| | 685 | } else $this->non_primary[] = $field['Field']; |
| | 686 | /** |
| | 687 | * Si se indica que no puede ser nulo, pero se indica un |
| | 688 | * valor por defecto, entonces no se incluye en la lista, ya que |
| | 689 | * al colocar un valor por defecto, el campo nunca sera nulo |
| | 690 | * |
| | 691 | */ |
| | 692 | if ($field['Null'] == 'NO' && !(isset($field['Default']) && $field['Default'])) { |
| | 693 | $this->not_null[] = $field['Field']; |
| | 694 | } |
| | 695 | if (isset($field['Default']) && $field['Default']) { |
| | 696 | $this->_with_default[] = $field['Field']; |
| | 697 | } |
| | 698 | if ($field['Type']) { |
| | 699 | $this->_data_type[$field['Field']] = strtolower($field['Type']); |
| | 700 | } |
| | 701 | if (substr($field['Field'], strlen($field['Field']) - 3, 3) == '_at') { |
| | 702 | $this->_at[] = $field['Field']; |
| | 703 | } |
| | 704 | if (substr($field['Field'], strlen($field['Field']) - 3, 3) == '_in') { |
| | 705 | $this->_in[] = $field['Field']; |
| | 706 | } |
| | 707 | } |
| | 708 | $this->attributes_names = $this->fields; |
| | 709 | $this->_dump_lock = false; |
| | 710 | return true; |
| | 711 | } |
| | 712 | /** |
| | 713 | * Commit a Transaction |
| | 714 | * |
| | 715 | * @return success |
| | 716 | */ |
| | 717 | public function commit() { |
| | 718 | $this->_connect(); |
| | 719 | return $this->db->commit(); |
| | 720 | } |
| | 721 | /** |
| | 722 | * Rollback a Transaction |
| | 723 | * |
| | 724 | * @return success |
| | 725 | */ |
| | 726 | public function rollback() { |
| | 727 | $this->_connect(); |
| | 728 | return $this->db->rollback(); |
| | 729 | } |
| | 730 | /** |
| | 731 | * Start a transaction in RDBM |
| | 732 | * |
| | 733 | * @return success |
| | 734 | */ |
| | 735 | public function begin() { |
| | 736 | $this->_connect(true); |
| | 737 | return $this->db->begin(); |
| | 738 | } |
| | 739 | /** |
| | 740 | * Find all records in this table using a SQL Statement |
| | 741 | * |
| | 742 | * @param string $sqlQuery |
| | 743 | * @return ActiveRecord Cursor |
| | 744 | */ |
| | 745 | public function find_all_by_sql($sqlQuery) { |
| | 746 | $this->_connect(); |
| | 747 | $results = array(); |
| | 748 | foreach($this->db->fetch_all($sqlQuery) as $result) { |
| | 749 | $results[] = $this->dump_result($result); |
| | 750 | } |
| | 751 | return $results; |
| | 752 | } |
| | 753 | /** |
| | 754 | * Find a record in this table using a SQL Statement |
| | 755 | * |
| | 756 | * @param string $sqlQuery |
| | 757 | * @return ActiveRecord Cursor |
| | 758 | */ |
| | 759 | public function find_by_sql($sqlQuery) { |
| | 760 | $this->_connect(); |
| | 761 | $row = $this->db->fetch_one($sqlQuery); |
| | 762 | if ($row !== false) { |
| | 763 | $this->dump_result_self($row); |
| | 764 | return $this->dump_result($row); |
| | 765 | } else { |
| | 766 | return false; |
| | 767 | } |
| | 768 | } |
| | 769 | /** |
| | 770 | * Execute a SQL Statement directly |
| | 771 | * |
| | 772 | * @param string $sqlQuery |
| | 773 | * @return int affected |
| | 774 | */ |
| | 775 | public function sql($sqlQuery) { |
| | 776 | $this->_connect(); |
| | 777 | return $this->db->query($sqlQuery); |
| | 778 | } |
| | 779 | /** |
| | 780 | * Return Fist Record |
| | 781 | * |
| | 782 | * @param mixed $what |
| | 783 | * @param boolean $debug |
| | 784 | * |
| | 785 | * Recibe los mismos parametros que find |
| | 786 | * |
| | 787 | * @return ActiveRecord Cursor |
| | 788 | */ |
| | 789 | public function find_first($what = '') { |
| | 790 | $this->_connect(); |
| | 791 | $what = get_params(func_get_args()); |
| | 792 | $select = "SELECT "; |
| | 793 | if (isset($what['columns'])) { |
| | 794 | $select.= ActiveRecord::sql_sanizite($what['columns']); |
| | 795 | } elseif (isset($what['distinct'])) { |
| | 796 | $select.= 'DISTINCT '; |
| | 797 | $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields); |
| | 798 | } else { |
| | 799 | $select.= join(",", $this->fields); |
| | 800 | } |
| | 801 | if ($this->schema) { |
| | 802 | $select.= " FROM {$this->schema}.{$this->source}"; |
| | 803 | } else { |
| | 804 | $select.= " FROM {$this->source}"; |
| | 805 | } |
| | 806 | $what['limit'] = 1; |
| | 807 | $select.= $this->convert_params_to_sql($what); |
| | 808 | $resp = false; |
| | 809 | try { |
| | 810 | $result = $this->db->fetch_one($select); |
| | 811 | if ($result) { |
| | 812 | $this->dump_result_self($result); |
| | 813 | $resp = $this->dump_result($result); |
| | 814 | } |
| | 815 | } |
| | 816 | catch(Exception $e) { |
| | 817 | $this->exceptions($e); |
| | 818 | } |
| | 819 | return $resp; |
| | 820 | } |
| | 821 | /** |
| | 822 | * Find data on Relational Map table |
| | 823 | * |
| | 824 | * @param string $what |
| | 825 | * @return ActiveRecord Cursor |
| | 826 | * |
| | 827 | * columns: columnas a utilizar |
| | 828 | * conditions : condiciones de busqueda en WHERE |
| | 829 | * join: inclusion inner join o outer join |
| | 830 | * group : campo para grupo en GROUP BY |
| | 831 | * having : condicion para el grupo |
| | 832 | * order : campo para criterio de ordenamiento ORDER BY |
| | 833 | * distinct: campos para hacer select distinct |
| | 834 | */ |
| | 835 | public function find($what = '') { |
| | 836 | $this->_connect(); |
| | 837 | $what = get_params(func_get_args()); |
| | 838 | $select = "SELECT "; |
| | 839 | if (isset($what['columns'])) { |
| | 840 | $select.= $what['columns'] ? ActiveRecord::sql_sanizite($what['columns']) : join(",", $this->fields); |
| | 841 | } elseif (isset($what['distinct'])) { |
| | 842 | $select.= 'DISTINCT '; |
| | 843 | $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields); |
| | 844 | } else { |
| | 845 | $select.= join(",", $this->fields); |
| | 846 | } |
| | 847 | if ($this->schema) { |
| | 848 | $select.= " FROM {$this->schema}.{$this->source}"; |
| | 849 | } else { |
| | 850 | $select.= " FROM {$this->source}"; |
| | 851 | } |
| | 852 | $select.= $this->convert_params_to_sql($what); |
| | 853 | $results = array(); |
| | 854 | $all_results = $this->db->in_query($select); |
| | 855 | foreach($all_results AS $result) { |
| | 856 | $results[] = $this->dump_result($result); |
| | 857 | } |
| | 858 | $this->count = count($results); |
| | 859 | if (isset($what[0]) && is_numeric($what[0])) { |
| | 860 | if (!isset($results[0])) { |
| | 861 | $this->count = 0; |
| | 862 | return false; |
| | 863 | } else { |
| | 864 | $this->dump_result_self($all_results[0]); |
| | 865 | $this->count = 1; |
| | 866 | return $results[0]; |
| | 867 | } |
| | 868 | } else { |
| | 869 | $this->count = count($results); |
| | 870 | return $results; |
| | 871 | } |
| | 872 | } |
| | 873 | /* |
| | 874 | * Arma una consulta SQL con el parametro $what, as�: |
| | 875 | * $what = get_params(func_get_args()); |
| | 876 | * $select = "SELECT * FROM Clientes"; |
| | 877 | * $select.= $this->convert_params_to_sql($what); |
| | 878 | * |
| | 879 | * @param string $what |
| | 880 | * @return string |
| | 881 | */ |
| | 882 | public function convert_params_to_sql($what = '') { |
| | 883 | $select = ""; |
| | 884 | if (is_array($what)) { |
| | 885 | if (!isset($what['conditions'])) { |
| | 886 | if (!isset($this->primary_key[0]) && (isset($this->id) || $this->is_view)) { |
| | 887 | $this->primary_key[0] = "id"; |
| | 888 | } |
| | 889 | ActiveRecord::sql_item_sanizite($this->primary_key[0]); |
| | 890 | if (isset($what[0])) { |
| | 891 | if (is_numeric($what[0])) { |
| | 892 | $what['conditions'] = "{$this->primary_key[0]} = {$this->db->add_quotes($what[0]) }"; |
| | 893 | } else { |
| | 894 | if ($what[0] == '') { |
| | 895 | $what['conditions'] = "{$this->primary_key[0]} = ''"; |
| | 896 | } else { |
| | 897 | $what['conditions'] = $what[0]; |
| | 898 | } |
| | 899 | } |
| | 900 | } |
| | 901 | } |
| | 902 | if (isset($what['join']) && $what['join']) { |
| | 903 | $select.= " {$what['join']}"; |
| | 904 | } |
| | 905 | if (isset($what['conditions']) && $what['conditions']) { |
| | 906 | $select.= " WHERE {$what['conditions']}"; |
| | 907 | } |
| | 908 | if (isset($what['group']) && $what['group']) { |
| | 909 | $select.= " GROUP BY {$what['group']}"; |
| | 910 | } |
| | 911 | if (isset($what['having']) && $what['having']) { |
| | 912 | $select.= " HAVING {$what['having']}"; |
| | 913 | } |
| | 914 | if (isset($what['order']) && $what['order']) { |
| | 915 | ActiveRecord::sql_sanizite($what['order']); |
| | 916 | $select.= " ORDER BY {$what['order']}"; |
| | 917 | } |
| | 918 | $limit_args = array($select); |
| | 919 | if (isset($what['limit'])) { |
| | 920 | array_push($limit_args, "limit: $what[limit]"); |
| | 921 | } |
| | 922 | if (isset($what['offset'])) { |
| | 923 | array_push($limit_args, "offset: $what[offset]"); |
| | 924 | } |
| | 925 | if (count($limit_args) > 1) { |
| | 926 | $select = call_user_func_array(array($this, 'limit'), $limit_args); |
| | 927 | } |
| | 928 | } else { |
| | 929 | if (strlen($what)) { |
| | 930 | if (is_numeric($what)) { |
| | 931 | $select.= "WHERE {$this->primary_key[0]} = '$what'"; |
| | 932 | } else { |
| | 933 | $select.= "WHERE $what"; |
| | 934 | } |
| | 935 | } |
| | 936 | } |
| | 937 | return $select; |
| | 938 | } |
| | 939 | /* |
| | 940 | * Devuelve una clausula LIMIT adecuada al RDBMS empleado |
| | 941 | * |
| | 942 | * limit: maxima cantidad de elementos a mostrar |
| | 943 | * offset: desde que elemento se comienza a mostrar |
| | 944 | * |
| | 945 | * @param string $sql consulta select |
| | 946 | * @return String clausula LIMIT adecuada al RDBMS empleado |
| | 947 | */ |
| | 948 | public function limit($sql) { |
| | 949 | $args = func_get_args(); |
| | 950 | return call_user_func_array(array($this->db, 'limit'), $args); |
| | 951 | } |
| | 952 | /** |
| | 953 | * Ejecuta un SELECT DISTINCT |
| | 954 | * @param string $what |
| | 955 | * @return array |
| | 956 | * |
| | 957 | * Soporta parametros iguales a find |
| | 958 | * |
| | 959 | */ |
| | 960 | public function distinct($what = '') { |
| | 961 | $this->_connect(); |
| | 962 | $what = get_params(func_get_args()); |
| | 963 | if ($this->schema) { |
| | 964 | $table = $this->schema . "." . $this->source; |
| | 965 | } else { |
| | 966 | $table = $this->source; |
| | 967 | } |
| | 968 | if (!isset($what['columns'])) { |
| | 969 | $what['columns'] = $what['0']; |
| | 970 | } else { |
| | 971 | if (!$what['columns']) { |
| | 972 | $what['columns'] = $what['0']; |
| | 973 | } |
| | 974 | } |
| | 975 | $what['columns'] = ActiveRecord::sql_sanizite($what['columns']); |
| | 976 | $select = "SELECT DISTINCT {$what['columns']} FROM $table "; |
| | 977 | /** |
| | 978 | * Se elimina el de indice cero ya que por defecto convert_params_to_sql lo considera como una condicion en WHERE |
| | 979 | */ |
| | 980 | unset($what[0]); |
| | 981 | $select.= $this->convert_params_to_sql($what); |
| | 982 | $results = array(); |
| | 983 | foreach($this->db->fetch_all($select) as $result) { |
| | 984 | $results[] = $result[0]; |
| | 985 | } |
| | 986 | return $results; |
| | 987 | } |
| | 988 | /** |
| | 989 | * Ejecuta una consulta en el RDBM directamente |
| | 990 | * |
| | 991 | * @param string $sql |
| | 992 | * @return resource |
| | 993 | */ |
| | 994 | public function select_one($sql) { |
| | 995 | $this->_connect(); |
| | 996 | if (substr(ltrim($sql), 0, 7) != "SELECT") { |
| | 997 | $sql = "SELECT " . $sql; |
| | 998 | } |
| | 999 | $num = $this->db->fetch_one($sql); |
| | 1000 | return $num[0]; |
| | 1001 | } |
| | 1002 | static public function static_select_one($sql) { |
| | 1003 | $db = db::raw_connect(); |
| | 1004 | if (substr(ltrim($sql), 0, 7) != "SELECT") { |
| | 1005 | $sql = "SELECT " . $sql; |
| | 1006 | } |
| | 1007 | $num = $db->fetch_one($sql); |
| | 1008 | return $num[0]; |
| | 1009 | } |
| | 1010 | /** |
| | 1011 | * Realiza un conteo de filas |
| | 1012 | * |
| | 1013 | * @param string $what |
| | 1014 | * @return integer |
| | 1015 | */ |
| | 1016 | public function count($what = '') { |
| | 1017 | $this->_connect(); |
| | 1018 | $what = get_params(func_get_args()); |
| | 1019 | if ($this->schema) { |
| | 1020 | $table = "{$this->schema}.{$this->source}"; |
| | 1021 | } else { |
| | 1022 | $table = $this->source; |
| | 1023 | } |
| | 1024 | if (isset($what['distinct']) && $what['distinct']) { |
| | 1025 | if (isset($what['group']) || isset($what['order'])) { |
| | 1026 | $select = "SELECT COUNT(*) FROM (SELECT DISTINCT {$what['distinct']} FROM $table "; |
| | 1027 | $select.= $this->convert_params_to_sql($what); |
| | 1028 | $select.= ') AS t '; |
| | 1029 | } else { |
| | 1030 | $select = "SELECT COUNT(DISTINCT {$what['distinct']}) FROM $table "; |
| | 1031 | $select.= $this->convert_params_to_sql($what); |
| | 1032 | } |
| | 1033 | } else { |
| | 1034 | $select = "SELECT COUNT(*) FROM $table "; |
| | 1035 | $select.= $this->convert_params_to_sql($what); |
| | 1036 | } |
| | 1037 | $num = $this->db->fetch_one($select); |
| | 1038 | return $num[0]; |
| | 1039 | } |
| | 1040 | /** |
| | 1041 | * Realiza un promedio sobre el campo $what |
| | 1042 | * |
| | 1043 | * @param string $what |
| | 1044 | * @return array |
| | 1045 | */ |
| | 1046 | public function average($what = '') { |
| | 1047 | $this->_connect(); |
| | 1048 | $what = get_params(func_get_args()); |
| | 1049 | if (isset($what['column'])) { |
| | 1050 | if (!$what['column']) { |
| | 1051 | $what['column'] = $what[0]; |
| | 1052 | } |
| | 1053 | } else { |
| | 1054 | $what['column'] = $what[0]; |
| | 1055 | } |
| | 1056 | unset($what[0]); |
| | 1057 | ActiveRecord::sql_item_sanizite($what['column']); |
| | 1058 | if ($this->schema) { |
| | 1059 | $table = "{$this->schema}.{$this->source}"; |
| | 1060 | } else { |
| | 1061 | $table = $this->source; |
| | 1062 | } |
| | 1063 | $select = "SELECT AVG({$what['column']}) FROM $table "; |
| | 1064 | $select.= $this->convert_params_to_sql($what); |
| | 1065 | $num = $this->db->fetch_one($select); |
| | 1066 | return $num[0]; |
| | 1067 | } |
| | 1068 | public function sum($what = '') { |
| | 1069 | $this->_connect(); |
| | 1070 | $what = get_params(func_get_args()); |
| | 1071 | if (isset($what['column'])) { |
| | 1072 | if (!$what['column']) { |
| | 1073 | $what['column'] = $what[0]; |
| | 1074 | } |
| | 1075 | } else { |
| | 1076 | $what['column'] = $what[0]; |
| | 1077 | } |
| | 1078 | unset($what[0]); |
| | 1079 | ActiveRecord::sql_item_sanizite($what['column']); |
| | 1080 | if ($this->schema) { |
| | 1081 | $table = "{$this->schema}.{$this->source}"; |
| | 1082 | } else { |
| | 1083 | $table = $this->source; |
| | 1084 | } |
| | 1085 | $select = "SELECT SUM({$what['column']}) FROM $table "; |
| | 1086 | $select.= $this->convert_params_to_sql($what); |
| | 1087 | $num = $this->db->fetch_one($select); |
| | 1088 | return $num[0]; |
| | 1089 | } |
| | 1090 | /** |
| | 1091 | * Busca el valor maximo para el campo $what |
| | 1092 | * |
| | 1093 | * @param string $what |
| | 1094 | * @return mixed |
| | 1095 | */ |
| | 1096 | public function maximum($what = '') { |
| | 1097 | $this->_connect(); |
| | 1098 | $what = get_params(func_get_args()); |
| | 1099 | if (isset($what['column'])) { |
| | 1100 | if (!$what['column']) { |
| | 1101 | $what['column'] = $what[0]; |
| | 1102 | } |
| | 1103 | } else { |
| | 1104 | $what['column'] = $what[0]; |
| | 1105 | } |
| | 1106 | unset($what[0]); |
| | 1107 | ActiveRecord::sql_item_sanizite($what['column']); |
| | 1108 | if ($this->schema) { |
| | 1109 | $table = "{$this->schema}.{$this->source}"; |
| | 1110 | } else { |
| | 1111 | $table = $this->source; |
| | 1112 | } |
| | 1113 | $select = "SELECT MAX({$what['column']}) FROM $table "; |
| | 1114 | $select.= $this->convert_params_to_sql($what); |
| | 1115 | $num = $this->db->fetch_one($select); |
| | 1116 | return $num[0]; |
| | 1117 | } |
| | 1118 | /** |
| | 1119 | * Busca el valor minimo para el campo $what |
| | 1120 | * |
| | 1121 | * @param string $what |
| | 1122 | * @return mixed |
| | 1123 | */ |
| | 1124 | public function minimum($what = '') { |
| | 1125 | $this->_connect(); |
| | 1126 | $what = get_params(func_get_args()); |
| | 1127 | if (isset($what['column'])) { |
| | 1128 | if (!$what['column']) { |
| | 1129 | $what['column'] = $what[0]; |
| | 1130 | } |
| | 1131 | } else { |
| | 1132 | $what['column'] = $what[0]; |
| | 1133 | } |
| | 1134 | unset($what[0]); |
| | 1135 | ActiveRecord::sql_item_sanizite($what['column']); |
| | 1136 | if ($this->schema) { |
| | 1137 | $table = "{$this->schema}.{$this->source}"; |
| | 1138 | } else { |
| | 1139 | $table = $this->source; |
| | 1140 | } |
| | 1141 | $select = "SELECT MIN({$what['column']}) FROM $table "; |
| | 1142 | $select.= $this->convert_params_to_sql($what); |
| | 1143 | $num = $this->db->fetch_one($select); |
| | 1144 | return $num[0]; |
| | 1145 | } |
| | 1146 | /** |
| | 1147 | * Realiza un conteo directo mediante $sql |
| | 1148 | * |
| | 1149 | * @param string $sqlQuery |
| | 1150 | * @return mixed |
| | 1151 | */ |
| | 1152 | public function count_by_sql($sqlQuery) { |
| | 1153 | $this->_connect(); |
| | 1154 | $num = $this->db->fetch_one($sqlQuery); |
| | 1155 | return $num[0]; |
| | 1156 | } |
| | 1157 | /** |
| | 1158 | * Iguala los valores de un resultado de la base de datos |
| | 1159 | * en un nuevo objeto con sus correspondientes |
| | 1160 | * atributos de la clase |
| | 1161 | * |
| | 1162 | * @param array $result |
| | 1163 | * @return ActiveRecord |
| | 1164 | */ |
| | 1165 | function dump_result($result) { |
| | 1166 | $this->_connect(); |
| | 1167 | $obj = clone $this; |
| | 1168 | /** |
| | 1169 | * Consulta si la clase es padre de otra y crea el tipo de dato correcto |
| | 1170 | */ |
| | 1171 | if (isset($result['type'])) { |
| | 1172 | if (in_array($result['type'], $this->parent_of)) { |
| | 1173 | if (class_exists($result['type'])) { |
| | 1174 | $obj = new $result['type']; |
| | 1175 | unset($result['type']); |
| | 1176 | } |
| | 1177 | } |
| | 1178 | } |
| | 1179 | $this->_dump_lock = true; |
| | 1180 | if (is_array($result)) { |
| | 1181 | foreach($result as $k => $r) { |
| | 1182 | if (!is_numeric($k)) { |
| | 1183 | $obj->$k = stripslashes($r); |
| | 1184 | } |
| | 1185 | } |
| | 1186 | } |
| | 1187 | $this->_dump_lock = false; |
| | 1188 | return $obj; |
| | 1189 | } |
| | 1190 | /** |
| | 1191 | * Iguala los valores de un resultado de la base de datos |
| | 1192 | * con sus correspondientes atributos de la clase |
| | 1193 | * |
| | 1194 | * @param array $result |
| | 1195 | * @return ActiveRecord |
| | 1196 | */ |
| | 1197 | public function dump_result_self($result) { |
| | 1198 | $this->_connect(); |
| | 1199 | $this->_dump_lock = true; |
| | 1200 | if (is_array($result)) { |
| | 1201 | foreach($result as $k => $r) { |
| | 1202 | if (!is_numeric($k)) { |
| | 1203 | $this->$k = stripslashes($r); |
| | 1204 | } |
| | 1205 | } |
| | 1206 | } |
| | 1207 | $this->_dump_lock = false; |
| | 1208 | } |
| | 1209 | /** |
| | 1210 | * Create a new Row using values from $_REQUEST |
| | 1211 | * |
| | 1212 | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
| | 1213 | * @return boolean success |
| | 1214 | */ |
| | 1215 | public function create_from_request($form = '') { |
| | 1216 | if ($form) { |
| | 1217 | return $this->create($_REQUEST[$form]); |
| | 1218 | } else { |
| | 1219 | return $this->create($_REQUEST); |
| | 1220 | } |
| | 1221 | } |
| | 1222 | /** |
| | 1223 | * Saves a new Row using values from $_REQUEST |
| | 1224 | * |
| | 1225 | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
| | 1226 | * @return boolean success |
| | 1227 | */ |
| | 1228 | public function save_from_request($form = '') { |
| | 1229 | if ($form) { |
| | 1230 | return $this->save($_REQUEST[$form]); |
| | 1231 | } else { |
| | 1232 | return $this->save($_REQUEST); |
| | 1233 | } |
| | 1234 | } |
| | 1235 | /** |
| | 1236 | * Updates a Row using values from $_REQUEST |
| | 1237 | * |
| | 1238 | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
| | 1239 | * @return boolean success |
| | 1240 | */ |
| | 1241 | public function update_from_request($form = '') { |
| | 1242 | if ($form) { |
| | 1243 | return $this->update($_REQUEST[$form]); |
| | 1244 | } else { |
| | 1245 | return $this->update($_REQUEST); |
| | 1246 | } |
| | 1247 | } |
| | 1248 | /** |
| | 1249 | * Creates a new Row in map table |
| | 1250 | * |
| | 1251 | * @param mixed $values |
| | 1252 | * @return success boolean |
| | 1253 | */ |
| | 1254 | public function create() { |
| | 1255 | $this->_connect(); |
| | 1256 | if (func_num_args() > 0) { |
| | 1257 | $params = get_params(func_get_args()); |
| | 1258 | $values = (isset($params[0]) && is_array($params[0])) ? $params[0] : $params; |
| | 1259 | foreach($this->fields as $field) { |
| | 1260 | if (isset($values[$field])) { |
| | 1261 | $this->$field = $values[$field]; |
| | 1262 | } |
| | 1263 | } |
| | 1264 | } |
| | 1265 | if ($this->primary_key[0] == 'id') { |
| | 1266 | $this->id = null; |
| | 1267 | } |
| | 1268 | return $this->save(); |
| | 1269 | } |
| | 1270 | /** |
| | 1271 | * Consulta si un determinado registro existe o no |
| | 1272 | * en la entidad de la base de datos |
| | 1273 | * |
| | 1274 | * @return boolean |
| | 1275 | */ |
| | 1276 | function exists($where_pk = '') { |
| | 1277 | $this->_connect(); |
| | 1278 | if ($this->schema) { |
| | 1279 | $table = "{$this->schema}.{$this->source}"; |
| | 1280 | } else { |
| | 1281 | $table = $this->source; |
| | 1282 | } |
| | 1283 | if (!$where_pk) { |
| | 1284 | $where_pk = array(); |
| | 1285 | foreach($this->primary_key as $key) { |
| | 1286 | if ($this->$key) { |
| | 1287 | $where_pk[] = " $key = '{$this->$key}'"; |
| | 1288 | } |
| | 1289 | } |
| | 1290 | if (count($where_pk)) { |
| | 1291 | $this->_where_pk = join(" AND ", $where_pk); |
| | 1292 | } else { |
| | 1293 | return 0; |
| | 1294 | } |
| | 1295 | $query = "SELECT COUNT(*) FROM $table WHERE {$this->_where_pk}"; |
| | 1296 | } else { |
| | 1297 | if (is_numeric($where_pk)) { |
| | 1298 | $query = "SELECT(*) FROM $table WHERE id = '$where_pk'"; |
| | 1299 | } else { |
| | 1300 | $query = "SELECT COUNT(*) FROM $table WHERE $where_pk"; |
| | 1301 | } |
| | 1302 | } |
| | 1303 | $num = $this->db->fetch_one($query); |
| | 1304 | return $num[0]; |
| | 1305 | } |
| | 1306 | /** |
| | 1307 | * Saves Information on the ActiveRecord Properties |
| | 1308 | * @param array $values array de valores a cargar |
| | 1309 | * @return boolean success |
| | 1310 | */ |
| | 1311 | public function save() { |
| | 1312 | $this->_connect(); |
| | 1313 | if (func_num_args() > 0) { |
| | 1314 | $params = get_params(func_get_args()); |
| | 1315 | $values = (isset($params[0]) && is_array($params[0])) ? $params[0] : $params; |
| | 1316 | foreach($this->fields as $field) { |
| | 1317 | if (isset($values[$field])) { |
| | 1318 | $this->$field = $values[$field]; |
| | 1319 | } |
| | 1320 | } |
| | 1321 | } |
| | 1322 | $ex = $this->exists(); |
| | 1323 | if ($this->schema) { |
| | 1324 | $table = $this->schema . "." . $this->source; |
| | 1325 | } else { |
| | 1326 | $table = $this->source; |
| | 1327 | } |
| | 1328 | #Run Validation Callbacks Before |
| | 1329 | if (method_exists($this, 'before_validation')) { |
| | 1330 | if ($this->before_validation() == 'cancel') { |
| | 1331 | return false; |
| | 1332 | } |
| | 1333 | } else { |
| | 1334 | if (isset($this->before_validation)) { |
| | 1335 | $method = $this->before_validation; |
| | 1336 | if ($this->$method() == 'cancel') { |
| | 1337 | return false; |
| | 1338 | } |
| | 1339 | } |
| | 1340 | } |
| | 1341 | if (!$ex && method_exists($this, "before_validation_on_create")) { |
| | 1342 | if ($this->before_validation_on_create() == 'cancel') { |
| | 1343 | return false; |
| | 1344 | } |
| | 1345 | } else { |
| | 1346 | if (isset($this->before_validation_on_create)) { |
| | 1347 | $method = $this->before_validation_on_create; |
| | 1348 | if ($this->$method() == 'cancel') { |
| | 1349 | return false; |
| | 1350 | } |
| | 1351 | } |
| | 1352 | } |
| | 1353 | if ($ex && method_exists($this, "before_validation_on_update")) { |
| | 1354 | if ($this->before_validation_on_update() == 'cancel') { |
| | 1355 | return false; |
| | 1356 | } |
| | 1357 | } else { |
| | 1358 | if (isset($this->before_validation_on_update)) { |
| | 1359 | $method = $this->before_validation_on_update; |
| | 1360 | if ($this->$method() == 'cancel') { |
| | 1361 | return false; |
| | 1362 | } |
| | 1363 | } |
| | 1364 | } |
| | 1365 | /** |
| | 1366 | * Recordamos que aqui no aparecen los que tienen valores por defecto, |
| | 1367 | * pero sin embargo se debe estar pendiente de validar en las otras verificaciones |
| | 1368 | * los campos nulos, ya que en estas si el campo es nulo, realmente se refiere a un campo que |
| | 1369 | * debe tomar el valor por defecto |
| | 1370 | * |
| | 1371 | */ |
| | 1372 | $e = false; |
| | 1373 | for ($i = 0;$i <= count($this->not_null) - 1;$i++) { |
| | 1374 | $f = $this->not_null[$i]; |
| | 1375 | if (isset($this->$f) && (is_null($this->$f) || $this->$f == '')) { |
| | 1376 | if (!$ex && $f == 'id') { |
| | 1377 | continue; |
| | 1378 | } |
| | 1379 | if (!$ex && in_array($f, $this->_at)) { |
| | 1380 | continue; |
| | 1381 | } |
| | 1382 | if ($ex && in_array($f, $this->_in)) { |
| | 1383 | continue; |
| | 1384 | } |
| | 1385 | Flash::error("Error: El campo $f no puede ser nulo"); |
| | 1386 | $e = true; |
| | 1387 | } |
| | 1388 | } |
| | 1389 | if ($e) { |
| | 1390 | return false; |
| | 1391 | } |
| | 1392 | /** |
| | 1393 | * Validacion validates_presence |
| | 1394 | * |
| | 1395 | */ |
| | 1396 | $e = false; |
| | 1397 | foreach($this->_validates_presence as $f => $opt) { |
| | 1398 | $f = $this->not_null[$i]; |
| | 1399 | if (isset($this->$f) && (is_null($this->$f) || $this->$f == '')) { |
| | 1400 | if (!$ex && $f == 'id') { |
| | 1401 | continue; |
| | 1402 | } |
| | 1403 | if (!$ex && in_array($f, $this->_at)) { |
| | 1404 | continue; |
| | 1405 | } |
| | 1406 | if ($ex && in_array($f, $this->_in)) { |
| | 1407 | continue; |
| | 1408 | } |
| | 1409 | if (isset($opt['message'])) { |
| | 1410 | Flash::error($opt['message']); |
| | 1411 | } else { |
| | 1412 | $field = isset($opt['field']) ? $opt['field'] : $f; |
| | 1413 | Flash::error("Error: El campo $field no puede ser nulo"); |
| | 1414 | } |
| | 1415 | $e = true; |
| | 1416 | } |
| | 1417 | } |
| | 1418 | if ($e) { |
| | 1419 | return false; |
| | 1420 | } |
| | 1421 | /** |
| | 1422 | * Validacion validates_length |
| | 1423 | * |
| | 1424 | */ |
| | 1425 | $e = false; |
| | 1426 | foreach($this->_validates_length as $f => $opt) { |
| | 1427 | if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') { |
| | 1428 | $field = isset($opt['field']) ? $opt['field'] : $f; |
| | 1429 | if (isset($opt['in'])) { |
| | 1430 | $in = explode(":", $opt['in']); |
| | 1431 | if (is_numeric($in[0]) && is_numeric($in[1])) { |
| | 1432 | $opt['minimum'] = $in[0]; |
| | 1433 | $opt['maximum'] = $in[1]; |
| | 1434 | } |
| | 1435 | } |
| | 1436 | if (isset($opt['minimum']) && is_numeric($opt['minimum'])) { |
| | 1437 | $n = $opt['minimum']; |
| | 1438 | if (strlen($this->$f) < $n) { |
| | 1439 | if (!isset($opt['too_short'])) { |
| | 1440 | Flash::error("Error: El campo $field debe tener como mínimo $n caracteres"); |
| | 1441 | $e = true; |
| | 1442 | } else { |
| | 1443 | Flash::error($opt['too_short']); |
| | 1444 | $e = true; |
| | 1445 | } |
| | 1446 | } |
| | 1447 | } |
| | 1448 | if (isset($opt['maximum']) && is_numeric($opt['maximum'])) { |
| | 1449 | $n = $opt['maximum']; |
| | 1450 | if (strlen($this->$f) > $n) { |
| | 1451 | if (!isset($opt['too_long'])) { |
| | 1452 | Flash::error("Error: El campo $field debe tener como máximo $n caracteres"); |
| | 1453 | $e = true; |
| | 1454 | } else { |
| | 1455 | Flash::error($opt['too_long']); |
| | 1456 | $e = true; |
| | 1457 | } |
| | 1458 | } |
| | 1459 | } |
| | 1460 | } |
| | 1461 | } |
| | 1462 | if ($e) { |
| | 1463 | return false; |
| | 1464 | } |
| | 1465 | /** |
| | 1466 | * Validacion validates_inclusion |
| | 1467 | * |
| | 1468 | */ |
| | 1469 | $e = false; |
| | 1470 | foreach($this->_validates_inclusion as $finc => $list) { |
| | 1471 | if (isset($this->$finc) && !is_null($this->$finc) && $this->$finc != '') { |
| | 1472 | if (!is_array($list[1]) && $this->$finc != $list[1]) { |
| | 1473 | if (isset($list['message'])) { |
| | 1474 | Flash::error($list['message']); |
| | 1475 | } else { |
| | 1476 | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
| | 1477 | Flash::error("$field debe tener un valor entre ({$list[1]})"); |
| | 1478 | } |
| | 1479 | $e = true; |
| | 1480 | } else { |
| | 1481 | if (!in_array($this->$finc, $list[1])) { |
| | 1482 | if (isset($list['message'])) { |
| | 1483 | Flash::error($list['message']); |
| | 1484 | } else { |
| | 1485 | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
| | 1486 | Flash::error("$field debe tener un valor entre (" . join(",", $list[1]) . ")"); |
| | 1487 | } |
| | 1488 | $e = true; |
| | 1489 | } |
| | 1490 | } |
| | 1491 | } |
| | 1492 | } |
| | 1493 | if ($e) { |
| | 1494 | return false; |
| | 1495 | } |
| | 1496 | /** |
| | 1497 | * Validacion validates_exclusion |
| | 1498 | * |
| | 1499 | */ |
| | 1500 | $e = false; |
| | 1501 | foreach($this->_validates_exclusion as $finc => $list) { |
| | 1502 | if (isset($this->$finc) && !is_null($this->$finc) && $this->$finc != '') { |
| | 1503 | if (!is_array($list[1]) && $this->$finc == $list[1]) { |
| | 1504 | if (isset($list['message'])) { |
| | 1505 | Flash::error($list['message']); |
| | 1506 | } else { |
| | 1507 | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
| | 1508 | Flash::error("$field no debe tener un valor entre ({$list[1]})"); |
| | 1509 | } |
| | 1510 | $e = true; |
| | 1511 | } else { |
| | 1512 | if (in_array($this->$finc, $list[1])) { |
| | 1513 | if (isset($list['message'])) { |
| | 1514 | Flash::error($list['message']); |
| | 1515 | } else { |
| | 1516 | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
| | 1517 | Flash::error("$field no debe tener un valor entre (" . join(",", $list[1]) . ")"); |
| | 1518 | } |
| | 1519 | $e = true; |
| | 1520 | } |
| | 1521 | } |
| | 1522 | } |
| | 1523 | } |
| | 1524 | if ($e) { |
| | 1525 | return false; |
| | 1526 | } |
| | 1527 | /** |
| | 1528 | * Validacion validates_numericality |
| | 1529 | * |
| | 1530 | */ |
| | 1531 | $e = false; |
| | 1532 | foreach($this->_validates_numericality as $fnum => $opt) { |
| | 1533 | if (isset($this->$fnum) && !is_null($this->$fnum) && $this->$fnum != '') { |
| | 1534 | if (!is_numeric($this->$fnum)) { |
| | 1535 | if (isset($opt['message'])) { |
| | 1536 | Flash::error($opt['message']); |
| | 1537 | } else { |
| | 1538 | $field = isset($opt['field']) ? $opt['field'] : ucwords($fnum); |
| | 1539 | Flash::error("$field debe tener un valor numérico"); |
| | 1540 | } |
| | 1541 | $e = true; |
| | 1542 | } |
| | 1543 | } |
| | 1544 | } |
| | 1545 | if ($e) { |
| | 1546 | return false; |
| | 1547 | } |
| | 1548 | /** |
| | 1549 | * Validacion validates_format |
| | 1550 | * |
| | 1551 | */ |
| | 1552 | $e = false; |
| | 1553 | foreach($this->_validates_format as $fkey => $opt) { |
| | 1554 | if (isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey != '') { |
| | 1555 | if (!ereg($opt[1], $this->$fkey)) { |
| | 1556 | if (isset($opt['message'])) { |
| | 1557 | Flash::error($opt['message']); |
| | 1558 | } else { |
| | 1559 | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
| | 1560 | Flash::error("Formato erroneo para $field"); |
| | 1561 | } |
| | 1562 | $e = true; |
| | 1563 | } |
| | 1564 | } |
| | 1565 | } |
| | 1566 | if ($e) { |
| | 1567 | return false; |
| | 1568 | } |
| | 1569 | /** |
| | 1570 | * Validacion validates_date |
| | 1571 | * |
| | 1572 | */ |
| | 1573 | $e = false; |
| | 1574 | foreach($this->_validates_date as $fkey => $opt) { |
| | 1575 | if (isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey != '') { |
| | 1576 | if (!ereg("^[0-9]{4}[-/](0[1-9]|1[12])[-/](0[1-9]|[12][0-9]|3[01])$", $this->$fkey, $regs)) { |
| | 1577 | if (isset($opt['message'])) { |
| | 1578 | Flash::error($opt['message']); |
| | 1579 | } else { |
| | 1580 | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
| | 1581 | Flash::error("Formato de fecha ({$this->$fkey}) erroneo para $field"); |
| | 1582 | } |
| | 1583 | $e = true; |
| | 1584 | } |
| | 1585 | } |
| | 1586 | } |
| | 1587 | if ($e) { |
| | 1588 | return false; |
| | 1589 | } |
| | 1590 | /** |
| | 1591 | * Validacion validates_email |
| | 1592 | * |
| | 1593 | */ |
| | 1594 | $e = false; |
| | 1595 | foreach($this->_validates_email as $fkey) { |
| | 1596 | if (isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey != '') { |
| | 1597 | if (!ereg("^[a-zA-Z0-9_\.\+]+@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*$", $this->$fkey, $regs)) { |
| | 1598 | if (isset($opt['message'])) { |
| | 1599 | Flash::error($opt['message']); |
| | 1600 | } else { |
| | 1601 | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
| | 1602 | Flash::error("Formato de e-mail erroneo en el campo $field"); |
| | 1603 | } |
| | 1604 | $e = true; |
| | 1605 | } |
| | 1606 | } |
| | 1607 | } |
| | 1608 | if ($e) { |
| | 1609 | return false; |
| | 1610 | } |
| | 1611 | /** |
| | 1612 | * Validacion validates_uniqueness |
| | 1613 | * |
| | 1614 | */ |
| | 1615 | $e = false; |
| | 1616 | foreach($this->_validates_uniqueness as $fkey => $opt) { |
| | 1617 | ActiveRecord::sql_item_sanizite($fkey); |
| | 1618 | if (isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey != '') { |
| | 1619 | if ($ex) { |
| | 1620 | $copy = clone $this; |
| | 1621 | $copy->find_first("$fkey = {$this->db->add_quotes($this->$fkey) }"); |
| | 1622 | $number = $this->db->fetch_one("SELECT COUNT(*) FROM $table WHERE $fkey = {$this->db->add_quotes($this->$fkey) } and $fkey <> {$this->db->add_quotes($copy->$fkey) }"); |
| | 1623 | } else { |
| | 1624 | $number = $this->db->fetch_one("SELECT COUNT(*) FROM $table WHERE $fkey = {$this->db->add_quotes($this->$fkey) }"); |
| | 1625 | } |
| | 1626 | if ((int)$number[0]) { |
| | 1627 | if (isset($opt['message'])) { |
| | 1628 | Flash::error($opt['message']); |
| | 1629 | } else { |
| | 1630 | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
| | 1631 | Flash::error("El valor '{$this->$fkey}' ya existe para el campo $field"); |
| | 1632 | } |
| | 1633 | $e = true; |
| | 1634 | } |
| | 1635 | } |
| | 1636 | } |
| | 1637 | if ($e) { |
| | 1638 | return false; |
| | 1639 | } |
| | 1640 | #Run Validation Callbacks After |
| | 1641 | if (!$ex && method_exists($this, "after_validation_on_create")) { |
| | 1642 | if ($this->after_validation_on_create() == 'cancel') { |
| | 1643 | return false; |
| | 1644 | } |
| | 1645 | } else { |
| | 1646 | if (isset($this->after_validation_on_create)) { |
| | 1647 | $method = $this->after_validation_on_create; |
| | 1648 | if ($this->$method() == 'cancel') { |
| | 1649 | return false; |
| | 1650 | } |
| | 1651 | } |
| | 1652 | } |
| | 1653 | if ($ex && method_exists($this, "after_validation_on_update")) { |
| | 1654 | if ($this->after_validation_on_update() == 'cancel') { |
| | 1655 | return false; |
| | 1656 | } |
| | 1657 | } else { |
| | 1658 | if (isset($this->after_validation_on_update)) { |
| | 1659 | $method = $this->after_validation_on_update; |
| | 1660 | if ($this->$method() == 'cancel') return false; |
| | 1661 | } |
| | 1662 | } |
| | 1663 | if (method_exists($this, 'after_validation')) { |
| | 1664 | if ($this->after_validation() == 'cancel') { |
| | 1665 | return false; |
| | 1666 | } |
| | 1667 | } else { |
| | 1668 | if (isset($this->after_validation)) { |
| | 1669 | $method = $this->after_validation; |
| | 1670 | if ($this->$method() == 'cancel') { |
| | 1671 | return false; |
| | 1672 | } |
| | 1673 | } |
| | 1674 | } |
| | 1675 | # Run Before Callbacks |
| | 1676 | if (method_exists($this, "before_save")) { |
| | 1677 | if ($this->before_save() == 'cancel') { |
| | 1678 | return false; |
| | 1679 | } |
| | 1680 | } else { |
| | 1681 | if (isset($this->before_save)) { |
| | 1682 | $method = $this->before_save; |
| | 1683 | if ($this->$method() == 'cancel') { |
| | 1684 | return false; |
| | 1685 | } |
| | 1686 | } |
| | 1687 | } |
| | 1688 | if ($ex && method_exists($this, "before_update")) { |
| | 1689 | if ($this->before_update() == 'cancel') { |
| | 1690 | return false; |
| | 1691 | } |
| | 1692 | } else { |
| | 1693 | if (isset($this->before_update)) { |
| | 1694 | $method = $this->before_update; |
| | 1695 | if ($this->$method() == 'cancel') { |
| | 1696 | return false; |
| | 1697 | } |
| | 1698 | } |
| | 1699 | } |
| | 1700 | if (!$ex && method_exists($this, "before_create")) { |
| | 1701 | if ($this->before_create() == 'cancel') { |
| | 1702 | return false; |
| | 1703 | } |
| | 1704 | } else { |
| | 1705 | if (isset($this->before_create)) { |
| | 1706 | $method = $this->before_create; |
| | 1707 | if ($this->$method() == 'cancel') { |
| | 1708 | return false; |
| | 1709 | } |
| | 1710 | } |
| | 1711 | } |
| | 1712 | $environment = Config::read("environment.ini"); |
| | 1713 | $config = $environment->{$this->get_mode() }; |
| | 1714 | if ($ex) { |
| | 1715 | $fields = array(); |
| | 1716 | $values = array(); |
| | 1717 | foreach($this->non_primary as $np) { |
| | 1718 | $np = ActiveRecord::sql_item_sanizite($np); |
| | 1719 | if (in_array($np, $this->_in)) { |
| | 1720 | if ($config->database->type == 'oracle') { |
| | 1721 | $this->$np = date("Y-m-d"); |
| | 1722 | } else { |
| | 1723 | $this->$np = date("Y-m-d G:i:s"); |
| | 1724 | } |
| | 1725 | } |
| | 1726 | if (isset($this->$np)) { |
| | 1727 | $fields[] = $np; |
| | 1728 | if (is_null($this->$np) || $this->$np == '') { |
| | 1729 | $values[] = "NULL"; |
| | 1730 | } elseif (substr($this->$np, 0, 1) == "%") { |
| | 1731 | $values[] = str_replace("%", "", $this->$np); |
| | 1732 | } else { |
| | 1733 | /** |
| | 1734 | * Se debe especificar el formato de fecha en Oracle |
| | 1735 | */ |
| | 1736 | if ($this->_data_type[$np] == 'date' && $config->database->type == 'oracle') { |
| | 1737 | $values[] = "TO_DATE(" . $this->db->add_quotes($this->$np) . ", 'YYYY-MM-DD')"; |
| | 1738 | } else { |
| | 1739 | $values[] = $this->db->add_quotes($this->$np); |
| | 1740 | } |
| | 1741 | } |
| | 1742 | } |
| | 1743 | } |
| | 1744 | $val = $this->db->update($table, $fields, $values, $this->_where_pk); |
| | 1745 | } else { |
| | 1746 | $fields = array(); |
| | 1747 | $values = array(); |
| | 1748 | foreach($this->fields as $field) { |
| | 1749 | if ($field != 'id' && !$this->id) { |
| | 1750 | if (in_array($field, $this->_at)) { |
| | 1751 | if ($config->database->type == 'oracle') { |
| | 1752 | $this->$field = date("Y-m-d"); |
| | 1753 | } else { |
| | 1754 | $this->$field = date("Y-m-d G:i:s"); |
| | 1755 | } |
| | 1756 | } |
| | 1757 | if (in_array($field, $this->_in)) { |
| | 1758 | unset($this->$field); |
| | 1759 | } |
| | 1760 | $use_default = in_array($field, $this->_with_default) && isset($this->$field) && (is_null($this->$field) || $this->$field == ''); |
| | 1761 | if (isset($this->$field) && !$use_default) { |
| | 1762 | $fields[] = ActiveRecord::sql_sanizite($field); |
| | 1763 | if (substr($this->$field, 0, 1) == "%") { |
| | 1764 | $values[] = str_replace("%", "", $this->$field); |
| | 1765 | } else { |
| | 1766 | if ($this->is_a_numeric_type($field) || $this->$field == null) { |
| | 1767 | $values[] = addslashes($this->$field !== '' && $this->$field !== null ? $this->$field : "NULL"); |
| | 1768 | } else { |
| | 1769 | if ($this->_data_type[$field] == 'date' && $config->database->type == 'oracle') { |
| | 1770 | /** |
| | 1771 | * Se debe especificar el formato de fecha en Oracle |
| | 1772 | */ |
| | 1773 | $values[] = "TO_DATE(" . $this->db->add_quotes($this->$field) . ", 'YYYY-MM-DD')"; |
| | 1774 | } else { |
| | 1775 | if (!is_null($this->$field) && $this->$field != '') { |
| | 1776 | $values[] = $this->db->add_quotes($this->$field); |
| | 1777 | } else { |
| | 1778 | $values[] = "NULL"; |
| | 1779 | } |
| | 1780 | } |
| | 1781 | } |
| | 1782 | } |
| | 1783 | } |
| | 1784 | } else { |
| | 1785 | /** |
| | 1786 | * Campos autonumericos en Oracle deben utilizar una sequencia auxiliar |
| | 1787 | */ |
| | 1788 | if ($config->database->type == 'oracle') { |
| | 1789 | if (!$this->id) { |
| | 1790 | $fields[] = "id"; |
| | 1791 | $values[] = $this->source . "_id_seq.NEXTVAL"; |
| | 1792 | } |
| | 1793 | } |
| | 1794 | if ($config->database->type == 'informix') { |
| | 1795 | if (!$this->id) { |
| | 1796 | $fields[] = "id"; |
| | 1797 | $values[] = 0; |
| | 1798 | } |
| | 1799 | } |
| | 1800 | } |
| | 1801 | } |
| | 1802 | $val = $this->db->insert($table, $values, $fields); |
| | 1803 | } |
| | 1804 | if (!isset($config->database->pdo) && $config->database->type == 'oracle') { |
| | 1805 | $this->commit(); |
| | 1806 | } |
| | 1807 | if (!$ex) { |
| | 1808 | $this->db->logger = true; |
| | 1809 | $m = $this->db->last_insert_id($table, $this->primary_key[0]); |
| | 1810 | $this->find_first($m); |
| | 1811 | } |
| | 1812 | if ($val) { |
| | 1813 | if ($ex && method_exists($this, "after_update")) { |
| | 1814 | if ($this->after_update() == 'cancel') { |
| | 1815 | return false; |
| | 1816 | } |
| | 1817 | } else { |
| | 1818 | if (isset($this->after_update)) { |
| | 1819 | $method = $this->after_update; |
| | 1820 | if ($this->$method() == 'cancel') { |
| | 1821 | return false; |
| | 1822 | } |
| | 1823 | } |
| | 1824 | } |
| | 1825 | if (!$ex && method_exists($this, "after_create")) { |
| | 1826 | if ($this->after_create() == 'cancel') { |
| | 1827 | return false; |
| | 1828 | } |
| | 1829 | } else { |
| | 1830 | if (isset($this->after_create)) { |
| | 1831 | $method = $this->after_create; |
| | 1832 | if ($this->$method() == 'cancel') { |
| | 1833 | return false; |
| | 1834 | } |
| | 1835 | } |
| | 1836 | } |
| | 1837 | if (method_exists($this, "after_save")) { |
| | 1838 | if ($this->after_save() == 'cancel') { |
| | 1839 | return false; |
| | 1840 | } |
| | 1841 | } else { |
| | 1842 | if (isset($this->after_save)) { |
| | 1843 | $method = $this->after_save; |
| | 1844 | if ($this->$method() == 'cancel') { |
| | 1845 | return false; |
| | 1846 | } |
| | 1847 | } |
| | 1848 | } |
| | 1849 | return $val; |
| | 1850 | } else { |
| | 1851 | return false; |
| | 1852 | } |
| | 1853 | } |
| | 1854 | /** |
| | 1855 | * Find All data in the Relational Table |
| | 1856 | * |
| | 1857 | * @param string $field |
| | 1858 | * @param string $value |
| | 1859 | * @return ActiveRecod Cursor |
| | 1860 | */ |
| | 1861 | function find_all_by($field, $value) { |
| | 1862 | ActiveRecord::sql_item_sanizite($field); |
| | 1863 | return $this->find("conditions: $field = {$this->db->add_quotes($value) }"); |
| | 1864 | } |
| | 1865 | /** |
| | 1866 | * Updates Data in the Relational Table |
| | 1867 | * |
| | 1868 | * @param mixed $values |
| | 1869 | * @return boolean sucess |
| | 1870 | */ |
| | 1871 | function update() { |
| | 1872 | $this->_connect(); |
| | 1873 | if (func_num_args() > 0) { |
| | 1874 | $params = get_params(func_get_args()); |
| | 1875 | $values = (isset($params[0]) && is_array($params[0])) ? $params[0] : $params; |
| | 1876 | foreach($this->fields as $field) { |
| | 1877 | if (isset($values[$field])) { |
| | 1878 | $this->$field = $values[$field]; |
| | 1879 | } |
| | 1880 | } |
| | 1881 | } |
| | 1882 | if ($this->exists()) { |
| | 1883 | return $this->save(); |
| | 1884 | } else { |
| | 1885 | Flash::error('No se puede actualizar porque el registro no existe'); |
| | 1886 | return false; |
| | 1887 | } |
| | 1888 | } |
| | 1889 | /** |
| | 1890 | * Deletes data from Relational Map Table |
| | 1891 | * |
| | 1892 | * @param mixed $what |
| | 1893 | */ |
| | 1894 | function delete($what = '') { |
| | 1895 | $this->_connect(); |
| | 1896 | if (func_num_args() > 1) { |
| | 1897 | $what = get_params(func_get_args()); |
| | 1898 | } |
| | 1899 | if ($this->schema) { |
| | 1900 | $table = $this->schema . "." . $this->source; |
| | 1901 | } else { |
| | 1902 | $table = $this->source; |
| | 1903 | } |
| | 1904 | $conditions = ""; |
| | 1905 | if (is_array($what)) { |
| | 1906 | if ($what["conditions"]) { |
| | 1907 | $conditions = $what["conditions"]; |
| | 1908 | } |
| | 1909 | } else { |
| | 1910 | if (is_numeric($what)) { |
| | 1911 | ActiveRecord::sql_sanizite($this->primary_key[0]); |
| | 1912 | $conditions = "{$this->primary_key[0]} = '$what'"; |
| | 1913 | } else { |
| | 1914 | if ($what) { |
| | 1915 | $conditions = $what; |
| | 1916 | } else { |
| | 1917 | ActiveRecord::sql_sanizite($this->primary_key[0]); |
| | 1918 | $conditions = "{$this->primary_key[0]} = '{$this->{$this->primary_key[0]}}'"; |
| | 1919 | } |
| | 1920 | } |
| | 1921 | } |
| | 1922 | if (method_exists($this, "before_delete")) { |
| | 1923 | if ($this->id) { |
| | 1924 | $this->find($this->id); |
| | 1925 | } |
| | 1926 | if ($this->before_delete() == 'cancel') { |
| | 1927 | return false; |
| | 1928 | } |
| | 1929 | } else { |
| | 1930 | if (isset($this->before_delete)) { |
| | 1931 | if ($this->id) { |
| | 1932 | $this->find($this->id); |
| | 1933 | } |
| | 1934 | $method = $this->before_delete; |
| | 1935 | if ($this->$method() == 'cancel') { |
| | 1936 | return false; |
| | 1937 | } |
| | 1938 | } |
| | 1939 | } |
| | 1940 | $val = $this->db->delete($table, $conditions); |
| | 1941 | if ($val) { |
| | 1942 | if (method_exists($this, "after_delete")) { |
| | 1943 | if ($this->after_delete() == 'cancel') { |
| | 1944 | return false; |
| | 1945 | } |
| | 1946 | } else { |
| | 1947 | if (isset($this->after_delete)) { |
| | 1948 | $method = $this->after_delete; |
| | 1949 | if ($this->$method() == 'cancel') { |
| | 1950 | return false; |
| | 1951 | } |
| | 1952 | } |
| | 1953 | } |
| | 1954 | } |
| | 1955 | return $val; |
| | 1956 | } |
| | 1957 | /** |
| | 1958 | * Actualiza todos los atributos de la entidad |
| | 1959 | * $Clientes->update_all("estado='A', fecha='2005-02-02'", "id>100"); |
| | 1960 | * $Clientes->update_all("estado='A', fecha='2005-02-02'", "id>100", "limit: 10"); |
| | 1961 | * |
| | 1962 | * @param string $values |
| | 1963 | */ |
| | 1964 | function update_all($values) { |
| | 1965 | $this->_connect(); |
| | 1966 | $params = array(); |
| | 1967 | if ($this->schema) { |
| | 1968 | $table = $this->schema . "." . $this->source; |
| | 1969 | } else { |
| | 1970 | $table = $this->source; |
| | 1971 | } |
| | 1972 | if (func_num_args() > 1) { |
| | 1973 | $params = get_params(func_get_args()); |
| | 1974 | } |
| | 1975 | if (!isset($params['conditions']) || !$params['conditions']) { |
| | 1976 | if (isset($params[1])) { |
| | 1977 | $params['conditions'] = $params[1]; |
| | 1978 | } else { |
| | 1979 | $params['conditions'] = ""; |
| | 1980 | } |
| | 1981 | } |
| | 1982 | if ($params['conditions']) { |
| | 1983 | $params['conditions'] = " WHERE " . $params['conditions']; |
| | 1984 | } |
| | 1985 | $sql = "UPDATE $table SET $values {$params['conditions']}"; |
| | 1986 | $limit_args = array($sql); |
| | 1987 | if (isset($params['limit'])) { |
| | 1988 | array_push($limit_args, "limit: $params[limit]"); |
| | 1989 | } |
| | 1990 | if (isset($params['offset'])) { |
| | 1991 | array_push($limit_args, "offset: $params[offset]"); |
| | 1992 | } |
| | 1993 | if (count($limit_args) > 1) { |
| | 1994 | $sql = call_user_func_array(array($this, 'limit'), $limit_args); |
| | 1995 | } |
| | 1996 | $environment = Config::read("environment.ini"); |
| | 1997 | $config = $environment->{$this->get_mode() }; |
| | 1998 | if (!isset($config->database->pdo) || !$config->database->pdo) { |
| | 1999 | if ($config->database->type == "informix") { |
| | 2000 | $this->db->set_return_rows(false); |
| | 2001 | } |
| | 2002 | } |
| | 2003 | return $this->db->query($sql); |
| | 2004 | } |
| | 2005 | /** |
| | 2006 | * Delete All data from Relational Map Table |
| | 2007 | * |
| | 2008 | * @param string $conditions |
| | 2009 | * @return boolean |
| | 2010 | */ |
| | 2011 | function delete_all($conditions = '') { |
| | 2012 | $this->_connect(); |
| | 2013 | $limit = ""; |
| | 2014 | if ($this->schema) { |
| | 2015 | $table = $this->schema . "." . $this->source; |
| | 2016 | } else { |
| | 2017 | $table = $this->source; |
| | 2018 | } |
| | 2019 | if (func_num_args() > 1) { |
| | 2020 | $params = get_params(func_get_args()); |
| | 2021 | $limit_args = array($select); |
| | 2022 | if (isset($params['limit'])) { |
| | 2023 | array_push($limit_args, "limit: $params[limit]"); |
| | 2024 | } |
| | 2025 | if (isset($params['offset'])) { |
| | 2026 | array_push($limit_args, "offset: $params[offset]"); |
| | 2027 | } |
| | 2028 | if (count($limit_args) > 1) { |
| | 2029 | $select = call_user_func_array(array($this, 'limit'), $limit_args); |
| | 2030 | } |
| | 2031 | } |
| | 2032 | return $this->db->delete($table, $conditions); |
| | 2033 | } |
| | 2034 | /** |
| | 2035 | * ********************************************************************************* |
| | 2036 | * Metodos de Debug |
| | 2037 | * ********************************************************************************* |
| | 2038 | */ |
| | 2039 | /** |
| | 2040 | * Imprime una version humana de los valores de los campos |
| | 2041 | * del modelo en una sola linea |
| | 2042 | * |
| | 2043 | */ |
| | 2044 | public function inspect() { |
| | 2045 | $this->_connect(); |
| | 2046 | $inspect = array(); |
| | 2047 | foreach($this->fields as $field) { |
| | 2048 | if (!is_array($field)) { |
| | 2049 | $inspect[] = "$field: {$this->$field}"; |
| | 2050 | } |
| | 2051 | } |
| | 2052 | return join(", ", $inspect); |
| | 2053 | } |
| | 2054 | /** |
| | 2055 | * ********************************************************************************* |
| | 2056 | * Metodos de Validacion |
| | 2057 | * ********************************************************************************* |
| | 2058 | */ |
| | 2059 | /** |
| | 2060 | * Validate that Attributes cannot have a NULL value |
| | 2061 | */ |
| | 2062 | protected function validates_presence_of() { |
| | 2063 | $this->_connect(); |
| | 2064 | $params = get_params(func_get_args()); |
| | 2065 | if (is_array($params[0])) { |
| | 2066 | $params = $params[0]; |
| | 2067 | } |
| | 2068 | $fields = array(); |
| | 2069 | for ($i = 0;isset($params[$i]);$i++) { |
| | 2070 | $fields[] = $params[$i]; |
| | 2071 | unset($params[$i]); |
| | 2072 | $i++; |
| | 2073 | } |
| | 2074 | foreach($fields as $p) { |
| | 2075 | if (!in_array($p, $this->fields) && !isset($this->$p)) { |
| | 2076 | throw new ActiveRecordException('No se puede validar presencia de "' . $p . '" |
| | 2077 | en el modelo ' . $this->source . ' porque no existe el atributo</u><br> |
| 2455 | | return false; |
| 2456 | | } |
| 2457 | | if(!isset($this->_validates_date[$p])){ |
| 2458 | | $this->_validates_date[$p] = $params; |
| 2459 | | } |
| 2460 | | } |
| 2461 | | return true; |
| 2462 | | } |
| 2463 | | |
| 2464 | | |
| 2465 | | /** |
| 2466 | | * Verifica si un campo es de tipo de dato numerico o no |
| 2467 | | * |
| 2468 | | * @param string $field |
| 2469 | | * @return boolean |
| 2470 | | */ |
| 2471 | | public function is_a_numeric_type($field){ |
| 2472 | | if(strpos(" ".$this->_data_type[$field], "int") || strpos(" ".$this->_data_type[$field], "decimal") |
| 2473 | | || strpos(" ".$this->_data_type[$field], "number")){ |
| 2474 | | return true; |
| 2475 | | } else { |
| 2476 | | return false; |
| 2477 | | } |
| 2478 | | } |
| 2479 | | |
| 2480 | | /** |
| 2481 | | * Obtiene los datos de los metadatos generados por Primera vez en la Sesión |
| 2482 | | * |
| 2483 | | * @param string $table |
| 2484 | | * @return array |
| 2485 | | */ |
| 2486 | | static function get_meta_data($table){ |
| 2487 | | if(isset(self::$models[$table])){ |
| 2488 | | return self::$models[$table]; |
| 2489 | | } else { |
| 2490 | | $active_app = Router::get_active_app(); |
| 2491 | | if(isset($_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']][$active_app][$table])){ |
| 2492 | | self::set_meta_data($table, $_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']][$active_app][$table]); |
| 2493 | | return self::$models[$table]; |
| 2494 | | } |
| 2495 | | return array(); |
| 2496 | | } |
| 2497 | | } |
| 2498 | | |
| 2499 | | /** |
| 2500 | | * Crea un registro de meta datos para la tabla especificada |
| 2501 | | * |
| 2502 | | * @param string $table |
| 2503 | | * @param array $meta_data |
| 2504 | | */ |
| 2505 | | static function set_meta_data($table, $meta_data){ |
| 2506 | | $active_app = Router::get_active_app(); |
| 2507 | | if(!isset($_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']][$active_app][$table])){ |
| 2508 | | $_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']][$active_app][$table] = $meta_data; |
| 2509 | | } |
| 2510 | | self::$models[$table] = $meta_data; |
| 2511 | | return true; |
| 2512 | | } |
| 2513 | | |
| 2514 | | /** |
| 2515 | | * Elimina la información de cache del objeto y hace que sea cargada en la proxima operación |
| 2516 | | * |
| 2517 | | */ |
| 2518 | | public function reset_cache_information(){ |
| 2519 | | $active_app = Router::get_active_app(); |
| 2520 | | unset($_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']][$active_app][$this->source]); |
| 2521 | | $this->_dumped = false; |
| 2522 | | if(!$this->is_dumped()){ |
| 2523 | | $this->dump(); |
| 2524 | | } |
| 2525 | | } |
| 2526 | | |
| 2527 | | |
| 2528 | | /******************************************************************************************* |
| 2529 | | * Metodos para generacion de relaciones |
| 2530 | | *******************************************************************************************/ |
| 2531 | | |
| 2532 | | /** |
| 2533 | | * Crea una relacion 1-1 entre dos modelos |
| 2534 | | * |
| 2535 | | * @param string $relation |
| 2536 | | * |
| 2537 | | * model : nombre del modelo al que se refiere |
| 2538 | | * fk : campo por el cual se relaciona (llave foranea) |
| 2539 | | */ |
| 2540 | | protected function has_one($relation){ |
| 2541 | | $params = get_params(func_get_args()); |
| 2542 | | for($i=0; isset($params[$i]); $i++){ |
| 2543 | | $relation = uncamelize(lcfirst($params[$i])); |
| 2544 | | if(!array_key_exists($relation, $this->_has_one)){ |
| 2545 | | $this->_has_one[$relation] = new stdClass(); |
| 2546 | | $this->_has_one[$relation]->model = ucfirst(camelize(isset($params['model']) ? $params['model'] : $relation)); |
| 2547 | | $this->_has_one[$relation]->fk = isset($params['fk']) ? $params['fk'] : uncamelize(lcfirst(get_class($this))).'_id'; |
| 2548 | | } |
| 2549 | | } |
| 2550 | | } |
| 2551 | | |
| 2552 | | /** |
| 2553 | | * Crea una relacion 1-1 inversa entre dos modelos |
| 2554 | | * |
| 2555 | | * @param string $relation |
| 2556 | | * |
| 2557 | | * model : nombre del modelo al que se refiere |
| 2558 | | * fk : campo por el cual se relaciona (llave foranea) |
| 2559 | | */ |
| 2560 | | protected function belongs_to($relation){ |
| 2561 | | $params = get_params(func_get_args()); |
| 2562 | | for($i=0; isset($params[$i]); $i++){ |
| 2563 | | $relation = uncamelize(lcfirst($params[$i])); |
| 2564 | | if(!array_key_exists($relation, $this->_belongs_to)){ |
| 2565 | | $this->_belongs_to[$relation] = new stdClass(); |
| 2566 | | $this->_belongs_to[$relation]->model = ucfirst(camelize(isset($params['model']) ? $params['model'] : $relation)); |
| 2567 | | $this->_belongs_to[$relation]->fk = isset($params['fk']) ? $params['fk'] : "{$relation}_id"; |
| 2568 | | } |
| 2569 | | } |
| 2570 | | } |
| 2571 | | |
| 2572 | | /** |
| 2573 | | * Crea una relacion 1-n entre dos modelos |
| 2574 | | * |
| 2575 | | * @param string $relation |
| 2576 | | * |
| 2577 | | * model : nombre del modelo al que se refiere |
| 2578 | | * fk : campo por el cual se relaciona (llave foranea) |
| 2579 | | */ |
| 2580 | | protected function has_many($relation){ |
| 2581 | | $params = get_params(func_get_args()); |
| 2582 | | for($i=0; isset($params[$i]); $i++){ |
| 2583 | | $relation = uncamelize(lcfirst($params[$i])); |
| 2584 | | if(!array_key_exists($relation, $this->_has_many)){ |
| 2585 | | $this->_has_many[$relation] = new stdClass(); |
| 2586 | | $this->_has_many[$relation]->model = ucfirst(camelize(isset($params['model']) ? $params['model'] : $relation)); |
| 2587 | | $this->_has_many[$relation]->fk = isset($params['fk']) ? $params['fk'] : uncamelize(lcfirst(get_class($this))).'_id'; |
| 2588 | | } |
| 2589 | | } |
| 2590 | | } |
| 2591 | | |
| 2592 | | /** |
| 2593 | | * Crea una relacion n-n o 1-n inversa entre dos modelos |
| 2594 | | * |
| 2595 | | * @param string $relation |
| 2596 | | * |
| 2597 | | * model : nombre del modelo al que se refiere |
| 2598 | | * fk : campo por el cual se relaciona (llave foranea) |
| 2599 | | * key: campo llave que identifica al propio modelo |
| 2600 | | * through : atrav�s de que tabla |
| 2601 | | */ |
| 2602 | | protected function has_and_belongs_to_many($relation){ |
| 2603 | | $params = get_params(func_get_args()); |
| 2604 | | for($i=0; isset($params[$i]); $i++){ |
| 2605 | | $relation = uncamelize(lcfirst($params[$i])); |
| 2606 | | if(!array_key_exists($relation, $this->_has_and_belongs_to_many)){ |
| 2607 | | $this->_has_and_belongs_to_many[$relation] = new stdClass(); |
| 2608 | | $this->_has_and_belongs_to_many[$relation]->model = ucfirst(camelize(isset($params['model']) ? $params['model'] : $relation)); |
| 2609 | | $this->_has_and_belongs_to_many[$relation]->fk = isset($params['fk']) ? $params['fk'] : "{$relation}_id"; |
| 2610 | | $this->_has_and_belongs_to_many[$relation]->key = isset($params['key']) ? $params['key'] : uncamelize(lcfirst(get_class($this))).'_id'; |
| 2611 | | |
| 2612 | | if(isset($params['through'])) { |
| 2613 | | $this->_has_and_belongs_to_many[$relation]->through = $params['through']; |
| 2614 | | } |
| 2615 | | } |
| 2616 | | } |
| 2617 | | } |
| 2618 | | |
| 2619 | | /** |
| 2620 | | * Herencia Simple |
| 2621 | | */ |
| 2622 | | |
| 2623 | | /** |
| 2624 | | * Especifica que la clase es padre de otra |
| 2625 | | * |
| 2626 | | * @param string $parent |
| 2627 | | */ |
| 2628 | | public function parent_of($parent){ |
| 2629 | | $parents = func_get_args(); |
| 2630 | | foreach($parents as $parent){ |
| 2631 | | if(!in_array($parent, $this->parent_of)){ |
| 2632 | | $this->parent_of[] = $parent; |
| 2633 | | } |
| 2634 | | } |
| 2635 | | } |
| 2636 | | |
| 2637 | | /** |
| 2638 | | * Elimina caracteres que podrian ayudar a ejecutar |
| 2639 | | * un ataque de Inyeccion SQL |
| 2640 | | * |
| 2641 | | * @param string $sql_item |
| 2642 | | */ |
| 2643 | | public static function sql_item_sanizite($sql_item){ |
| 2644 | | $sql_item = trim($sql_item); |
| 2645 | | if($sql_item!==''&&$sql_item!==null){ |
| 2646 | | $sql_item = ereg_replace("[ ]+", "", $sql_item); |
| 2647 | | if(!ereg("^[a-zA-Z0-9_\.]+$", $sql_item)){ |
| 2648 | | throw new ActiveRecordException("Se esta tratando de ejecutar una operacion maliciosa!"); |
| 2649 | | } |
| 2650 | | } |
| 2651 | | return $sql_item; |
| 2652 | | } |
| 2653 | | |
| 2654 | | /** |
| 2655 | | * Elimina caracteres que podrian ayudar a ejecutar |
| 2656 | | * un ataque de Inyeccion SQL |
| 2657 | | * |
| 2658 | | * @param string $sql_item |
| 2659 | | */ |
| 2660 | | public static function sql_sanizite($sql_item){ |
| 2661 | | $sql_item = trim($sql_item); |
| 2662 | | if($sql_item!==''&&$sql_item!==null){ |
| 2663 | | $sql_item = ereg_replace("[ ]+", "", $sql_item); |
| 2664 | | if(!ereg("^[a-zA-Z_0-9\,\(\)\.\*]+$", $sql_item)){ |
| 2665 | | throw new ActiveRecordException("Se esta tratando de ejecutar una operacion maliciosa!"); |
| 2666 | | } |
| 2667 | | } |
| 2668 | | return $sql_item; |
| 2669 | | } |
| 2670 | | |
| 2671 | | /** |
| 2672 | | * Al sobreescribir este metodo se puede controlar las excepciones de un modelo |
| 2673 | | * |
| 2674 | | * @param unknown_type $e |
| 2675 | | */ |
| 2676 | | protected function exceptions($e){ |
| 2677 | | throw $e; |
| 2678 | | } |
| 2679 | | |
| 2680 | | /** |
| 2681 | | * Implementacion de __toString Standard |
| 2682 | | * |
| 2683 | | */ |
| 2684 | | public function __toString(){ |
| 2685 | | return "<".get_class()." Object>"; |
| 2686 | | } |
| 2687 | | |
| 2688 | | /** |
| 2689 | | * Paginador para el modelo |
| 2690 | | * |
| 2691 | | * conditions: condiciones para paginacion |
| 2692 | | * page: numero de pagina a mostrar (por defecto la pagina 1) |
| 2693 | | * per_page: cantidad de elementos por pagina (por defecto 10 items por pagina) |
| 2694 | | * |
| 2695 | | * @return un objeto Page identico al que se regresa con el util paginate |
| 2696 | | **/ |
| 2697 | | public function paginate() { |
| 2698 | | $args = func_get_args(); |
| 2699 | | array_num_insert($args, 0, $this); |
| 2700 | | return call_user_func_array('paginate', $args); |
| 2701 | | } |
| 2702 | | |
| 2703 | | /** |
| 2704 | | * Paginador para el modelo atraves de consulta sql |
| 2705 | | * |
| 2706 | | * @param string $sql consulta sql |
| 2707 | | * |
| 2708 | | * page: numero de pagina a mostrar (por defecto la pagina 1) |
| 2709 | | * per_page: cantidad de elementos por pagina (por defecto 10 items por pagina) |
| 2710 | | * |
| 2711 | | * @return un objeto Page identico al que se regresa con el util paginate_by_sql |
| 2712 | | **/ |
| 2713 | | public function paginate_by_sql($sql) { |
| 2714 | | $args = func_get_args(); |
| 2715 | | array_num_insert($args, 0, $this); |
| 2716 | | return call_user_func_array('paginate_by_sql', $args); |
| 2717 | | } |
| 2718 | | |
| | 2283 | return false; |
| | 2284 | } |
| | 2285 | if (!isset($this->_validates_date[$p])) { |
| | 2286 | $this->_validates_date[$p] = $params; |
| | 2287 | } |
| | 2288 | } |
| | 2289 | return true; |
| | 2290 | } |
| | 2291 | /** |
| | 2292 | * Verifica si un campo es de tipo de dato numerico o no |
| | 2293 | * |
| | 2294 | * @param string $field |
| | 2295 | * @return boolean |
| | 2296 | */ |
| | 2297 | public function is_a_numeric_type($field) { |
| | 2298 | if (strpos(" " . $this->_data_type[$field], "int") || strpos(" " . $this->_data_type[$field], "decimal") || strpos(" " . $this->_data_type[$field], "number")) { |
| | 2299 | return true; |
| | 2300 | } else { |
| | 2301 | return false; |
| | 2302 | } |
| | 2303 | } |
| | 2304 | /** |
| | 2305 | * Obtiene los datos de los metadatos generados por Primera vez en la Sesión |
| | 2306 | * |
| | 2307 | * @param string $table |
| | 2308 | * @return array |
| | 2309 | */ |
| | 2310 | static function get_meta_data($table) { |
| | 2311 | if (isset(self::$models[$table])) { |
| | 2312 | return self::$models[$table]; |
| | 2313 | } else { |
| | 2314 | $active_app = Router::get_active_app(); |
| | 2315 | if (isset($_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']][$active_app][$table])) { |
| | 2316 | self::set_meta_data($table, $_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']][$active_app][$table]); |
| | 2317 | return self::$models[$table]; |
| | 2318 | } |
| | 2319 | return array(); |
| | 2320 | } |
| | 2321 | } |
| | 2322 | /** |
| | 2323 | * Crea un registro de meta datos para la tabla especificada |
| | 2324 | * |
| | 2325 | * @param string $table |
| | 2326 | * @param array $meta_data |
| | 2327 | */ |
| | 2328 | static function set_meta_data($table, $meta_data) { |
| | 2329 | $active_app = Router::get_active_app(); |
| | 2330 | if (!isset($_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']][$active_app][$table])) { |
| | 2331 | $_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']][$active_app][$table] = $meta_data; |
| | 2332 | } |
| | 2333 | self::$models[$table] = $meta_data; |
| | 2334 | return true; |
| | 2335 | } |
| | 2336 | /** |
| | 2337 | * Elimina la información de cache del objeto y hace que sea cargada en la proxima operación |
| | 2338 | * |
| | 2339 | */ |
| | 2340 | public function reset_cache_information() { |
| | 2341 | $active_app = Router::get_active_app(); |
| | 2342 | unset($_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']][$active_app][$this->source]); |
| | 2343 | $this->_dumped = false; |
| | 2344 | if (!$this->is_dumped()) { |
| | 2345 | $this->dump(); |
| | 2346 | } |
| | 2347 | } |
| | 2348 | /******************************************************************************************* |
| | 2349 | * Metodos para generacion de relaciones |
| | 2350 | *******************************************************************************************/ |
| | 2351 | /** |
| | 2352 | * Crea una relacion 1-1 entre dos modelos |
| | 2353 | * |
| | 2354 | * @param string $relation |
| | 2355 | * |
| | 2356 | * model : nombre del modelo al que se refiere |
| | 2357 | * fk : campo por el cual se relaciona (llave foranea) |
| | 2358 | */ |
| | 2359 | protected function has_one($relation) { |
| | 2360 | $params = get_params(func_get_args()); |
| | 2361 | for ($i = 0;isset($params[$i]);$i++) { |
| | 2362 | $relation = uncamelize(lcfirst($params[$i])); |
| | 2363 | if (!array_key_exists($relation, $this->_has_one)) { |
| | 2364 | $this->_has_one[$relation] = new stdClass(); |
| | 2365 | $this->_has_one[$relation]->model = ucfirst(camelize(isset($params['model']) ? $params['model'] : $relation)); |
| | 2366 | $this->_has_one[$relation]->fk = isset($params['fk']) ? $params['fk'] : uncamelize(lcfirst(get_class($this))) . '_id'; |
| | 2367 | } |
| | 2368 | } |
| | 2369 | } |
| | 2370 | /** |
| | 2371 | * Crea una relacion 1-1 inversa entre dos modelos |
| | 2372 | * |
| | 2373 | * @param string $relation |
| | 2374 | * |
| | 2375 | * model : nombre del modelo al que se refiere |
| | 2376 | * fk : campo por el cual se relaciona (llave foranea) |
| | 2377 | */ |
| | 2378 | protected function belongs_to($relation) { |
| | 2379 | $params = get_params(func_get_args()); |
| | 2380 | for ($i = 0;isset($params[$i]);$i++) { |
| | 2381 | $relation = uncamelize(lcfirst($params[$i])); |
| | 2382 | if (!array_key_exists($relation, $this->_belongs_to)) { |
| | 2383 | $this->_belongs_to[$relation] = new stdClass(); |
| | 2384 | $this->_belongs_to[$relation]->model = ucfirst(camelize(isset($params['model']) ? $params['model'] : $relation)); |
| | 2385 | $this->_belongs_to[$relation]->fk = isset($params['fk']) ? $params['fk'] : "{$relation}_id"; |
| | 2386 | } |
| | 2387 | } |
| | 2388 | } |
| | 2389 | /** |
| | 2390 | * Crea una relacion 1-n entre dos modelos |
| | 2391 | * |
| | 2392 | * @param string $relation |
| | 2393 | * |
| | 2394 | * model : nombre del modelo al que se refiere |
| | 2395 | * fk : campo por el cual se relaciona (llave foranea) |
| | 2396 | */ |
| | 2397 | protected function has_many($relation) { |
| | 2398 | $params = get_params(func_get_args()); |
| | 2399 | for ($i = 0;isset($params[$i]);$i++) { |
| | 2400 | $relation = uncamelize(lcfirst($params[$i])); |
| | 2401 | if (!array_key_exists($relation, $this->_has_many)) { |
| | 2402 | $this->_has_many[$relation] = new stdClass(); |
| | 2403 | $this->_has_many[$relation]->model = ucfirst(camelize(isset($params['model']) ? $params['model'] : $relation)); |
| | 2404 | $this->_has_many[$relation]->fk = isset($params['fk']) ? $params['fk'] : uncamelize(lcfirst(get_class($this))) . '_id'; |
| | 2405 | } |
| | 2406 | } |
| | 2407 | } |
| | 2408 | /** |
| | 2409 | * Crea una relacion n-n o 1-n inversa entre dos modelos |
| | 2410 | * |
| | 2411 | * @param string $relation |
| | 2412 | * |
| | 2413 | * model : nombre del modelo al que se refiere |
| | 2414 | * fk : campo por el cual se relaciona (llave foranea) |
| | 2415 | * key: campo llave que identifica al propio modelo |
| | 2416 | * through : atrav�s de que tabla |
| | 2417 | */ |
| | 2418 | protected function has_and_belongs_to_many($relation) { |
| | 2419 | $params = get_params(func_get_args()); |
| | 2420 | for ($i = 0;isset($params[$i]);$i++) { |
| | 2421 | $relation = uncamelize(lcfirst($params[$i])); |
| | 2422 | if (!array_key_exists($relation, $this->_has_and_belongs_to_many)) { |
| | 2423 | $this->_has_and_belongs_to_many[$relation] = new stdClass(); |
| | 2424 | $this->_has_and_belongs_to_many[$relation]->model = ucfirst(camelize(isset($params['model']) ? $params['model'] : $relation)); |
| | 2425 | $this->_has_and_belongs_to_many[$relation]->fk = isset($params['fk']) ? $params['fk'] : "{$relation}_id"; |
| | 2426 | $this->_has_and_belongs_to_many[$relation]->key = isset($params['key']) ? $params['key'] : uncamelize(lcfirst(get_class($this))) . '_id'; |
| | 2427 | if (isset($params['through'])) { |
| | 2428 | $this->_has_and_belongs_to_many[$relation]->through = $params['through']; |
| | 2429 | } |
| | 2430 | } |
| | 2431 | } |
| | 2432 | } |
| | 2433 | /** |
| | 2434 | * Herencia Simple |
| | 2435 | */ |
| | 2436 | /** |
| | 2437 | * Especifica que la clase es padre de otra |
| | 2438 | * |
| | 2439 | * @param string $parent |
| | 2440 | */ |
| | 2441 | public function parent_of($parent) { |
| | 2442 | $parents = func_get_args(); |
| | 2443 | foreach($parents as $parent) { |
| | 2444 | if (!in_array($parent, $this->parent_of)) { |
| | 2445 | $this->parent_of[] = $parent; |
| | 2446 | } |
| | 2447 | } |
| | 2448 | } |
| | 2449 | /** |
| | 2450 | * Elimina caracteres que podrian ayudar a ejecutar |
| | 2451 | * un ataque de Inyeccion SQL |
| | 2452 | * |
| | 2453 | * @param string $sql_item |
| | 2454 | */ |
| | 2455 | public static function sql_item_sanizite($sql_item) { |
| | 2456 | $sql_item = trim($sql_item); |
| | 2457 | if ($sql_item !== '' && $sql_item !== null) { |
| | 2458 | $sql_item = ereg_replace("[ ]+", "", $sql_item); |
| | 2459 | if (!ereg("^[a-zA-Z0-9_\.]+$", $sql_item)) { |
| | 2460 | throw new ActiveRecordException("Se esta tratando de ejecutar una operacion maliciosa!"); |
| | 2461 | } |
| | 2462 | } |
| | 2463 | return $sql_item; |
| | 2464 | } |
| | 2465 | /** |
| | 2466 | * Elimina caracteres que podrian ayudar a ejecutar |
| | 2467 | * un ataque de Inyeccion SQL |
| | 2468 | * |
| | 2469 | * @param string $sql_item |
| | 2470 | */ |
| | 2471 | public static function sql_sanizite($sql_item) { |
| | 2472 | $sql_item = trim($sql_item); |
| | 2473 | if ($sql_item !== '' && $sql_item !== null) { |
| | 2474 | $sql_item = ereg_replace("[ ]+", "", $sql_item); |
| | 2475 | if (!ereg("^[a-zA-Z_0-9\,\(\)\.\*]+$", $sql_item)) { |
| | 2476 | throw new ActiveRecordException("Se esta tratando de ejecutar una operacion maliciosa!"); |
| | 2477 | } |
| | 2478 | } |
| | 2479 | return $sql_item; |
| | 2480 | } |
| | 2481 | /** |
| | 2482 | * Al sobreescribir este metodo se puede controlar las excepciones de un modelo |
| | 2483 | * |
| | 2484 | * @param unknown_type $e |
| | 2485 | */ |
| | 2486 | protected function exceptions($e) { |
| | 2487 | throw $e; |
| | 2488 | } |
| | 2489 | /** |
| | 2490 | * Implementacion de __toString Standard |
| | 2491 | * |
| | 2492 | */ |
| | 2493 | public function __toString() { |
| | 2494 | return "<" . get_class() . " Object>"; |
| | 2495 | } |
| | 2496 | /** |
| | 2497 | * Paginador para el modelo |
| | 2498 | * |
| | 2499 | * conditions: condiciones para paginacion |
| | 2500 | * page: numero de pagina a mostrar (por defecto la pagina 1) |
| | 2501 | * per_page: cantidad de elementos por pagina (por defecto 10 items por pagina) |
| | 2502 | * |
| | 2503 | * @return un objeto Page identico al que se regresa con el util paginate |
| | 2504 | * |
| | 2505 | */ |
| | 2506 | public function paginate() { |
| | 2507 | $args = func_get_args(); |
| | 2508 | array_num_insert($args, 0, $this); |
| | 2509 | return call_user_func_array('paginate', $args); |
| | 2510 | } |
| | 2511 | /** |
| | 2512 | * Paginador para el modelo atraves de consulta sql |
| | 2513 | * |
| | 2514 | * @param string $sql consulta sql |
| | 2515 | * |
| | 2516 | * page: numero de pagina a mostrar (por defecto la pagina 1) |
| | 2517 | * per_page: cantidad de elementos por pagina (por defecto 10 items por pagina) |
| | 2518 | * |
| | 2519 | * @return un objeto Page identico al que se regresa con el util paginate_by_sql |
| | 2520 | * |
| | 2521 | */ |
| | 2522 | public function paginate_by_sql($sql) { |
| | 2523 | $args = func_get_args(); |
| | 2524 | array_num_insert($args, 0, $this); |
| | 2525 | return call_user_func_array('paginate_by_sql', $args); |
| | 2526 | } |