Issues (281)

Branch: master

Backend/Modules/Profiles/Actions/MassAction.php (1 issue)

1
<?php
2
3
namespace Backend\Modules\Profiles\Actions;
4
5
use Backend\Core\Engine\Base\Action as BackendBaseAction;
6
use Backend\Core\Engine\Model as BackendModel;
7
use Backend\Modules\Profiles\Engine\Model as BackendProfilesModel;
8
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
9
10
/**
11
 * Mass action handler to delete profiles or add them to a specific group.
12
 */
13
class MassAction extends BackendBaseAction
14
{
15
    public function execute(): void
16
    {
17
        // call parent, this will probably add some general CSS/JS or other required files
18
        parent::execute();
19
20
        $this->checkToken();
21
22
        // action to execute
23
        $action = $this->getRequest()->query->get('action');
24
        if (!in_array($action, ['addToGroup', 'delete'])) {
25
            $this->redirect(BackendModel::createUrlForAction('Index') . '&error=no-action-selected');
26
        }
27
        $ids = $this->getRequest()->query->has('id') ? (array) $this->getRequest()->query->get('id') : [];
28
        $newGroupId = $this->getRequest()->query->get('newGroup');
29
30
        // no ids provided
31
        if (empty($ids)) {
32
            $this->redirect(BackendModel::createUrlForAction('Index') . '&error=no-profiles-selected');
33
        }
34
35
        // delete the given profiles
36
        if ($action === 'delete') {
37
            BackendProfilesModel::delete($ids);
38
            $report = 'deleted';
39
        } elseif ($action === 'addToGroup') {
40
            // add the profiles to the given group
41
            // no group id provided
42
            if (!array_key_exists($newGroupId, BackendProfilesModel::getGroups())) {
43
                $this->redirect(
44
                    BackendModel::createUrlForAction('Index') . '&error=no-group-selected'
45
                );
46
            }
47
48
            // set new status
49
            foreach ($ids as $id) {
50
                // profile must exist
51
                if (BackendProfilesModel::exists($id)) {
52
                    // make sure the user is not already part of this group without an expiration date
53
                    foreach (BackendProfilesModel::getProfileGroups($id) as $existingGroup) {
54
                        // if he is, skip to the next user
55
                        if ($existingGroup['group_id'] === $newGroupId) {
56
                            continue 2;
57
                        }
58
                    }
59
60
                    // OK, it's safe to add the user to this group
61
                    BackendProfilesModel::insertProfileGroup(
62
                        [
63
                             'profile_id' => $id,
64
                             'group_id' => $newGroupId,
65
                             'starts_on' => BackendModel::getUTCDate(),
66
                        ]
67
                    );
68
                }
69
            }
70
71
            // report
72
            $report = 'added-to-group';
73
        } else {
74
            // unknown action
75
            $this->redirect(BackendModel::createUrlForAction('Index') . '&error=unknown-action');
76
        }
77
78
        // report
79
        $report = (count($ids) > 1 ? 'profiles-' : 'profile-') . $report;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $report does not seem to be defined for all execution paths leading up to this point.
Loading history...
80
81
        // redirect
82
        $this->redirect(
83
            BackendModel::createUrlForAction(
84
                'Index',
85
                null,
86
                null,
87
                [
88
                     'offset' => $this->getRequest()->query->get('offset', ''),
89
                     'order' => $this->getRequest()->query->get('order', ''),
90
                     'sort' => $this->getRequest()->query->get('sort', ''),
91
                     'email' => $this->getRequest()->query->get('email', ''),
92
                     'status' => $this->getRequest()->query->get('status', ''),
93
                     'group' => $this->getRequest()->query->get('group', ''),
94
                ]
95
            ) . '&report=' . $report
96
        );
97
    }
98
}
99