From 3760b493595945674acd6903e2f5aac91962ea7a Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 26 Jul 2018 20:24:16 +0200 Subject: [PATCH] a --- ...07_25_194106_add_global_comments_stuff.php | 56 +++++++++++++++ src/comments.php | 70 +++++++++++++++++++ src/manage.php | 10 +++ 3 files changed, 136 insertions(+) create mode 100644 database/2018_07_25_194106_add_global_comments_stuff.php diff --git a/database/2018_07_25_194106_add_global_comments_stuff.php b/database/2018_07_25_194106_add_global_comments_stuff.php new file mode 100644 index 00000000..07be5214 --- /dev/null +++ b/database/2018_07_25_194106_add_global_comments_stuff.php @@ -0,0 +1,56 @@ +exec(' + CREATE TABLE `msz_comments_categories` ( + `category_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, + `category_name` VARCHAR(255) NOT NULL, + `category_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `category_locked` TIMESTAMP NULL DEFAULT NULL, + PRIMARY KEY (`category_id`), + UNIQUE INDEX `comments_categories_name_unique` (`category_name`) + ); + '); + + $conn->exec(' + CREATE TABLE `msz_comments_posts` ( + `comment_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, + `category_id` INT(10) UNSIGNED NOT NULL, + `user_id` INT(10) UNSIGNED NULL DEFAULT NULL, + `comment_reply_to` INT(10) UNSIGNED NULL DEFAULT NULL, + `comment_text` TEXT NOT NULL, + `comment_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `comment_edited` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, + `comment_deleted` TIMESTAMP NULL DEFAULT NULL, + PRIMARY KEY (`comment_id`), + INDEX `comments_posts_category_foreign` (`category_id`), + INDEX `comments_posts_user_foreign` (`user_id`), + INDEX `comments_posts_reply_id` (`comment_reply_to`), + INDEX `comments_posts_dates` ( + `comment_created`, + `comment_edited`, + `comment_deleted` + ), + CONSTRAINT `comments_posts_category_foreign` + FOREIGN KEY (`category_id`) + REFERENCES `msz_comments_categories` (`category_id`) + ON UPDATE CASCADE + ON DELETE CASCADE, + CONSTRAINT `comments_posts_user_foreign` + FOREIGN KEY (`user_id`) + REFERENCES `msz_users` (`user_id`) + ON UPDATE CASCADE + ON DELETE SET NULL + ); + '); +} + +function migrate_down(PDO $conn): void +{ + $conn->exec('DROP TABLE `msz_comments_posts`'); + $conn->exec('DROP TABLE `msz_comments_categories`'); +} diff --git a/src/comments.php b/src/comments.php index b1ae2812..03f944c2 100644 --- a/src/comments.php +++ b/src/comments.php @@ -1,7 +1,77 @@ bindValue('name', $name); + return $create->execute() ? Database::lastInsertId() : 0; +} + +function comments_category_lock(int $category, bool $lock): void +{ + $lock = Database::prepare(' + UPDATE `msz_comments_categories` + SET `category_locked` = IF(:lock, NOW(), NULL) + WHERE `category_id` = :category + '); + $lock->bindValue('category', $category); + $lock->bindValue('lock', $lock ? 1 : 0); + $lock->execute(); +} + +function comments_category_exists(string $name): bool +{ + $exists = Database::prepare(' + SELECT COUNT(`category_name`) > 0 + FROM `msz_comments_categories` + WHERE `category_name` = :name + '); + $exists->bindValue('name', $name); + return $exists->execute() ? (bool)$exists->fetchColumn() : false; +} + +function comments_category_get(int $category): array +{ + $posts = Database::prepare(' + SELECT + p.`comment_id`, p.`comment_text`, + u.`user_id`, u.`username`, + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` + FROM `msz_comments_posts` as p + LEFT JOIN `msz_users` as u + ON u.`user_id` = p.`user_id` + LEFT JOIN `msz_roles` as r + ON r.`role_id` = u.`display_role` + WHERE c.`category_id` = :category + '); + $posts->bindValue('category', $category); + return $posts->execute() ? $posts->fetchAll(PDO::FETCH_ASSOC) : []; +} + +function comments_post_create(int $user, int $category, string $text): int +{ + $create = Database::prepare(' + INSERT INTO `msz_comments_posts` + (`user_id`, `category_id`, `comment_text`) + VALUES + (:user, :category, :text) + '); + $create->bindValue('user', $user); + $create->bindValue('category', $category); + $create->bindValue('text', $text); + return $create->execute() ? Database::lastInsertId() : 0; +} diff --git a/src/manage.php b/src/manage.php index 43193790..bc37d58c 100644 --- a/src/manage.php +++ b/src/manage.php @@ -430,6 +430,16 @@ function manage_perms_list(array $rawPerms): array $rawPerms['comments_perms_deny'] ), ], + [ + 'section' => 'lock', + 'title' => 'Can lock comment threads.', + 'perm' => MSZ_COMMENTS_PERM_LOCK, + 'value' => manage_perms_value( + MSZ_COMMENTS_PERM_LOCK, + $rawPerms['comments_perms_allow'], + $rawPerms['comments_perms_deny'] + ), + ], ], ], [