From 2a301de29feb08d40a9740a574dbef8b07bd595c Mon Sep 17 00:00:00 2001 From: Michael Save Date: Sat, 21 Apr 2012 15:15:32 +1000 Subject: [PATCH] Paginate ?/noticeboard --- inc/config.php | 4 +- .../Twig/Extensions/Extension/Tinyboard.php | 1 + inc/mod/pages.php | 55 ++++++++++++++-- mod.php | 6 +- templates/mod/ban_list.html | 2 +- templates/mod/log.html | 2 +- templates/mod/noticeboard.html | 65 +++++++++++++++++++ 7 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 templates/mod/noticeboard.html diff --git a/inc/config.php b/inc/config.php index 8d93f266..06e100ea 100644 --- a/inc/config.php +++ b/inc/config.php @@ -832,8 +832,8 @@ // Maximum number of results to display for a search, per board $config['mod']['search_results'] = 75; - // Maximum number of notices to display on the moderator noticeboard - $config['mod']['noticeboard_display'] = 50; + // How many entries to show per page in the moderator noticeboard + $config['mod']['noticeboard_page'] = 50; // Number of entries to summarize and display on the dashboard $config['mod']['noticeboard_dashboard'] = 5; diff --git a/inc/lib/Twig/Extensions/Extension/Tinyboard.php b/inc/lib/Twig/Extensions/Extension/Tinyboard.php index df652444..c4810dad 100644 --- a/inc/lib/Twig/Extensions/Extension/Tinyboard.php +++ b/inc/lib/Twig/Extensions/Extension/Tinyboard.php @@ -37,6 +37,7 @@ class Twig_Extensions_Extension_Tinyboard extends Twig_Extension { return Array( 'time' => new Twig_Filter_Function('time'), + 'floor' => new Twig_Filter_Function('floor'), 'timezone' => new Twig_Filter_Function('twig_timezone_function'), 'hiddenInputs' => new Twig_Filter_Function('hiddenInputs'), 'hiddenInputsHash' => new Twig_Filter_Function('hiddenInputsHash'), diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 3754268d..d111a480 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -67,14 +67,14 @@ function mod_dashboard() { $args['boards'] = listBoards(); - if(hasPermission($config['mod']['noticeboard'])) { - if(!$config['cache']['enabled'] || !$args['noticeboard'] = cache::get('noticeboard_preview')) { + if (hasPermission($config['mod']['noticeboard'])) { + if (!$config['cache']['enabled'] || !$args['noticeboard'] = cache::get('noticeboard_preview')) { $query = prepare("SELECT `noticeboard`.*, `username` FROM `noticeboard` LEFT JOIN `mods` ON `mods`.`id` = `mod` ORDER BY `id` DESC LIMIT :limit"); $query->bindValue(':limit', $config['mod']['noticeboard_dashboard'], PDO::PARAM_INT); $query->execute() or error(db_error($query)); $args['noticeboard'] = $query->fetchAll(PDO::FETCH_ASSOC); - if($config['cache']['enabled']) + if ($config['cache']['enabled']) cache::set('noticeboard_preview', $args['noticeboard']); } } @@ -82,6 +82,47 @@ function mod_dashboard() { mod_page('Dashboard', 'mod/dashboard.html', $args); } +function mod_noticeboard($page_no = 1) { + global $config, $pdo, $mod; + + if (!hasPermission($config['mod']['noticeboard'])) + error($config['error']['noaccess']); + + if (isset($_POST['subject'], $_POST['body'])) { + if (!hasPermission($config['mod']['noticeboard_post'])) + error($config['error']['noaccess']); + + markup($_POST['body']); + + $query = prepare('INSERT INTO `noticeboard` VALUES (NULL, :mod, :time, :subject, :body)'); + $query->bindValue(':mod', $mod['id']); + $query->bindvalue(':time', time()); + $query->bindValue(':subject', $_POST['subject']); + $query->bindValue(':body', $_POST['body']); + $query->execute() or error(db_error($query)); + + if($config['cache']['enabled']) + cache::delete('noticeboard_preview'); + + header('Location: ?/noticeboard#' . $pdo->lastInsertId(), true, $config['redirect_http']); + } + + $query = prepare("SELECT `noticeboard`.*, `username` FROM `noticeboard` LEFT JOIN `mods` ON `mods`.`id` = `mod` ORDER BY `id` DESC LIMIT :offset, :limit"); + $query->bindValue(':limit', $config['mod']['noticeboard_page'], PDO::PARAM_INT); + $query->bindValue(':offset', ($page_no - 1) * $config['mod']['noticeboard_page'], PDO::PARAM_INT); + $query->execute() or error(db_error($query)); + $noticeboard = $query->fetchAll(PDO::FETCH_ASSOC); + + if (empty($noticeboard)) + error($config['error']['404']); + + $query = prepare("SELECT COUNT(*) FROM `noticeboard`"); + $query->execute() or error(db_error($query)); + $count = $query->fetchColumn(0); + + mod_page('Noticeboard', 'mod/noticeboard.html', array('noticeboard' => $noticeboard, 'count' => $count)); +} + function mod_log($page_no = 1) { global $config; @@ -94,7 +135,10 @@ function mod_log($page_no = 1) { $query->execute() or error(db_error($query)); $logs = $query->fetchAll(PDO::FETCH_ASSOC); - $query = prepare("SELECT COUNT(*) AS `count` FROM `modlogs`"); + if (empty($logs)) + error($config['error']['404']); + + $query = prepare("SELECT COUNT(*) FROM `modlogs`"); $query->execute() or error(db_error($query)); $count = $query->fetchColumn(0); @@ -300,6 +344,9 @@ function mod_bans($page_no = 1) { $query->execute() or error(db_error($query)); $bans = $query->fetchAll(PDO::FETCH_ASSOC); + if (empty($bans)) + error($config['error']['404']); + $query = prepare("SELECT COUNT(*) FROM `bans`"); $query->execute() or error(db_error($query)); $count = $query->fetchColumn(0); diff --git a/mod.php b/mod.php index b5edd255..4495ae52 100644 --- a/mod.php +++ b/mod.php @@ -24,8 +24,6 @@ $pages = array( '!^$!' => ':?/', // redirect to dashboard '!^/$!' => 'dashboard', // dashboard '!^/confirm/(.+)$!' => 'confirm', // confirm action (if javascript didn't work) - '!^/log$!' => 'log', // modlog - '!^/log/(\d+)$!' => 'log', // modlog '!^/users$!' => 'users', // manage users '!^/users/(\d+)$!' => 'user', // edit user @@ -33,6 +31,10 @@ $pages = array( '!^/new_PM/([^/]+)$!' => 'new_pm', // create a new pm '!^/PM/(\d+)(/reply)?$!' => 'pm', // read a pm + '!^/noticeboard$!' => 'noticeboard', // view noticeboard + '!^/noticeboard/(\d+)$!' => 'noticeboard', // view noticeboard + '!^/log$!' => 'log', // modlog + '!^/log/(\d+)$!' => 'log', // modlog '!^/rebuild$!' => 'rebuild', // rebuild static files '!^/reports$!' => 'reports', // report queue '!^/reports/(\d+)/dismiss(all)?$!' => 'report_dismiss', // dismiss a report diff --git a/templates/mod/ban_list.html b/templates/mod/ban_list.html index 66471d6d..ea8183d6 100644 --- a/templates/mod/ban_list.html +++ b/templates/mod/ban_list.html @@ -75,7 +75,7 @@ {% if count > bans|count %}

