mirror of
https://github.com/Pathduck/gallery3.git
synced 2026-05-20 03:19:13 -04:00
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.
This commit is contained in:
@@ -18,104 +18,91 @@
|
||||
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
class Admin_Comments_Controller extends Admin_Controller {
|
||||
|
||||
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")
|
||||
->append(Menu::factory("link")
|
||||
->id("unpublished")
|
||||
->label(t2("Awaiting Moderation (%count)",
|
||||
"Awaiting Moderation (%count)",
|
||||
$view->content->unpublished->count()))
|
||||
->url(url::site("admin/comments/queue/unpublished")))
|
||||
->append(Menu::factory("link")
|
||||
->id("published")
|
||||
->label(t2("Approved (%count)",
|
||||
"Approved (%count)",
|
||||
$view->content->published->count()))
|
||||
->url(url::site("admin/comments/queue/published")))
|
||||
->append(Menu::factory("link")
|
||||
->id("spam")
|
||||
->label(t2("Spam (%count)",
|
||||
"Spam (%count)",
|
||||
$view->content->spam->count()))
|
||||
->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()))
|
||||
->url(url::site("admin/comments/queue/deleted")));
|
||||
return $view;
|
||||
}
|
||||
private static $items_per_page = 20;
|
||||
|
||||
public function index() {
|
||||
// Get rid of old deleted/spam comments
|
||||
// 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");
|
||||
|
||||
$this->queue("unpublished");
|
||||
// Redirect to the appropriate queue
|
||||
url::redirect("admin/comments/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 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) {
|
||||
$view = $this->_get_base_view();
|
||||
$page = max(Input::instance()->get("page"), 1);
|
||||
|
||||
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 = new Admin_View("admin.html");
|
||||
$view->content = new View("admin_comments.html");
|
||||
$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->comments->count(),
|
||||
"items_per_page" => 20,
|
||||
"total_items" => $view->content->counts->$state,
|
||||
"items_per_page" => self::$items_per_page,
|
||||
"style" => "classic"));
|
||||
|
||||
print $view;
|
||||
}
|
||||
|
||||
private function _query($states) {
|
||||
$query = ORM::factory("comment")
|
||||
->orderby("created", "DESC");
|
||||
if ($states) {
|
||||
$query->in("state", $states);
|
||||
private function _menu($counts) {
|
||||
return Menu::factory("root")
|
||||
->append(Menu::factory("link")
|
||||
->id("unpublished")
|
||||
->label(t2("Awaiting Moderation (%count)",
|
||||
"Awaiting Moderation (%count)",
|
||||
$counts->unpublished))
|
||||
->url(url::site("admin/comments/queue/unpublished")))
|
||||
->append(Menu::factory("link")
|
||||
->id("published")
|
||||
->label(t2("Approved (%count)",
|
||||
"Approved (%count)",
|
||||
$counts->published))
|
||||
->url(url::site("admin/comments/queue/published")))
|
||||
->append(Menu::factory("link")
|
||||
->id("spam")
|
||||
->label(t2("Spam (%count)",
|
||||
"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)",
|
||||
$counts->deleted))
|
||||
->url(url::site("admin/comments/queue/deleted")));
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user