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