| 1 |
<?php |
| 2 |
|
| 3 |
trait UserAutomationActions |
| 4 |
{ |
| 5 |
public function onAcymDeclareActions(array &$actions): void |
| 6 |
{ |
| 7 |
$allGroups = acym_getGroups(); |
| 8 |
$groups = ['none' => acym_translation('ACYM_SELECT_GROUP')]; |
| 9 |
foreach ($allGroups as $group) { |
| 10 |
$groups[$group->id] = $group->text; |
| 11 |
} |
| 12 |
unset($groups[ACYM_ADMIN_GROUP]); |
| 13 |
|
| 14 |
$actions['acy_group_action'] = new stdClass(); |
| 15 |
$actions['acy_group_action']->name = acym_translation('ACYM_ACTION_ON_GROUPS'); |
| 16 |
ob_start(); |
| 17 |
include acym_getPartial('actions', 'acy_group_action'); |
| 18 |
$actions['acy_group_action']->option = ob_get_clean(); |
| 19 |
} |
| 20 |
|
| 21 |
public function onAcymDeclareActionsScenario(array &$actions): void |
| 22 |
{ |
| 23 |
$this->onAcymDeclareActions($actions); |
| 24 |
} |
| 25 |
|
| 26 |
public function onAcymProcessAction_acy_group_action(&$query, $action) |
| 27 |
{ |
| 28 |
$nbAffected = 0; |
| 29 |
|
| 30 |
$queryBuilder = clone $query; |
| 31 |
$queryBuilder->where[] = 'user.cms_id != 0'; |
| 32 |
$queryBuilder->where[] = 'user.cms_id IS NOT NULL'; |
| 33 |
$userIds = acym_loadResultArray($queryBuilder->getQuery(['user.cms_id'])); |
| 34 |
|
| 35 |
if (empty($userIds)) { |
| 36 |
return ''; |
| 37 |
} |
| 38 |
|
| 39 |
foreach ($userIds as $userId) { |
| 40 |
$userAffected = false; |
| 41 |
|
| 42 |
if (ACYM_CMS === 'joomla') { |
| 43 |
if (!empty($action['add']) && $action['add'] !== 'none') { |
| 44 |
$affected = acym_query('INSERT IGNORE INTO #__user_usergroup_map (`user_id`, `group_id`) VALUES ('.intval($userId).', '.intval($action['add']).')'); |
| 45 |
if ($affected) { |
| 46 |
$userAffected = true; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if (!empty($action['remove']) && $action['remove'] !== 'none') { |
| 51 |
$affected = acym_query('DELETE FROM #__user_usergroup_map WHERE `group_id` = '.intval($action['remove']).' AND `user_id` = '.intval($userId)); |
| 52 |
if ($affected) { |
| 53 |
$userAffected = true; |
| 54 |
} |
| 55 |
} |
| 56 |
} else { |
| 57 |
$user = new WP_User($userId); |
| 58 |
if (empty($user->ID)) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
if (!empty($action['add']) && $action['add'] !== 'none' && !in_array($action['add'], $user->roles)) { |
| 63 |
$user->add_role($action['add']); |
| 64 |
$userAffected = true; |
| 65 |
} |
| 66 |
|
| 67 |
if (!empty($action['remove']) && $action['remove'] !== 'none' && in_array($action['remove'], $user->roles)) { |
| 68 |
$user->remove_role($action['remove']); |
| 69 |
$userAffected = true; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if ($userAffected) { |
| 74 |
$nbAffected++; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return acym_translationSprintf('ACYM_ACTION_ON_GROUPS_RESULT', $nbAffected); |
| 79 |
} |
| 80 |
|
| 81 |
public function onAcymDeclareSummary_actions(&$automationAction) |
| 82 |
{ |
| 83 |
if (empty($automationAction['acy_group_action'])) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$allGroups = acym_getGroups(); |
| 88 |
$summary = ''; |
| 89 |
|
| 90 |
if (!empty($automationAction['acy_group_action']['remove']) && $automationAction['acy_group_action']['remove'] !== 'none') { |
| 91 |
$groupIdToRemove = $automationAction['acy_group_action']['remove']; |
| 92 |
$groupNameToRemove = isset($allGroups[$groupIdToRemove]) ? $allGroups[$groupIdToRemove]->text : acym_translation('ACYM_UNKNOWN_GROUP'); |
| 93 |
$summary .= acym_translationSprintf('ACYM_GROUP_ACTION_SUMARY_DELETE', $groupNameToRemove).'<br />'; |
| 94 |
} |
| 95 |
|
| 96 |
if (!empty($automationAction['acy_group_action']['add']) && $automationAction['acy_group_action']['add'] !== 'none') { |
| 97 |
$groupIdToAdd = $automationAction['acy_group_action']['add']; |
| 98 |
$groupNameToAdd = isset($allGroups[$groupIdToAdd]) ? $allGroups[$groupIdToAdd]->text : acym_translation('ACYM_UNKNOWN_GROUP'); |
| 99 |
$summary .= acym_translationSprintf('ACYM_GROUP_ACTION_SUMARY_ADD', $groupNameToAdd); |
| 100 |
} |
| 101 |
|
| 102 |
$automationAction = $summary; |
| 103 |
} |
| 104 |
} |
| 105 |
|