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