-
-
Notifications
You must be signed in to change notification settings - Fork 576
/
EditForm.php
executable file
路136 lines (115 loc) 路 5.95 KB
/
EditForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* @author Pierre-Henry Soria <ph7software@gmail.com>
* @copyright (c) 2012-2018, Pierre-Henry Soria. All Rights Reserved.
* @license GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
* @package PH7 / App / System / Module / User / Form
*/
namespace PH7;
use PH7\Framework\Date\CDateTime;
use PH7\Framework\Mvc\Request\Http as HttpRequest;
use PH7\Framework\Mvc\Router\Uri;
use PH7\Framework\Session\Session;
use PH7\Framework\Url\Header;
class EditForm
{
public static function display()
{
$oHttpRequest = new HttpRequest;
$iProfileId = self::getProfileId($oHttpRequest);
if (isset($_POST['submit_user_edit_account'])) {
if (\PFBC\Form::isValid($_POST['submit_user_edit_account'])) {
new EditFormProcess($iProfileId);
}
Header::redirect();
}
$oUserModel = new UserModel;
$oUser = $oUserModel->readProfile($iProfileId);
// Birth Date with the date format for the date picker
$sBirthDate = (new CDateTime)->get($oUser->birthDate)->date('Y-m-d');
$oForm = new \PFBC\Form('form_user_edit_account');
$oForm->configure(['action' => '']);
$oForm->addElement(new \PFBC\Element\Hidden('submit_user_edit_account', 'form_user_edit_account'));
$oForm->addElement(new \PFBC\Element\Token('edit_account'));
if (self::isAdminLoggedAndUserIdExists($oHttpRequest)) {
$oForm->addElement(
new \PFBC\Element\HTMLExternal('<p class="center"><a class="bold btn btn-default btn-md" href="' . Uri::get(PH7_ADMIN_MOD, 'user', 'browse') . '">' . t('Back to Browse Users') . '</a></p>')
);
$oMemberships = (new AdminCoreModel)->getMemberships();
$aGroupName = [];
foreach ($oMemberships as $oGroup) {
// Retrieve only the activated memberships
if ($oGroup->enable == 1) {
$aGroupName[$oGroup->groupId] = $oGroup->name;
}
}
$oForm->addElement(new \PFBC\Element\Select(t('Membership Group:'), 'group_id', $aGroupName, ['value' => $oUser->groupId, 'required' => 1]));
unset($aGroupName);
}
unset($oHR);
$oForm->addElement(new \PFBC\Element\Textbox(t('First Name:'), 'first_name', ['id' => 'name_first', 'onblur' => 'CValid(this.value,this.id)', 'value' => $oUser->firstName, 'required' => 1, 'validation' => new \PFBC\Validation\Name]));
$oForm->addElement(new \PFBC\Element\HTMLExternal('<span class="input_error name_first"></span>'));
$oForm->addElement(new \PFBC\Element\Textbox(t('Last Name:'), 'last_name', ['id' => 'name_last', 'onblur' => 'CValid(this.value,this.id)', 'value' => $oUser->lastName, 'validation' => new \PFBC\Validation\Name]));
$oForm->addElement(new \PFBC\Element\HTMLExternal('<span class="input_error name_last"></span>'));
$oForm->addElement(new \PFBC\Element\Textbox(t('Nickname:'), 'username', ['description' => t('For security reasons, you cannot change your username.'), 'disabled' => 'disabled', 'value' => $oUser->username]));
$oForm->addElement(new \PFBC\Element\Email(t('Email:'), 'mail', ['description' => t('For security reasons and to avoid spam, you cannot change your email address.'), 'disabled' => 'disabled', 'value' => $oUser->email]));
if (AdminCore::auth()) {
// For security reasons, only admins are able to change profile gender
$oForm->addElement(
new \PFBC\Element\Radio(
t('Gender:'),
'sex',
[
GenderTypeUserCoreModel::FEMALE => t('Woman'),
GenderTypeUserCoreModel::MALE => t('Man'),
GenderTypeUserCoreModel::COUPLE => t('Couple')
],
['value' => $oUser->sex, 'required' => 1]
)
);
}
$oForm->addElement(
new \PFBC\Element\Checkbox(
t('Looking for a:'),
'match_sex',
[
GenderTypeUserCoreModel::MALE => t('Man'),
GenderTypeUserCoreModel::FEMALE => t('Woman'),
GenderTypeUserCoreModel::COUPLE => t('Couple')
],
['value' => Form::getVal($oUser->matchSex), 'required' => 1]
)
);
$oForm->addElement(new \PFBC\Element\Date(t('Date of birth:'), 'birth_date', ['id' => 'birth_date', 'onblur' => 'CValid(this.value, this.id)', 'value' => $sBirthDate, 'validation' => new \PFBC\Validation\BirthDate, 'required' => 1]));
$oForm->addElement(new \PFBC\Element\HTMLExternal('<span class="input_error birth_date"></span>'));
// Generate dynamic fields
$oFields = $oUserModel->getInfoFields($iProfileId);
foreach ($oFields as $sColumn => $sValue) {
$oForm = (new DynamicFieldCoreForm($oForm, $sColumn, $sValue))->generate();
}
$oForm->addElement(new \PFBC\Element\Button(t('Save'), 'submit', ['icon' => 'check']));
$oForm->addElement(new \PFBC\Element\HTMLExternal('<script src="' . PH7_URL_STATIC . PH7_JS . 'validate.js"></script><script src="' . PH7_URL_STATIC . PH7_JS . 'geo/autocompleteCity.js"></script>'));
$oForm->render();
}
/**
* @param HttpRequest $oHttpRequest
*
* @return int
*/
private static function getProfileId(HttpRequest $oHttpRequest)
{
if (self::isAdminLoggedAndUserIdExists($oHttpRequest)) {
return $oHttpRequest->get('profile_id', 'int');
}
return (new Session)->get('member_id');
}
/**
* @param HttpRequest $oHttpRequest
*
* @return bool
*/
private static function isAdminLoggedAndUserIdExists(HttpRequest $oHttpRequest)
{
return AdminCore::auth() && !User::auth() && $oHttpRequest->getExists('profile_id');
}
}