Completed
Push — 1 ( fdc87b...27ddc3 )
by Morven
01:40
created

code/control/Users_Account_Controller.php (4 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
9
{
10
11
    /**
12
     * URL That you can access this from
13
     *
14
     * @config
15
     */
16
    private static $url_segment = "users/account";
17
18
    /**
19
     * Allowed sub-URL's on this controller
20
     * 
21
     * @var array
22
     * @config
23
     */
24
    private static $allowed_actions = array(
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) {
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
     * If content controller exists, return it's menu function
138
     * @param int $level Menu level to return.
139
     * @return ArrayList
140
     */
141 View Code Duplication
    public function getMenu($level = 1)
142
    {
143
        if (class_exists(ContentController::class)) {
144
            $controller = Injector::inst()->get(ContentController::class);
145
            return $controller->getMenu($level);
146
        }
147
    }
148
149
    public function Menu($level)
150
    {
151
        return $this->getMenu();
152
    }
153
154
    /**
155
     * Display the currently outstanding orders for the current user
156
     *
157
     */
158
    public function index()
159
    {
160
        // Setup default profile summary sections
161
        $sections = ArrayList::create();
162
163
        $sections->push(ArrayData::create(array(
164
            "Title" => "",
165
            "Content" => $this->renderWith(
166
                "UsersProfileSummary",
167
                array("CurrentUser" => Member::currentUser())
168
            )
169
        )));
170
171
        // Allow users to add extra content sections to the
172
        // summary
173
        $this->extend("updateIndexSections", $sections);
174
175
        $this->customise(array(
176
            "Title" => _t('Users.ProfileSummary', 'Profile Summary'),
177
            "MetaTitle" => _t('Users.ProfileSummary', 'Profile Summary'),
178
            "Content" => $this->renderWith(
179
                "UsersAccountSections",
180
                array("Sections" => $sections)
181
            )
182
        ));
183
184
        $this->extend("onBeforeIndex");
185
186
        return $this->renderWith(array(
187
            "UserAccount",
188
            "Page"
189
        ));
190
    }
191
192
    public function edit()
193
    {
194
        $member = Member::currentUser();
195
        $form = $this->EditAccountForm();
196
197
        if ($member instanceof Member) {
198
            $form->loadDataFrom($member);
199
        }
200
201
        $this->customise(array(
202
            "Title" => _t("Users.EditAccountDetails", "Edit account details"),
203
            "MetaTitle" => _t("Users.EditAccountDetails", "Edit account details"),
204
            "Form"  => $form
205
        ));
206
207
        $this->extend("onBeforeEdit");
208
209
        return $this->renderWith(array(
210
            "UserAccount_edit",
211
            "UserAccount",
212
            "Page"
213
        ));
214
    }
215
216
    public function changepassword()
217
    {
218
        // Set the back URL for this form
219
        $back_url = Controller::join_links(
220
            $this->Link("changepassword"),
221
            "?s=1"
222
        );
223
        
224
        Session::set("BackURL", $back_url);
225
        
226
        $form = $this->ChangePasswordForm();
227
        
228
        // Is password changed, set a session message.
229
        $password_set = $this->request->getVar("s");
230
        if($password_set && $password_set == 1) {
231
            $form->sessionMessage(
232
                _t("Users.PasswordChangedSuccessfully","Password Changed Successfully"),
233
                "good"
234
            );
235
        }
236
237
        $this->customise(array(
238
            "Title" => _t("Security.ChangeYourPassword", "Change your password"),
239
            "MetaTitle" => _t("Security.ChangeYourPassword", "Change your password"),
240
            "Form"  => $form
241
        ));
242
243
        $this->extend("onBeforeChangePassword");
244
245
        return $this->renderWith(array(
246
            "UserAccount_changepassword",
247
            "UserAccount",
248
            "Page"
249
        ));
250
    }
251
252
    /**
253
     * Factory for generating a profile form. The form can be expanded using an
254
     * extension class and calling the updateEditProfileForm method.
255
     *
256
     * @return Form
257
     */
258
    public function EditAccountForm()
259
    {
260
        $form = Users_EditAccountForm::create($this, "EditAccountForm");
261
262
        $this->extend("updateEditAccountForm", $form);
263
264
        return $form;
265
    }
266
267
    /**
268
     * Factory for generating a change password form. The form can be expanded
269
     * using an extension class and calling the updateChangePasswordForm method.
270
     *
271
     * @return Form
272
     */
273
    public function ChangePasswordForm()
274
    {
275
        $form = ChangePasswordForm::create($this, "ChangePasswordForm");
276
277
        $form
278
            ->Actions()
279
            ->find("name", "action_doChangePassword")
280
            ->addExtraClass("btn")
281
            ->addExtraClass("btn-green");
282
283
        $cancel_btn = LiteralField::create(
284
            "CancelLink",
285
            '<a href="' . $this->Link() . '" class="btn btn-red">'. _t("Users.CANCEL", "Cancel") .'</a>'
286
        );
287
288
        $form
289
            ->Actions()
290
            ->insertBefore($cancel_btn, "action_doChangePassword");
291
292
        $this->extend("updateChangePasswordForm", $form);
293
294
        return $form;
295
    }
296
297
    /**
298
     * Return a list of nav items for managing a users profile. You can add new
299
     * items to this menu using the "updateAccountMenu" extension
300
     *
301
     * @return ArrayList
302
     */
303
    public function getAccountMenu()
304
    {
305
        $menu = ArrayList::create();
306
        
307
        $curr_action = $this->request->param("Action");
308
309
        $menu->add(ArrayData::create(array(
310
            "ID"    => 0,
311
            "Title" => _t('Users.PROFILESUMMARY', "Profile Summary"),
312
            "Link"  => $this->Link(),
313
            "LinkingMode" => (!$curr_action) ? "current" : "link"
314
        )));
315
316
        $menu->add(ArrayData::create(array(
317
            "ID"    => 10,
318
            "Title" => _t('Users.EDITDETAILS', "Edit account details"),
319
            "Link"  => $this->Link("edit"),
320
            "LinkingMode" => ($curr_action == "edit") ? "current" : "link"
321
        )));
322
323
        $menu->add(ArrayData::create(array(
324
            "ID"    => 30,
325
            "Title" => _t('Users.CHANGEPASSWORD', "Change password"),
326
            "Link"  => $this->Link("changepassword"),
327
            "LinkingMode" => ($curr_action == "changepassword") ? "current" : "link"
328
        )));
329
330
        $this->extend("updateAccountMenu", $menu);
331
332
        return $menu->sort("ID", "ASC");
333
    }
334
335
    public function providePermissions()
336
    {
337
        return array(
338
            "USERS_MANAGE_ACCOUNT" => array(
339
                'name' => 'Manage user account',
340
                'help' => 'Allow user to manage their account details',
341
                'category' => 'Frontend Users',
342
                'sort' => 100
343
            ),
344
            "USERS_VERIFIED" => array(
345
                'name' => 'Verified user',
346
                'help' => 'Users have verified their account',
347
                'category' => 'Frontend Users',
348
                'sort' => 100
349
            ),
350
        );
351
    }
352
}
353