| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Controllers\Automations; |
| 4 |
|
| 5 |
use AcyMailing\Classes\ActionClass; |
| 6 |
use AcyMailing\Classes\AutomationClass; |
| 7 |
use AcyMailing\Classes\ConditionClass; |
| 8 |
use AcyMailing\Classes\StepClass; |
| 9 |
use AcyMailing\Helpers\AutomationHelper; |
| 10 |
use AcyMailing\Helpers\WorkflowHelper; |
| 11 |
|
| 12 |
trait Filter |
| 13 |
{ |
| 14 |
public function filter(): void |
| 15 |
{ |
| 16 |
|
| 17 |
if (!acym_level(ACYM_ENTERPRISE)) { |
| 18 |
acym_redirect(acym_completeLink('dashboard&task=upgrade&version=enterprise', false, true)); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
private function setSaveFilters(bool $isMassAction = false): array |
| 23 |
{ |
| 24 |
$automationID = acym_getVar('int', 'id'); |
| 25 |
$actionId = acym_getVar('int', 'actionId'); |
| 26 |
$action = acym_getVar('array', 'acym_action', []); |
| 27 |
$actionClass = new ActionClass(); |
| 28 |
$conditionId = acym_getVar('int', 'conditionId'); |
| 29 |
|
| 30 |
$stepAutomationId = acym_getVar('int', 'stepAutomationId'); |
| 31 |
|
| 32 |
if (acym_conditionsContainRestrictedFilter($action) && !acym_hasAdminPermissions()) { |
| 33 |
acym_enqueueMessage(acym_translation('ACYM_ACCESS_DENIED'), 'error'); |
| 34 |
|
| 35 |
return [ |
| 36 |
'automationId' => $automationID, |
| 37 |
'stepId' => $stepAutomationId, |
| 38 |
'actionId' => $actionId, |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
if (!empty($stepAutomationId)) { |
| 43 |
$stepAutomation['id'] = $stepAutomationId; |
| 44 |
} |
| 45 |
|
| 46 |
if (!empty($conditionId)) { |
| 47 |
$action['condition_id'] = $conditionId; |
| 48 |
} |
| 49 |
|
| 50 |
if (!empty($actionId)) { |
| 51 |
$action['id'] = $actionId; |
| 52 |
} |
| 53 |
|
| 54 |
$action['filters']['type_filter'] = acym_getVar('string', 'type_filter'); |
| 55 |
|
| 56 |
if ($isMassAction) { |
| 57 |
$massAction = acym_getVar('array', 'massAction', [], 'SESSION'); |
| 58 |
$massAction['filters'] = $action['filters']; |
| 59 |
acym_setSession('massAction', $massAction); |
| 60 |
|
| 61 |
return []; |
| 62 |
} |
| 63 |
|
| 64 |
$action['filters'] = json_encode($action['filters']); |
| 65 |
|
| 66 |
$action['order'] = 1; |
| 67 |
|
| 68 |
foreach ($action as $column => $value) { |
| 69 |
acym_secureDBColumn($column); |
| 70 |
} |
| 71 |
|
| 72 |
//We need an object to save it so we make a object |
| 73 |
$action = (object)$action; |
| 74 |
|
| 75 |
$action->id = $actionClass->save($action); |
| 76 |
|
| 77 |
return [ |
| 78 |
'automationId' => $automationID, |
| 79 |
'stepId' => $stepAutomationId, |
| 80 |
'actionId' => $action->id, |
| 81 |
]; |
| 82 |
} |
| 83 |
|
| 84 |
public function saveExitFilters(): void |
| 85 |
{ |
| 86 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 87 |
|
| 88 |
$this->setSaveFilters(); |
| 89 |
|
| 90 |
acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED'), 'success'); |
| 91 |
|
| 92 |
$this->listing(); |
| 93 |
} |
| 94 |
|
| 95 |
public function saveFilters(): void |
| 96 |
{ |
| 97 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 98 |
|
| 99 |
$ids = $this->setSaveFilters(); |
| 100 |
|
| 101 |
acym_setVar('id', $ids['automationId']); |
| 102 |
acym_setVar('stepId', $ids['stepId']); |
| 103 |
acym_setVar('actionId', $ids['actionId']); |
| 104 |
$this->summary(); |
| 105 |
} |
| 106 |
|
| 107 |
public function countresults(): void |
| 108 |
{ |
| 109 |
$or = acym_getVar('int', 'or'); |
| 110 |
$and = acym_getVar('int', 'and'); |
| 111 |
$stepAutomation = acym_getVar('array', 'acym_action'); |
| 112 |
acym_blockRestrictedFilterAjax($stepAutomation); |
| 113 |
|
| 114 |
if (empty($stepAutomation['filters'][$or][$and])) { |
| 115 |
acym_sendAjaxResponse(acym_translation('ACYM_AUTOMATION_NOT_FOUND'), [], false); |
| 116 |
} |
| 117 |
|
| 118 |
$query = new AutomationHelper(); |
| 119 |
|
| 120 |
$filterName = key($stepAutomation['filters'][$or][$and]); |
| 121 |
$options = current($stepAutomation['filters'][$or][$and]); |
| 122 |
$messages = acym_trigger('onAcymProcessFilterCount_'.$filterName, [&$query, &$options, &$and]); |
| 123 |
|
| 124 |
acym_sendAjaxResponse(implode(' | ', $messages)); |
| 125 |
} |
| 126 |
|
| 127 |
public function countResultsOrTotal(): void |
| 128 |
{ |
| 129 |
$or = acym_getVar('int', 'or'); |
| 130 |
$stepAutomation = acym_getVar('array', 'acym_action'); |
| 131 |
acym_blockRestrictedFilterAjax($stepAutomation); |
| 132 |
|
| 133 |
$query = new AutomationHelper(); |
| 134 |
|
| 135 |
if (!empty($stepAutomation) && !empty($stepAutomation['filters'][$or])) { |
| 136 |
foreach ($stepAutomation['filters'][$or] as $and => $andValues) { |
| 137 |
$and = intval($and); |
| 138 |
foreach ($andValues as $filterName => $options) { |
| 139 |
$options['countTotal'] = true; |
| 140 |
acym_trigger('onAcymProcessFilter_'.$filterName, [&$query, &$options, &$and]); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$result = $query->count(); |
| 146 |
|
| 147 |
acym_sendAjaxResponse(acym_translationSprintf('ACYM_SELECTED_USERS_TOTAL', $result)); |
| 148 |
} |
| 149 |
} |
| 150 |
|