This commit is contained in:
flash 2018-07-26 20:24:16 +02:00
parent 0693b4a588
commit 3760b49359
3 changed files with 136 additions and 0 deletions

View file

@ -0,0 +1,56 @@
<?php
namespace Misuzu\DatabaseMigrations\AddGlobalCommentsStuff;
use PDO;
function migrate_up(PDO $conn): void
{
$conn->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`');
}

View file

@ -1,7 +1,77 @@
<?php <?php
use Misuzu\Database;
define('MSZ_COMMENTS_PERM_CREATE', 1); define('MSZ_COMMENTS_PERM_CREATE', 1);
define('MSZ_COMMENTS_PERM_EDIT_OWN', 1 << 1); define('MSZ_COMMENTS_PERM_EDIT_OWN', 1 << 1);
define('MSZ_COMMENTS_PERM_EDIT_ANY', 1 << 2); define('MSZ_COMMENTS_PERM_EDIT_ANY', 1 << 2);
define('MSZ_COMMENTS_PERM_DELETE_OWN', 1 << 3); define('MSZ_COMMENTS_PERM_DELETE_OWN', 1 << 3);
define('MSZ_COMMENTS_PERM_DELETE_ANY', 1 << 4); define('MSZ_COMMENTS_PERM_DELETE_ANY', 1 << 4);
define('MSZ_COMMENTS_PERM_PIN', 1 << 5); define('MSZ_COMMENTS_PERM_PIN', 1 << 5);
define('MSZ_COMMENTS_PERM_LOCK', 1 << 6);
function comments_category_create(string $name): int
{
$create = Database::prepare('
INSERT INTO `msz_comments_categories`
(`category_name`)
VALUES
(:name)
');
$create->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;
}

View file

@ -430,6 +430,16 @@ function manage_perms_list(array $rawPerms): array
$rawPerms['comments_perms_deny'] $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']
),
],
], ],
], ],
[ [