From 74306ec5be1b0453d19025fb2bee7f9bd9c8eabb Mon Sep 17 00:00:00 2001 From: Jay Trees Date: Thu, 7 Apr 2022 10:31:23 +0200 Subject: [PATCH] Redesign profile page Added new fields: - First name - Last name - Nickname --- index.php | 2 +- src/pages/install.php | 5 +- src/pages/profile.php | 290 ++++++++++++++---------------------------- src/pages/update.php | 47 +++++-- src/update/0-5-0.sql | 5 + 5 files changed, 140 insertions(+), 209 deletions(-) create mode 100644 src/update/0-5-0.sql diff --git a/index.php b/index.php index 18f5f9ec..69d109dd 100644 --- a/index.php +++ b/index.php @@ -6,7 +6,7 @@ * @author Jay Trees */ -define('VERSION', '0.4.0'); +define('VERSION', '0.5.0'); define('ROOT', __DIR__); define('DEFAULT_LOCALE', 'en_GB'); diff --git a/src/pages/install.php b/src/pages/install.php index 51431e70..28c469cb 100644 --- a/src/pages/install.php +++ b/src/pages/install.php @@ -133,7 +133,10 @@ switch ($step) { `last_login` DATETIME NOT NULL DEFAULT NOW(), `power` INT NOT NULL DEFAULT 0, `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`);'); diff --git a/src/pages/profile.php b/src/pages/profile.php index cc4fbb34..2bec5309 100644 --- a/src/pages/profile.php +++ b/src/pages/profile.php @@ -8,49 +8,96 @@ use wishthis\{Page, User}; -if (isset($_POST['user-id'])) { - $set = array(); +$page = new Page(__FILE__, __('Profile')); - $loginRequired = false; +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; - if (!empty($_POST['user-email'])) { - $set[] = '`email` = "' . $_POST['user-email'] . '"'; + foreach ($formFieldsString as $field) { + 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!'), + '' . $field['label'] . '' + ), + __('Success') + ); + } } - if (!empty($_POST['user-password']) && !empty($_POST['user-password-repeat'])) { + if (!empty($_POST['user-email']) && $_POST['user-email'] !== $user->email) { + $loginRequired = true; + } + + if (isset($_POST['user-birthdate'])) { + if (empty($_POST['user-birthdate'])) { + $user->birthdate = null; + + $set[] = '`birthdate` = NULL'; + } else { + $user->birthdate = date('Y-m-d', strtotime($_POST['user-birthdate'])); + + $set[] = '`birthdate` = "' . $user->birthdate . '"'; + } + } + + 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; } - if ($_POST['user-birthdate']) { - $user->birthdate = date('Y-m-d', strtotime($_POST['user-birthdate'])); - - $set[] = '`birthdate` = "' . $user->birthdate . '"'; - } else { - $user->birthdate = null; - - $set[] = '`birthdate` = NULL'; - } - - $set[] = '`locale` = "' . $_POST['user-locale'] . '"'; - $database ->query('UPDATE `users` SET ' . implode(',', $set) . ' WHERE `id` = ' . $_POST['user-id']); - - if ($_POST['user-email'] !== $_SESSION['user']['email']) { - $loginRequired = true; - } - if ($loginRequired) { 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->bodyStart(); $page->navigation(); @@ -60,20 +107,22 @@ $page->navigation();

title ?>

+ messages() ?> +
@@ -84,48 +133,35 @@ $page->navigation();
+
- +
- +
- -
-
- -
- - - -
- -
-
- - - -
- -
- - - +
+
+ + + +
+
@@ -140,24 +176,6 @@ $page->navigation();
- -
- - - -
@@ -177,32 +195,7 @@ $page->navigation(); - -
-
- - - -
- -
- - - -
- -
- - - -
-
- -
- - - -
+
@@ -218,41 +211,6 @@ $page->navigation();
-
-
- - -
-
- - -
-
-
- -
- - - -
-
-
navigation();
-
+
- -
-
- - - -
- -
- - - -
- -
- - - -
-
- -
- - - -
+
-
- - - -
- -
- - - -
-
- -
-
- - -
-
- - -
-
-
-
@@ -360,10 +264,6 @@ $page->navigation();
- -
- asdf -
diff --git a/src/pages/update.php b/src/pages/update.php index 1d3f464c..54dd18d2 100644 --- a/src/pages/update.php +++ b/src/pages/update.php @@ -8,29 +8,50 @@ use wishthis\{Page, User}; +$page = new Page(__FILE__, __('Update'), 100); + /** * Update */ if ('POST' === $_SERVER['REQUEST_METHOD']) { - /** - * Database - */ + $versions_directory = ROOT . '/src/update'; + $versions_contents = scandir($versions_directory); + $versions = array(); - /** 0.5.0 *//* - if (-1 === version_compare($options->version, '0.5.0')) { - $database->query('ALTER TABLE `users` - ADD `status` VARCHAR(32) NOT NULL AFTER `url` - ;'); - }*/ + foreach ($versions_contents as $filename) { + $filepath = $versions_directory . '/' . $filename; + $pathinfo = pathinfo($filepath); + + 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 */ $options->setOption('version', VERSION); $options->setOption('updateAvailable', false); - - redirect('/?page=home'); } -$page = new Page(__FILE__, __('Update'), 100); $page->header(); $page->bodyStart(); $page->navigation(); @@ -40,6 +61,8 @@ $page->navigation();

title ?>

+ messages() ?> +

diff --git a/src/update/0-5-0.sql b/src/update/0-5-0.sql new file mode 100644 index 00000000..8430d93d --- /dev/null +++ b/src/update/0-5-0.sql @@ -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 +;