PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.1.0
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.1.0
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 / dynamics / user / UserAutomationConditions.php

UserAutomationConditions.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 11.1.0, at back/dynamics/user/UserAutomationConditions.php

332 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use AcyMailing\Helpers\AutomationHelper;
4 use AcyMailing\Types\OperatorInType;
5 use AcyMailing\Types\OperatorType;
6
7 trait UserAutomationConditions
8 {
9 protected function initAutomationConditions(): void
10 {
11 $this->automationConditions = [
12 'user' => [
13 'acy_group' => acym_translation('ACYM_GROUP'),
14 'acy_cmsfield' => acym_translation('ACYM_ACCOUNT_USER_FIELD'),
15 ],
16 'classic' => [
17 'acy_totaluser' => acym_translation('ACYM_NUMBER_OF_SUBSCRIBERS'),
18 ],
19 'both' => [
20 'acy_toss' => acym_translation('ACYM_TOSS'),
21 ],
22 ];
23 }
24
25 public function displayConditionOptions_acy_group(string $fieldName): void
26 {
27 $allGroups = acym_getGroups();
28 $groups = ['' => acym_translation('ACYM_NO_GROUP')];
29 foreach ($allGroups as $group) {
30 $groups[$group->id] = $group->text;
31 }
32 $operatorIn = new OperatorInType();
33 ?>
34 <div class="intext_select_automation cell">
35 <?php $operatorIn->display($fieldName.'[in]', '', 'acym__select', true); ?>
36 </div>
37 <div class="intext_select_automation cell">
38 <?php acym_select(
39 $groups,
40 $fieldName.'[group]',
41 null,
42 ['class' => 'acym__select'],
43 'value',
44 'text',
45 null,
46 false,
47 true
48 ); ?>
49 </div>
50 <?php if (ACYM_CMS === 'joomla') { ?>
51 <div class="cell grid-x medium-3">
52 <?php acym_switch([
53 'name' => $fieldName.'[subgroup]',
54 'value' => 1,
55 'label' => acym_translation('ACYM_INCLUDE_SUB_GROUPS'),
56 // One switch per row of the form, the JS replaces the placeholder to keep the ids unique
57 'idPrefix' => '__numand__',
58 ]); ?>
59 </div>
60 <?php }
61 }
62
63 private function getCmsFieldsForConditions(): array
64 {
65 $cmsFields = [];
66 foreach (acym_getColumns('users', false) as $key => $column) {
67 $cmsFields[$column] = $column;
68 }
69
70 // Handle custom fields
71 if (ACYM_CMS == 'joomla' && ACYM_J37) {
72 $query = 'SELECT id, title
73 FROM #__fields
74 WHERE context = "com_users.user"
75 AND state = 1
76 AND type IN ("calendar", "checkboxes", "color", "integer", "list", "radio", "sql", "text", "url")
77 ORDER BY title ASC';
78 $customFields = acym_loadObjectList($query);
79 foreach ($customFields as $oneCF) {
80 $cmsFields['cf_'.$oneCF->id] = $oneCF->title;
81 }
82 }
83 $excluded = ['password', 'params', 'activation', 'lastResetTime', 'resetCount', 'optKey', 'otep', 'requireReset', 'user_pass', 'user_activation_key'];
84 foreach ($excluded as $oneExcluded) {
85 unset($cmsFields[$oneExcluded]);
86 }
87
88 return $cmsFields;
89 }
90
91 public function displayConditionOptions_acy_cmsfield(string $fieldName): void
92 {
93 $operator = new OperatorType();
94 ?>
95 <div class="intext_select_automation cell">
96 <?php acym_select(
97 $this->getCmsFieldsForConditions(),
98 $fieldName.'[field]',
99 null,
100 ['class' => 'acym__select'],
101 'value',
102 'text',
103 null,
104 false,
105 true
106 ); ?>
107 </div>
108 <div class="intext_select_automation cell">
109 <?php $operator->display($fieldName.'[operator]', '', 'acym__select', true); ?>
110 </div>
111 <input class="intext_input_automation cell" type="text" name="<?php echo esc_attr($fieldName.'[value]'); ?>">
112 <?php
113 }
114
115 public function displayConditionOptions_acy_totaluser(string $fieldName): void
116 {
117 ?>
118 <div class="cell shrink acym__automation__inner__text"><?php echo esc_html(acym_translation('ACYM_THERE_IS')); ?></div>
119 <div class="intext_select_automation cell">
120 <?php acym_select(
121 [
122 '=' => acym_translation('ACYM_EXACTLY'),
123 '>' => acym_translation('ACYM_MORE_THAN'),
124 '<' => acym_translation('ACYM_LESS_THAN'),
125 ],
126 $fieldName.'[operator]',
127 null,
128 ['class' => 'intext_select_automation acym__select'],
129 'value',
130 'text',
131 null,
132 false,
133 true
134 ); ?>
135 </div>
136 <input type="number" min="0" class="intext_input_automation cell" name="<?php echo esc_attr($fieldName.'[number]'); ?>">
137 <div class="cell shrink acym__automation__inner__text"><?php echo esc_html(acym_translation('ACYM_ACYMAILING_USERS')); ?></div>
138 <?php
139 }
140
141 public function displayConditionOptions_acy_toss(string $fieldName): void
142 {
143 ?>
144 <input type="hidden" name="<?php echo esc_attr($fieldName.'[toss]'); ?>" value="true">
145 <div class="acym__automation__inner__text"><?php echo esc_html(acym_translation('ACYM_TOSS_DESC')); ?></div>
146 <?php
147 }
148
149 public function onAcymProcessCondition_acy_toss(&$query, $option, $num, &$conditionNotValid)
150 {
151 if (!acym_rand(0, 1)) $conditionNotValid++;
152 }
153
154 public function onAcymProcessCondition_acy_totaluser(&$query, $option, $num, &$conditionNotValid)
155 {
156 $numberUsers = $query->count();
157 $res = false;
158
159 switch ($option['operator']) {
160 case '=':
161 $res = $numberUsers == $option['number'];
162 break;
163 case '>':
164 $res = $numberUsers > $option['number'];
165 break;
166 case '<':
167 $res = $numberUsers < $option['number'];
168 break;
169 }
170
171 if (!$res) $conditionNotValid++;
172 }
173
174 public function onAcymProcessCondition_acy_group(&$query, $options, $num, &$conditionNotValid)
175 {
176 $affectedRows = $this->processAcyGroup($query, $options, $num);
177 if (empty($affectedRows)) $conditionNotValid++;
178 }
179
180 private function processAcyGroup(&$query, $options, $num)
181 {
182 if (empty($options['group'])) {
183 if (ACYM_CMS === 'joomla') {
184 $operator = (empty($options['in']) || $options['in'] === 'in') ? 'IS NULL' : 'IS NOT NULL AND cmsuser'.$num.'.user_id != 0';
185 $query->leftjoin['cmsuser'.$num] = '#__user_usergroup_map AS cmsuser'.$num.'
186 ON cmsuser'.$num.'.user_id = user.cms_id ';
187 } else {
188 $operator = (empty($options['in']) || $options['in'] === 'in') ? 'IS NOT NULL AND cmsuser'.$num.'.user_id != 0' : 'IS NULL AND user.cms_id != 0';
189 $query->leftjoin['cmsuser'.$num] = '#__usermeta AS cmsuser'.$num.'
190 ON cmsuser'.$num.'.user_id = user.cms_id
191 AND cmsuser'.$num.'.meta_key = "#__capabilities"
192 AND cmsuser'.$num.'.meta_value = "a:0:{}"';
193 }
194 } else {
195 $operator = (empty($options['in']) || $options['in'] === 'in') ? 'IS NOT NULL AND cmsuser'.$num.'.user_id != 0' : 'IS NULL';
196
197 if (ACYM_CMS === 'joomla') {
198 if (empty($options['subgroup'])) {
199 $value = ' = '.intval($options['group']);
200 } else {
201 $lftrgt = acym_loadObject('SELECT lft, rgt FROM #__usergroups WHERE id = '.intval($options['group']));
202 $allSubGroups = acym_loadResultArray('SELECT id FROM #__usergroups WHERE lft > '.intval($lftrgt->lft).' AND rgt < '.intval($lftrgt->rgt));
203 array_unshift($allSubGroups, intval($options['group']));
204 $value = ' IN ('.implode(', ', $allSubGroups).')';
205 }
206
207 $query->leftjoin['cmsuser'.$num] = '#__user_usergroup_map AS cmsuser'.$num.'
208 ON cmsuser'.$num.'.user_id = user.cms_id
209 AND cmsuser'.$num.'.group_id'.$value;
210 } else {
211 $query->leftjoin['cmsuser'.$num] = '#__usermeta AS cmsuser'.$num.'
212 ON cmsuser'.$num.'.user_id = user.cms_id
213 AND cmsuser'.$num.'.meta_key = "#__capabilities"
214 AND cmsuser'.$num.'.meta_value LIKE '.acym_escapeDB('%'.strlen($options['group']).':"'.acym_secureDBColumn($options['group']).'"%');
215 }
216 }
217
218 $query->where[] = 'cmsuser'.$num.'.user_id '.$operator;
219
220 return $query->count();
221 }
222
223 public function onAcymProcessCondition_acy_cmsfield(&$query, $options, $num, &$conditionNotValid)
224 {
225 $affectedRows = $this->processAcyCMSField($query, $options, $num);
226 if (empty($affectedRows)) $conditionNotValid++;
227 }
228
229 private function processAcyCMSField(&$query, $options, $num)
230 {
231 if (empty($options['field'])) {
232 return;
233 }
234
235 // Handle custom fields
236 if (strpos($options['field'], 'cf_') !== false) {
237 $cfId = substr($options['field'], 3);
238 $query->leftjoin['cmsuserfields'.$num] = '#__fields_values AS cmsuserfields'.$num.' ON cmsuserfields'.$num.'.item_id = user.cms_id AND cmsuserfields'.$num.'.field_id = '.intval(
239 $cfId
240 );
241 $query->where[] = $query->convertQuery('cmsuserfields'.$num, 'value', $options['operator'], $options['value']);
242 } else {
243 // Handle normal fields
244 $type = '';
245 $query->leftjoin['cmsuser'.$num] = '#__users AS cmsuser'.$num.' ON cmsuser'.$num.'.id = user.cms_id';
246
247 if (in_array($options['field'], ['registerDate', 'lastvisitDate', 'user_registered'])) {
248 $type = AutomationHelper::TYPE_DATETIME;
249 $options['value'] = acym_replaceDate($options['value']);
250
251 if (!is_numeric($options['value']) && strtotime($options['value']) !== false) {
252 $options['value'] = strtotime($options['value']);
253 }
254 if (is_numeric($options['value'])) {
255 $options['value'] = gmdate('Y-m-d H:i:s', $options['value']);
256 }
257 }
258
259 $query->where[] = $query->convertQuery(
260 'cmsuser'.$num,
261 $options['field'],
262 $options['operator'],
263 $options['value'],
264 $type
265 );
266 }
267
268 return $query->count();
269 }
270
271 public function onAcymDeclareSummary_conditions(&$automation)
272 {
273 $this->summaryGroup($automation);
274
275 if (!empty($automation['acy_cmsfield'])) {
276 $automation = acym_translationSprintf(
277 'ACYM_CONDITION_ACY_CMS_FIELD_SUMMARY',
278 $automation['acy_cmsfield']['field'],
279 $automation['acy_cmsfield']['operator'],
280 $automation['acy_cmsfield']['value']
281 );
282 }
283
284 if (!empty($automation['acy_totaluser'])) {
285 $operators = ['=' => acym_translation('ACYM_EXACTLY'), '>' => acym_translation('ACYM_MORE_THAN'), '<' => acym_translation('ACYM_LESS_THAN')];
286 $automation = acym_translation('ACYM_THERE_IS').' '.acym_strtolower(
287 $operators[$automation['acy_totaluser']['operator']]
288 ).' '.$automation['acy_totaluser']['number'].' '.acym_translation('ACYM_ACYMAILING_USERS').' ';
289 }
290
291 if (!empty($automation['acy_toss'])) {
292 $automation = acym_translation('ACYM_TOSS_DESC');
293 }
294 }
295
296 private function summaryGroup(&$automation)
297 {
298 if (empty($automation['acy_group'])) return;
299
300 if ('joomla' === ACYM_CMS) {
301 $allGroups = acym_getGroups();
302 $groups = [];
303 foreach ($allGroups as $group) {
304 if ($automation['acy_group']['group'] == $group->id) {
305 $automation['acy_group']['group'] = $group->text;
306 }
307 $groups[$group->id] = $group->text;
308 }
309 } else {
310 $groupKey = 'ACYM_'.strtoupper($automation['acy_group']['group']);
311 if (acym_translationExists($groupKey)) {
312 $automation['acy_group']['group'] = $groupKey;
313 }
314 }
315
316 if (empty($automation['acy_group']['group'])) {
317 $automation['acy_group']['group'] = acym_translation('ACYM_NO_GROUP');
318 }
319
320 $finalText = acym_translationSprintf(
321 'ACYM_FILTER_ACY_GROUP_SUMMARY',
322 acym_translation($automation['acy_group']['in'] == 'in' ? 'ACYM_IN' : 'ACYM_NOT_IN'),
323 acym_translation($automation['acy_group']['group'])
324 );
325 if ('joomla' === ACYM_CMS) {
326 $finalText .= $automation['acy_group']['subgroup'] == 1 ? ' '.acym_translation('ACYM_FILTER_ACY_GROUP_SUBGROUP_SUMMARY') : '';
327 }
328
329 $automation = $finalText;
330 }
331 }
332