Skip to content

Commit

Permalink
Add basic admin features
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohutai committed Feb 8, 2018
1 parent ba2ebb2 commit b7ac12f
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 7 deletions.
112 changes: 111 additions & 1 deletion src/Controller/BackendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class BackendController extends Base
*/
public function addRoutes(ControllerCollection $ctr)
{
// General

$ctr
->get('/', [$this, 'indexGet'])
->before([$this, 'before'])
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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') );
}
}
2 changes: 2 additions & 0 deletions src/Table/IsUsefulFeedbackTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, ]);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions templates/backend/_navigation.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
<p>
<a class="btn btn-tertiary" href="{{ path('is_useful.index') }}">Index</a>
<a class="btn btn-tertiary" href="{{ path('is_useful.unread') }}">View unread feedback</a>
</p>
</div>
19 changes: 14 additions & 5 deletions templates/backend/feedback.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% block page_main %}

<a href="{{ path('is_useful.index') }}">Index</a>
{{ include('@is_useful/backend/_navigation.twig') }}

{% if data is not empty %}
{% set item = data|first %}
Expand Down Expand Up @@ -41,16 +41,23 @@
<th>IP</th>
<th>Datetime</th>
<th>Message</th>
<th>Actions</th>
</thead>
<tbody>
{% 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 }) %}
<tr>
<td>{{ ip }}</td>
<td>{{ datetime }}</td>
<td>{{ message }} </td>
<td>
<a class="btn btn-tertiary" href="{{ deleteLink }}" onclick="return confirm('Are you sure?')">Delete</a>
<a class="btn btn-secondary" href="{{ markLink }}">Mark as read</a>
</td>
</tr>
{% endfor %}
</tbody>
Expand All @@ -63,7 +70,9 @@
<script type="text/javascript" src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function(){
$('[data-table]').DataTable();
$('[data-table]').DataTable({
pageLength: 100
});
});
</script>

Expand Down
27 changes: 26 additions & 1 deletion templates/backend/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

{% block page_main %}

{{ include('@is_useful/backend/_navigation.twig') }}

{% set cumulativeNo = 0 %}
{% set cumulativeYes = 0 %}

<table data-table>
<thead>
<th>Record</th>
Expand Down Expand Up @@ -44,15 +49,35 @@
<a class="btn btn-tertiary" href="{{ path('is_useful.view', { id: item.id }) }}">View</a>
</td>
</tr>
{% set cumulativeNo = cumulativeNo + totalsNo %}
{% set cumulativeYes = cumulativeYes + totalsYes %}
{% endfor %}
</tbody>
</table>

<hr>

<ul>
<li>
<strong>Total stats</strong>:
<ul>
<li>
<strong>No</strong>: {{ cumulativeNo }} ({{ (100 * cumulativeNo / ( cumulativeNo + cumulativeYes ))|number_format(2,'.','') }})
</li>
<li>
<strong>Yes</strong>: {{ cumulativeYes }} ({{ (100 * cumulativeYes / ( cumulativeNo + cumulativeYes ))|number_format(2,'.','') }}%)
</li>
</ul>
</li>
</ul>

<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<script type="text/javascript" src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function(){
$('[data-table]').DataTable();
$('[data-table]').DataTable({
pageLength: 100
});
});
</script>

Expand Down
52 changes: 52 additions & 0 deletions templates/backend/unread.twig
Original file line number Diff line number Diff line change
@@ -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 %}
<table data-table>
<thead>
<th>IP</th>
<th>Datetime</th>
<th>Message</th>
<th>Actions</th>
</thead>
<tbody>
{% 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 }) %}
<tr>
<td>{{ ip }}</td>
<td>{{ datetime }}</td>
<td>{{ message }} </td>
<td>
<a class="btn btn-tertiary" href="{{ deleteLink }}" onclick="return confirm('Are you sure?')">Delete</a>
<a class="btn btn-secondary" href="{{ markLink }}">Mark as read</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
This item has no feedback yet.
{% endif %}

<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<script type="text/javascript" src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function(){
$('[data-table]').DataTable({
pageLength: 100
});
});
</script>

{% endblock page_main %}

0 comments on commit b7ac12f

Please sign in to comment.