| 1 |
<?php |
| 2 |
/** |
| 3 |
* Provides Report Model Class |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace BitCode\BitForm\Core\Database; |
| 7 |
|
| 8 |
use WP_Error; |
| 9 |
|
| 10 |
/** |
| 11 |
* |
| 12 |
*/ |
| 13 |
class ReportsModel extends Model |
| 14 |
{ |
| 15 |
protected static $table = 'bitforms_reports'; |
| 16 |
|
| 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]); |
| 32 |
} |
| 33 |
} |
| 34 |
$columnOrder = array_values($columnOrder); |
| 35 |
if (!empty($initialOrder)) { |
| 36 |
foreach ($initialOrder as $name => $label) { |
| 37 |
$columnOrder[] = $name; |
| 38 |
} |
| 39 |
} |
| 40 |
$columnOrder[] = $tableAction; |
| 41 |
array_unshift($columnOrder, 'selection', 'sl'); |
| 42 |
$reportDetails->order = $columnOrder; |
| 43 |
} |
| 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; |
| 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 |
} |
| 94 |
} |
| 95 |
|