PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Database / Model.php

Model.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.0, at includes/Core/Database/Model.php

454 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Provides Base Model Class
5 */
6
7 namespace BitCode\BitForm\Core\Database;
8
9 /**
10 * Undocumented class
11 */
12
13 use WP_Error;
14
15 class Model {
16 protected static $table;
17 protected static $primary_key;
18 protected $app_db;
19 protected $table_name;
20 protected $db_response;
21
22 /**
23 * Undocumented function
24 */
25 public function __construct() {
26 global $wpdb;
27 $this->app_db = $wpdb;
28 $this->table_name = $wpdb->prefix . static::$table;
29 }
30
31 /**
32 * Undocumented function
33 *
34 * @return void
35 */
36 public function insert($data = []) {
37 if (is_null($data)) {
38 return new WP_Error('empty_data', __('Form data is empty', 'bit-form'));
39 }
40 $result = $this->app_db->insert(
41 $this->table_name,
42 $data
43 );
44 return $this->getResult($result);
45 }
46
47 /**
48 * Undocumented function
49 *
50 * @param string $item
51 * @param array $condition
52 *
53 * @return mixed
54 */
55 public function get($item = '*', $condition = [], $limit = null, $offset = null, $order_by = null, $order_follow = null) {
56 if (\is_array($item)) {
57 $column_to_select = implode(',', $item);
58 } else {
59 $column_to_select = $item;
60 }
61 $checkCondition = $this->checkCondition($condition);
62 if (is_wp_error($checkCondition)) {
63 return $checkCondition;
64 }
65 $order = null;
66 if (!\is_null($order_by)) {
67 $order_follow = \is_null($order_follow) ? 'ASC' : $order_follow;
68 $order .= " ORDER BY $order_by $order_follow";
69 }
70 $paginate = null;
71 if (!\is_null($limit)) {
72 $limit = \intval($limit);
73 $paginate .= " LIMIT $limit ";
74 }
75 if (!\is_null($offset)) {
76 $offset = \intval($offset);
77 $paginate .= " OFFSET $offset ";
78 }
79 if (empty($condition)) {
80 $sql = "SELECT $column_to_select FROM `$this->table_name` $order $paginate";
81 $all_values = null;
82 } else {
83 $formatted_conditions = $this->getFormatedCondition($condition);
84 if ($formatted_conditions) {
85 $condition_to_check = $formatted_conditions['conditions'];
86 $all_values = $formatted_conditions['values'];
87 } else {
88 $condition_to_check = null;
89 $all_values = null;
90 }
91 $sql = "SELECT $column_to_select FROM `$this->table_name`"
92 . $condition_to_check . $order . $paginate;
93 }
94 return $this->execute($sql, $all_values)->getResult();
95 }
96
97 /**
98 * Undocumented function
99 *
100 * @param string $item
101 * @param array $condition
102 *
103 * @return void
104 */
105 public function count($condition = null) {
106 $checkCondition = $this->checkCondition($condition);
107 if (is_wp_error($checkCondition)) {
108 return $checkCondition;
109 }
110 if (empty($condition)) {
111 $result = $this->app_db->query(
112 "SELECT COUNT(*) FROM `$this->table_name`"
113 );
114 } else {
115 $formatted_conditions = $this->getFormatedCondition($condition);
116 if ($formatted_conditions) {
117 $condition_to_check = $formatted_conditions['conditions'];
118 $all_values = $formatted_conditions['values'];
119 } else {
120 $condition_to_check = null;
121 $all_values = null;
122 }
123 $result = $this->app_db->query(
124 $this->app_db->prepare(
125 "SELECT COUNT(*) as count FROM `$this->table_name`"
126 . $condition_to_check,
127 $all_values
128 )
129 );
130 }
131 if (!$result) {
132 if ($this->app_db->last_error) {
133 return new WP_Error('db_error', $this->app_db->last_error);
134 }
135 return new WP_Error('db_error', __('Result is empty', 'bit-form'));
136 } else {
137 return $this->app_db->last_result;
138 }
139 }
140
141 /**
142 * Undocumented function
143 *
144 * @param array $data_to_update
145 * @param array $condition
146 *
147 * @return void
148 */
149 public function update(array $data, array $condition) {
150 if (
151 !\is_null($data)
152 && \is_array($data)
153 && array_keys($data) !== range(0, count($data) - 1)
154 ) {
155 $data_to_update = $data;
156 } else {
157 return new WP_Error(
158 'update_error',
159 __('Nothing to update', 'bit-form')
160 );
161 }
162 $update_condition = (!\is_null($condition) &&
163 array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null;
164 $result = $this->app_db->update(
165 $this->table_name,
166 $data_to_update,
167 $update_condition
168 );
169 return $this->getResult($result);
170 }
171
172 /**
173 * Undocumented function
174 *
175 * @param array $data_to_update
176 * @param array $condition
177 *
178 * @return void
179 */
180 public function bulkUpdate(array $data = null, array $condition = null) {
181 if (
182 !\is_null($data)
183 && \is_array($data)
184 && array_keys($data) !== range(0, count($data) - 1)
185 ) {
186 $data_to_update = $data;
187 } else {
188 return new WP_Error(
189 'update_error',
190 __('Nothing to update', 'bit-form')
191 );
192 }
193
194 $update_fields = '';
195 $all_values = [];
196 $index_checker = 0;
197 $data_count = count($data_to_update) - 1;
198 foreach ($data_to_update as $field_name => $field_value) {
199 $update_fields .= $field_name . ' = ' . $this->getFieldFormat($field_value);
200 if ($index_checker < $data_count) {
201 $update_fields .= ',';
202 }
203 $index_checker = $index_checker + 1;
204 $all_values[] = $field_value;
205 }
206 $update_condition = (!\is_null($condition) &&
207 array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null;
208 $formatted_conditions = $this->getFormatedCondition($update_condition);
209 if ($formatted_conditions) {
210 $condition_to_check = $formatted_conditions['conditions'];
211 $all_values = array_merge($all_values, $formatted_conditions['values']);
212 } else {
213 $condition_to_check = null;
214 }
215 $result = $this->app_db->query(
216 $this->app_db->prepare(
217 "UPDATE $this->table_name SET $update_fields $condition_to_check",
218 $all_values
219 )
220 );
221 return $this->getResult($result);
222 }
223
224 /**
225 * Duplicate's row
226 *
227 * @param array $data_to_update
228 * @param array $condition
229 *
230 * @return void
231 */
232 public function duplicate(array $columns, array $duplicate, array $condition) {
233 if (!(!\is_null($columns)
234 && \is_array($columns)
235 && array_keys($columns) === range(0, count($columns) - 1)
236 && !\is_null($duplicate)
237 && \is_array($duplicate)
238 && array_keys($duplicate) === range(0, count($duplicate) - 1))) {
239 return new WP_Error(
240 'duplicate_error',
241 __('Nothing to duplicate', 'bit-form')
242 );
243 }
244
245 $dupCol = '';
246 $insCol = \implode(',', $columns);
247 $all_values = [];
248 $data_count = count($duplicate) - 1;
249 foreach ($duplicate as $dupKey => $dupColName) {
250 if (in_array($dupColName, $columns)) {
251 $dupCol .= $dupColName;
252 } else {
253 $dupCol .= $this->getFieldFormat($dupColName);
254 $all_values[] = $dupColName;
255 }
256 if ($dupKey < $data_count) {
257 $dupCol .= ',';
258 }
259 }
260 $condition_to_check = null;
261 $update_condition = (!\is_null($condition) &&
262 array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null;
263 $formatted_conditions = $this->getFormatedCondition($update_condition);
264 if ($formatted_conditions) {
265 $condition_to_check = $formatted_conditions['conditions'];
266 $all_values = array_merge($all_values, $formatted_conditions['values']);
267 }
268 $query = "INSERT INTO $this->table_name ($insCol)
269 SELECT $dupCol FROM $this->table_name $condition_to_check";
270 $this->execute($query, $all_values);
271 return $this->getResult();
272 }
273
274 public function trash(array $condition = null) {
275 if (
276 !\is_null($condition)
277 && \is_array($condition)
278 && array_keys($condition) !== range(0, count($condition) - 1)
279 ) {
280 $delete_condition = $condition;
281 } else {
282 return new WP_Error(
283 'deletion_error',
284 __('At least 1 condition needed', 'bit-form')
285 );
286 }
287 $update_condition = (!\is_null($condition) &&
288 array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null;
289 $result = $this->app_db->update(
290 $this->table_name,
291 $data_to_update,
292 $update_condition
293 );
294 return $this->getResult($result);
295 }
296
297 public function delete(array $condition = null) {
298 if (
299 !\is_null($condition)
300 && \is_array($condition)
301 && array_keys($condition) !== range(0, count($condition) - 1)
302 ) {
303 $delete_condition = $condition;
304 } else {
305 return new WP_Error(
306 'deletion_error',
307 __('At least 1 condition needed', 'bit-form')
308 );
309 }
310 $result = $this->app_db->delete(
311 $this->table_name,
312 $delete_condition
313 );
314 return $this->getResult($result);
315 }
316
317 public function bulkDelete(array $condition = null) {
318 if (
319 !\is_null($condition)
320 && \is_array($condition)
321 && array_keys($condition) !== range(0, count($condition) - 1)
322 ) {
323 $delete_condition = $condition;
324 } else {
325 return new WP_Error(
326 'deletion_error',
327 __('At least 1 condition needed', 'bit-form')
328 );
329 }
330 // $formatted_conditions = $this->getFormatedCondition($delete_condition, $check_operator);
331 $formatted_conditions = $this->getFormatedCondition($delete_condition);
332 if ($formatted_conditions) {
333 $condition_to_check = $formatted_conditions['conditions'];
334 $all_values = $formatted_conditions['values'];
335 } else {
336 $condition_to_check = null;
337 return new WP_Error(
338 'deletion_error',
339 __('At least 1 condition needed', 'bit-form')
340 );
341 }
342 $result = $this->app_db->query(
343 $this->app_db->prepare(
344 "DELETE FROM $this->table_name $condition_to_check",
345 $all_values
346 )
347 );
348 return $this->getResult($result);
349 }
350
351 protected function getFieldFormat($value) {
352 return ('integer' === gettype($value)) ?
353 '%d' : (('double' === gettype($value)) ? '%f' : '%s');
354 }
355
356 protected function getFormatedCondition($condition, $check_operator = null, $join_operator = ' AND ') {
357 if (\is_null($condition)) {
358 return false;
359 }
360 $no_condition = count($condition);
361 $index_checker = 0;
362 $condition_to_check = ' WHERE ';
363 $all_values = [];
364 foreach ($condition as $key => $value) {
365 $value_type = '';
366 if (is_array($value)) {
367 if (isset($value['operator'])) {
368 $set_check_operator = $value['operator'];
369 $value_type .= $this->getFieldFormat($value['value']);
370 $all_values[] = $value['value'];
371 } else {
372 $set_check_operator = \is_null($check_operator) ? 'in' : $check_operator;
373 $value_type .= ' ( ';
374 $value_index_checker = 0;
375 $value_count = count($value) - 1;
376 foreach ($value as $condKey => $condValue) {
377 $value_type .= $this->getFieldFormat($condValue);
378 $all_values[] = $condValue;
379 if ($value_index_checker < $value_count) {
380 $value_type .= ', ';
381 }
382 $value_index_checker = $value_index_checker + 1;
383 }
384 $value_type .= ' )';
385 }
386 } else {
387 $set_check_operator = \is_null($check_operator) ? '=' : $check_operator;
388 $value_type .= $this->getFieldFormat($value);
389 $all_values[] = $value;
390 }
391 $condition_to_check = $condition_to_check . $key . " $set_check_operator " . $value_type;
392 if ($index_checker < $no_condition - 1) {
393 $condition_to_check = $condition_to_check . " $join_operator ";
394 }
395 $index_checker = $index_checker + 1;
396 }
397 return [
398 'conditions' => $condition_to_check,
399 'values' => $all_values
400 ];
401 }
402
403 protected function checkCondition(array $condition) {
404 if (!is_null($condition) && array_keys($condition) === range(0, count($condition) - 1)) {
405 return new WP_Error(
406 'get_condition',
407 'Require ASSOC_ARRAY but found N_ARRAY'
408 );
409 }
410 return true;
411 }
412
413 protected function execute($sql, $values = null) {
414 if (is_null($values)) {
415 $preparedQuery = $sql;
416 } else {
417 $preparedQuery = $this->app_db->prepare($sql, $values);
418 }
419 // echo " Q S " . $preparedQuery . " Q EE";
420 if (empty($preparedQuery)) {
421 $this->db_response = new WP_Error('null_query', __('prepared query is empty', 'bit-form'));
422 } else {
423 $this->db_response = false !== stripos($preparedQuery, 'DELETE') ? $this->app_db->query($preparedQuery)
424 : $this->app_db->get_results($preparedQuery, OBJECT_K);
425 }
426 // print_r($this->app_db->last_query);
427 return $this;
428 }
429
430 protected function getResult($db_response = null) {
431 $db_response = !empty($this->db_response) ? $this->db_response : $db_response;
432 if (!empty($this->app_db->last_error)) {
433 return new WP_Error('db_error', $this->app_db->last_error);
434 }
435 if (!$db_response) {
436 if ($this->app_db->num_rows > 0) {
437 $response = $this->app_db->num_rows;
438 }
439 if (is_wp_error($db_response)) {
440 $response = $db_response;
441 }
442 $response = new WP_Error('result_empty', __('Result is empty', 'bit-form'));
443 } elseif (is_array($this->app_db->last_result) && !empty($this->app_db->last_result)) {
444 $response = $this->app_db->last_result;
445 } elseif ($this->app_db->insert_id) {
446 $response = $this->app_db->insert_id;
447 } else {
448 $response = $db_response;
449 }
450 $this->app_db->flush();
451 return $response;
452 }
453 }
454