allow a sort specification of "shuffle"
authorTony Cook <tony@develop-help.com>
Fri, 1 Nov 2013 00:25:35 +0000 (11:25 +1100)
committerTony Cook <tony@develop-help.com>
Fri, 1 Nov 2013 00:25:35 +0000 (11:25 +1100)
site/cgi-bin/modules/BSE/UI/AdminReorder.pm
site/templates/admin/edit_0.tmpl
site/templates/admin/edit_1.tmpl

index 7b62f69a4145ab021e365e96c55b5cae7eb5ddc2..ab0e7844f775be0260234de704b9df0858ccda13 100644 (file)
@@ -3,8 +3,9 @@ use strict;
 use base 'BSE::UI::AdminDispatch';
 use Articles;
 use OtherParents;
+use List::Util ();
 
-our $VERSION = "1.000";
+our $VERSION = "1.001";
 
 =head1 NAME
 
@@ -33,6 +34,10 @@ by the reverse flag), or article id.  Default: C<current>.
 
 =item *
 
+the keyword C<shuffle> which randomizes the order.
+
+=item *
+
 a comma separated list of article field names, with optional reverse
 flags.  eg. C<author,-title> to sort by author ascending, title
 descending.
@@ -112,7 +117,7 @@ sub req_byparent {
   $req->user_can(bse_edit_reorder_children => $parentid)
     or return $self->access_error($req, $msg);
 
-  my ($kids, $order) = $self->_limit_and_sort
+  my ($kids, $order) = $self->_limit_and_order
     (
      $req,
      [
@@ -173,7 +178,7 @@ sub req_bystepparent {
   my %stepkids = map { $_->{id}, $_ } @stepkids;
   my @kids =
 
-  my ($kids, $order) = $self->_limit_and_sort
+  my ($kids, $order) = $self->_limit_and_order
     (
      $req,
      [
@@ -235,7 +240,7 @@ sub req_bystepchild {
   my %stepparents = map { $_->{id}, $_ } @stepparents;
 
 
-  my ($parents, $order) = $self->_limit_and_sort
+  my ($parents, $order) = $self->_limit_and_order
     (
      $req,
      [
@@ -261,18 +266,9 @@ sub req_bystepchild {
   return $req->get_refresh($r);
 }
 
-sub _limit_and_sort {
-  my ($self, $req, $kids) = @_;
-
-  my $cgi = $req->cgi;
-  my $type = $cgi->param("type");
-  if ($type) {
-    $kids = [ grep $_->[0]{generator} =~ /::\Q$type\E$/, @$kids ];
-  }
+sub _sort {
+  my ($self, $sort, $cgi, $kids) = @_;
 
-  my @order = sort { $b <=> $a } map $_->[1]{$_->[2]}, @$kids;
-  my $sort = join(",", $cgi->param('sort')) || 'current';
-  $sort =~ s/-,/-/g;
   my $reverse = $cgi->param('reverse');
   
   my $code;
@@ -321,14 +317,47 @@ sub _limit_and_sort {
     $code = sub { -$temp->() };
     $order .= $order ? ", reversed" : "reverse";
   }
-  if ($code) {
-    $kids = [ sort $code @$kids ];
-    for my $i (0..$#$kids) {
-      my $kid = $kids->[$i];
-      $kid->[1]{$kid->[2]} = $order[$i];
-      $kid->[1]->save();
-    }
+
+  $kids = [ sort $code @$kids ];
+
+  return ( $kids, $order );
+}
+
+sub _shuffle {
+  my ($self, $kids) = @_;
+
+  $kids = [ List::Util::shuffle(@$kids) ];
+
+  return ( $kids, "shuffle" );
+}
+
+sub _limit_and_order {
+  my ($self, $req, $kids) = @_;
+
+  my $cgi = $req->cgi;
+  my $type = $cgi->param("type");
+  if ($type) {
+    $kids = [ grep $_->[0]{generator} =~ /::\Q$type\E$/, @$kids ];
   }
+
+  my @order = sort { $b <=> $a } map $_->[1]{$_->[2]}, @$kids;
+  my $sort = join(",", $cgi->param('sort')) || 'current';
+  $sort =~ s/-,/-/g;
+
+  my $order;
+  if ($sort eq 'shuffle') {
+    ($kids, $order) = $self->_shuffle($kids);
+  }
+  else {
+    ($kids, $order) = $self->_sort($sort, $cgi, $kids);
+  }
+
+  for my $i (0..$#$kids) {
+    my $kid = $kids->[$i];
+    $kid->[1]{$kid->[2]} = $order[$i];
+    $kid->[1]->save();
+  }
+
   return [ map $_->[0], @$kids ], $order;
 }
 
index cbb4d51f7e76905f18e5104022c85db54922e9fe..5b06a05d95cb5eed5882c68b853a5dc145b568f5 100644 (file)
         </tr>
 <:if UserCan edit_reorder_children:>
         <tr> 
-          <td colspan="5">Sort by: <a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;sort=title&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>" data-sort="title">title</a> | <a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;sort=date&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>" data-sort="date">date</a> | <a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;reverse=1&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>" data-reverse="1">reverse</a></td>
+          <td colspan="5">Sort by:
+<a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;sort=title&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>" data-sort="title">title</a> |
+<a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;sort=date&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>" data-sort="date">date</a> |
+<a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;reverse=1&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>" data-reverse="1">reverse</a> |
+<a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;sort=shuffle&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>" data-shuffle="1">shuffle</a></td>
         </tr>
 <:or UserCan:><:eif UserCan:>
         <:iterator begin children:> 
index 6e12e06002436cfc96b6fbf8d3903841779a4d5c..26ed67641c5188888c5173e13a4d9b75d27a6c1a 100644 (file)
         </tr>
 <:if UserCan edit_reorder_children:article:>
         <tr> 
-          <td colspan="5" bgcolor="#FFFFFF">Sort by: <a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;sort=title&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>">title</a> | <a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;sort=date&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>">date</a> | <a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;reverse=1&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>">reverse</a></td>
+          <td colspan="5" bgcolor="#FFFFFF">Sort by:
+<a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;sort=title&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>">title</a> |
+<a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;sort=date&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>">date</a> |
+<a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;reverse=1&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>">reverse</a> |
+<a href="/cgi-bin/admin/reorder.pl?parentid=<:article id:>&amp;sort=shuffle&amp;refreshto=/cgi-bin/admin/add.pl?id=<:article id:>">shuffle</a></td>
         </tr>
 <:or UserCan:><:eif UserCan:>
         <:iterator begin children:>