Redesign profile page

Added new fields:
- First name
- Last name
- Nickname
This commit is contained in:
Jay Trees 2022-04-07 10:31:23 +02:00
parent 0585061164
commit 74306ec5be
5 changed files with 140 additions and 209 deletions

View file

@ -6,7 +6,7 @@
* @author Jay Trees <github.jay@grandel.anonaddy.me> * @author Jay Trees <github.jay@grandel.anonaddy.me>
*/ */
define('VERSION', '0.4.0'); define('VERSION', '0.5.0');
define('ROOT', __DIR__); define('ROOT', __DIR__);
define('DEFAULT_LOCALE', 'en_GB'); define('DEFAULT_LOCALE', 'en_GB');

View file

@ -133,7 +133,10 @@ switch ($step) {
`last_login` DATETIME NOT NULL DEFAULT NOW(), `last_login` DATETIME NOT NULL DEFAULT NOW(),
`power` INT NOT NULL DEFAULT 0, `power` INT NOT NULL DEFAULT 0,
`birthdate` DATE NULL DEFAULT NULL, `birthdate` DATE NULL DEFAULT NULL,
`locale` VARCHAR(5) NOT NULL DEFAULT "' . DEFAULT_LOCALE . '" `locale` VARCHAR(5) NOT NULL DEFAULT "' . DEFAULT_LOCALE . '",
`name_first` VARCHAR(32) NULL DEFAULT NULL,
`name_last` VARCHAR(32) NULL DEFAULT NULL,
`name_nick` VARCHAR(32) NULL DEFAULT NULL
);'); );');
$database->query('CREATE INDEX `idx_password` ON `users` (`password`);'); $database->query('CREATE INDEX `idx_password` ON `users` (`password`);');

View file

