Skip to content

Commit

Permalink
Add sorting to document index
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Feb 12, 2019
1 parent 503327b commit 95a384f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
40 changes: 29 additions & 11 deletions src/controllers/Document/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,37 @@ class Index extends Controller {

public function &run(Router &$router, View &$view, array &$args) {

$model = new DocumentIndexModel();
$model->documents = Document::getAllDocuments();

// Alphabetically sort the documents for HTML
if ($view instanceof DocumentIndexHtmlView && $model->documents) {
usort($model->documents, function($a, $b){
$a1 = $a->getTitle();
$b1 = $b->getTitle();
if ($a1 == $b1) return 0;
return ($a1 < $b1 ? -1 : 1);
});
$model = new DocumentIndexModel();

$query = $router->getRequestQueryArray();

$model->order = (
isset($query['order']) ? $query['order'] : 'title-asc'
);

switch ($model->order) {
case 'created-asc':
$order = ['created_datetime','ASC']; break;
case 'created-desc':
$order = ['created_datetime','DESC']; break;
case 'id-asc':
$order = ['id','ASC']; break;
case 'id-desc':
$order = ['id','DESC']; break;
case 'title-asc':
$order = ['title','ASC']; break;
case 'title-desc':
$order = ['title','DESC']; break;
case 'updated-asc':
$order = ['edited_datetime','ASC']; break;
case 'updated-desc':
$order = ['edited_datetime','DESC']; break;
default:
$order = null;
}

$model->documents = Document::getAllDocuments( $order );

// Remove documents that are not published
if ($model->documents) {
$i = count($model->documents) - 1;
Expand Down
20 changes: 11 additions & 9 deletions src/libraries/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public static function delete($id) {
}
}

public static function getAllDocuments() {
$cache_key = "bnetdocs-documents";
public static function getAllDocuments( $order = null ) {
$cache_key = 'bnetdocs-documents-' . hash('md5', $order[0] . $order[1]);
$cache_val = Common::$cache->get($cache_key);
if ($cache_val !== false && !empty($cache_val)) {
$ids = explode(",", $cache_val);
Expand All @@ -126,7 +126,7 @@ public static function getAllDocuments() {
Common::$database = DatabaseDriver::getDatabaseObject();
}
try {
$stmt = Common::$database->prepare("
$stmt = Common::$database->prepare('
SELECT
`content`,
`created_datetime`,
Expand All @@ -137,25 +137,27 @@ public static function getAllDocuments() {
`title`,
`user_id`
FROM `documents`
ORDER BY `id` ASC;
");
ORDER BY
' . ($order ? '`' . $order[0] . '` ' . $order[1] . ',' : '') . '
`id` ' . ($order ? $order[1] : 'ASC') . ';'
);
if (!$stmt->execute()) {
throw new QueryException("Cannot refresh documents");
throw new QueryException('Cannot refresh documents');
}
$ids = [];
$objects = [];
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$ids[] = (int) $row->id;
$objects[] = new self($row);
Common::$cache->set(
"bnetdocs-document-" . $row->id, serialize($row), 300
'bnetdocs-document-' . $row->id, serialize($row), 300
);
}
$stmt->closeCursor();
Common::$cache->set($cache_key, implode(",", $ids), 300);
Common::$cache->set($cache_key, implode(',', $ids), 300);
return $objects;
} catch (PDOException $e) {
throw new QueryException("Cannot refresh documents", $e);
throw new QueryException('Cannot refresh documents', $e);
}
return null;
}
Expand Down
1 change: 1 addition & 0 deletions src/models/Document/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class Index extends Model {

public $documents;
public $order;
public $sum_documents;

}
42 changes: 38 additions & 4 deletions src/templates/Document/Index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,49 @@ use \BNETDocs\Libraries\User;
use \CarlBennett\MVC\Libraries\Common;
use \CarlBennett\MVC\Libraries\Pair;

$title = "Document Index";
$description = "The index for documents on BNETDocs";
$this->opengraph->attach(new Pair("url", "/document/index"));
$title = 'Document Index';
$description = 'The index for documents on BNETDocs';
$this->opengraph->attach(new Pair('url', '/document/index'));

require("./header.inc.phtml");
$order = $this->getContext()->order;

$this->additional_css[] = '/a/forms.css';
require('./header.inc.phtml');
?>
<article>
<header>Document Index</header>
<section>
<form method="GET">
<label for="order">Order by:</label>
<select name="order" id="order" onchange="form.submit();"
style="display:inline-block;width:200px;">
<option value="created-asc"<?php
if ($order === 'created-asc') { echo ' selected="selected"';
} ?>>Created (Ascending)</option>
<option value="created-desc"<?php
if ($order === 'created-desc') { echo ' selected="selected"';
} ?>>Created (Descending)</option>
<option value="id-asc"<?php
if ($order === 'id-asc') { echo ' selected="selected"';
} ?>>Id (Ascending)</option>
<option value="id-desc"<?php
if ($order === 'id-desc') { echo ' selected="selected"';
} ?>>Id (Descending)</option>
<option value="title-asc"<?php
if ($order === 'title-asc') { echo ' selected="selected"';
} ?>>Title (Ascending)</option>
<option value="title-desc"<?php
if ($order === 'title-desc') { echo ' selected="selected"';
} ?>>Title (Descending)</option>
<option value="updated-asc"<?php
if ($order === 'updated-asc') { echo ' selected="selected"';
} ?>>Updated (Ascending)</option>
<option value="updated-desc"<?php
if ($order === 'updated-desc') { echo ' selected="selected"';
} ?>>Updated (Descending)</option>
</select>
<input type="submit" value="Reorder"/>
</form>
<table>
<thead>
<tr>
Expand Down

0 comments on commit 95a384f

Please sign in to comment.