1 <?php
2 3 4 5 6 7
8 class ProfilePage extends Page {
9
10 static $icon = "auth/img/user";
11 static $allowed_children = 'none';
12
13 static $default = array(
14 'URLSegment' => 'profile',
15 'AutoChild' => 0,
16 );
17
18 static function find_link($action = '') {
19 if (!$page = DataObject::get_one('ProfilePage')) {
20 user_error('Создайте страницу "Личный кабинет"', E_USER_ERROR);
21 }
22
23 return $page->Link($action);
24 }
25
26 function getCMSFields() {
27 $fields = parent::getCMSFields();
28 $fields->removeByName('AutoChild');
29 return $fields;
30 }
31
32 function canCreate($member = false) {
33 if (DataObject::get_one('ProfilePage')) return false;
34 if (!Director::isDev()) return false;
35 return parent::canCreate($member);
36 }
37
38 function canDelete() {
39 return Director::isDev();
40 }
41 }
42
43 class ProfilePage_Controller extends Page_Controller {
44
45 static $allowed_actions = array('index', 'edit', 'EditProfileForm');
46
47 function init() {
48 parent::init();
49 if (!Member::currentUserID()) {
50 return Security::permissionFailure($this);
51 }
52 }
53
54 55 56 57 58 59 60
61 function ($current = false) {
62 $items = array(
63 array(
64 'Link' => $this->Link(),
65 'Title' => _t('ProfilePage.MenuProfile', 'My Profile'),
66 ),
67 array(
68 'Link' => $this->Link('edit'),
69 'Title' => _t('ProfilePage.MenuEdit', 'My Profile'),
70 ),
71 72 73 74 75 76
77 );
78
79 $this->extend('updateUserMenu', $items);
80
81
82 if (!$current)
83 $current = $this->getAction();
84
85 foreach ($items as &$item) {
86 $link = $item['Link'];
87 if (strpos($item['Link'], '?') > 0) {
88 $link = substr($link, 0, strpos($link, '?'));
89 }
90 $action = trim(substr($link, strrpos($link, '/') + 1));
91 if (!$action) $action = 'index';
92 $item['IsCurrent'] = ($current == $action) ? 1 : 0;
93 $item['LinkOrCurrent'] = ($current == $action) ? 'current' : 'link';
94 $item['LinkingMode'] = ($current == $action) ? 'current' : 'link';
95 }
96
97 return new DataObjectSet($items);
98 }
99
100 101 102
103 function edit() {
104 if (!$form = $this->EditProfileForm())
105 $form = '<p class="error message">' . _t('UserProfile.WRONGPERMISSION', 'You don\'t have the permission to edit that member.') . '</p>';
106
107 return array(
108 "Title" => _t('ProfilePage.TitleEdit', 'Edit Profile'),
109 "Form" => $form,
110 );
111 }
112
113 114 115 116 117 118
119 function EditProfileForm() {
120 $member = Member::currentUser();
121 $fields = $member->getMemberFormFields();
122 $fields->push(new HiddenField('ID', 'ID', $member->ID));
123 if ($member && $member->hasMethod('canEdit') && $member->canEdit()) {
124 $form = new Form(
125 $this,
126 'EditProfileForm',
127 $fields,
128 new FieldSet(new FormAction("saveprofile", _t('ProfilePage.SaveProfile', 'Save'))),
129 $member->getValidator()
130 );
131 $this->extend('updateEditProfileForm', $form);
132 $form->loadDataFrom($member);
133 return $form;
134 }
135 return null;
136 }
137
138 139 140 141 142 143 144
145 function saveprofile($data, $form) {
146 $member = Member::currentUser();
147 $form->saveInto($member);
148 $member->write();
149 $backURL = Session::get('BackURL');
150 if ($backURL) {
151 Director::redirect($backURL);
152 } else {
153 Director::redirect($this->Link());
154 }
155 }
156
157 }
158
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-