@ -8,49 +8,96 @@
use wishthis\{Page, User}; use wishthis\{Page, User};
if (isset($_POST['user-id'])) { $page = new Page(__FILE__, __('Profile'));
$set = array();
if (isset($_POST['user-id'], $_POST['section'])) {
$set = array();
$formFieldsString = array(
array(
'column' => 'name_first',
'key' => 'user-name-first',
'label' => __('First name'),
),
array(
'column' => 'name_last',
'key' => 'user-name-last',
'label' => __('Last name'),
),
array(
'column' => 'name_nick',
'key' => 'user-name-nick',
'label' => __('Nickname'),
),
array(
'column' => 'email',
'key' => 'user-email',
'label' => __('Email'),
),
array(
'column' => 'locale',
'key' => 'user-locale',
'label' => __('Language'),
),
);
$loginRequired = false; $loginRequired = false;
if (!empty($_POST['user-email'])) { foreach ($formFieldsString as $field) {
$set[] = '`email` = "' . $_POST['user-email'] . '"'; if (!empty($_POST[$field['key']]) && $_POST[$field['key']] !== $user->{$field['column']}) {
$set[] = '`' . $field['column'] . '` = "' . $_POST[$field['key']] . '"';
$user->{$field['column']} = $_POST[$field['key']];
$page->messages[] = Page::success(
sprintf(
__('%s successfully updated!'),
'<strong>' . $field['label'] . '</strong>'
),
__('Success')
);
}
} }
if (!empty($_POST['user-password']) && !empty($_POST['user-password-repeat'])) { if (!empty($_POST['user-email']) && $_POST['user-email'] !== $user->email) {
$set[] = '`password` = "' . User::generatePassword($_POST['user-password']) . '"'; $loginRequired = true;
} }
if ($_POST['user-birthdate']) { if (isset($_POST['user-birthdate'])) {
$user->birthdate = date('Y-m-d', strtotime($_POST['user-birthdate'])); if (empty($_POST['user-birthdate'])) {
$set[] = '`birthdate` = "' . $user->birthdate . '"';
} else {
$user->birthdate = null; $user->birthdate = null;
$set[] = '`birthdate` = NULL'; $set[] = '`birthdate` = NULL';
} else {
$user->birthdate = date('Y-m-d', strtotime($_POST['user-birthdate']));
$set[] = '`birthdate` = "' . $user->birthdate . '"';
}
} }
$set[] = '`locale` = "' . $_POST['user-locale'] . '"'; if (
!empty($_POST['user-password'])
&& !empty($_POST['user-password-repeat'])
&& $_POST['user-password'] === $_POST['user-password-repeat']
) {
$set[] = '`password` = "' . User::generatePassword($_POST['user-password']) . '"';
$loginRequired = true;
}
$database $database
->query('UPDATE `users` ->query('UPDATE `users`
SET ' . implode(',', $set) . ' SET ' . implode(',', $set) . '
WHERE `id` = ' . $_POST['user-id']); WHERE `id` = ' . $_POST['user-id']);
if ($_POST['user-email'] !== $_SESSION['user']['email']) {
$loginRequired = true;
}
if ($loginRequired) { if ($loginRequired) {
session_destroy(); session_destroy();
}
redirect('/?page=profile'); $page->messages[] = Page::warning(
__('It is required for you to login again.'),
__('Success')
);
}
} }
$page = new Page(__FILE__, __('Profile'));
$page->header(); $page->header();
$page->bodyStart(); $page->bodyStart();
$page->navigation(); $page->navigation();
@ -60,20 +107,22 @@ $page->navigation();
<div class="ui container"> <div class="ui container">
<h1 class="ui header"><?= $page->title ?></h1> <h1 class="ui header"><?= $page->title ?></h1>
<?= $page->messages() ?>
<div class="ui stackable grid"> <div class="ui stackable grid">
<div class="four wide column"> <div class="four wide column">
<div class="ui vertical pointing fluid menu profile"> <div class="ui vertical pointing fluid menu profile">
<div class="item" data-tab="personal"> <div class="item" data-tab="personal">
<h4 class="ui header">Personal</h4> <h4 class="ui header"><?= __('Personal') ?></h4>
<p>Information regarding yourself</p> <p><?= __('Information regarding yourself') ?></p>
</div> </div>
<div class="item" data-tab="password"> <div class="item" data-tab="password">
<h4 class="ui header">Password</h4> <h4 class="ui header"><?= __('Password') ?></h4>
<p>Change your password</p> <p><?= __('Change your password') ?></p>
</div> </div>
<div class="item" data-tab="language"> <div class="item" data-tab="preferences">
<h4 class="ui header">Language</h4> <h4 class="ui header"><?= __('Preferences') ?></h4>
<p>Your language preferences</p> <p><?= __('Improve your withthis experience') ?></p>
</div> </div>
</div> </div>
</div> </div>
@ -84,48 +133,35 @@ $page->navigation();
<form class="ui form" method="POST"> <form class="ui form" method="POST">
<input type="hidden" name="user-id" value="<?= $user->id ?>" /> <input type="hidden" name="user-id" value="<?= $user->id ?>" />
<input type="hidden" name="section" value="personal" />
<div class="three fields"> <div class="three fields">
<div class="field"> <div class="field">
<label><?= __('First name') ?></label> <label><?= __('First name') ?></label>
<input type="text" name="user-name-first" value="" /> <input type="text" name="user-name-first" value="<?= $user->name_first ?>" />
</div> </div>
<div class="field"> <div class="field">
<label><?= __('Last name') ?></label> <label><?= __('Last name') ?></label>
<input type="text" name="user-name-last" value="" /> <input type="text" name="user-name-last" value="<?= $user->name_last ?>" />
</div> </div>
<div class="field"> <div class="field">
<label><?= __('Nickname') ?></label> <label><?= __('Nickname') ?></label>
<input type="text" name="user-name-nick" value="" /> <input type="text" name="user-name-nick" value="<?= $user->name_nick ?>" />
</div> </div>
</div> </div>
<div class="two fields">
<div class="field"> <div class="field">
<label><?= __('Email') ?></label> <label><?= __('Email') ?></label>
<input type="email" name="user-email" value="<?= $user->email ?>" /> <input type="email" name="user-email" value="<?= $user->email ?>" />
</div> </div>
<div class="two fields">
<div class="field">
<label><?= __('Password') ?></label>
<input type="password" name="user-password" autocomplete="new-password" />
</div>
<div class="field">
<label><?= __('Password (repeat)') ?></label>
<input type="password" name="user-password-repeat" autocomplete="new-password" />
</div>
</div>
<div class="two fields">
<div class="field"> <div class="field">
<label><?= __('Birthdate') ?></label> <label><?= __('Birthdate') ?></label>
@ -140,24 +176,6 @@ $page->navigation();
</div> </div>
</div> </div>
</div> </div>
<div class="field">
<label><?= __('Language') ?></label>
<select class="ui search dropdown" name="user-locale">
<?php if (!in_array('en', $locales)) { ?>
<option value="<?= 'en' ?>"><?= \Locale::getDisplayName('en', $user->locale) ?></option>
<?php } ?>
<?php foreach ($locales as $locale) { ?>
<?php if ($locale === $user->locale) { ?>
<option value="<?= $locale ?>" selected><?= \Locale::getDisplayName($locale, $user->locale) ?></option>
<?php } else { ?>
<option value="<?= $locale ?>"><?= \Locale::getDisplayName($locale, $user->locale) ?></option>
<?php } ?>
<?php } ?>
</select>
</div>
</div> </div>
<div class="ui error message"></div> <div class="ui error message"></div>
@ -177,32 +195,7 @@ $page->navigation();
<form class="ui form" method="POST"> <form class="ui form" method="POST">
<input type="hidden" name="user-id" value="<?= $user->id ?>" /> <input type="hidden" name="user-id" value="<?= $user->id ?>" />
<input type="hidden" name="section" value="password" />
<div class="three fields">
<div class="field">
<label><?= __('First name') ?></label>
<input type="text" name="user-name-first" value="" />
</div>
<div class="field">
<label><?= __('Last name') ?></label>
<input type="text" name="user-name-last" value="" />
</div>
<div class="field">
<label><?= __('Nickname') ?></label>
<input type="text" name="user-name-nick" value="" />
</div>
</div>
<div class="field">
<label><?= __('Email') ?></label>
<input type="email" name="user-email" value="<?= $user->email ?>" />
</div>
<div class="two fields"> <div class="two fields">
<div class="field"> <div class="field">
@ -218,41 +211,6 @@ $page->navigation();
</div> </div>
</div> </div>
<div class="two fields">
<div class="field">
<label><?= __('Birthdate') ?></label>
<div class="ui calendar">
<div class="ui input left icon">
<i class="calendar icon"></i>
<input type="text"
name="user-birthdate"
placeholder="<?= __('Pick a date') ?>"
value="<?= $user->birthdate ?>"
/>
</div>
</div>
</div>
<div class="field">
<label><?= __('Language') ?></label>
<select class="ui search dropdown" name="user-locale">
<?php if (!in_array('en', $locales)) { ?>
<option value="<?= 'en' ?>"><?= \Locale::getDisplayName('en', $user->locale) ?></option>
<?php } ?>
<?php foreach ($locales as $locale) { ?>
<?php if ($locale === $user->locale) { ?>
<option value="<?= $locale ?>" selected><?= \Locale::getDisplayName($locale, $user->locale) ?></option>
<?php } else { ?>
<option value="<?= $locale ?>"><?= \Locale::getDisplayName($locale, $user->locale) ?></option>
<?php } ?>
<?php } ?>
</select>
</div>
</div>
<div class="ui error message"></div> <div class="ui error message"></div>
<input class="ui primary button" <input class="ui primary button"
@ -265,68 +223,14 @@ $page->navigation();
</div> </div>
</div> </div>
<div class="ui tab" data-tab="password"> <div class="ui tab" data-tab="preferences">
<div class="ui segment"> <div class="ui segment">
<form class="ui form" method="POST"> <form class="ui form" method="POST">
<input type="hidden" name="user-id" value="<?= $user->id ?>" /> <input type="hidden" name="user-id" value="<?= $user->id ?>" />
<input type="hidden" name="section" value="preferences" />
<div class="three fields">
<div class="field">
<label><?= __('First name') ?></label>
<input type="text" name="user-name-first" value="" />
</div>
<div class="field">
<label><?= __('Last name') ?></label>
<input type="text" name="user-name-last" value="" />
</div>
<div class="field">
<label><?= __('Nickname') ?></label>
<input type="text" name="user-name-nick" value="" />
</div>
</div>
<div class="field">
<label><?= __('Email') ?></label>
<input type="email" name="user-email" value="<?= $user->email ?>" />
</div>
<div class="two fields"> <div class="two fields">
<div class="field">
<label><?= __('Password') ?></label>
<input type="password" name="user-password" autocomplete="new-password" />
</div>
<div class="field">
<label><?= __('Password (repeat)') ?></label>
<input type="password" name="user-password-repeat" autocomplete="new-password" />
</div>
</div>
<div class="two fields">
<div class="field">
<label><?= __('Birthdate') ?></label>
<div class="ui calendar">
<div class="ui input left icon">
<i class="calendar icon"></i>
<input type="text"
name="user-birthdate"
placeholder="<?= __('Pick a date') ?>"
value="<?= $user->birthdate ?>"
/>
</div>
</div>
</div>
<div class="field"> <div class="field">
<label><?= __('Language') ?></label> <label><?= __('Language') ?></label>
@ -360,10 +264,6 @@ $page->navigation();
</div> </div>
</div> </div>
<div class="ui segment">
asdf
</div>
</div> </div>
</main> </main>

