| 1 |
<?php |
| 2 |
|
| 3 |
use AcyMailing\Classes\UserClass; |
| 4 |
use AcyMailing\Classes\MailClass; |
| 5 |
use AcyMailing\Classes\FieldClass; |
| 6 |
use AcyMailing\Helpers\MailerHelper; |
| 7 |
|
| 8 |
trait SubscriberAutomationActions |
| 9 |
{ |
| 10 |
public function searchEmails() |
| 11 |
{ |
| 12 |
$id = acym_getVar('int', 'id'); |
| 13 |
if (!empty($id)) { |
| 14 |
if ($id == -1) { |
| 15 |
$name = acym_translation('ACYM_ALL_MAILS'); |
| 16 |
} else { |
| 17 |
$name = acym_loadResult( |
| 18 |
'SELECT name |
| 19 |
FROM #__acym_mail |
| 20 |
WHERE id = '.intval($id) |
| 21 |
); |
| 22 |
$name = empty($name) ? '' : acym_utf8Decode($name); |
| 23 |
} |
| 24 |
echo json_encode(['value' => $name]); |
| 25 |
exit; |
| 26 |
} |
| 27 |
|
| 28 |
$return = []; |
| 29 |
$return[] = [-1, acym_translation('ACYM_ALL_MAILS')]; |
| 30 |
$search = acym_utf8Encode(acym_getVar('string', 'search', '')); |
| 31 |
$elements = acym_loadObjectList( |
| 32 |
'SELECT `id`, `subject`, `name` |
| 33 |
FROM #__acym_mail |
| 34 |
WHERE (`subject` LIKE '.acym_escapeDB('%'.$search.'%').' |
| 35 |
OR `name` LIKE '.acym_escapeDB('%'.$search.'%').') |
| 36 |
AND `type` != '.acym_escapeDB(MailClass::TYPE_NOTIFICATION).' |
| 37 |
ORDER BY `subject` ASC |
| 38 |
LIMIT 20' |
| 39 |
); |
| 40 |
|
| 41 |
foreach ($elements as $oneElement) { |
| 42 |
$return[] = [$oneElement->id, $oneElement->name]; |
| 43 |
} |
| 44 |
|
| 45 |
echo json_encode($return); |
| 46 |
exit; |
| 47 |
} |
| 48 |
|
| 49 |
public function onAcymDeclareActions(&$actions) |
| 50 |
{ |
| 51 |
$userActions = [ |
| 52 |
'confirm' => acym_translation('ACYM_CONFIRM_USER'), |
| 53 |
'unconfirm' => acym_translation('ACYM_UNCONFIRM_USER'), |
| 54 |
'active' => acym_translation('ACYM_ACTIVE_USER'), |
| 55 |
'block' => acym_translation('ACYM_BLOCK_USER'), |
| 56 |
'delete' => acym_translation('ACYM_DELETE_USER'), |
| 57 |
]; |
| 58 |
|
| 59 |
$actions['acy_user'] = new stdClass(); |
| 60 |
$actions['acy_user']->name = acym_translation('ACYM_ACTION_ON_USERS'); |
| 61 |
|
| 62 |
ob_start(); |
| 63 |
include acym_getPartial('actions', 'acy_user'); |
| 64 |
$actions['acy_user']->option = ob_get_clean(); |
| 65 |
|
| 66 |
$userClass = new UserClass(); |
| 67 |
$userFields = $userClass->getAllColumnsUserAndCustomField(true); |
| 68 |
unset($userFields['id']); |
| 69 |
unset($userFields['cms_id']); |
| 70 |
unset($userFields['key']); |
| 71 |
unset($userFields['active']); |
| 72 |
unset($userFields['source']); |
| 73 |
unset($userFields['confirmed']); |
| 74 |
unset($userFields['automation']); |
| 75 |
unset($userFields['creation_date']); |
| 76 |
|
| 77 |
$userOperator = [ |
| 78 |
'=' => '=', |
| 79 |
'-' => '-', |
| 80 |
'+' => '+', |
| 81 |
'add_end' => acym_translation('ACYM_ADD_AT_END'), |
| 82 |
'add_begin' => acym_translation('ACYM_ADD_AT_BEGINNING'), |
| 83 |
]; |
| 84 |
|
| 85 |
$fieldClass = new FieldClass(); |
| 86 |
$customFields = $fieldClass->getAllFieldsForUser(); |
| 87 |
$customFieldValues = []; |
| 88 |
foreach ($customFields as $field) { |
| 89 |
if (in_array($field->type, ['single_dropdown', 'radio', 'checkbox', 'multiple_dropdown']) && !empty($field->value)) { |
| 90 |
$values = []; |
| 91 |
$field->value = json_decode($field->value, true); |
| 92 |
foreach ($field->value as $value) { |
| 93 |
$valueTmp = new stdClass(); |
| 94 |
$valueTmp->text = $value['title']; |
| 95 |
$valueTmp->value = $value['value']; |
| 96 |
if ($value['disabled'] == 'y') $valueTmp->disable = true; |
| 97 |
$values[$value['value']] = $valueTmp; |
| 98 |
} |
| 99 |
$customFieldValues[$field->id] = '<div class="acym__automation__one-field intext_select_automation cell" style="display: none">'; |
| 100 |
$customFieldValues[$field->id] .= acym_select( |
| 101 |
$values, |
| 102 |
'[actions][__and__][acy_user_value][value]', |
| 103 |
null, |
| 104 |
[ |
| 105 |
'class' => 'acym__select acym__automation__actions__fields__select', |
| 106 |
'data-action-field' => $field->id, |
| 107 |
] |
| 108 |
); |
| 109 |
$customFieldValues[$field->id] .= '</div>'; |
| 110 |
} elseif ('date' == $field->type) { |
| 111 |
$field->option = json_decode($field->option, true); |
| 112 |
$customFieldValues[$field->id] = acym_tooltip( |
| 113 |
[ |
| 114 |
'hoveredText' => '<input class="acym__automation__one-field acym__automation__actions__fields__select intext_input_automation cell" type="text" name="[actions][__and__][acy_user_value][value]" style="display: none" data-action-field="'.$field->id.'">', |
| 115 |
'textShownInTooltip' => acym_translationSprintf('ACYM_DATE_AUTOMATION_INPUT', $field->option['format']), |
| 116 |
'classContainer' => 'intext_select_automation cell', |
| 117 |
] |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
$actions['acy_user_value'] = new stdClass(); |
| 123 |
$actions['acy_user_value']->name = acym_translation('ACYM_SET_USER_VALUE'); |
| 124 |
ob_start(); |
| 125 |
include acym_getPartial('actions', 'acy_user_value'); |
| 126 |
$actions['acy_user_value']->option = ob_get_clean(); |
| 127 |
|
| 128 |
$actions['acy_add_queue'] = new stdClass(); |
| 129 |
$actions['acy_add_queue']->name = acym_translation('ACYM_ADD_EMAIL_QUEUE'); |
| 130 |
ob_start(); |
| 131 |
include acym_getPartial('actions', 'acy_add_queue'); |
| 132 |
$actions['acy_add_queue']->option = ob_get_clean(); |
| 133 |
|
| 134 |
$actions['acy_send_email'] = new stdClass(); |
| 135 |
$actions['acy_send_email']->name = acym_translation('ACYM_SEND_EMAIL'); |
| 136 |
ob_start(); |
| 137 |
include acym_getPartial('actions', 'acy_send_email'); |
| 138 |
$actions['acy_send_email']->option = ob_get_clean(); |
| 139 |
|
| 140 |
$actions['acy_remove_queue'] = new stdClass(); |
| 141 |
$actions['acy_remove_queue']->name = acym_translation('ACYM_REMOVE_EMAIL_QUEUE'); |
| 142 |
$ajaxParams = json_encode( |
| 143 |
[ |
| 144 |
'plugin' => __CLASS__, |
| 145 |
'trigger' => 'searchEmails', |
| 146 |
] |
| 147 |
); |
| 148 |
ob_start(); |
| 149 |
include acym_getPartial('actions', 'acy_remove_queue'); |
| 150 |
$actions['acy_remove_queue']->option = ob_get_clean(); |
| 151 |
|
| 152 |
if ($this->config->get('require_confirmation', '1') === '1') { |
| 153 |
$actions['resend_confirmation'] = new stdClass(); |
| 154 |
$actions['resend_confirmation']->name = acym_translation('ACYM_RESEND_CONFIRMATION'); |
| 155 |
$actions['resend_confirmation']->option = '<input type="hidden" name="acym_action[actions][__and__][resend_confirmation][save]" />'; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
public function onAcymDeclareActionsScenario(&$actions) |
| 160 |
{ |
| 161 |
$this->onAcymDeclareActions($actions); |
| 162 |
|
| 163 |
unset($actions['acy_add_queue']); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
public function onAcymProcessAction_acy_user(&$query, $action) |
| 168 |
{ |
| 169 |
if ($action['action'] == 'delete') { |
| 170 |
$userClass = new UserClass(); |
| 171 |
$usersToDelete = acym_loadResultArray($query->getQuery(['user.id'])); |
| 172 |
if (!empty($usersToDelete)) $userClass->delete($usersToDelete, true); |
| 173 |
} else { |
| 174 |
$fieldToUpdate = ''; |
| 175 |
if ($action['action'] == 'confirm') $fieldToUpdate = 'confirmed = 1'; |
| 176 |
if ($action['action'] == 'unconfirm') $fieldToUpdate = 'confirmed = 0'; |
| 177 |
if ($action['action'] == 'active') $fieldToUpdate = 'active = 1'; |
| 178 |
if ($action['action'] == 'block') $fieldToUpdate = 'active = 0'; |
| 179 |
|
| 180 |
$queryToProcess = 'UPDATE #__acym_user AS `user` SET '.$fieldToUpdate.' WHERE ('.implode(') AND (', $query->where).')'; |
| 181 |
$nbRows = acym_query($queryToProcess); |
| 182 |
|
| 183 |
return acym_translationSprintf('ACYM_X_USERS_X', $nbRows, acym_translation('ACYM_ACTION_'.strtoupper($action['action']))); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
public function onAcymProcessAction_acy_user_value(&$query, $action) |
| 188 |
{ |
| 189 |
$value = $action['value']; |
| 190 |
$value = acym_replaceDateTags($value); |
| 191 |
|
| 192 |
if (empty($action['operator'])) $action['operator'] = '='; |
| 193 |
|
| 194 |
if (in_array($action['operator'], ['+', '-'])) { |
| 195 |
$value = intval($value); |
| 196 |
} else { |
| 197 |
$value = acym_escapeDB($value); |
| 198 |
} |
| 199 |
|
| 200 |
$usersColumns = acym_getColumns('user'); |
| 201 |
|
| 202 |
if (in_array($action['field'], $usersColumns)) { |
| 203 |
$execute = 'UPDATE #__acym_user AS user'; |
| 204 |
|
| 205 |
$column = "user.`".acym_secureDBColumn($action['field'])."`"; |
| 206 |
} else { |
| 207 |
$fieldClass = new FieldClass(); |
| 208 |
$field = $fieldClass->getOneById($action['field']); |
| 209 |
if (empty($field)) return 'Unknown field: '.$action['field']; |
| 210 |
if ('date' == $field->type) $value = acym_escapeDB(json_encode(explode('/', trim($value, '"\'')))); |
| 211 |
|
| 212 |
$allColumn = "`user_id`, `field_id`, `value`"; |
| 213 |
$column = "`value`"; |
| 214 |
} |
| 215 |
|
| 216 |
if ($action['operator'] == '=') { |
| 217 |
$newValue = $value; |
| 218 |
} elseif (in_array($action['operator'], ['+', '-'])) { |
| 219 |
$newValue = $column.' '.$action['operator']." ".$value; |
| 220 |
} elseif ($action['operator'] == 'add_end') { |
| 221 |
$newValue = "CONCAT(".$column.", ".$value.")"; |
| 222 |
} elseif ($action['operator'] == 'add_begin') { |
| 223 |
$newValue = "CONCAT(".$value.", ".$column.")"; |
| 224 |
} else { |
| 225 |
return 'Unknown operator: '.acym_escape($action['operator']); |
| 226 |
} |
| 227 |
|
| 228 |
if (in_array($action['field'], $usersColumns)) { |
| 229 |
$execute .= " SET ".$column." = ".$newValue; |
| 230 |
if (!empty($query->where)) $execute .= ' WHERE ('.implode(') AND (', $query->where).')'; |
| 231 |
} else { |
| 232 |
$customFieldAlreadyExists = acym_loadResult('SELECT COUNT(user_id) FROM #__acym_user_has_field WHERE field_id = '.intval($action['field'])); |
| 233 |
$execute = 'INSERT INTO #__acym_user_has_field ('.$allColumn.') SELECT id AS user_id, '.intval( |
| 234 |
$action['field'] |
| 235 |
).' AS field_id, '.$newValue.' AS value FROM #__acym_user AS user WHERE ('.implode( |
| 236 |
') AND (', |
| 237 |
$query->where |
| 238 |
).') ON DUPLICATE KEY UPDATE '.$column.' = VALUES('.$column.')'; |
| 239 |
} |
| 240 |
|
| 241 |
$nbAffected = acym_query($execute); |
| 242 |
|
| 243 |
if (!empty($customFieldAlreadyExists)) { |
| 244 |
$nbAffected -= $customFieldAlreadyExists; |
| 245 |
} |
| 246 |
|
| 247 |
return acym_translationSprintf('ACYM_UPDATED_USERS', $nbAffected); |
| 248 |
} |
| 249 |
|
| 250 |
public function onAcymProcessAction_acy_send_email(&$query, &$action) |
| 251 |
{ |
| 252 |
if (empty($action['mail_id'])) { |
| 253 |
return acym_translation('ACYM_NO_MAIL_SET'); |
| 254 |
} |
| 255 |
|
| 256 |
$mailerHelper = new MailerHelper(); |
| 257 |
|
| 258 |
$userIds = acym_loadResultArray($query->getQuery(['user.id'])); |
| 259 |
if (empty($userIds)) { |
| 260 |
return acym_translation('ACYM_NO_USER_FOUND'); |
| 261 |
} |
| 262 |
|
| 263 |
$errors = []; |
| 264 |
|
| 265 |
foreach ($userIds as $userId) { |
| 266 |
try { |
| 267 |
$mailSent = $mailerHelper->sendOne($action['mail_id'], $userId); |
| 268 |
|
| 269 |
if (!$mailSent) { |
| 270 |
$errors[] = acym_translationSprintf('ACYM_COULD_NOT_SEND_EMAIL_TO_USER_X', $userId); |
| 271 |
acym_logError('Error while sending email', 'action_acy_send_email'); |
| 272 |
} |
| 273 |
} catch (Exception $e) { |
| 274 |
$errors[] = acym_translationSprintf('ACYM_COULD_NOT_SEND_EMAIL_TO_USER_X_ERROR_X', $userId, $e->getMessage()); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
return empty($errors) ? acym_translation('ACYM_EMAILS_SENT') : implode('<br>', $errors); |
| 279 |
} |
| 280 |
|
| 281 |
public function onAcymProcessAction_acy_add_queue(&$query, &$action, $automationAdmin) |
| 282 |
{ |
| 283 |
if (empty($action['time']) || empty($action['mail_id'])) return ''; |
| 284 |
|
| 285 |
$sendDate = acym_replaceDate($action['time']); |
| 286 |
$sendDate = acym_date($sendDate, 'Y-m-d H:i:s', false); |
| 287 |
$mailClass = new MailClass(); |
| 288 |
|
| 289 |
$mail = $mailClass->getOneById($action['mail_id']); |
| 290 |
|
| 291 |
if (empty($mail)) return ''; |
| 292 |
|
| 293 |
if (MailClass::TYPE_AUTOMATION !== $mail->type) { |
| 294 |
unset($mail->id); |
| 295 |
$mail->type = MailClass::TYPE_AUTOMATION; |
| 296 |
$mail->id = $mailClass->save($mail); |
| 297 |
} |
| 298 |
|
| 299 |
$userIds = acym_loadResultArray($query->getQuery(['user.id'])); |
| 300 |
if (empty($userIds)) { |
| 301 |
$result = 0; |
| 302 |
} else { |
| 303 |
$result = $mailClass->sendAutomation($mail->id, $userIds, $sendDate, $automationAdmin); |
| 304 |
} |
| 305 |
|
| 306 |
if (is_numeric($result)) { |
| 307 |
return acym_translationSprintf('ACYM_EMAILS_ADDED_QUEUE', $result); |
| 308 |
} else { |
| 309 |
return $result; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
public function onAcymProcessAction_acy_remove_queue(&$query, $action) |
| 314 |
{ |
| 315 |
if (empty($action['mail_id'])) return ''; |
| 316 |
|
| 317 |
$mailCondition = ''; |
| 318 |
if ($action['mail_id'] != -1) { |
| 319 |
$mailCondition = ' AND `mail_id` = '.intval($action['mail_id']); |
| 320 |
} |
| 321 |
$nbRows = acym_query('DELETE FROM #__acym_queue WHERE `user_id` IN ('.$query->getQuery(['user.id']).')'.$mailCondition); |
| 322 |
|
| 323 |
return acym_translationSprintf('ACYM_EMAILS_REMOVED_QUEUE', $nbRows); |
| 324 |
} |
| 325 |
|
| 326 |
public function onAcymProcessAction_resend_confirmation(&$query, &$action) |
| 327 |
{ |
| 328 |
$mailClass = new MailClass(); |
| 329 |
$confirmationMail = $mailClass->getOneByName('acy_confirm'); |
| 330 |
$sendDate = acym_date('now', 'Y-m-d H:i:s', false); |
| 331 |
|
| 332 |
$queryInsert = 'INSERT IGNORE INTO #__acym_queue (`mail_id`, `user_id`, `sending_date`, `priority`, `try`) '; |
| 333 |
$queryInsert .= '('.$query->getQuery( |
| 334 |
[ |
| 335 |
intval($confirmationMail->id), |
| 336 |
'user.id', |
| 337 |
acym_escapeDB($sendDate), |
| 338 |
'1', |
| 339 |
'0', |
| 340 |
] |
| 341 |
).')'; |
| 342 |
$nbInserted = acym_query($queryInsert); |
| 343 |
|
| 344 |
if (is_numeric($nbInserted)) { |
| 345 |
return acym_translationSprintf('ACYM_EMAILS_ADDED_QUEUE', $nbInserted); |
| 346 |
} else { |
| 347 |
return $nbInserted; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
public function onAcymDeclareSummary_actions(&$automationAction) |
| 352 |
{ |
| 353 |
if (!empty($automationAction['acy_user'])) { |
| 354 |
$userActions = [ |
| 355 |
'confirm' => acym_translation('ACYM_WILL_CONFIRM'), |
| 356 |
'unconfirm' => acym_translation('ACYM_WILL_UNCONFIRM'), |
| 357 |
'active' => acym_translation('ACYM_WILL_ACTIVE'), |
| 358 |
'block' => acym_translation('ACYM_WILL_BLOCK'), |
| 359 |
'delete' => acym_translation('ACYM_WILL_DELETE'), |
| 360 |
]; |
| 361 |
$automationAction = $userActions[$automationAction['acy_user']['action']]; |
| 362 |
} |
| 363 |
|
| 364 |
if (!empty($automationAction['acy_user_value'])) { |
| 365 |
$usersColumns = acym_getColumns('user'); |
| 366 |
|
| 367 |
if (!in_array($automationAction['acy_user_value']['field'], $usersColumns)) { |
| 368 |
$fieldClass = new FieldClass(); |
| 369 |
$field = $fieldClass->getOneById($automationAction['acy_user_value']['field']); |
| 370 |
$automationAction['acy_user_value']['field'] = $field->name; |
| 371 |
} |
| 372 |
$automationAction = acym_translationSprintf( |
| 373 |
'ACYM_ACTION_USER_VALUE_SUMMARY', |
| 374 |
$automationAction['acy_user_value']['field'], |
| 375 |
$automationAction['acy_user_value']['operator'], |
| 376 |
$automationAction['acy_user_value']['value'] |
| 377 |
); |
| 378 |
} |
| 379 |
|
| 380 |
if (!empty($automationAction['acy_add_queue'])) { |
| 381 |
$mailClass = new MailClass(); |
| 382 |
$mail = $mailClass->getOneById($automationAction['acy_add_queue']['mail_id']); |
| 383 |
if (empty($mail)) { |
| 384 |
$automationAction = '<span class="acym__color__red">'.acym_translation('ACYM_SELECT_AN_EMAIL').'</span>'; |
| 385 |
} else { |
| 386 |
$automationAction = acym_translationSprintf( |
| 387 |
'ACYM_ACTION_ADD_QUEUE_SUMMARY', |
| 388 |
$mail->name, |
| 389 |
acym_date(acym_replaceDate($automationAction['acy_add_queue']['time']), 'd M Y H:i') |
| 390 |
); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
if (!empty($automationAction['acy_remove_queue'])) { |
| 395 |
$mailClass = new MailClass(); |
| 396 |
$mail = $mailClass->getOneById($automationAction['acy_remove_queue']['mail_id']); |
| 397 |
if (empty($mail)) { |
| 398 |
if ($automationAction['acy_remove_queue']['mail_id'] == -1) { |
| 399 |
$automationAction = acym_translation('ACYM_EMPTY_QUEUE_USER'); |
| 400 |
} else { |
| 401 |
$automationAction = '<span class="acym__color__red">'.acym_translation('ACYM_SELECT_AN_EMAIL').'</span>'; |
| 402 |
} |
| 403 |
} else { |
| 404 |
$automationAction = acym_translationSprintf('ACYM_ACTION_REMOVE_QUEUE_SUMMARY', $mail->name); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
if (!empty($automationAction['resend_confirmation'])) { |
| 409 |
$automationAction = acym_translation('ACYM_RESEND_CONFIRMATION_SUMMARY'); |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|