Completed
Push — 1 ( 13c7dd...13037b )
by Morven
01:38
created

code/control/Users_Account_Controller.php (10 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Controller that is used to allow users to manage their accounts via
5
 * the front end of the site.
6
 *
7
 */
8
class Users_Account_Controller extends Controller implements PermissionProvider
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    /**
12
     * URL That you can access this from
13
     *
14
     * @config
15
     */
16
    private static $url_segment = "users/account";
0 ignored issues
show
The property $url_segment is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
    /**
19
     * Allowed sub-URL's on this controller
20
     * 
21
     * @var array
22
     * @config
23
     */
24
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
25
        "edit",
26
        "changepassword",
27
        "EditAccountForm",
28
        "ChangePasswordForm",
29
    );
30
31
    /**
32
     * User account associated with this controller
33
     *
34
     * @var Member
35
     */
36
    protected $member;
37
38
    /**
39
     * Getter for member
40
     *
41
     * @return Member
42
     */
43
    public function getMember()
44
    {
45
        return $this->member;
46
    }
47
48
    /**
49
     * Setter for member
50
     *
51
     * @param Member $member
52
     * @return self
53
     */
54
    public function setMember(Member $member)
55
    {
56
        $this->member = $member;
57
        return $this;
58
    }
59
60
    /**
61
     * Determine if current user requires verification (based on their
62
     * account and Users verification setting).
63
     *
64
     * @return boolean
65
     */
66
    public function RequireVerification()
67
    {
68
        if (!$this->member->isVerified() && Users::config()->require_verification) {
0 ignored issues
show
The if-else statement can be simplified to return !$this->member->i...->require_verification;.
Loading history...
69
            return true;
70
        } else {
71
            return false;
72
        }
73
    }
74
75
    /**
76
     * Perorm setup when this controller is initialised
77
     *
78
     * @return void
79
     */
80
    public function init()
81
    {
82
        parent::init();
83
84
        // Check we are logged in as a user who can access front end management
85
        if (!Permission::check("USERS_MANAGE_ACCOUNT")) {
86
            Security::permissionFailure();
87
        }
88
89
        // Set our member object
90
        $member = Member::currentUser();
91
92
        if ($member instanceof Member) {
93
            $this->member = $member;
94
        }
95
    }
96
97
    /**
98
     * Get the link to this controller
99
     * 
100
     * @param string $action
0 ignored issues
show
Should the type for parameter $action not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
101
     * @return string|null
102
     */
103
    public function Link($action = null)
104
    {
105
        return Controller::join_links(
106
            $this->config()->url_segment,
107
            $action
108
        );
109
    }
110
111
    /**
112
     * Get an absolute link to this controller
113
     *
114
     * @param string $action
0 ignored issues
show
Should the type for parameter $action not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
115
     * @return string|null
0 ignored issues
show
Should the return type not be false|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
116
     */
117
    public function AbsoluteLink($action = null)
118
    {
119
        return Director::absoluteURL($this->Link($action));
120
    }
121
122
    /**
123
     * Get a relative (to the root url of the site) link to this
124
     * controller
125
     *
126
     * @param string $action
0 ignored issues
show
Should the type for parameter $action not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
127
     * @return string|null
128
     */
129
    public function RelativeLink($action = null)
130
    {
131
        return Controller::join_links(
132
            $this->Link($action)
133
        );
134
    }
135
136
    /**
137
     * Display the currently outstanding orders for the current user
138
     *
139
     */
140
    public function index()
141
    {
142
        $this->customise(array(
143
            "ClassName" => "AccountPage"
144
        ));
145
146
        $this->extend("onBeforeIndex");
147
148
        return $this->renderWith(array(
149
            "UserAccount",
150
            "UserAccount",
151
            "Page"
152
        ));
153
    }
154
155
    public function edit()
156
    {
157
        $member = Member::currentUser();
158
        $form = $this->EditAccountForm();
159
160
        if ($member instanceof Member) {
161
            $form->loadDataFrom($member);
162
        }
163
164
        $this->customise(array(
165
            "ClassName" => "AccountPage",
166
            "Form"  => $form
167
        ));
168
169
        $this->extend("onBeforeEdit");
170
171
        return $this->renderWith(array(
172
            "UserAccount_edit",
173
            "UserAccount",
174
            "Page"
175
        ));
176
    }
177
178
    public function changepassword()