View file

@ -8,29 +8,50 @@
use wishthis\{Page, User}; use wishthis\{Page, User};
$page = new Page(__FILE__, __('Update'), 100);
/** /**
* Update * Update
*/ */
if ('POST' === $_SERVER['REQUEST_METHOD']) { if ('POST' === $_SERVER['REQUEST_METHOD']) {
/** $versions_directory = ROOT . '/src/update';
* Database $versions_contents = scandir($versions_directory);
*/ $versions = array();
/** 0.5.0 *//* foreach ($versions_contents as $filename) {
if (-1 === version_compare($options->version, '0.5.0')) { $filepath = $versions_directory . '/' . $filename;
$database->query('ALTER TABLE `users` $pathinfo = pathinfo($filepath);
ADD `status` VARCHAR(32) NOT NULL AFTER `url`
;'); if ('sql' === $pathinfo['extension']) {
}*/ $versions[] = array(
'version' => str_replace('-', '.', $pathinfo['filename']),
'filepath' => $filepath,
);
}
}
foreach ($versions as $version) {
if (-1 !== version_compare(VERSION, $version['version'])) {
$sql = file_get_contents($version['filepath']);
if ($sql) {
$database->query($sql);
$page->messages[] = Page::info(
sprintf(
__('Database successfully migrated to %s.'),
'v' . $version['version']
)
);
}
}
}
/** Update version */ /** Update version */
$options->setOption('version', VERSION); $options->setOption('version', VERSION);
$options->setOption('updateAvailable', false); $options->setOption('updateAvailable', false);
redirect('/?page=home');
} }
$page = new Page(__FILE__, __('Update'), 100);
$page->header(); $page->header();
$page->bodyStart(); $page->bodyStart();
$page->navigation(); $page->navigation();
@ -40,6 +61,8 @@ $page->navigation();
<div class="ui container"> <div class="ui container">
<h1 class="ui header"><?= $page->title ?></h1> <h1 class="ui header"><?= $page->title ?></h1>
<?= $page->messages() ?>
<div class="ui segment"> <div class="ui segment">
<h2 class="ui header"><?= __('Database migration') ?></h2> <h2 class="ui header"><?= __('Database migration') ?></h2>
<p><?= __('Thank you for updating withthis! To complete this update, some changes are required to the database structure.') ?></p> <p><?= __('Thank you for updating withthis! To complete this update, some changes are required to the database structure.') ?></p>

5
src/update/0-5-0.sql Normal file
View file

@ -0,0 +1,5 @@
ALTER TABLE `users`
ADD COLUMN `name_first` VARCHAR(32) NULL DEFAULT NULL,
ADD COLUMN `name_last` VARCHAR(32) NULL DEFAULT NULL,
ADD COLUMN `name_nick` VARCHAR(32) NULL DEFAULT NULL
;