diff --git a/assets/less/mio/classes/profile/header.less b/assets/less/mio/classes/profile/header.less index f4f4b00a..f44f5da0 100644 --- a/assets/less/mio/classes/profile/header.less +++ b/assets/less/mio/classes/profile/header.less @@ -46,6 +46,7 @@ border-radius: 2px; line-height: 1.2em; padding: 1px 5px 4px; + cursor: default; } } @@ -98,6 +99,7 @@ color: inherit; text-decoration: none; padding: 10px; + cursor: default; &--date { min-width: 130px; @@ -106,11 +108,13 @@ &__name { font-size: .9em; font-variant: small-caps; + cursor: inherit; } &__value { font-size: 1.3em; text-align: right; + cursor: inherit; } } diff --git a/database/2018_09_17_195802_add_relations_table.php b/database/2018_09_17_195802_add_relations_table.php new file mode 100644 index 00000000..9d57e1af --- /dev/null +++ b/database/2018_09_17_195802_add_relations_table.php @@ -0,0 +1,33 @@ +exec(' + CREATE TABLE `msz_user_relations` ( + `user_id` INT(10) UNSIGNED NOT NULL, + `subject_id` INT(10) UNSIGNED NOT NULL, + `relation_type` TINYINT(3) UNSIGNED NOT NULL, + `relation_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE INDEX `user_relations_unique` (`user_id`, `subject_id`), + INDEX `user_relations_subject_id_foreign` (`subject_id`), + CONSTRAINT `user_relations_subject_id_foreign` + FOREIGN KEY (`subject_id`) + REFERENCES `msz_users` (`user_id`) + ON UPDATE CASCADE + ON DELETE CASCADE, + CONSTRAINT `user_relations_user_id_foreign` + FOREIGN KEY (`user_id`) + REFERENCES `msz_users` (`user_id`) + ON UPDATE CASCADE + ON DELETE CASCADE + ) + '); +} + +function migrate_down(PDO $conn): void +{ + $conn->exec('DROP TABLE `msz_user_relations`'); +} diff --git a/misuzu.php b/misuzu.php index 05baf9d4..dae0df08 100644 --- a/misuzu.php +++ b/misuzu.php @@ -42,6 +42,7 @@ require_once __DIR__ . '/src/Forum/topic.php'; require_once __DIR__ . '/src/Forum/validate.php'; require_once __DIR__ . '/src/Users/login_attempt.php'; require_once __DIR__ . '/src/Users/profile.php'; +require_once __DIR__ . '/src/Users/relations.php'; require_once __DIR__ . '/src/Users/role.php'; require_once __DIR__ . '/src/Users/session.php'; require_once __DIR__ . '/src/Users/user.php'; diff --git a/public/profile.php b/public/profile.php index e6b60cb8..e560d674 100644 --- a/public/profile.php +++ b/public/profile.php @@ -94,6 +94,36 @@ switch ($mode) { break; } + if ($app->hasActiveSession()) { + $getFriendInfo = Database::prepare(' + SELECT + :visitor as `visitor`, :profile as `profile`, + ( + SELECT `relation_type` + FROM `msz_user_relations` + WHERE `user_id` = `visitor` + AND `subject_id` = `profile` + ) as `visitor_relation`, + ( + SELECT `relation_type` + FROM `msz_user_relations` + WHERE `subject_id` = `visitor` + AND `user_id` = `profile` + ) as `profile_relation`, + ( + SELECT MAX(`relation_created`) + FROM `msz_user_relations` + WHERE (`user_id` = `visitor` AND `subject_id` = `profile`) + OR (`user_id` = `profile` AND `subject_id` = `visitor`) + ) as `relation_created` + '); + $getFriendInfo->bindValue('visitor', $app->getUserId()); + $getFriendInfo->bindValue('profile', $profile['user_id']); + $friendInfo = $getFriendInfo->execute() ? $getFriendInfo->fetch(PDO::FETCH_ASSOC) : []; + + tpl_var('friend_info', $friendInfo); + } + tpl_vars([ 'profile' => $profile, 'profile_fields' => $app->hasActiveSession() ? user_profile_fields_display($profile) : [], diff --git a/public/relations.php b/public/relations.php new file mode 100644 index 00000000..1e7165cf --- /dev/null +++ b/public/relations.php @@ -0,0 +1,41 @@ +hasActiveSession()) { + echo render_error(403); + return; +} + +$subjectId = (int)($_GET['u'] ?? 0); + +switch ($_GET['m'] ?? null) { + case 'add': + switch ($_GET['t'] ?? null) { + case 'follow': + default: + $type = MSZ_USER_RELATION_FOLLOW; + break; + } + + if (user_relation_add($app->getUserId(), $subjectId, $type) !== MSZ_USER_RELATION_E_OK) { + echo render_error(500); + return; + } + break; + + case 'remove': + if (!user_relation_remove($app->getUserId(), $subjectId)) { + echo render_error(500); + return; + } + break; +} + +header('Location: ' . $_SERVER['HTTP_REFERER']); diff --git a/src/Users/relations.php b/src/Users/relations.php new file mode 100644 index 00000000..9d0f0414 --- /dev/null +++ b/src/Users/relations.php @@ -0,0 +1,56 @@ +bindValue('user_id', $userId); + $addRelation->bindValue('subject_id', $subjectId); + $addRelation->bindValue('type', $type); + $addRelation->execute(); + + return $addRelation->execute() + ? MSZ_USER_RELATION_E_OK + : MSZ_USER_RELATION_E_DATABASE; +} + +function user_relation_remove(int $userId, int $subjectId): bool +{ + if ($userId < 1 || $subjectId < 1) { + return false; + } + + $removeRelation = Database::prepare(' + DELETE FROM `msz_user_relations` + WHERE `user_id` = :user_id + AND `subject_id` = :subject_id + '); + $removeRelation->bindValue('user_id', $userId); + $removeRelation->bindValue('subject_id', $subjectId); + + return $removeRelation->execute(); +} diff --git a/templates/user/profile.twig b/templates/user/profile.twig index 37263a8f..c53b6f67 100644 --- a/templates/user/profile.twig +++ b/templates/user/profile.twig @@ -38,18 +38,29 @@ - + {% if friend_info is defined and (friend_info.profile_relation is not null or friend_info.visitor_relation is not null) %} +