| @@ -1,11 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Provides Report Model Class |
| 4 | 4 | */ |
| 5 | + | |
| 5 | 6 | namespace BitCode\BitForm\Core\Database; |
| 6 | 7 | |
| 7 | -use BitCode\BitForm\Core\Database\Model; | |
| 8 | +use WP_Error; | |
| 8 | 9 | |
| 9 | 10 | /** |
| 10 | 11 | * |
| 11 | 12 | */ |
| @@ -10,50 +11,84 @@ | ||
| 10 | 11 | * |
| 11 | 12 | */ |
| 12 | 13 | class ReportsModel extends Model |
| 13 | 14 | { |
| 14 | - protected static $table = 'bitforms_reports'; | |
| 15 | + protected static $table = 'bitforms_reports'; | |
| 15 | 16 | |
| 16 | - public function validateReportFields($reportData, $fieldNames) | |
| 17 | - { | |
| 18 | - $reportDetails = []; | |
| 19 | - switch ($reportData->type) { | |
| 20 | - case 'table': | |
| 21 | - $tableAction = "table_ac"; | |
| 22 | - $reportDetails = !is_string($reportData->details)? $reportData->details : json_decode($reportData->details); | |
| 23 | - if (!empty($reportDetails->order)) { | |
| 24 | - $initialOrder = $fieldNames; | |
| 25 | - $columnOrder = $reportDetails->order; | |
| 26 | - foreach ($reportDetails->order as $key => $value) { | |
| 27 | - if (isset($initialOrder[$value])) { | |
| 28 | - unset($initialOrder[$value]); | |
| 29 | - } elseif (!isset($initialOrder[$value])) { | |
| 30 | - unset($columnOrder[$key]); | |
| 31 | - } | |
| 32 | - } | |
| 33 | - $columnOrder = array_values($columnOrder); | |
| 34 | - if (!empty($initialOrder)) { | |
| 35 | - foreach ($initialOrder as $name => $label) { | |
| 36 | - $columnOrder[] = $name; | |
| 37 | - } | |
| 38 | - } | |
| 39 | - $columnOrder[] = $tableAction; | |
| 40 | - array_unshift($columnOrder, 'selection', 'sl'); | |
| 41 | - $reportDetails->order = $columnOrder; | |
| 17 | + public function validateReportFields($reportData, $fieldNames) | |
| 18 | + { | |
| 19 | + $reportDetails = []; | |
| 20 | + switch ($reportData->type) { | |
| 21 | + case 'table': | |
| 22 | + $tableAction = 'table_ac'; | |
| 23 | + $reportDetails = !is_string($reportData->details) ? $reportData->details : json_decode($reportData->details); | |
| 24 | + if (!empty($reportDetails->order)) { | |
| 25 | + $initialOrder = $fieldNames; | |
| 26 | + $columnOrder = $reportDetails->order; | |
| 27 | + foreach ($reportDetails->order as $key => $value) { | |
| 28 | + if (isset($initialOrder[$value])) { | |
| 29 | + unset($initialOrder[$value]); | |
| 30 | + } elseif (!isset($initialOrder[$value])) { | |
| 31 | + unset($columnOrder[$key]); | |
| 42 | 32 | } |
| 43 | - if (!empty($reportDetails->hiddenColumns)) { | |
| 44 | - $hiddenColumns = $reportDetails->hiddenColumns; | |
| 45 | - foreach ($reportDetails->hiddenColumns as $key => $value) { | |
| 46 | - if (!isset($fieldNames[$value]) && ($value !== 'sl' && $value !== 'selection' && $value !== 'table_ac')) { | |
| 47 | - unset($hiddenColumns[$key]); | |
| 48 | - } | |
| 49 | - } | |
| 50 | - $reportDetails->hiddenColumns = array_values($hiddenColumns); | |
| 33 | + } | |
| 34 | + $columnOrder = array_values($columnOrder); | |
| 35 | + if (!empty($initialOrder)) { | |
| 36 | + foreach ($initialOrder as $name => $label) { | |
| 37 | + $columnOrder[] = $name; | |
| 51 | 38 | } |
| 52 | - break; | |
| 53 | - | |
| 54 | - default: | |
| 55 | - break; | |
| 39 | + } | |
| 40 | + $columnOrder[] = $tableAction; | |
| 41 | + array_unshift($columnOrder, 'selection', 'sl'); | |
| 42 | + $reportDetails->order = $columnOrder; | |
| 56 | 43 | } |
| 57 | - return $reportDetails; | |
| 44 | + if (!empty($reportDetails->hiddenColumns)) { | |
| 45 | + $hiddenColumns = $reportDetails->hiddenColumns; | |
| 46 | + foreach ($reportDetails->hiddenColumns as $key => $value) { | |
| 47 | + if (!isset($fieldNames[$value]) && ('sl' !== $value && 'selection' !== $value && 'table_ac' !== $value)) { | |
| 48 | + unset($hiddenColumns[$key]); | |
| 49 | + } | |
| 50 | + } | |
| 51 | + $reportDetails->hiddenColumns = array_values($hiddenColumns); | |
| 52 | + } | |
| 53 | + break; | |
| 54 | + | |
| 55 | + default: | |
| 56 | + break; | |
| 58 | 57 | } |
| 58 | + return $reportDetails; | |
| 59 | + } | |
| 60 | + | |
| 61 | + public function bulkDelete(array $condition = null, $check_operator = null) | |
| 62 | + { | |
| 63 | + if ( | |
| 64 | + !\is_null($condition) | |
| 65 | + && \is_array($condition) | |
| 66 | + && array_keys($condition) !== range(0, count($condition) - 1) | |
| 67 | + ) { | |
| 68 | + $delete_condition = $condition; | |
| 69 | + } else { | |
| 70 | + return new WP_Error( | |
| 71 | + 'deletion_error', | |
| 72 | + __('At least 1 condition needed', 'bit-form') | |
| 73 | + ); | |
| 74 | + } | |
| 75 | + $formatted_conditions = $this->getFormatedCondition($delete_condition, $check_operator); | |
| 76 | + if ($formatted_conditions) { | |
| 77 | + $condition_to_check = $formatted_conditions['conditions']; | |
| 78 | + $all_values = $formatted_conditions['values']; | |
| 79 | + } else { | |
| 80 | + $condition_to_check = null; | |
| 81 | + return new WP_Error( | |
| 82 | + 'deletion_error', | |
| 83 | + __('At least 1 condition needed', 'bit-form') | |
| 84 | + ); | |
| 85 | + } | |
| 86 | + $result = $this->app_db->query( | |
| 87 | + $this->app_db->prepare( | |
| 88 | + "DELETE FROM $this->table_name $condition_to_check", | |
| 89 | + $all_values | |
| 90 | + ) | |
| 91 | + ); | |
| 92 | + return $this->getResult($result); | |
| 93 | + } | |
| 59 | 94 | } |