| 1 |
<?php |
| 2 |
|
| 3 |
use AcyMailing\Classes\ListClass; |
| 4 |
|
| 5 |
trait SubscriptionAutomationConditions |
| 6 |
{ |
| 7 |
public function onAcymDeclareConditions(array &$conditions): void |
| 8 |
{ |
| 9 |
$listClass = new ListClass(); |
| 10 |
$allLists = $listClass->getAllForSelect(false); |
| 11 |
$listsWithAny = ['' => acym_translation('ACYM_ANY_LIST')] + $allLists; |
| 12 |
$list = [ |
| 13 |
'type' => [ |
| 14 |
'sub' => acym_translation('ACYM_SUBSCRIBED'), |
| 15 |
'unsub' => acym_translation('ACYM_UNSUBSCRIBED'), |
| 16 |
'notsub' => acym_translation('ACYM_NO_SUBSCRIPTION_STATUS'), |
| 17 |
'notcurrentsub' => acym_translation('ACYM_NOT_CURRENTLY_SUBSCRIBED'), |
| 18 |
], |
| 19 |
'lists' => $listsWithAny, |
| 20 |
'date' => [ |
| 21 |
'subscription_date' => acym_translation('ACYM_SUBSCRIPTION_DATE'), |
| 22 |
'unsubscribe_date' => acym_translation('ACYM_UNSUBSCRIPTION_DATE'), |
| 23 |
], |
| 24 |
]; |
| 25 |
|
| 26 |
$conditions['user']['acy_list'] = new stdClass(); |
| 27 |
$conditions['user']['acy_list']->name = acym_translation('ACYM_ACYMAILING_LIST'); |
| 28 |
|
| 29 |
$conditions['user']['acy_list']->option = '<div class="intext_select_automation cell">'; |
| 30 |
$conditions['user']['acy_list']->option .= acym_select( |
| 31 |
$list['type'], |
| 32 |
'acym_condition[conditions][__numor__][__numand__][acy_list][action]', |
| 33 |
null, |
| 34 |
['class' => 'intext_select_automation acym__select'] |
| 35 |
); |
| 36 |
$conditions['user']['acy_list']->option .= '</div>'; |
| 37 |
$conditions['user']['acy_list']->option .= '<div class="intext_select_automation cell">'; |
| 38 |
$conditions['user']['acy_list']->option .= acym_select( |
| 39 |
$list['lists'], |
| 40 |
'acym_condition[conditions][__numor__][__numand__][acy_list][list]', |
| 41 |
null, |
| 42 |
['class' => 'intext_select_automation acym__select'] |
| 43 |
); |
| 44 |
$conditions['user']['acy_list']->option .= '</div><br>'; |
| 45 |
$conditions['user']['acy_list']->option .= '<div class="cell grid-x grid-margin-x">'; |
| 46 |
$conditions['user']['acy_list']->option .= acym_dateField('acym_condition[conditions][__numor__][__numand__][acy_list][date-min]'); |
| 47 |
$conditions['user']['acy_list']->option .= '<span class="acym__title acym__title__secondary acym_vcenter margin-bottom-0 margin-left-1 margin-right-1"><</span>'; |
| 48 |
$conditions['user']['acy_list']->option .= '<div class="intext_select_automation">'; |
| 49 |
$conditions['user']['acy_list']->option .= acym_select( |
| 50 |
$list['date'], |
| 51 |
'acym_condition[conditions][__numor__][__numand__][acy_list][date-type]', |
| 52 |
null, |
| 53 |
['class' => 'intext_select_automation acym__select cell'] |
| 54 |
); |
| 55 |
$conditions['user']['acy_list']->option .= '</div>'; |
| 56 |
$conditions['user']['acy_list']->option .= '<span class="acym__title acym__title__secondary acym_vcenter margin-bottom-0 margin-left-1 margin-right-1"><</span>'; |
| 57 |
$conditions['user']['acy_list']->option .= acym_dateField('acym_condition[conditions][__numor__][__numand__][acy_list][date-max]'); |
| 58 |
$conditions['user']['acy_list']->option .= '</div>'; |
| 59 |
|
| 60 |
$conditions['classic']['acy_list_all'] = new stdClass(); |
| 61 |
$conditions['classic']['acy_list_all']->name = acym_translation('ACYM_NUMBER_USERS_LIST'); |
| 62 |
$conditions['classic']['acy_list_all']->option = '<div class="cell shrink acym__automation__inner__text">'.acym_translation('ACYM_THERE_IS').'</div>'; |
| 63 |
$conditions['classic']['acy_list_all']->option .= '<div class="intext_select_automation cell">'; |
| 64 |
$conditions['classic']['acy_list_all']->option .= acym_select( |
| 65 |
['>' => acym_translation('ACYM_MORE_THAN'), '<' => acym_translation('ACYM_LESS_THAN'), '=' => acym_translation('ACYM_EXACTLY')], |
| 66 |
'acym_condition[conditions][__numor__][__numand__][acy_list_all][operator]', |
| 67 |
null, |
| 68 |
['class' => 'intext_select_automation acym__select'] |
| 69 |
); |
| 70 |
$conditions['classic']['acy_list_all']->option .= '</div>'; |
| 71 |
$conditions['classic']['acy_list_all']->option .= '<input type="number" min="0" class="intext_input_automation cell" name="acym_condition[conditions][__numor__][__numand__][acy_list_all][number]">'; |
| 72 |
$conditions['classic']['acy_list_all']->option .= '<div class="cell shrink acym__automation__inner__text">'.acym_translation('ACYM_ACYMAILING_USERS').'</div>'; |
| 73 |
$listTypeAll = [ |
| 74 |
'sub' => acym_translation('ACYM_SUBSCRIBED'), |
| 75 |
'unsub' => acym_translation('ACYM_UNSUBSCRIBED'), |
| 76 |
'notsub' => acym_translation('ACYM_NO_SUBSCRIPTION_STATUS'), |
| 77 |
]; |
| 78 |
$conditions['classic']['acy_list_all']->option .= '<div class="cell grid-x grid-margin-x margin-left-0 margin-right-0"><div class="intext_select_automation cell">'; |
| 79 |
$conditions['classic']['acy_list_all']->option .= acym_select( |
| 80 |
$listTypeAll, |
| 81 |
'acym_condition[conditions][__numor__][__numand__][acy_list_all][action]', |
| 82 |
null, |
| 83 |
['class' => 'intext_select_automation acym__select'] |
| 84 |
); |
| 85 |
$conditions['classic']['acy_list_all']->option .= '</div>'; |
| 86 |
$conditions['classic']['acy_list_all']->option .= '<div class="intext_select_automation cell">'; |
| 87 |
$conditions['classic']['acy_list_all']->option .= acym_select( |
| 88 |
$list['lists'], |
| 89 |
'acym_condition[conditions][__numor__][__numand__][acy_list_all][list]', |
| 90 |
null, |
| 91 |
['class' => 'intext_select_automation acym__select'] |
| 92 |
); |
| 93 |
$conditions['classic']['acy_list_all']->option .= '</div></div>'; |
| 94 |
$conditions['classic']['acy_list_all']->option .= '<br><div class="cell grid-x grid-margin-x">'; |
| 95 |
$conditions['classic']['acy_list_all']->option .= acym_dateField('acym_condition[conditions][__numor__][__numand__][acy_list_all][date-min]'); |
| 96 |
$conditions['classic']['acy_list_all']->option .= '<span class="acym__title acym__title__secondary acym_vcenter margin-bottom-0 margin-left-1 margin-right-1"><</span>'; |
| 97 |
$conditions['classic']['acy_list_all']->option .= '<div class="intext_select_automation">'; |
| 98 |
$conditions['classic']['acy_list_all']->option .= acym_select( |
| 99 |
$list['date'], |
| 100 |
'acym_condition[conditions][__numor__][__numand__][acy_list_all][date-type]', |
| 101 |
null, |
| 102 |
['class' => 'intext_select_automation acym__select cell'] |
| 103 |
); |
| 104 |
$conditions['classic']['acy_list_all']->option .= '</div>'; |
| 105 |
$conditions['classic']['acy_list_all']->option .= '<span class="acym__title acym__title__secondary acym_vcenter margin-bottom-0 margin-left-1 margin-right-1"><</span>'; |
| 106 |
$conditions['classic']['acy_list_all']->option .= acym_dateField('acym_condition[conditions][__numor__][__numand__][acy_list_all][date-max]'); |
| 107 |
} |
| 108 |
|
| 109 |
public function onAcymDeclareConditionsScenario(array &$conditions): void |
| 110 |
{ |
| 111 |
$this->onAcymDeclareConditions($conditions); |
| 112 |
} |
| 113 |
|
| 114 |
private function processConditionAcyLists(&$query, &$options, $num) |
| 115 |
{ |
| 116 |
$isAnyList = empty($options['list']); |
| 117 |
|
| 118 |
$otherConditions = ''; |
| 119 |
if (!empty($options['date-min'])) { |
| 120 |
$options['date-min'] = acym_replaceDate($options['date-min']); |
| 121 |
if (!is_numeric($options['date-min'])) { |
| 122 |
$options['date-min'] = strtotime($options['date-min']); |
| 123 |
} |
| 124 |
if (!empty($options['date-min'])) { |
| 125 |
$otherConditions .= ' AND userlist'.$num.'.'.acym_secureDBColumn($options['date-type']).' > '.acym_escapeDB(acym_date($options['date-min'], 'Y-m-d H:i:s', false)); |
| 126 |
} |
| 127 |
} |
| 128 |
if (!empty($options['date-max'])) { |
| 129 |
$options['date-max'] = acym_replaceDate($options['date-max']); |
| 130 |
if (!is_numeric($options['date-max'])) { |
| 131 |
$options['date-max'] = strtotime($options['date-max']); |
| 132 |
} |
| 133 |
if (!empty($options['date-max'])) { |
| 134 |
$otherConditions .= ' AND userlist'.$num.'.'.acym_secureDBColumn($options['date-type']).' < '.acym_escapeDB(acym_date($options['date-max'], 'Y-m-d H:i:s', false)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if ($isAnyList) { |
| 139 |
$standardListType = acym_escapeDB(ListClass::LIST_TYPE_STANDARD); |
| 140 |
|
| 141 |
if (in_array($options['action'], ['notsub', 'notcurrentsub'])) { |
| 142 |
$statusCondition = $options['action'] == 'notcurrentsub' ? 'userlist.status = 1 AND ' : ''; |
| 143 |
$subquery = 'SELECT userlist.user_id FROM #__acym_user_has_list AS userlist |
| 144 |
INNER JOIN #__acym_list AS list ON userlist.list_id = list.id |
| 145 |
WHERE '.$statusCondition.'list.type = '.$standardListType; |
| 146 |
$query->where[] = 'user.id NOT IN ('.$subquery.')'; |
| 147 |
} else { |
| 148 |
$status = $options['action'] === 'sub' ? '1' : '0'; |
| 149 |
$query->join['list'.$num] = '#__acym_user_has_list AS userlist'.$num.' ON user.id = userlist'.$num.'.user_id AND userlist'.$num.'.status = '.$status.$otherConditions; |
| 150 |
$query->join['listtype'.$num] = '#__acym_list AS list'.$num.' ON userlist'.$num.'.list_id = list'.$num.'.id AND list'.$num.'.type = '.$standardListType; |
| 151 |
} |
| 152 |
} else { |
| 153 |
// specific list |
| 154 |
$query->leftjoin['list'.$num] = '#__acym_user_has_list AS userlist'.$num.' ON user.id = userlist'.$num.'.user_id AND userlist'.$num.'.list_id = '.intval( |
| 155 |
$options['list'] |
| 156 |
).$otherConditions; |
| 157 |
if ($options['action'] == 'notsub') { |
| 158 |
$query->where[] = 'userlist'.$num.'.user_id IS NULL'; |
| 159 |
} elseif ($options['action'] === 'notcurrentsub') { |
| 160 |
$query->where[] = '(userlist'.$num.'.user_id IS NULL OR userlist'.$num.'.status = 0)'; |
| 161 |
} else { |
| 162 |
$status = $options['action'] == 'sub' ? '1' : '0'; |
| 163 |
$query->where[] = 'userlist'.$num.'.status = '.intval($status); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return $query->count(); |
| 168 |
} |
| 169 |
|
| 170 |
public function onAcymProcessCondition_acy_list(&$query, &$options, $num, &$conditionNotValid) |
| 171 |
{ |
| 172 |
$affectedRows = $this->processConditionAcyLists($query, $options, $num); |
| 173 |
if (empty($affectedRows)) $conditionNotValid++; |
| 174 |
} |
| 175 |
|
| 176 |
public function onAcymProcessCondition_acy_list_all(&$query, &$options, $num, &$conditionNotValid) |
| 177 |
{ |
| 178 |
$affectedRows = $this->processConditionAcyLists($query, $options, $num); |
| 179 |
|
| 180 |
$res = false; |
| 181 |
switch ($options['operator']) { |
| 182 |
case '=': |
| 183 |
$res = $affectedRows == $options['number']; |
| 184 |
break; |
| 185 |
case '>': |
| 186 |
$res = $affectedRows > $options['number']; |
| 187 |
break; |
| 188 |
case '<': |
| 189 |
$res = $affectedRows < $options['number']; |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
if (!$res) { |
| 194 |
$conditionNotValid++; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
public function onAcymDeclareSummary_conditions(&$automation) |
| 199 |
{ |
| 200 |
if (!empty($automation['acy_list_all'])) { |
| 201 |
$operators = ['=' => acym_translation('ACYM_EXACTLY'), '>' => acym_translation('ACYM_MORE_THAN'), '<' => acym_translation('ACYM_LESS_THAN')]; |
| 202 |
$finalText = acym_translation('ACYM_THERE_IS').' '.acym_strtolower( |
| 203 |
$operators[$automation['acy_list_all']['operator']] |
| 204 |
).' '.$automation['acy_list_all']['number'].' '.acym_translation('ACYM_ACYMAILING_USERS').' '; |
| 205 |
$listClass = new ListClass(); |
| 206 |
$automation['acy_list_all']['list'] = $listClass->getOneById($automation['acy_list_all']['list'] ? : 0); |
| 207 |
if (empty($automation['acy_list_all']['list'])) { |
| 208 |
$automation = '<span class="acym__color__red">'.acym_translation('ACYM_SELECT_A_LIST').'</span>'; |
| 209 |
|
| 210 |
return; |
| 211 |
} |
| 212 |
if ($automation['acy_list_all']['action'] == 'sub') $automation['acy_list_all']['action'] = 'ACYM_SUBSCRIBED'; |
| 213 |
if ($automation['acy_list_all']['action'] == 'unsub') $automation['acy_list_all']['action'] = 'ACYM_UNSUBSCRIBED'; |
| 214 |
if ($automation['acy_list_all']['action'] == 'notsub') $automation['acy_list_all']['action'] = 'ACYM__NOT_SUBSCRIBED'; |
| 215 |
$finalText .= acym_translationSprintf( |
| 216 |
'ACYM_CONDITION_ACY_LIST_SUMMARY', |
| 217 |
acym_translation($automation['acy_list_all']['action']), |
| 218 |
$automation['acy_list_all']['list']->name |
| 219 |
).' '; |
| 220 |
|
| 221 |
$automation = $this->summaryDate($automation['acy_list_all'], $finalText); |
| 222 |
} |
| 223 |
|
| 224 |
$this->onAcymDeclareSummary_conditionsFilters( |
| 225 |
$automation, |
| 226 |
'ACYM_CONDITION_ACY_LIST_SUMMARY', |
| 227 |
'ACYM_IS_SUBSCRIBED', |
| 228 |
'ACYM_IS_UNSUBSCRIBED', |
| 229 |
'ACYM_IS_NOT_SUBSCRIBED', |
| 230 |
'ACYM_IS_NOT_CURRENTLY_SUBSCRIBED' |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
private function onAcymDeclareSummary_conditionsFilters( |
| 235 |
&$automation, |
| 236 |
string $key, |
| 237 |
string $keySub, |
| 238 |
string $keyUnsub, |
| 239 |
string $keyNotSub, |
| 240 |
string $keyNotCurrentSub |
| 241 |
): void { |
| 242 |
if (!empty($automation['acy_list'])) { |
| 243 |
$finalText = ''; |
| 244 |
$listClass = new ListClass(); |
| 245 |
$automation['acy_list']['list'] = $listClass->getOneById($automation['acy_list']['list'] ? : 0); |
| 246 |
|
| 247 |
$listName = empty($automation['acy_list']['list']) |
| 248 |
? acym_translation('ACYM_ANY_LIST') |
| 249 |
: $automation['acy_list']['list']->name; |
| 250 |
|
| 251 |
if ($automation['acy_list']['action'] === 'sub') $automation['acy_list']['action'] = $keySub; |
| 252 |
if ($automation['acy_list']['action'] === 'unsub') $automation['acy_list']['action'] = $keyUnsub; |
| 253 |
if ($automation['acy_list']['action'] === 'notsub') $automation['acy_list']['action'] = $keyNotSub; |
| 254 |
if ($automation['acy_list']['action'] === 'notcurrentsub') $automation['acy_list']['action'] = $keyNotCurrentSub; |
| 255 |
$finalText .= acym_translationSprintf( |
| 256 |
$key, |
| 257 |
acym_translation($automation['acy_list']['action']), |
| 258 |
$listName |
| 259 |
).' '; |
| 260 |
|
| 261 |
$automation = $this->summaryDate($automation['acy_list'], $finalText); |
| 262 |
} |
| 263 |
|
| 264 |
if (!empty($automation['unconfirmed'])) { |
| 265 |
$automation = acym_translation('ACYM_ACTION_UNCONFIRM'); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
private function summaryDate($automation, $finalText) |
| 270 |
{ |
| 271 |
if (!empty($automation['date-min']) || !empty($automation['date-max'])) { |
| 272 |
$finalText .= acym_translationSprintf('ACYM_WHERE_DATE_ACY_LIST_SUMMARY', acym_strtolower(acym_translation('ACYM_'.strtoupper($automation['date-type'])))); |
| 273 |
|
| 274 |
$dates = []; |
| 275 |
if (!empty($automation['date-min'])) { |
| 276 |
$automation['date-min'] = acym_replaceDate($automation['date-min']); |
| 277 |
$dates[] = acym_translationSprintf('ACYM_WHERE_DATE_MIN_ACY_LIST_SUMMARY', acym_date($automation['date-min'], 'd M Y H:i')); |
| 278 |
} |
| 279 |
if (!empty($automation['date-max'])) { |
| 280 |
$automation['date-max'] = acym_replaceDate($automation['date-max']); |
| 281 |
$dates[] = acym_translationSprintf('ACYM_WHERE_DATE_MAX_ACY_LIST_SUMMARY', acym_date($automation['date-max'], 'd M Y H:i')); |
| 282 |
} |
| 283 |
|
| 284 |
$finalText .= ' '.implode(' '.acym_strtolower(acym_translation('ACYM_AND')).' ', $dates); |
| 285 |
} |
| 286 |
|
| 287 |
return $finalText; |
| 288 |
} |
| 289 |
} |
| 290 |
|