From b7ac12f0e79a2467193828889ba38dc4d0f73df1 Mon Sep 17 00:00:00 2001 From: Xiao Hu Tai Date: Thu, 8 Feb 2018 12:13:55 +0100 Subject: [PATCH] Add basic admin features --- src/Controller/BackendController.php | 112 ++++++++++++++++++++++++++- src/Table/IsUsefulFeedbackTable.php | 2 + templates/backend/_navigation.twig | 6 ++ templates/backend/feedback.twig | 19 +++-- templates/backend/index.twig | 27 ++++++- templates/backend/unread.twig | 52 +++++++++++++ 6 files changed, 211 insertions(+), 7 deletions(-) create mode 100644 templates/backend/_navigation.twig create mode 100644 templates/backend/unread.twig diff --git a/src/Controller/BackendController.php b/src/Controller/BackendController.php index d509e20..c6d6895 100644 --- a/src/Controller/BackendController.php +++ b/src/Controller/BackendController.php @@ -20,6 +20,8 @@ class BackendController extends Base */ public function addRoutes(ControllerCollection $ctr) { + // General + $ctr ->get('/', [$this, 'indexGet']) ->before([$this, 'before']) @@ -33,6 +35,28 @@ public function addRoutes(ControllerCollection $ctr) ->bind('is_useful.view') ; + $ctr + ->get('/unread', [$this, 'unreadGet']) + ->before([$this, 'before']) + ->bind('is_useful.unread') + ; + + // Feedback + + $ctr + ->get('/delete/{id}', [$this, 'deleteFeedback']) + ->assert('id', '\d+') + ->before([$this, 'before']) + ->bind('is_useful.feedback.delete') + ; + + $ctr + ->get('/markasread/{id}', [$this, 'markFeedback']) + ->assert('id', '\d+') + ->before([$this, 'before']) + ->bind('is_useful.feedback.markasread') + ; + return $ctr; } @@ -69,6 +93,21 @@ public function indexGet(Application $app, Request $request) ], []); } + /** + * + */ + public function unreadGet(Application $app, Request $request) + { + $stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful_feedback` WHERE `read` = 0"); + $stmt->execute(); + $feedback = $stmt->fetchAll(); + + return $this->render('@is_useful/backend/unread.twig', [ + 'title' => 'Unread Feedback', + 'feedback'=> $feedback, + ], []); + } + /** * * @param Application $app @@ -84,7 +123,7 @@ public function view(Application $app, Request $request, $id) // check iff empty - $stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful_feedback` WHERE `is_useful_id` = :id"); + $stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful_feedback` WHERE `is_useful_id` = :id AND `hide` = 0"); $stmt->bindParam('id', $id); $stmt->execute(); $feedback = $stmt->fetchAll(); @@ -95,4 +134,75 @@ public function view(Application $app, Request $request, $id) 'feedback' => $feedback, ], []); } + + /** + * Removes feedback by ID + */ + public function deleteFeedback(Application $app, Request $request, $id) + { + // (1) Remove + $stmt = $app['db']->prepare("UPDATE `bolt_is_useful_feedback` SET `hide` = 1 WHERE `id` = :id"); + $stmt->bindParam('id', $id); + $stmt->execute(); + + // (2) Fetch + $stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful_feedback` WHERE `id` = :id"); + $stmt->bindParam('id', $id); + $stmt->execute(); + $feedback = $stmt->fetch(); + + // (3) Get the parent item + $sql = "SELECT `bolt_is_useful`.*"; + $sql .= " FROM `bolt_is_useful`"; + $sql .= " JOIN `bolt_is_useful_feedback` ON `bolt_is_useful`.`id` = `bolt_is_useful_feedback`.`is_useful_id`"; + $sql .= " WHERE `bolt_is_useful_feedback`.`id` = :id"; + + $stmt = $app['db']->prepare($sql); + $stmt->bindParam('id', $id); + $stmt->execute(); + $parent = $stmt->fetch(); + + $totals = json_decode($parent['totals']); + $ips = json_decode($parent['ips']); + + // Warning: this can make data inconsistent! + if (isset($totals->no)) { + $totals->no--; + if ($totals->no < 0) { + $totals->no = 0; + } + } + unset($ips->{$feedback['ip']}); + + $totals = json_encode($totals); + $ips = json_encode($ips); + + // (4) Set parent item + $sql = "UPDATE `bolt_is_useful`"; + $sql .= " SET totals = :totals,"; + $sql .= " ips = :ips"; + $sql .= " WHERE `id` = :id"; + + $stmt = $app['db']->prepare($sql); + $stmt->bindParam('id', $parent['id']); + $stmt->bindParam('totals', $totals); + $stmt->bindParam('ips', $ips); + $stmt->execute(); + + return $this->redirect( $request->headers->get('referer') ); + + // return $this->redirect(); + } + + /** + * Mark feedback by ID as read + */ + public function markFeedback(Application $app, Request $request, $id) + { + $stmt = $app['db']->prepare("UPDATE `bolt_is_useful_feedback` SET `read` = 1 WHERE `id` = :id"); + $stmt->bindParam('id', $id); + $stmt->execute(); + + return $this->redirect( $request->headers->get('referer') ); + } } diff --git a/src/Table/IsUsefulFeedbackTable.php b/src/Table/IsUsefulFeedbackTable.php index 376f107..fb59a24 100644 --- a/src/Table/IsUsefulFeedbackTable.php +++ b/src/Table/IsUsefulFeedbackTable.php @@ -19,6 +19,8 @@ protected function addColumns() $this->table->addColumn('message' , 'text' , ['notnull' => false]); $this->table->addColumn('url' , 'string' , ['notnull' => false]); $this->table->addColumn('datetime' , 'datetime', ['notnull' => false]); + $this->table->addColumn('read' , 'integer' , ['notnull' => false, 'default' => 0, ]); + $this->table->addColumn('hide' , 'integer' , ['notnull' => false, 'default' => 0, ]); } /** diff --git a/templates/backend/_navigation.twig b/templates/backend/_navigation.twig new file mode 100644 index 0000000..4cf4479 --- /dev/null +++ b/templates/backend/_navigation.twig @@ -0,0 +1,6 @@ +
+

+ Index + View unread feedback +

+
\ No newline at end of file diff --git a/templates/backend/feedback.twig b/templates/backend/feedback.twig index 778c553..ea2d125 100644 --- a/templates/backend/feedback.twig +++ b/templates/backend/feedback.twig @@ -6,7 +6,7 @@ {% block page_main %} - Index + {{ include('@is_useful/backend/_navigation.twig') }} {% if data is not empty %} {% set item = data|first %} @@ -41,16 +41,23 @@ IP Datetime Message + Actions {% for item in feedback %} - {% set ip = item.ip %} - {% set datetime = item.datetime %} - {% set message = item.message %} + {% set ip = item.ip %} + {% set datetime = item.datetime %} + {% set message = item.message %} + {% set deleteLink = path('is_useful.feedback.delete', { id: item.id }) %} + {% set markLink = path('is_useful.feedback.markasread', { id: item.id }) %} {{ ip }} {{ datetime }} {{ message }} + + Delete + Mark as read + {% endfor %} @@ -63,7 +70,9 @@ diff --git a/templates/backend/index.twig b/templates/backend/index.twig index b987255..61de6f8 100644 --- a/templates/backend/index.twig +++ b/templates/backend/index.twig @@ -6,6 +6,11 @@ {% block page_main %} + {{ include('@is_useful/backend/_navigation.twig') }} + + {% set cumulativeNo = 0 %} + {% set cumulativeYes = 0 %} + @@ -44,15 +49,35 @@ View + {% set cumulativeNo = cumulativeNo + totalsNo %} + {% set cumulativeYes = cumulativeYes + totalsYes %} {% endfor %}
Record
+
+ + + diff --git a/templates/backend/unread.twig b/templates/backend/unread.twig new file mode 100644 index 0000000..f409668 --- /dev/null +++ b/templates/backend/unread.twig @@ -0,0 +1,52 @@ +{% extends '_base/_page-nav.twig' %} + +{% block page_nav title %} + +{% block page_title __(title) %} + +{% block page_main %} + + {{ include('@is_useful/backend/_navigation.twig') }} + + {% if feedback is not empty %} + + + + + + + + + {% for item in feedback %} + {% set ip = item.ip %} + {% set datetime = item.datetime %} + {% set message = item.message %} + {% set deleteLink = path('is_useful.feedback.delete', { id: item.id }) %} + {% set markLink = path('is_useful.feedback.markasread', { id: item.id }) %} + + + + + + + {% endfor %} + +
IPDatetimeMessageActions
{{ ip }}{{ datetime }}{{ message }} + Delete + Mark as read +
+ {% else %} + This item has no feedback yet. + {% endif %} + + + + + +{% endblock page_main %}