CMSProfileController::getEditForm()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 36
rs 9.2568
1
<?php
2
3
namespace LeKoala\Admini;
4
5
use RuntimeException;
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\Security\Member;
8
use SilverStripe\Forms\FormAction;
9
use SilverStripe\Forms\HiddenField;
10
use SilverStripe\Security\Security;
11
use SilverStripe\Security\Permission;
12
use SilverStripe\Control\HTTPResponse;
13
14
class CMSProfileController extends LeftAndMain
15
{
16
    private static $url_segment = 'myprofile';
17
18
    private static $menu_title = 'My Profile';
19
20
    private static $required_permission_codes = false;
21
22
    private static $tree_class = Member::class;
23
24
    private static $menu_icon = MaterialIcons::PERSON;
0 ignored issues
show
introduced by
The private property $menu_icon is not used, and could be removed.
Loading history...
25
26
    private static $ignore_menuitem = true; // access through custom ui
0 ignored issues
show
introduced by
The private property $ignore_menuitem is not used, and could be removed.
Loading history...
27
28
    public function getEditForm($id = null, $fields = null)
29
    {
30
        $user = Security::getCurrentUser();
31
        if (!$user) {
0 ignored issues
show
introduced by
$user is of type SilverStripe\Security\Member, thus it always evaluated to true.
Loading history...
32
            throw new RuntimeException("No user");
33
        }
34
        $form = parent::getEditForm($user->ID, $fields);
35
36
        if (!$form) {
0 ignored issues
show
introduced by
$form is of type SilverStripe\Forms\Form, thus it always evaluated to true.
Loading history...
37
            throw new RuntimeException("No form for '$id'");
38
        }
39
        if ($form instanceof HTTPResponse) {
0 ignored issues
show
introduced by
$form is never a sub-type of SilverStripe\Control\HTTPResponse.
Loading history...
40
            return $form;
41
        }
42
43
        $form->Fields()->removeByName('LastVisited');
44
        $form->Fields()->push(new HiddenField('ID', null, Security::getCurrentUser()->ID));
45
46
        $form->Actions()->unshift(
47
            FormAction::create('save', _t('LeKoala\\Admini\\LeftAndMain.SAVE', 'Save'))
48
                ->setIcon(MaterialIcons::DONE)
49
                ->addExtraClass('btn-outline-success')
50
                ->setUseButtonTag(true)
51
        );
52
53
        $form->Actions()->removeByName('action_delete');
54
55
        if ($member = Security::getCurrentUser()) {
56
            $form->setValidator($member->getValidator());
57
        } else {
58
            $form->setValidator(Member::singleton()->getValidator());
59
        }
60
61
        $this->setCMSTabset($form);
62
63
        return $form;
64
    }
65
66
    public function canView($member = null)
67
    {
68
        if (!$member && $member !== false) {
69
            $member = Security::getCurrentUser();
70
        }
71
72
        // cms menus only for logged-in members
73
        if (!$member) {
74
            return false;
75
        }
76
77
        // Check they can access the CMS and that they are trying to edit themselves
78
        $canAccess = Permission::checkMember($member, "CMS_ACCESS")
79
            && $member->ID === Security::getCurrentUser()->ID;
80
81
        if ($canAccess) {
82
            return true;
83
        }
84
85
        return false;
86
    }
87
88
    public function save($data, $form)
89
    {
90
        /** @var Member $Member */
91
        $member = Member::get()->byID($data['ID']);
92
        if (!$member) {
93
            return $this->httpError(404);
94
        }
95
        $origLocale = $member->Locale;
96
97
        if (!$member->canEdit()) {
98
            $this->sessionMessage(_t(__CLASS__ . '.CANTEDIT', 'You don\'t have permission to do that'), 'bad');
99
            return $this->redirectBack();
100
        }
101
102
        $response = parent::save($data, $form);
103
104
        // Current locale has changed
105
        if (isset($data['Locale']) && $origLocale != $data['Locale']) {
106
            // TODO: implement our own ajax loading
107
            // $response->addHeader('X-Reload', true);
108
            // $response->addHeader('X-ControllerURL', $this->Link());
109
        }
110
111
        return $response;
112
    }
113
114
    /**
115
     * Only show first element, as the profile form is limited to editing
116
     * the current member it doesn't make much sense to show the member name
117
     * in the breadcrumbs.
118
     *
119
     * @param bool $unlinked
120
     * @return ArrayList
121
     */
122
    public function Breadcrumbs($unlinked = false)
123
    {
124
        $items = parent::Breadcrumbs($unlinked);
125
        return new ArrayList(array($items[0]));
126
    }
127
}
128