Skip to content

Commit 95a384f

Browse files
committed
Add sorting to document index
1 parent 503327b commit 95a384f

File tree

4 files changed

+79
-24
lines changed

4 files changed

+79
-24
lines changed

src/controllers/Document/Index.php

+29-11
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,37 @@ class Index extends Controller {
1818

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

21-
$model = new DocumentIndexModel();
22-
$model->documents = Document::getAllDocuments();
23-
24-
// Alphabetically sort the documents for HTML
25-
if ($view instanceof DocumentIndexHtmlView && $model->documents) {
26-
usort($model->documents, function($a, $b){
27-
$a1 = $a->getTitle();
28-
$b1 = $b->getTitle();
29-
if ($a1 == $b1) return 0;
30-
return ($a1 < $b1 ? -1 : 1);
31-
});
21+
$model = new DocumentIndexModel();
22+
23+
$query = $router->getRequestQueryArray();
24+
25+
$model->order = (
26+
isset($query['order']) ? $query['order'] : 'title-asc'
27+
);
28+
29+
switch ($model->order) {
30+
case 'created-asc':
31+
$order = ['created_datetime','ASC']; break;
32+
case 'created-desc':
33+
$order = ['created_datetime','DESC']; break;
34+
case 'id-asc':
35+
$order = ['id','ASC']; break;
36+
case 'id-desc':
37+
$order = ['id','DESC']; break;
38+
case 'title-asc':
39+
$order = ['title','ASC']; break;
40+
case 'title-desc':
41+
$order = ['title','DESC']; break;
42+
case 'updated-asc':
43+
$order = ['edited_datetime','ASC']; break;
44+
case 'updated-desc':
45+
$order = ['edited_datetime','DESC']; break;
46+
default:
47+
$order = null;
3248
}
3349

50+
$model->documents = Document::getAllDocuments( $order );
51+
3452
// Remove documents that are not published
3553
if ($model->documents) {
3654
$i = count($model->documents) - 1;

src/libraries/Document.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public static function delete($id) {
111111
}
112112
}
113113

114-
public static function getAllDocuments() {
115-
$cache_key = "bnetdocs-documents";
114+
public static function getAllDocuments( $order = null ) {
115+
$cache_key = 'bnetdocs-documents-' . hash('md5', $order[0] . $order[1]);
116116
$cache_val = Common::$cache->get($cache_key);
117117
if ($cache_val !== false && !empty($cache_val)) {
118118
$ids = explode(",", $cache_val);
@@ -126,7 +126,7 @@ public static function getAllDocuments() {
126126
Common::$database = DatabaseDriver::getDatabaseObject();
127127
}
128128
try {
129-
$stmt = Common::$database->prepare("
129+
$stmt = Common::$database->prepare('
130130
SELECT
131131
`content`,
132132
`created_datetime`,
@@ -137,25 +137,27 @@ public static function getAllDocuments() {
137137
`title`,
138138
`user_id`
139139
FROM `documents`
140-
ORDER BY `id` ASC;
141-
");
140+
ORDER BY
141+
' . ($order ? '`' . $order[0] . '` ' . $order[1] . ',' : '') . '
142+
`id` ' . ($order ? $order[1] : 'ASC') . ';'
143+
);
142144
if (!$stmt->execute()) {
143-
throw new QueryException("Cannot refresh documents");
145+
throw new QueryException('Cannot refresh documents');
144146
}
145147
$ids = [];
146148
$objects = [];
147149
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
148150
$ids[] = (int) $row->id;
149151
$objects[] = new self($row);
150152
Common::$cache->set(
151-
"bnetdocs-document-" . $row->id, serialize($row), 300
153+
'bnetdocs-document-' . $row->id, serialize($row), 300
152154
);
153155
}
154156
$stmt->closeCursor();
155-
Common::$cache->set($cache_key, implode(",", $ids), 300);
157+
Common::$cache->set($cache_key, implode(',', $ids), 300);
156158
return $objects;
157159
} catch (PDOException $e) {
158-
throw new QueryException("Cannot refresh documents", $e);
160+
throw new QueryException('Cannot refresh documents', $e);
159161
}
160162
return null;
161163
}

src/models/Document/Index.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Index extends Model {
88

99
public $documents;
10+
public $order;
1011
public $sum_documents;
1112

1213
}

src/templates/Document/Index.phtml

+38-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,49 @@ use \BNETDocs\Libraries\User;
77
use \CarlBennett\MVC\Libraries\Common;
88
use \CarlBennett\MVC\Libraries\Pair;
99

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

14-
require("./header.inc.phtml");
14+
$order = $this->getContext()->order;
15+
16+
$this->additional_css[] = '/a/forms.css';
17+
require('./header.inc.phtml');
1518
?>
1619
<article>
1720
<header>Document Index</header>
1821
<section>
22+
<form method="GET">
23+
<label for="order">Order by:</label>
24+
<select name="order" id="order" onchange="form.submit();"
25+
style="display:inline-block;width:200px;">
26+
<option value="created-asc"<?php
27+
if ($order === 'created-asc') { echo ' selected="selected"';
28+
} ?>>Created (Ascending)</option>
29+
<option value="created-desc"<?php
30+
if ($order === 'created-desc') { echo ' selected="selected"';
31+
} ?>>Created (Descending)</option>
32+
<option value="id-asc"<?php
33+
if ($order === 'id-asc') { echo ' selected="selected"';
34+
} ?>>Id (Ascending)</option>
35+
<option value="id-desc"<?php
36+
if ($order === 'id-desc') { echo ' selected="selected"';
37+
} ?>>Id (Descending)</option>
38+
<option value="title-asc"<?php
39+
if ($order === 'title-asc') { echo ' selected="selected"';
40+
} ?>>Title (Ascending)</option>
41+
<option value="title-desc"<?php
42+
if ($order === 'title-desc') { echo ' selected="selected"';
43+
} ?>>Title (Descending)</option>
44+
<option value="updated-asc"<?php
45+
if ($order === 'updated-asc') { echo ' selected="selected"';
46+
} ?>>Updated (Ascending)</option>
47+
<option value="updated-desc"<?php
48+
if ($order === 'updated-desc') { echo ' selected="selected"';
49+
} ?>>Updated (Descending)</option>
50+
</select>
51+
<input type="submit" value="Reorder"/>
52+
</form>
1953
<table>
2054
<thead>
2155
<tr>

0 commit comments

Comments
 (0)