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 +
+Record | @@ -44,15 +49,35 @@ View + {% set cumulativeNo = cumulativeNo + totalsNo %} + {% set cumulativeYes = cumulativeYes + totalsYes %} {% endfor %}
---|
IP | +Datetime | +Message | +Actions | + + + {% 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 }) %} +
---|---|---|---|
{{ ip }} | +{{ datetime }} | +{{ message }} | ++ Delete + Mark as read + | +