PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.0.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.0.1
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.0.1, at back/Controllers/Segments/Edition.php

214 lines 7.9 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()
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()
20 {
21 $segmentId = $this->store();
22 if (!$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'), 'success');
27 acym_setVar('segmentId', $segmentId);
28 $this->edit();
29 }
30 }
31
32 public function save()
33 {
34 $segmentId = $this->store();
35 acym_enqueueMessage(acym_translation($segmentId ? 'ACYM_SEGMENT_WELL_SAVE' : 'ACYM_COULD_NOT_SAVE_SEGMENT'), $segmentId ? 'success' : 'error');
36 $this->listing();
37 }
38
39 private function store()
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)) return false;
53 }
54
55 $segment->name = $segmentRequest['name'];
56 $segment->active = $segmentRequest['active'];
57 $segment->filters = json_encode($filters['filters'], JSON_FORCE_OBJECT);
58
59 return $segmentClass->save($segment);
60 }
61
62 public function countResultsTotal()
63 {
64 $segmentSelected = acym_getVar('int', 'segment_selected');
65 if (empty($segmentSelected)) {
66 $stepAutomation = acym_getVar('array', 'acym_action');
67 } else {
68 $segmentClass = new SegmentClass();
69 $segment = $segmentClass->getOneById($segmentSelected);
70 if (empty($segment)) {
71 $stepAutomation = [];
72 } else {
73 $stepAutomation = ['filters' => $segment->filters];
74 }
75 }
76
77 $listsIds = acym_getVar('string', 'list_selected', '');
78 if (!empty($listsIds)) {
79 $listsIds = json_decode($listsIds);
80 }
81
82 $isExclude = acym_getVar('boolean', 'exclude', false);
83 acym_sendAjaxResponse($this->countSegmentByParams($stepAutomation, $listsIds, $isExclude));
84 }
85
86 public function countSegmentByParams($segment, $listsIds, $exclude = false)
87 {
88 $automationHelpers = [];
89
90 if (!empty($segment) && !empty($segment['filters'])) {
91 foreach ($segment['filters'] as $or => $orValues) {
92 if (empty($orValues)) continue;
93
94 $automationHelpers[$or] = new AutomationHelper();
95 foreach ($orValues as $and => $andValues) {
96 $and = intval($and);
97 foreach ($andValues as $filterName => $options) {
98 acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelpers[$or], &$options, $and.'_'.$or]);
99 }
100 }
101
102 $automationHelpers[$or]->excludeSelected = $exclude;
103 $automationHelpers[$or]->addFlag(self::FLAG_COUNT);
104 }
105 }
106
107 if (!empty($listsIds)) {
108 acym_arrayToInteger($listsIds);
109
110 $join = ' #__acym_user_has_list AS user_list ON user_list.user_id = user.id ';
111 $join .= 'AND user_list.list_id IN ('.implode(',', $listsIds).') ';
112 $join .= 'AND user_list.status = 1 ';
113 $join .= 'AND user.active = 1 ';
114 if ($this->config->get('require_confirmation', 1) == 1) {
115 $join .= 'AND user.confirmed = 1 ';
116 }
117 }
118
119 if (empty($automationHelpers)) {
120 $automationHelperBase = new AutomationHelper();
121 if (!empty($listsIds)) {
122 $automationHelperBase->join['user_list'] = $join;
123 }
124
125 return acym_loadResult($automationHelperBase->getQuery(['COUNT(DISTINCT user.id)']));
126 } else {
127 $automationHelperBase = array_pop($automationHelpers);
128 if (!empty($listsIds)) {
129 $automationHelperBase->join['user_list'] = $join;
130 }
131 $numberOfRecipients = acym_loadResult($automationHelperBase->getQuery(['COUNT(DISTINCT user.id)']));
132 $automationHelperBase->removeFlag(self::FLAG_COUNT);
133
134 return $numberOfRecipients;
135 }
136 }
137
138 public function countResults()
139 {
140 $and = acym_getVar('int', 'and');
141 $or = acym_getVar('int', 'or');
142 $stepAutomation = acym_getVar('array', 'acym_action');
143
144 $listsIds = acym_getVar('string', 'list_selected', '');
145
146 if (empty($stepAutomation['filters'][$or][$and])) {
147 acym_sendAjaxResponse(acym_translation('ACYM_AUTOMATION_NOT_FOUND'), [], false);
148 }
149
150 $automationHelper = new AutomationHelper();
151
152 if (!empty($listsIds)) {
153 $listsIds = json_decode($listsIds);
154 acym_arrayToInteger($listsIds);
155 $join = $this->config->get('require_confirmation', 1) == 1 ? ' AND user.confirmed = 1' : '';
156 $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(
157 ',',
158 $listsIds
159 ).') and user_list.status = 1 '.$join;
160 }
161
162 $filterName = key($stepAutomation['filters'][$or][$and]);
163 $options = current($stepAutomation['filters'][$or][$and]);
164 $messages = acym_trigger('onAcymProcessFilterCount_'.$filterName, [&$automationHelper, &$options, &$and]);
165 $messages = $messages[0];
166
167 if (!empty($listsIds)) $messages .= acym_info('ACYM_CONDITION_WITH_LISTS_COUNT', '', '', 'wysid_tooltip');
168
169 acym_sendAjaxResponse($messages);
170 }
171
172 public function usersSummary()
173 {
174 $offset = acym_getVar('int', 'offset', 0);
175 $limit = acym_getVar('int', 'limit', 50);
176 $search = acym_getVar('string', 'modal_search', '');
177
178 $and = acym_getVar('int', 'and');
179 $or = acym_getVar('int', 'or');
180 $stepAutomation = acym_getVar('array', 'acym_action');
181
182 if (empty($stepAutomation['filters'][$or][$and])) acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_RETRIEVE_DATA'), [], false);
183
184 $automationHelper = new AutomationHelper();
185
186 $listsIds = acym_getVar('string', 'list_selected', '');
187
188 if (!empty($listsIds)) {
189 $listsIds = json_decode($listsIds);
190 acym_arrayToInteger($listsIds);
191 $join = $this->config->get('require_confirmation', 1) == 1 ? ' AND user.confirmed = 1' : '';
192 $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(
193 ',',
194 $listsIds
195 ).') and user_list.status = 1 '.$join;
196 }
197
198 $filterName = key($stepAutomation['filters'][$or][$and]);
199 $options = current($stepAutomation['filters'][$or][$and]);
200 acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelper, &$options, &$and]);
201
202 if (!empty($search)) {
203 $search = acym_escapeDB('%'.$search.'%');
204 $automationHelper->where[] = ' (user.email LIKE '.$search.' OR user.name LIKE '.$search.' OR user.id LIKE '.$search.') ';
205 }
206
207 $automationHelper->limit = intval($offset).', '.intval($limit);
208
209 $users = acym_loadObjectList($automationHelper->getQuery(['user.email', 'user.name', 'user.id']), 'id');
210
211 acym_sendAjaxResponse('', ['users' => $users]);
212 }
213 }
214