179
    {
180
        // Set the back URL for this form
181
        $back_url = Controller::join_links(
182
            $this->Link("changepassword"),
183
            "?s=1"
184
        );
185
        
186
        Session::set("BackURL", $back_url);
187
        
188
        $form = $this->ChangePasswordForm();
189
        
190
        // Is password changed, set a session message.
191
        $password_set = $this->request->getVar("s");
192
        if($password_set && $password_set == 1) {
193
            $form->sessionMessage(
194
                _t("Users.PasswordChangedSuccessfully","Password Changed Successfully"),
195
                "good"
196
            );
197
        }
198
199
        $this->customise(array(
200
            "ClassName" => "AccountPage",
201
            "Form"  => $form
202
        ));
203
204
        $this->extend("onBeforeChangePassword");
205
206
        return $this->renderWith(array(
207
            "UserAccount_changepassword",
208
            "UserAccount",
209
            "Page"
210
        ));
211
    }
212
213
    /**
214
     * Factory for generating a profile form. The form can be expanded using an
215
     * extension class and calling the updateEditProfileForm method.
216
     *
217
     * @return Form
218
     */
219
    public function EditAccountForm()
220
    {
221
        $form = Users_EditAccountForm::create($this, "EditAccountForm");
222
223
        $this->extend("updateEditAccountForm", $form);
224
225
        return $form;
226
    }
227
228
    /**
229
     * Factory for generating a change password form. The form can be expanded
230
     * using an extension class and calling the updateChangePasswordForm method.
231
     *
232
     * @return Form
233
     */
234
    public function ChangePasswordForm()
235
    {
236
        $form = ChangePasswordForm::create($this, "ChangePasswordForm");
237
238
        $form
239
            ->Actions()
240
            ->find("name", "action_doChangePassword")
241
            ->addExtraClass("btn")
242
            ->addExtraClass("btn-green");
243
244
        $cancel_btn = LiteralField::create(
245
            "CancelLink",
246
            '<a href="' . $this->Link() . '" class="btn btn-red">'. _t("Users.CANCEL", "Cancel") .'</a>'
247
        );
248
249
        $form
250
            ->Actions()
251
            ->insertBefore($cancel_btn, "action_doChangePassword");
0 ignored issues
show
'action_doChangePassword' is of type string, but the function expects a object<FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
252
253
        $this->extend("updateChangePasswordForm", $form);
254
255
        return $form;
256
    }
257
258
    /**
259
     * Return a list of nav items for managing a users profile. You can add new
260
     * items to this menu using the "updateAccountMenu" extension
261
     *
262
     * @return ArrayList
263
     */
264
    public function getAccountMenu()
265
    {
266
        $menu = new ArrayList();
267
        
268
        $curr_action = $this->request->param("Action");
269
270
        $menu->add(new ArrayData(array(
271
            "ID"    => 0,
272
            "Title" => _t('Users.PROFILESUMMARY', "Profile Summary"),
273
            "Link"  => $this->Link(),
274
            "LinkingMode" => (!$curr_action) ? "current" : "link"
275
        )));
276
277
        $menu->add(new ArrayData(array(
278
            "ID"    => 10,
279
            "Title" => _t('Users.EDITDETAILS', "Edit account details"),
280
            "Link"  => $this->Link("edit"),
281
            "LinkingMode" => ($curr_action == "edit") ? "current" : "link"
282
        )));
283
284
        $menu->add(new ArrayData(array(
285
            "ID"    => 30,
286
            "Title" => _t('Users.CHANGEPASSWORD', "Change password"),
287
            "Link"  => $this->Link("changepassword"),
288
            "LinkingMode" => ($curr_action == "changepassword") ? "current" : "link"
289
        )));
290
291
        $this->extend("updateAccountMenu", $menu);
292
293
        return $menu->sort("ID", "ASC");
294
    }
295
296
    public function providePermissions()
297
    {
298
        return array(
299
            "USERS_MANAGE_ACCOUNT" => array(
300
                'name' => 'Manage user account',
301
                'help' => 'Allow user to manage their account details',
302
                'category' => 'Frontend Users',
303
                'sort' => 100
304
            ),
305
            "USERS_VERIFIED" => array(
306
                'name' => 'Verified user',
307
                'help' => 'Users have verified their account',
308
                'category' => 'Frontend Users',
309
                'sort' => 100
310
            ),
311
        );
312
    }
313
}
314