- {% for i in range(0, count / config.mod.modlog_page) %} + {% for i in range(0, (count - 1) / config.mod.modlog_page) %} [{{ i + 1 }}] {% endfor %}

diff --git a/templates/mod/log.html b/templates/mod/log.html index 19924b1d..9b0979cb 100644 --- a/templates/mod/log.html +++ b/templates/mod/log.html @@ -33,7 +33,7 @@ {% if count > logs|count %}

- {% for i in range(0, count / config.mod.modlog_page) %} + {% for i in range(0, (count - 1) / config.mod.modlog_page) %} [{{ i + 1 }}] {% endfor %}

diff --git a/templates/mod/noticeboard.html b/templates/mod/noticeboard.html new file mode 100644 index 00000000..4d764d80 --- /dev/null +++ b/templates/mod/noticeboard.html @@ -0,0 +1,65 @@ +{% if mod|hasPermission(config.mod.noticeboard_post) %} +
+ {% trans 'New post' %} +
+ + + + + + + + + + + + + +
{% trans 'Name' %}{{ mod.username|e }}
{% trans 'Body' %}
+

+ +

+
+
+{% endif %} + +{% for post in noticeboard %} +
+ {% if mod|hasPermission(config.mod.noticeboard_delete) %} + + [{% trans 'delete' %}] + + {% endif %} +

+ + # + + {% if post.subject %} + {{ post.subject|e }} + {% else %} + {% trans 'no subject' %} + {% endif %} + + — by + {% if post.username %} + {{ post.username }} + {% else %} + deleted? + {% endif %} + at + {{ notice.time|date(config.post_date) }} + +

+

+ {{ post.body }} +

+
+{% endfor %} + +{% if count > noticeboard|count %} +

+ {% for i in range(0, (count - 1) / config.mod.noticeboard_page) %} + [{{ i + 1 }}] + {% endfor %} +

+{% endif %}