PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.10.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.10.2
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Controllers / Segments / Edition.php

Edition.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.10.2, at back/Controllers/Segments/Edition.php

218 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Controllers\Segments;
4
5 use AcyMailing\Classes\SegmentClass;
6 use AcyMailing\Controllers\AutomationController;
7 use AcyMailing\Helpers\AutomationHelper;
8
9 trait Edition
10 {
11 public function edit(): void
12 {
13 if (!acym_level(ACYM_ENTERPRISE)) {
14 acym_redirect(acym_completeLink('dashboard&task=upgrade&version=enterprise', false, true));
15 }
16
17 }
18
19 public function apply(): void
20 {
21 $segmentId = $this->store();
22 if (empty($segmentId)) {
23 acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_SAVE_SEGMENT'), 'error');
24 $this->listing();
25 } else {
26 acym_enqueueMessage(acym_translation('ACYM_SEGMENT_WELL_SAVE'));
27 acym_setVar('segmentId', $segmentId);
28 $this->edit();
29 }
30 }
31
32 public function save(): void
33 {
34 $segmentId = $this->store();
35 acym_enqueueMessage(acym_translation(empty($segmentId) ? 'ACYM_COULD_NOT_SAVE_SEGMENT' : 'ACYM_SEGMENT_WELL_SAVE'), empty($segmentId) ? 'error' : 'success');
36 $this->listing();
37 }
38
39 private function store(): int
40 {
41 $segmentId = acym_getVar('int', 'segmentId');
42 $filters = acym_getVar('array', 'acym_action');
43 $segmentRequest = acym_getVar('array', 'segment');
44
45 $segmentClass = new SegmentClass();
46
47 if (empty($segmentId)) {
48 $segment = new \stdClass();
49 $segment->creation_date = acym_date('now', 'Y-m-d H:i:s');
50 } else {
51 $segment = $segmentClass->getOneById($segmentId);
52 if (empty($segment)) {
53 return 0;
54 }
55 }
56
57 $segment->name = $segmentRequest['name'];
58 $segment->active = $segmentRequest['active'];
59 $segment->filters = json_encode($filters['filters'], JSON_FORCE_OBJECT);
60
61 $segmentId = $segmentClass->save($segment);
62
63 return empty($segmentId) ? 0 : $segmentId;
64 }
65
66 public function countResultsTotal(): void
67 {
68 $segmentSelected = acym_getVar('int', 'segment_selected');
69 if (empty($segmentSelected)) {
70 $stepAutomation = acym_getVar('array', 'acym_action', []);
71 } else {
72 $segmentClass = new SegmentClass();
73 $segment = $segmentClass->getOneById($segmentSelected);
74 if (empty($segment)) {
75 $stepAutomation = [];
76 } else {
77 $stepAutomation = ['filters' => $segment->filters];
78 }
79 }
80
81 $listsIds = acym_getVar('string', 'list_selected', '[]');
82 $listsIds = empty($listsIds) ? [] : json_decode($listsIds, true);
83
84 $isExclude = acym_getVar('boolean', 'exclude', false);
85 acym_sendAjaxResponse($this->countSegmentByParams($stepAutomation, $listsIds, $isExclude));
86 }
87
88 public function countSegmentByParams(array $segment, array $listsIds, bool $exclude = false): int
89 {
90 $automationHelpers = [];
91
92 if (!empty($segment) && !empty($segment['filters'])) {
93 foreach ($segment['filters'] as $or => $orValues) {
94 if (empty($orValues)) continue;
95
96 $automationHelpers[$or] = new AutomationHelper();
97 foreach ($orValues as $and => $andValues) {
98 $and = intval($and);
99 foreach ($andValues as $filterName => $options) {
100 acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelpers[$or], &$options, $and.'_'.$or]);
101 }
102 }
103
104 $automationHelpers[$or]->excludeSelected = $exclude;
105 $automationHelpers[$or]->addFlag(self::FLAG_COUNT);
106 }
107 }
108
109 if (!empty($listsIds)) {
110 acym_arrayToInteger($listsIds);
111
112 $join = ' #__acym_user_has_list AS user_list ON user_list.user_id = user.id ';
113 $join .= 'AND user_list.list_id IN ('.implode(',', $listsIds).') ';
114 $join .= 'AND user_list.status = 1 ';
115 $join .= 'AND user.active = 1 ';
116 if ($this->config->get('require_confirmation', 1) == 1) {
117 $join .= 'AND user.confirmed = 1 ';
118 }
119 }
120
121 if (empty($automationHelpers)) {
122 $automationHelperBase = new AutomationHelper();
123 if (!empty($listsIds)) {
124 $automationHelperBase->join['user_list'] = $join;
125 }
126
127 return acym_loadResult($automationHelperBase->getQuery(['COUNT(DISTINCT user.id)']));
128 } else {
129 $automationHelperBase = array_pop($automationHelpers);
130 if (!empty($listsIds)) {
131 $automationHelperBase->join['user_list'] = $join;
132 }
133 $numberOfRecipients = acym_loadResult($automationHelperBase->getQuery(['COUNT(DISTINCT user.id)']));
134 $automationHelperBase->removeFlag(self::FLAG_COUNT);
135
136 return $numberOfRecipients;
137 }
138 }
139
140 public function countResults(): void
141 {
142 $and = acym_getVar('int', 'and');
143 $or = acym_getVar('int', 'or');
144 $stepAutomation = acym_getVar('array', 'acym_action');
145
146 $listsIds = acym_getVar('string', 'list_selected', '');
147
148 if (empty($stepAutomation['filters'][$or][$and])) {
149 acym_sendAjaxResponse(acym_translation('ACYM_AUTOMATION_NOT_FOUND'), [], false);
150 }
151
152 $automationHelper = new AutomationHelper();
153
154 if (!empty($listsIds)) {
155 $listsIds = json_decode($listsIds);
156 acym_arrayToInteger($listsIds);
157 $join = $this->config->get('require_confirmation', 1) == 1 ? ' AND user.confirmed = 1' : '';
158 $automationHelper->join['user_list'] = ' #__acym_user_has_list AS user_list ON user_list.user_id = user.id AND user_list.list_id IN ('.implode(
159 ',',
160 $listsIds
161 ).') and user_list.status = 1 '.$join;
162 }
163
164 $filterName = key($stepAutomation['filters'][$or][$and]);
165 $options = current($stepAutomation['filters'][$or][$and]);
166 $messages = acym_trigger('onAcymProcessFilterCount_'.$filterName, [&$automationHelper, &$options, &$and]);
167 $messages = $messages[0];
168
169 if (!empty($listsIds)) $messages .= acym_info(['textShownInTooltip' => 'ACYM_CONDITION_WITH_LISTS_COUNT', 'classText' => 'wysid_tooltip']);
170
171 acym_sendAjaxResponse($messages);
172 }
173
174 public function usersSummary(): void
175 {
176 $offset = acym_getVar('int', 'offset', 0);
177 $limit = acym_getVar('int', 'limit', 50);
178 $search = acym_getVar('string', 'modal_search', '');
179
180 $and = acym_getVar('int', 'and');
181 $or = acym_getVar('int', 'or');
182 $stepAutomation = acym_getVar('array', 'acym_action');
183
184 if (empty($stepAutomation['filters'][$or][$and])) {
185 acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_RETRIEVE_DATA'), [], false);
186 }
187
188 $automationHelper = new AutomationHelper();
189
190 $listsIds = acym_getVar('string', 'list_selected', '');
191
192 if (!empty($listsIds)) {
193 $listsIds = json_decode($listsIds);
194 acym_arrayToInteger($listsIds);
195 $join = $this->config->get('require_confirmation', 1) == 1 ? ' AND user.confirmed = 1' : '';
196 $automationHelper->join['user_list'] = ' #__acym_user_has_list AS user_list ON user_list.user_id = user.id AND user_list.list_id IN ('.implode(
197 ',',
198 $listsIds
199 ).') and user_list.status = 1 '.$join;
200 }
201
202 $filterName = key($stepAutomation['filters'][$or][$and]);
203 $options = current($stepAutomation['filters'][$or][$and]);
204 acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelper, &$options, &$and]);
205
206 if (!empty($search)) {
207 $search = acym_escapeDB('%'.$search.'%');
208 $automationHelper->where[] = ' (user.email LIKE '.$search.' OR user.name LIKE '.$search.' OR user.id LIKE '.$search.') ';
209 }
210
211 $automationHelper->limit = intval($offset).', '.intval($limit);
212
213 $users = acym_loadObjectList($automationHelper->getQuery(['user.email', 'user.name', 'user.id']), 'id');
214
215 acym_sendAjaxResponse('', ['users' => $users]);
216 }
217 }
218