PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.5.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.5.2
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 1.5.2, at includes/Core/Database/Model.php

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