Completed
Push — 1 ( 9b7a9d...a879de )
by Morven
07:43
created

Users_Account_Controller::edit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 0
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
Unused Code introduced by
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...
Unused Code introduced by
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
Coding Style introduced by
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
Documentation introduced by
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
Documentation introduced by
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
Documentation introduced by
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
Documentation introduced by
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
        // Setup default profile summary sections
143
        $sections = ArrayList::create();
144
145
        $sections->push(ArrayData::create(array(
146
            "Title" => "",
147
            "Content" => $this->renderWith(
148
                "UsersProfileSummary",
149
                array("CurrentUser" => Member::currentUser())
150
            )
151
        )));
152
153
        // Allow users to add extra content sections to the
154
        // summary
155
        $this->extend("updateContentSections", $sections);
156
157
        $this->customise(array(
158
            "Title" => _t('Users.ProfileSummary', 'Profile Summary'),
159
            "MetaTitle" => _t('Users.ProfileSummary', 'Profile Summary'),
160
            "Content" => $this->renderWith(
161
                "UsersAccountSections",
162
                array("Sections" => $sections)
163
            )
164
        ));
165
166
        $this->extend("onBeforeIndex");
167
168
        return $this->renderWith(array(
169
            "UserAccount",
170
            "Page"
171
        ));
172
    }
173
174
    public function edit()
175
    {
176
        $member = Member::currentUser();
177
        $form = $this->EditAccountForm();
178
179
        if ($member instanceof Member) {
180
            $form->loadDataFrom($member);
181
        }
182
183
        $this->customise(array(
184
            "Title" => _t("Users.EditAccountDetails", "Edit account details"),
185
            "MetaTitle" => _t("Users.EditAccountDetails", "Edit account details"),
186
            "Form"  => $form
187
        ));
188
189
        $this->extend("onBeforeEdit");
190
191
        return $this->renderWith(array(
192
            "UserAccount_edit",
193
            "UserAccount",
194
            "Page"
195
        ));
196
    }
197
198
    public function changepassword()
199
    {
200
        // Set the back URL for this form
201
        $back_url = Controller::join_links(
202
            $this->Link("changepassword"),
203
            "?s=1"
204
        );
205
        
206
        Session::set("BackURL", $back_url);
207
        
208
        $form = $this->ChangePasswordForm();
209
        
210
        // Is password changed, set a session message.
211
        $password_set = $this->request->getVar("s");
212
        if($password_set && $password_set == 1) {
213
            $form->sessionMessage(
214
                _t("Users.PasswordChangedSuccessfully","Password Changed Successfully"),
215
                "good"
216
            );
217
        }
218
219
        $this->customise(array(
220
            "Title" => _t("Security.ChangeYourPassword", "Change your password"),
221
            "MetaTitle" => _t("Security.ChangeYourPassword", "Change your password"),
222
            "Form"  => $form
223
        ));
224
225
        $this->extend("onBeforeChangePassword");
226
227
        return $this->renderWith(array(
228
            "UserAccount_changepassword",
229
            "UserAccount",
230
            "Page"
231
        ));
232
    }
233
234
    /**
235
     * Factory for generating a profile form. The form can be expanded using an
236
     * extension class and calling the updateEditProfileForm method.
237
     *
238
     * @return Form
239
     */
240
    public function EditAccountForm()
241
    {
242
        $form = Users_EditAccountForm::create($this, "EditAccountForm");
243
244
        $this->extend("updateEditAccountForm", $form);
245
246
        return $form;
247
    }
248
249
    /**
250
     * Factory for generating a change password form. The form can be expanded
251
     * using an extension class and calling the updateChangePasswordForm method.
252
     *
253
     * @return Form
254
     */
255
    public function ChangePasswordForm()
256
    {
257
        $form = ChangePasswordForm::create($this, "ChangePasswordForm");
258
259
        $form
260
            ->Actions()
261
            ->find("name", "action_doChangePassword")
262
            ->addExtraClass("btn")
263
            ->addExtraClass("btn-green");
264
265
        $cancel_btn = LiteralField::create(
266
            "CancelLink",
267
            '<a href="' . $this->Link() . '" class="btn btn-red">'. _t("Users.CANCEL", "Cancel") .'</a>'
268
        );
269
270
        $form
271
            ->Actions()
272
            ->insertBefore($cancel_btn, "action_doChangePassword");
0 ignored issues
show
Documentation introduced by
'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...
273
274
        $this->extend("updateChangePasswordForm", $form);
275
276
        return $form;
277
    }
278
279
    /**
280
     * Return a list of nav items for managing a users profile. You can add new
281
     * items to this menu using the "updateAccountMenu" extension
282
     *
283
     * @return ArrayList
284
     */
285
    public function getAccountMenu()
286
    {
287
        $menu = ArrayList::create();
288
        
289
        $curr_action = $this->request->param("Action");
290
291
        $menu->add(ArrayData::create(array(
292
            "ID"    => 0,
293
            "Title" => _t('Users.PROFILESUMMARY', "Profile Summary"),
294
            "Link"  => $this->Link(),
295
            "LinkingMode" => (!$curr_action) ? "current" : "link"
296
        )));
297
298
        $menu->add(ArrayData::create(array(
299
            "ID"    => 10,
300
            "Title" => _t('Users.EDITDETAILS', "Edit account details"),
301
            "Link"  => $this->Link("edit"),
302
            "LinkingMode" => ($curr_action == "edit") ? "current" : "link"
303
        )));
304
305
        $menu->add(ArrayData::create(array(
306
            "ID"    => 30,
307
            "Title" => _t('Users.CHANGEPASSWORD', "Change password"),
308
            "Link"  => $this->Link("changepassword"),
309
            "LinkingMode" => ($curr_action == "changepassword") ? "current" : "link"
310
        )));
311
312
        $this->extend("updateAccountMenu", $menu);
313
314
        return $menu->sort("ID", "ASC");
315
    }
316
317
    public function providePermissions()
318
    {
319
        return array(
320
            "USERS_MANAGE_ACCOUNT" => array(
321
                'name' => 'Manage user account',
322
                'help' => 'Allow user to manage their account details',
323
                'category' => 'Frontend Users',
324
                'sort' => 100
325
            ),
326
            "USERS_VERIFIED" => array(
327
                'name' => 'Verified user',
328
                'help' => 'Users have verified their account',
329
                'category' => 'Frontend Users',
330
                'sort' => 100
331
            ),
332
        );
333
    }
334
}
335