summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-05-11 20:15:24 +0000
committerBharat Mediratta <bharat@menalto.com>2009-05-11 20:15:24 +0000
commitde812e1e8225f49deec27063d96a81ce4431ebcc (patch)
tree4479ec0d45507b33c1d6727c8b8d1f80523ddbbd /modules
parenta2c188ac37a748734f4a9f3413eae44152750d4a (diff)
Refactor to support pagination and simplify the code.
- Simplify the public controller methods - Fix a bug where missing thumbnails would cause a divide by zero error - actually pay attention to the page # for pagination and limit the query accordingly.
Diffstat (limited to 'modules')
-rw-r--r--modules/comment/controllers/admin_comments.php131
-rw-r--r--modules/comment/views/admin_comments.html.php23
2 files changed, 77 insertions, 77 deletions
diff --git a/modules/comment/controllers/admin_comments.php b/modules/comment/controllers/admin_comments.php
index c1a30bd2..48a9bf72 100644
--- a/modules/comment/controllers/admin_comments.php
+++ b/modules/comment/controllers/admin_comments.php
@@ -18,104 +18,91 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Admin_Comments_Controller extends Admin_Controller {
+ private static $items_per_page = 20;
+
+ public function index() {
+ // Get rid of old deleted/spam comments once in a while
+ Database::instance()->query(
+ "DELETE FROM {comments} " .
+ "WHERE state IN ('deleted', 'spam') " .
+ "AND unix_timestamp(now()) - updated > 86400 * 7");
+
+ // Redirect to the appropriate queue
+ url::redirect("admin/comments/queue/unpublished");
+ }
+
+ public function menu_labels() {
+ $menu = $this->_menu($this->_counts());
+ print json_encode(array($menu->get("unpublished")->label,
+ $menu->get("published")->label,
+ $menu->get("spam")->label,
+ $menu->get("deleted")->label));
+ }
+
+ public function queue($state) {
+ $page = max(Input::instance()->get("page"), 1);
- private function _get_base_view() {
$view = new Admin_View("admin.html");
$view->content = new View("admin_comments.html");
- $view->content->published = $this->_query(array("published"));
- $view->content->unpublished = $this->_query(array("unpublished"));
- $view->content->spam = $this->_query(array("spam"));
- $view->content->deleted = $this->_query(array("deleted"));
- $view->content->menu = Menu::factory("root")
+ $view->content->counts = $this->_counts();
+ $view->content->menu = $this->_menu($view->content->counts);
+ $view->content->state = $state;
+ $view->content->comments = ORM::factory("comment")
+ ->orderby("created", "DESC")
+ ->where("state", $state)
+ ->limit(self::$items_per_page, ($page - 1) * self::$items_per_page)
+ ->find_all();
+ $view->content->pager = new Pagination();
+ $view->content->pager->initialize(
+ array("query_string" => "page",
+ "total_items" => $view->content->counts->$state,
+ "items_per_page" => self::$items_per_page,
+ "style" => "classic"));
+
+ print $view;
+ }
+
+ private function _menu($counts) {
+ return Menu::factory("root")
->append(Menu::factory("link")
->id("unpublished")
->label(t2("Awaiting Moderation (%count)",
"Awaiting Moderation (%count)",
- $view->content->unpublished->count()))
+ $counts->unpublished))
->url(url::site("admin/comments/queue/unpublished")))
->append(Menu::factory("link")
->id("published")
->label(t2("Approved (%count)",
"Approved (%count)",
- $view->content->published->count()))
+ $counts->published))
->url(url::site("admin/comments/queue/published")))
->append(Menu::factory("link")
->id("spam")
->label(t2("Spam (%count)",
"Spam (%count)",
- $view->content->spam->count()))
+ $counts->spam))
->url(url::site("admin/comments/queue/spam")))
->append(Menu::factory("link")
->id("deleted")
->label(t2("Recently Deleted (%count)",
"Recently Deleted (%count)",
- $view->content->deleted->count()))
+ $counts->deleted))
->url(url::site("admin/comments/queue/deleted")));
- return $view;
- }
-
- public function index() {
- // Get rid of old deleted/spam comments
- Database::instance()->query(
- "DELETE FROM {comments} " .
- "WHERE state IN ('deleted', 'spam') " .
- "AND unix_timestamp(now()) - updated > 86400 * 7");
-
- $this->queue("unpublished");
- }
-
- public function menu_labels($state) {
- $view = $this->_get_base_view();
- print json_encode(array($view->content->menu->get("unpublished")->label,
- $view->content->menu->get("published")->label,
- $view->content->menu->get("spam")->label,
- $view->content->menu->get("deleted")->label));
- }
-
- public function queue($state) {
- $view = $this->_get_base_view();
-
- switch ($state) {
- case "published":
- $view->content->comments = $view->content->published;
- $view->content->title = t("Approved Comments");
- break;
-
- case "unpublished":
- $view->content->comments = $view->content->unpublished;
- $view->content->title = t("Comments Awaiting Moderation");
- break;
-
- case "spam":
- $view->content->title = t("Spam Comments");
- $view->content->comments = $view->content->spam;
- $view->content->spam_caught = module::get_var("comment", "spam_caught");
- break;
-
- case "deleted":
- $view->content->title = t("Recently Deleted Comments");
- $view->content->comments = $view->content->deleted;
- break;
- }
-
- $view->content->queue = $state;
- $view->content->pager = new Pagination();
- $view->content->pager->initialize(
- array("query_string" => "page",
- "total_items" => $view->content->comments->count(),
- "items_per_page" => 20,
- "style" => "classic"));
-
- print $view;
}
- private function _query($states) {
- $query = ORM::factory("comment")
- ->orderby("created", "DESC");
- if ($states) {
- $query->in("state", $states);
+ private function _counts() {
+ $counts->unpublished = 0;
+ $counts->published = 0;
+ $counts->spam = 0;
+ $counts->deleted = 0;
+ foreach (Database::instance()
+ ->select("state", "count(*) as c")
+ ->from("comments")
+ ->groupby("state")
+ ->get() as $row) {
+ $counts->{$row->state} = $row->c;
}
- return $query->find_all();
+ return $counts;
}
public function set_state($id, $state) {
diff --git a/modules/comment/views/admin_comments.html.php b/modules/comment/views/admin_comments.html.php
index 63b1a394..16816636 100644
--- a/modules/comment/views/admin_comments.html.php
+++ b/modules/comment/views/admin_comments.html.php
@@ -44,11 +44,20 @@
<!-- @todo: Remove after setting active option? -->
<h2>
- <?= $title ?>
+ <? if ($state == "published"): ?>
+ <?= t("Approved Comments") ?>
+ <? elseif ($state == "unpublished"): ?>
+ <?= t("Comments Awaiting Moderation") ?>
+ <? elseif ($state == "spam"): ?>
+ <?= t("Spam Comments") ?>
+ <? elseif ($state == "deleted"): ?>
+ <?= t("Recently Deleted Comments") ?>
+ <? endif ?>
</h2>
- <? if ($queue == "spam"): ?>
+ <? if ($state == "spam"): ?>
<div>
+ <? $spam_caught = module::get_var("comment", "spam_caught") ?>
<? if ($spam_caught > 0): ?>
<p>
<?= t2("Gallery has caught %count spam for you since you installed spam filtering.",
@@ -57,10 +66,10 @@
</p>
<? endif ?>
<p>
- <? if ($spam->count()): ?>
+ <? if ($counts->spam): ?>
<?= t2("There is currently one comment in your spam queue. You can delete it with a single click, but there is no undo operation so you may want to check the message first to make sure that it really is spam.",
"There are currently %count comments in your spam queue. You can delete them all with a single click, but there is no undo operation so you may want to check the messages first to make sure that they really are spam. All spam messages will be deleted after 7 days automatically.",
- $spam->count()) ?>
+ $counts->spam) ?>
</p>
<p>
<a href="<?= url::site("admin/comments/delete_all_spam?csrf=$csrf") ?>">
@@ -73,7 +82,7 @@
</div>
<? endif ?>
- <? if ($queue == "deleted"): ?>
+ <? if ($state == "deleted"): ?>
<div>
<p>
<?= t("These are messages that have been recently deleted. They will be permanently erased automatically after 7 days.") ?>
@@ -111,10 +120,14 @@
<? $item = $comment->item(); ?>
<div class="gItem gPhoto">
<a href="<?= $item->url() ?>">
+ <? if ($item->has_thumb()): ?>
<img src="<?= $item->thumb_url() ?>"
alt="<?= $item->title ?>"
<?= photo::img_dimensions($item->thumb_width, $item->thumb_height, 75) ?>
/>
+ <? else: ?>
+ <?= t("No thumbnail") ?>
+ <? endif ?>
</a>
</div>
</div>