Added baseline for birthday field.

This commit is contained in:
flash 2019-01-18 15:11:15 +01:00
parent a03da59888
commit 205b019cef
15 changed files with 219 additions and 17 deletions

View file

@ -1,5 +1,4 @@
.profile__about {
margin-bottom: 2px;
&__content {
max-height: 1000px;

View file

@ -1,5 +1,4 @@
.profile__accounts {
margin-bottom: 2px;
&__content {
display: flex;

View file

@ -1,5 +1,4 @@
.profile__background-settings {
margin-bottom: 2px;
&__content {
display: flex;

View file

@ -0,0 +1,19 @@
.profile__birthdate {
&__content {
padding: 2px 5px;
}
&__select {
min-width: auto;
}
&__label {
display: inline-block;
}
&__title {
font-size: .9em;
line-height: 1.8em;
}
}

View file

@ -0,0 +1,3 @@
.profile__container {
margin-bottom: 2px;
}

View file

@ -3,7 +3,6 @@
flex-wrap: auto;
justify-content: space-evenly;
padding: 2px;
margin-bottom: 2px;
@media (max-width: @site-mobile-width) {
flex-direction: column;

View file

@ -1,6 +1,6 @@
.profile {
&__container {
&__content {
display: flex;
&__main {

View file

@ -132,6 +132,7 @@ html {
@import "classes/manage/blacklist";
// Profile
@import "classes/profile/container";
@import "classes/profile/profile";
@import "classes/profile/header";
@import "classes/profile/accounts";
@ -139,6 +140,7 @@ html {
@import "classes/profile/guidelines";
@import "classes/profile/background-settings";
@import "classes/profile/warning";
@import "classes/profile/birthdate";
// Changelog
@import "classes/changelog";

View file

@ -0,0 +1,22 @@
<?php
namespace Misuzu\DatabaseMigrations\AddBirthdayField;
use PDO;
function migrate_up(PDO $conn): void
{
$conn->exec("
ALTER TABLE `msz_users`
ADD COLUMN `user_birthdate` DATE NULL DEFAULT NULL AFTER `user_about_parser`,
DROP INDEX `users_indices`,
ADD INDEX `users_indices` (`user_country`, `user_created`, `user_active`, `user_deleted`, `user_birthdate`);
");
}
function migrate_down(PDO $conn): void
{
$conn->exec("
ALTER TABLE `msz_users`
DROP COLUMN `user_birthdate`;
");
}

View file

@ -65,6 +65,10 @@ $changelog = cache_get('index:changelog:v1', function () {
')->fetchAll(PDO::FETCH_ASSOC);
}, 300);
$birthdays = user_session_active()
? cache_get('index:birthdays:v1', user_get_birthdays(), 300)
: [];
$latestUser = cache_get('index:latest_user:v1', function () {
return db_query('
SELECT
@ -97,6 +101,7 @@ echo tpl_render('home.' . (user_session_active() ? 'home' : 'landing'), [
'statistics' => $stats,
'latest_user' => $latestUser,
'online_users' => $onlineUsers,
'birthdays' => $birthdays,
'featured_changelog' => $changelog,
'featured_news' => $news,
]);

View file

@ -145,6 +145,7 @@ switch ($mode) {
'edit_avatar' => perms_check($userPerms, MSZ_PERM_USER_CHANGE_AVATAR),
'edit_background' => perms_check($userPerms, MSZ_PERM_USER_CHANGE_BACKGROUND),
'edit_about' => perms_check($userPerms, MSZ_PERM_USER_EDIT_ABOUT),
'edit_birthdate' => perms_check($userPerms, MSZ_PERM_USER_EDIT_BIRTHDATE),
];
tpl_vars([
@ -197,6 +198,38 @@ switch ($mode) {
}
}
if (!empty($_POST['birthdate']) && is_array($_POST['birthdate'])) {
if (!$perms['edit_birthdate']) {
$notices[] = "You aren't allow to change your birthdate.";
} else {
$setBirthdate = user_set_birthdate(
$userId,
(int)($_POST['birthdate']['day'] ?? 0),
(int)($_POST['birthdate']['month'] ?? 0),
(int)($_POST['birthdate']['year'] ?? 0)
);
switch ($setBirthdate) {
case MSZ_E_USER_BIRTHDATE_USER:
$notices[] = 'Invalid user specified while setting birthdate?';
break;
case MSZ_E_USER_BIRTHDATE_DATE:
$notices[] = 'The given birthdate is invalid.';
break;
case MSZ_E_USER_BIRTHDATE_FAIL:
$notices[] = 'Failed to set birthdate.';
break;
case MSZ_E_USER_BIRTHDATE_YEAR:
$notices[] = 'The given birth year is invalid.';
break;
case MSZ_E_USER_BIRTHDATE_OK:
break;
default:
$notices[] = 'Something unexpected happened while setting your birthdate.';
}
}
}
if (!empty($_FILES['avatar'])) {
if (!empty($_POST['avatar']['delete'])) {
user_avatar_delete($userId);
@ -306,7 +339,7 @@ switch ($mode) {
sprintf(
'
SELECT
u.`user_id`, u.`username`, u.`user_country`,
u.`user_id`, u.`username`, u.`user_country`, u.`user_birthdate`,
u.`user_created`, u.`user_active`,
u.`user_about_parser`, u.`user_about_content`, u.`user_background_settings`,
%1$s,

View file

@ -7,6 +7,7 @@ define('MSZ_PERM_USER_EDIT_PROFILE', 1);
define('MSZ_PERM_USER_CHANGE_AVATAR', 1 << 1);
define('MSZ_PERM_USER_CHANGE_BACKGROUND', 1 << 2);
define('MSZ_PERM_USER_EDIT_ABOUT', 1 << 3);
define('MSZ_PERM_USER_EDIT_BIRTHDATE', 1 << 4);
define('MSZ_PERM_USER_MANAGE_USERS', 1 << 20);
define('MSZ_PERM_USER_MANAGE_ROLES', 1 << 21);
@ -191,6 +192,68 @@ function user_check_authority(int $userId, int $subjectId): bool
return (bool)($checkHierarchy->execute() ? $checkHierarchy->fetchColumn() : false);
}
define('MSZ_E_USER_BIRTHDATE_OK', 0);
define('MSZ_E_USER_BIRTHDATE_USER', 1);
define('MSZ_E_USER_BIRTHDATE_DATE', 2);
define('MSZ_E_USER_BIRTHDATE_FAIL', 3);
define('MSZ_E_USER_BIRTHDATE_YEAR', 4);
function user_set_birthdate(int $userId, int $day, int $month, int $year, int $yearRange = 100): int
{
if ($userId < 1) {
return MSZ_E_USER_BIRTHDATE_USER;
}
$unset = $day === 0 && $month === 0;
if ($year === 0) {
$checkYear = date('Y');
} else {
echo $year;
if ($year < date('Y') - $yearRange || $year > date('Y')) {
return MSZ_E_USER_BIRTHDATE_YEAR;
}
$checkYear = $year;
}
if (!$unset && !checkdate($month, $day, $checkYear)) {
return MSZ_E_USER_BIRTHDATE_DATE;
}
$birthdate = $unset ? null : implode('-', [$year, $month, $day]);
$setBirthdate = db_prepare('
UPDATE `msz_users`
SET `user_birthdate` = :birthdate
WHERE `user_id` = :user
');
$setBirthdate->bindValue('birthdate', $birthdate);
$setBirthdate->bindValue('user', $userId);
return $setBirthdate->execute()
? MSZ_E_USER_BIRTHDATE_OK
: MSZ_E_USER_BIRTHDATE_FAIL;
}
function user_get_birthdays(int $day = 0, int $month = 0)
{
if ($day < 1 || $month < 1) {
$date = date('%-m-d');
} else {
$date = "%-{$month}-{$day}";
}
$getBirthdays = db_prepare('
SELECT `user_id`, `username`, `user_birthdate`,
IF(YEAR(`user_birthdate`) < 1, NULL, YEAR(NOW()) - YEAR(`user_birthdate`)) AS `user_age`
FROM `msz_users`
WHERE `user_deleted` IS NULL
AND `user_birthdate` LIKE :birthdate
');
$getBirthdays->bindValue('birthdate', $date);
return db_fetch_all($getBirthdays);
}
define('MSZ_USER_ABOUT_MAX_LENGTH', 0xFFFF);
define('MSZ_USER_ABOUT_OK', 0);

View file

@ -13,7 +13,6 @@ function manage_get_menu(int $userId): array
$menu = [];
$menu['General']['Overview'] = '/manage/index.php?v=overview';
$menu['General']['Quotes'] = '/manage/index.php?v=quotes';
if (perms_check($perms['general'], MSZ_PERM_GENERAL_VIEW_LOGS)) {
$menu['General']['Logs'] = '/manage/index.php?v=logs';
@ -221,6 +220,11 @@ function manage_perms_list(array $rawPerms): array
'title' => 'Can change own about section.',
'perm' => MSZ_PERM_USER_EDIT_ABOUT,
],
[
'section' => 'edit-birthdate',
'title' => 'Can change own birthdate.',
'perm' => MSZ_PERM_USER_EDIT_BIRTHDATE,
],
[
'section' => 'manage-users',
'title' => 'Can manage other users.',

View file

@ -92,7 +92,27 @@
</div>
{% endif %}
{% if latest_user.user_id|default(0) > 0 %}
{% if current_user is defined and birthdays|length > 0 %}
<div class="container index__container">
{{ container_title('<i class="fas fa-birthday-cake fa-fw"></i> Happy Birthday!') }}
{% for birthday in birthdays %}
<a class="index__latest" style="{# birthday.user_colour|html_colour #}" href="/profile.php?u={{ birthday.user_id }}">
<div class="avatar index__latest__avatar" style="--avatar-url: url('/profile.php?m=avatar&amp;u={{ birthday.user_id }}')"></div>
<div class="index__latest__content">
<div class="index__latest__username">
{{ birthday.username }}
</div>
{% if birthday.user_age is not null %}
<div class="index__latest__joined">
Turned {{ birthday.user_age }} today!
</div>
{% endif %}
</div>
</a>
{% endfor %}
</div>
{% elseif latest_user.user_id|default(0) > 0 %}
<div class="container index__container">
{{ container_title('<i class="fas fa-user-plus fa-fw"></i> Newest User') }}

View file

@ -66,7 +66,7 @@
{% include 'user/_layout/header.twig' %}
{% if is_editing %}
<div class="container profile__guidelines">
<div class="container profile__container profile__guidelines">
<ul class="profile__guidelines__section">
<li class="profile__guidelines__line profile__guidelines__line--header">General</li>
<li class="profile__guidelines__line">Keep things sane and generally suitable for all ages.</li>
@ -104,15 +104,16 @@
</div>
{% endif %}
<div class="profile__container">
<div class="profile__content">
{% set show_profile_fields = is_editing ? perms.edit_profile : profile_fields|default([])|length > 0 %}
{% set show_background_settings = is_editing and perms.edit_background %}
{% set show_birthdate = is_editing and perms.edit_birthdate %}
{% set show_sidebar = current_user is not defined or show_profile_fields or show_background_settings %}
{% if show_sidebar %}
<div class="profile__container__side">
<div class="profile__content__side">
{% if show_background_settings %}
<div class="container profile__background-settings">
<div class="container profile__container profile__background-settings">
{{ container_title('Background') }}
<div class="profile__background-settings__content">
@ -129,13 +130,13 @@
</div>
{% endif %}
{% if current_user is not defined %}
<div class="container">
<div class="container profile__container">
<div class="profile__accounts__notice">
You must <a href="/auth.php?m=login" class="profile__accounts__link">log in</a> to view full profiles!
</div>
</div>
{% elseif show_profile_fields %}
<div class="container profile__accounts">
<div class="container profile__container profile__accounts">
{{ container_title('Elsewhere') }}
<div class="profile__accounts__content">
@ -163,12 +164,46 @@
</div>
</div>
{% endif %}
{% if show_birthdate %}
<div class="container profile__container profile__birthdate">
{{ container_title('Birthdate') }}
{% set birthdate = profile.user_birthdate|split('-') %}
<div class="profile__birthdate__content">
<div class="profile__birthdate__date">
<label class="profile__birthdate__label">
<div class="profile__birthdate__title">
Day
</div>
{{ input_select('birthdate[day]', ['-']|merge(range(1, 31)), birthdate[2]|default(0)|trim('0'), '', '', true, 'profile__birthdate__select profile__birthdate__select--day') }}
</label>
<label class="profile__birthdate__label">
<div class="profile__birthdate__title">
Month
</div>
{{ input_select('birthdate[month]', ['-']|merge(range(1, 12)), birthdate[1]|default(0)|trim('0'), '', '', true, 'profile__birthdate__select profile__birthdate__select--month') }}
</label>
</div>
<div class="profile__birthdate__year">
<label class="profile__birthdate__label">
<div class="profile__birthdate__title">
Year (may be left empty)
</div>
{{ input_select('birthdate[year]', ['-']|merge(range(null|date('Y'), null|date('Y') - 100)), birthdate[0]|default(0)|trim('0'), '', '', true, 'profile__birthdate__select profile__birthdate__select--year') }}
</label>
</div>
</div>
</div>
{% endif %}
</div>
{% endif %}
<div class="profile__container__main">
<div class="profile__content__main">
{% if (is_editing and perms.edit_about) or profile.user_about_content|length > 0 %}
<div class="container profile__about" id="about">
<div class="container profile__container profile__about" id="about">
{{ container_title('About ' ~ profile.username) }}
<div class="profile__about__content{% if is_editing %} profile__about__content--edit{% elseif profile.user_about_parser == constant('MSZ_PARSER_MARKDOWN') %} markdown{% endif %}">
@ -183,7 +218,7 @@
{% endif %}
{% if warnings|length > 0 %}
<div class="container profile__warning__container" id="account-standing">
<div class="container profile__container profile__warning__container" id="account-standing">
{{ container_title('Account Standing', false, can_manage_warnings ? '/manage/users.php?v=warnings&u=' ~ profile.user_id : '') }}
<div class="profile__warning">