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
← All changes | back/dynamics/subscriber/SubscriberAutomationConditions.php +169 -0 10.2.0 → 11.1.0 View file →
@@ -1,0 +1,169 @@
1 +<?php
2 +
3 +use AcyMailing\Classes\UserClass;
4 +use AcyMailing\Classes\FieldClass;
5 +use AcyMailing\Helpers\AutomationHelper;
6 +use AcyMailing\Types\OperatorType;
7 +
8 +trait SubscriberAutomationConditions
9 +{
10 + protected function initAutomationConditions(): void
11 + {
12 + $this->automationConditions['user']['acy_field'] = acym_translation('ACYM_ACYMAILING_FIELD');
13 + }
14 +
15 + public function displayConditionOptions_acy_field(string $fieldName): void
16 + {
17 + $userClass = new UserClass();
18 + $fields = $userClass->getAllColumnsUserAndCustomField();
19 + unset($fields['automation']);
20 +
21 + $operator = new OperatorType();
22 + ?>
23 + <div class="intext_select_automation cell">
24 + <?php acym_select(
25 + $fields,
26 + $fieldName.'[field]',
27 + null,
28 + ['class' => 'acym__select acym__automation__conditions__fields__dropdown'],
29 + 'value',
30 + 'text',
31 + null,
32 + false,
33 + true
34 + ); ?>
35 + </div>
36 + <div class="intext_select_automation cell">
37 + <?php $operator->display($fieldName.'[operator]', '', 'acym__automation__conditions__operator__dropdown', true); ?>
38 + </div>
39 + <input class="acym__automation__one-field intext_input_automation cell acym__automation__condition__regular-field"
40 + type="text"
41 + name="<?php echo esc_attr($fieldName.'[value]'); ?>">
42 + <?php
43 + $this->displayCustomFieldValues($fieldName);
44 + }
45 +
46 + /**
47 + * One hidden value field per custom field, shown by the JS when that field is selected
48 + */
49 + private function displayCustomFieldValues(string $fieldName): void
50 + {
51 + // Declared without the prefix so they are not posted, the JS adds it back on the shown one
52 + $valueFieldName = $this->getFieldNameWithoutPrefix($fieldName).'[value]';
53 +
54 + $fieldClass = new FieldClass();
55 + foreach ($fieldClass->getAllFieldsForUser() as $field) {
56 + if (in_array($field->type, ['single_dropdown', 'radio', 'checkbox', 'multiple_dropdown']) && !empty($field->value)) {
57 + $values = [];
58 + $field->value = json_decode($field->value, true);
59 + foreach ($field->value as $value) {
60 + $valueTmp = new stdClass();
61 + $valueTmp->text = $value['title'];
62 + $valueTmp->value = $value['value'];
63 + if ($value['disabled'] == 'y') $valueTmp->disable = true;
64 + $values[$value['value']] = $valueTmp;
65 + }
66 + ?>
67 + <div class="acym__automation__one-field intext_select_automation cell" style="display: none">
68 + <?php acym_select(
69 + $values,
70 + $valueFieldName,
71 + null,
72 + [
73 + 'class' => 'acym__select acym__automation__conditions__fields__select',
74 + 'data-condition-field' => intval($field->id),
75 + ],
76 + 'value',
77 + 'text',
78 + null,
79 + false,
80 + true
81 + ); ?>
82 + </div>
83 + <?php
84 + } elseif ('date' === $field->type) {
85 + acym_tooltip(
86 + [
87 + 'hoveredText' => '<input class="acym__automation__one-field acym__automation__conditions__fields__select intext_input_automation cell"
88 + type="text"
89 + name="'.esc_attr($valueFieldName).'"
90 + style="display: none"
91 + data-condition-field="'.intval($field->id).'">',
92 + 'textShownInTooltip' => acym_translation('ACYM_DATE_FORMAT_FILTER'),
93 + 'classContainer' => 'intext_select_automation cell',
94 + ]
95 + );
96 + }
97 + }
98 + }
99 +
100 + public function onAcymProcessCondition_acy_field(&$query, &$options, $num, &$conditionNotValid)
101 + {
102 + $affectedRows = $this->processAcyField($query, $options, $num);
103 + if (empty($affectedRows)) $conditionNotValid++;
104 + }
105 +
106 + public function onAcymDeclareSummary_conditions(&$automation)
107 + {
108 + $this->onAcymDeclareSummary_conditionsFilters($automation, 'ACYM_CONDITION_ACY_FIELD_SUMMARY');
109 + }
110 +
111 + private function processAcyField(&$query, &$options, $num)
112 + {
113 + $usersColumns = acym_getColumns('user');
114 +
115 + if (!in_array($options['field'], $usersColumns)) {
116 + $fieldClass = new FieldClass();
117 + $field = $fieldClass->getOneById($options['field']);
118 +
119 + $fieldType = empty($field->type) ? '' : $field->type;
120 +
121 + $query->leftjoin['userfield'.$num] = ' #__acym_user_has_field as userfield'.$num.' ON userfield'.$num.'.user_id = user.id AND userfield'.$num.'.field_id = '.intval(
122 + $options['field']
123 + );
124 + $query->where[] = $query->convertQuery(
125 + 'userfield'.$num,
126 + 'value',
127 + $options['operator'],
128 + $options['value'],
129 + 'phone' === $fieldType ? AutomationHelper::TYPE_PHONE : ''
130 + );
131 + } else {
132 + if (in_array($options['field'], ['creation_date', 'confirmation_date', 'last_sent_date', 'last_open_date', 'last_click_date', 'deactivation_date'])) {
133 + $options['value'] = acym_replaceDate($options['value']);
134 + if (!is_numeric($options['value'])) {
135 + $options['value'] = strtotime($options['value']);
136 + }
137 + $options['value'] = acym_date($options['value'], 'Y-m-d H:i:s');
138 + }
139 + $query->where[] = $query->convertQuery('user', $options['field'], $options['operator'], $options['value']);
140 + }
141 +
142 + return $query->count();
143 + }
144 +
145 + private function onAcymDeclareSummary_conditionsFilters(&$automation, $key)
146 + {
147 + if (!empty($automation['acy_field'])) {
148 + $usersColumns = acym_getColumns('user');
149 +
150 + if (!in_array($automation['acy_field']['field'], $usersColumns)) {
151 + $fieldClass = new FieldClass();
152 + $field = $fieldClass->getOneById($automation['acy_field']['field']);
153 + $automation['acy_field']['field'] = $field->name;
154 + }
155 +
156 + $automation = acym_translationSprintf(
157 + $key,
158 + $automation['acy_field']['field'],
159 + $automation['acy_field']['operator'],
160 + $automation['acy_field']['value']
161 + );
162 + } elseif (!empty($automation['random'])) {
163 + $automation = acym_translationSprintf(
164 + 'ACYM_RANDOMLY_SELECT_X_SUBSCRIBERS',
165 + $automation['random']['number']
166 + );
167 + }
168 + }
169 +}