alf (red_alf) wrote in changelog,
alf
red_alf
changelog

[ljcom] r12404: LJSUP-13174: Add pagination to userheads...

Committer: afedorov
LJSUP-13174: Add pagination to userheads admin tool

U   trunk/cgi-bin/LJ/UserHead.pm
U   trunk/htdocs/admin/userheads/manage.bml
Modified: trunk/cgi-bin/LJ/UserHead.pm
===================================================================
--- trunk/cgi-bin/LJ/UserHead.pm	2012-08-02 13:53:37 UTC (rev 12403)
+++ trunk/cgi-bin/LJ/UserHead.pm	2012-08-02 14:46:35 UTC (rev 12404)
@@ -79,13 +79,18 @@
 
 sub get_all_userheads {
     my $class       = shift;
+    my $is_enabled  = shift || 0;
+    my $limit       = shift;
+    my $offset      = shift;
 
     my $dbh         = LJ::get_db_writer();
-    my $is_enabled  = shift || 0;
 
     my $where = $is_enabled ? " WHERE visibility != 'D' " : "";
+    
+    my $limit_sql = ( $limit && $limit =~ /^\d+$/ ) ? "LIMIT $limit" : "";
+    $limit_sql .= ( $limit_sql && $offset && $offset =~ /^\d+$/ ) ? " OFFSET $offset" : "";
 
-    my $uhs = $dbh->selectall_arrayref ("SELECT * FROM shop_userheads $where ORDER BY uh_id DESC", { Slice => {} });
+    my $uhs = $dbh->selectall_arrayref("SELECT * FROM shop_userheads $where ORDER BY uh_id DESC $limit_sql", { Slice => {} });
 
     return [
         map {
@@ -98,5 +103,18 @@
     ];
 }
 
+sub get_count_userheads {
+    my $class       = shift;
+    my $is_enabled  = shift || 0;
+
+    my $dbh         = LJ::get_db_writer();
+
+    my $where = $is_enabled ? " WHERE visibility != 'D' " : "";
+    
+    my $count = $dbh->selectrow_array("SELECT COUNT(*) FROM shop_userheads $where");
+
+    return $count;
+}
+
 1;
 

Modified: trunk/htdocs/admin/userheads/manage.bml
===================================================================
--- trunk/htdocs/admin/userheads/manage.bml	2012-08-02 13:53:37 UTC (rev 12403)
+++ trunk/htdocs/admin/userheads/manage.bml	2012-08-02 14:46:35 UTC (rev 12404)
@@ -19,6 +19,9 @@
     unless $LJ::IS_DEV_SERVER || LJ::check_priv($remote, "admin", "userheads");
 
 my $save_error = '';
+my $items_per_page = 100;
+my $page   = $GET{page} || 0;
+my $offset = $page * $items_per_page;
 
 if ($FORM{'edit'}) {
     
@@ -69,7 +72,7 @@
         }
         $uh_fs = LJ::FileStore->get_path_info ( path => "/userhead/".$uh->get_uh_id );
     }
-    BML::redirect("/admin/userheads/manage.bml" . ($uh_fs->{'change_time'} ? "?v=".$uh_fs->{'change_time'} : ""));
+    BML::redirect("/admin/userheads/manage.bml?page=$page" . ($uh_fs->{'change_time'} ? "&v=".$uh_fs->{'change_time'} : ""));
 }
 
 if ($FORM{'save'}) {
@@ -142,7 +145,7 @@
 
 FORM
 
-my $uhs = LJ::UserHead->get_all_userheads();
+my $uhs = LJ::UserHead->get_all_userheads(0, $items_per_page, $offset);
 
 my @selects = (
     {
@@ -167,6 +170,20 @@
     },
 );
 
+my $pager = "Pages: ";
+
+my $pages = LJ::UserHead->get_count_userheads();
+$pages = int($pages / $items_per_page) + (($pages % $items_per_page) ? 1 : 0);
+
+for (my $i = 0; $i < $pages; $i++) {
+    $pager .= ', ' if $i;
+    $pager .= '<a href="/admin/userheads/manage.bml?page=' . $i . ( $GET{v} ? "&v=$GET{v}" : "" ) .'">';
+    $pager .= ( $i == $page ) ? '<strong>' . ( $i + 1 ) . '</strong>' : $i + 1;
+    $pager .= '</a>';
+}
+
+$ret .= $pager;
+
 $ret .= "<form method='POST'>";
 $ret .= "<table border='0' cellspacing='1' cellpadding='4'>";
 $ret .= "<tr><th>Id</th><th>Img Enb</th><th>Img Dis</th><th>Cost</th><th>&nbsp;</th><th>Author</th><th>Description</th><th>Description SUP</th><th>Individual user buyer</th><th>Indv. expdate (<small>YYYYMMDD</small>)</th><th>Edit</th></tr>";
@@ -216,6 +233,8 @@
 $ret .= "<tr><td colspan='5'><input type='submit' name='save' value='Save' /></td></tr>\n";
 $ret .= "</table></form>";
 
+$ret .= $pager;
+
 $ret .= "<hr />";
 
 $ret .= "<form method='POST'>";
@@ -229,7 +248,6 @@
 $ret .= "</form>";
 
 return $ret;
-#return "KKK";
 
 _code?>
 <=body

Tags: afedorov, bml, ljcom, pm, red_alf
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments