1 | <?php |
||
14 | trait CRUD_helpers { |
||
15 | use |
||
16 | CRUD; |
||
17 | /** |
||
18 | * Generic search |
||
19 | * |
||
20 | * @param mixed[] $search_parameters Array in form [attribute => value];<br> |
||
21 | * Or [attribute => [value1, value2, value3]];<br> |
||
22 | * Or [attribute => [from => a, to => b]];<br> |
||
23 | * Or [attribute => [...]] in case of joined tables, where ... is any of three constructions mentioned above;<br> |
||
24 | * if `total_count => true` element is present - total number of found rows will be returned instead of rows themselves |
||
25 | * @param int $page |
||
26 | * @param int $count |
||
27 | * @param string $order_by |
||
28 | * @param bool $asc |
||
29 | * |
||
30 | * @return false|int|int[]|string[] Array of `id` or number of elements |
||
31 | */ |
||
32 | 12 | protected function search ($search_parameters = [], $page = 1, $count = 100, $order_by = 'id', $asc = false) { |
|
50 | /** |
||
51 | * @param string $table_alias |
||
52 | * @param bool $total_count |
||
53 | * @param string[] $where |
||
54 | * @param array $where_params |
||
55 | * @param string $joins |
||
56 | * @param array $join_params |
||
57 | * @param int $page |
||
58 | * @param int $count |
||
59 | * @param string $order_by |
||
60 | * @param bool $asc |
||
61 | * |
||
62 | * @return false|int|int[]|string[] |
||
63 | */ |
||
64 | 12 | private function search_do ($table_alias, $total_count, $where, $where_params, $joins, $join_params, $page, $count, $order_by, $asc) { |
|
93 | /** |
||
94 | * @param string $table_alias |
||
95 | * @param string $key |
||
96 | * @param array $details |
||
97 | * @param string[] $where |
||
98 | * @param array $where_params |
||
99 | */ |
||
100 | 8 | private function search_conditions ($table_alias, $key, $details, &$where, &$where_params) { |
|
124 | /** |
||
125 | * @param string $table_alias |
||
126 | * @param string $table |
||
127 | * @param array|int|string $details |
||
128 | * @param string $joins |
||
129 | * @param array $join_params |
||
130 | * @param int $join_index |
||
131 | */ |
||
132 | 4 | private function search_conditions_join_table ($table_alias, $table, $details, &$joins, &$join_params, &$join_index) { |
|
133 | 4 | $data_model = $this->data_model[$table]; |
|
134 | 4 | $first_column = array_keys($this->data_model)[0]; |
|
135 | 4 | $first_column_join = array_keys($data_model['data_model'])[0]; |
|
136 | 4 | if (is_scalar($details)) { |
|
137 | $details = [ |
||
138 | 4 | array_keys($data_model['data_model'])[1] => $details |
|
139 | ]; |
||
140 | } |
||
141 | /** |
||
142 | * @var array $details |
||
143 | */ |
||
144 | 4 | ++$join_index; |
|
145 | $joins .= |
||
146 | 4 | "INNER JOIN `{$this->table}_$table` AS `j$join_index` |
|
147 | ON |
||
148 | 4 | `$table_alias`.`$first_column` = `j$join_index`.`$first_column_join`"; |
|
149 | 4 | $language_field = isset($data_model['language_field']) ? $data_model['language_field'] : false; |
|
150 | 4 | foreach ($details as $field => $value) { |
|
151 | 4 | if ($language_field === $field) { |
|
152 | 2 | continue; |
|
153 | } |
||
154 | 4 | $where_tmp = []; |
|
155 | 4 | $this->search_conditions("j$join_index", $field, $value, $where_tmp, $join_params); |
|
156 | 4 | $joins .= ' AND '.implode(' AND ', $where_tmp); |
|
157 | } |
||
158 | 4 | if ($language_field) { |
|
159 | /** @noinspection OffsetOperationsInspection */ |
||
160 | 2 | $clang = isset($details[$language_field]) ? $details[$language_field] : Language::instance()->clang; |
|
161 | $joins .= |
||
162 | " AND |
||
163 | ( |
||
164 | 2 | `j$join_index`.`lang` = '$clang' OR |
|
165 | 2 | `j$join_index`.`lang` = '' |
|
166 | 2 | )"; |
|
167 | } |
||
168 | 4 | } |
|
169 | /** |
||
170 | * @param string $table_alias |
||
171 | * @param string $order_by |
||
172 | * @param string $joins |
||
173 | * @param int $join_index |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | 12 | private function search_order_by ($table_alias, $order_by, &$joins, &$join_index) { |
|
211 | } |
||
212 |