| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Provides Report Model Class |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace BitCode\BitForm\Core\Database; |
| 8 |
|
| 9 |
use WP_Error; |
| 10 |
|
| 11 |
/** |
| 12 |
* |
| 13 |
*/ |
| 14 |
class ReportsModel extends Model |
| 15 |
{ |
| 16 |
protected static $table = 'bitforms_reports'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Whether a report payload carries enough to validate. |
| 20 |
* |
| 21 |
* The save path feeds this the request's `currentReport`, and the React `$reportSelector` atom |
| 22 |
* yields `{}` whenever the report list has not resolved — an empty stdClass, which PHP's |
| 23 |
* empty() reports as non-empty. Callers must therefore ask before acting on it. |
| 24 |
* |
| 25 |
* @param mixed $reportData |
| 26 |
* |
| 27 |
* @return bool |
| 28 |
*/ |
| 29 |
public static function isValidatableReport($reportData) |
| 30 |
{ |
| 31 |
return \is_object($reportData) && !empty($reportData->type) && isset($reportData->details); |
| 32 |
} |
| 33 |
|
| 34 |
public function validateReportFields($reportData, $fieldNames) |
| 35 |
{ |
| 36 |
$reportDetails = []; |
| 37 |
if (!self::isValidatableReport($reportData)) { |
| 38 |
return $reportDetails; |
| 39 |
} |
| 40 |
switch ($reportData->type) { |
| 41 |
case 'table': |
| 42 |
$tableAction = 'table_ac'; |
| 43 |
$reportDetails = !is_string($reportData->details) ? $reportData->details : json_decode($reportData->details); |
| 44 |
if (!empty($reportDetails->order)) { |
| 45 |
$initialOrder = $fieldNames; |
| 46 |
$columnOrder = $reportDetails->order; |
| 47 |
foreach ($reportDetails->order as $key => $value) { |
| 48 |
if (isset($initialOrder[$value])) { |
| 49 |
unset($initialOrder[$value]); |
| 50 |
} elseif (!isset($initialOrder[$value])) { |
| 51 |
unset($columnOrder[$key]); |
| 52 |
} |
| 53 |
} |
| 54 |
$columnOrder = array_values($columnOrder); |
| 55 |
if (!empty($initialOrder)) { |
| 56 |
foreach ($initialOrder as $name => $label) { |
| 57 |
$columnOrder[] = $name; |
| 58 |
} |
| 59 |
} |
| 60 |
$columnOrder[] = $tableAction; |
| 61 |
array_unshift($columnOrder, 'selection', 'sl'); |
| 62 |
$reportDetails->order = $columnOrder; |
| 63 |
} |
| 64 |
if (!empty($reportDetails->hiddenColumns)) { |
| 65 |
$hiddenColumns = $reportDetails->hiddenColumns; |
| 66 |
foreach ($reportDetails->hiddenColumns as $key => $value) { |
| 67 |
if (!isset($fieldNames[$value]) && ('sl' !== $value && 'selection' !== $value && 'table_ac' !== $value)) { |
| 68 |
unset($hiddenColumns[$key]); |
| 69 |
} |
| 70 |
} |
| 71 |
$reportDetails->hiddenColumns = array_values($hiddenColumns); |
| 72 |
} |
| 73 |
break; |
| 74 |
|
| 75 |
default: |
| 76 |
break; |
| 77 |
} |
| 78 |
return $reportDetails; |
| 79 |
} |
| 80 |
|
| 81 |
public function bulkDelete(array $condition = null, $check_operator = null) |
| 82 |
{ |
| 83 |
if ( |
| 84 |
!\is_null($condition) |
| 85 |
&& \is_array($condition) |
| 86 |
&& array_keys($condition) !== range(0, count($condition) - 1) |
| 87 |
) { |
| 88 |
$delete_condition = $condition; |
| 89 |
} else { |
| 90 |
return new WP_Error( |
| 91 |
'deletion_error', |
| 92 |
__('At least 1 condition needed', 'bit-form') |
| 93 |
); |
| 94 |
} |
| 95 |
$formatted_conditions = $this->getFormatedCondition($delete_condition, $check_operator); |
| 96 |
if ($formatted_conditions) { |
| 97 |
$condition_to_check = $formatted_conditions['conditions']; |
| 98 |
$all_values = $formatted_conditions['values']; |
| 99 |
} else { |
| 100 |
$condition_to_check = null; |
| 101 |
return new WP_Error( |
| 102 |
'deletion_error', |
| 103 |
__('At least 1 condition needed', 'bit-form') |
| 104 |
); |
| 105 |
} |
| 106 |
$result = $this->app_db->query( |
| 107 |
$this->app_db->prepare( |
| 108 |
"DELETE FROM `{$this->table_name}` $condition_to_check", |
| 109 |
$all_values |
| 110 |
) |
| 111 |
); |
| 112 |
return $this->getResult($result); |
| 113 |
} |
| 114 |
} |
| 115 |
|