Acq: add default sort order to canned searches, and provide middle layer
authorsenator <senator@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 21 Jun 2010 20:20:57 +0000 (20:20 +0000)
committersenator <senator@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 21 Jun 2010 20:20:57 +0000 (20:20 +0000)
support for sort order for unified search generally.  The UI for that may
come later.

git-svn-id: svn://svn.open-ils.org/ILS/trunk@16768 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Acq/Search.pm
Open-ILS/web/js/dojo/openils/acq/nls/acq.js
Open-ILS/web/js/ui/default/acq/search/unified.js

index e81977b..753e032 100644 (file)
@@ -366,8 +366,35 @@ sub unified_search {
     my $retriever = $RETRIEVERS{$ret_type};
     my $hint = F("acq::$ret_type")->{"hint"};
 
+    my $select_clause = {
+        $hint => [{"column" => "id", "transform" => "distinct"}]
+    };
+
+    if ($options->{"order_by"}) {
+        # What's the point of this block?  When using ORDER BY in conjuction
+        # with SELECT DISTINCT, the fields present in ORDER BY have to also
+        # be in the SELECT clause.  This will take _one_ such field and add
+        # it to the SELECT clause as needed.
+        my ($order_by, $class, $field);
+        unless (
+            ($order_by = $options->{"order_by"}->[0]) &&
+            ($class = $order_by->{"class"}) =~ /^[\da-z_]+$/ &&
+            ($field = $order_by->{"field"}) =~ /^[\da-z_]+$/
+        ) {
+            $e->disconnect;
+            return new OpenILS::Event(
+                "BAD_PARAMS", "note" =>
+q/order_by clause must be of the long form, like:
+"order_by": [{"class": "foo", "field": "bar", "direction": "asc"}]/
+            );
+        } else {
+            $select_clause->{$class} ||= [];
+            push @{$select_clause->{$class}}, $field;
+        }
+    }
+
     my $query = {
-        "select" => {$hint => [{"column" => "id", "transform" => "distinct"}]},
+        "select" => $select_clause,
         "from" => {
             "jub" => {
                 "acqpo" => {
@@ -394,7 +421,7 @@ sub unified_search {
                 }
             }
         },
-        "order_by" => {$hint => {"id" => {}}},
+        "order_by" => ($options->{"order_by"} || {$hint => {"id" => {}}}),
         "offset" => ($options->{"offset"} || 0)
     };
 
@@ -430,11 +457,12 @@ sub unified_search {
     }
 
     my $results = $e->json_query($query) or return $e->die_event;
+    my @id_list = map { $_->{"id"} } (grep { $_->{"id"} } @$results);
+
     if ($options->{"id_list"}) {
-        $conn->respond($_->{"id"}) foreach (grep { $_->{"id"} } @$results);
+        $conn->respond($_) foreach @id_list;
     } else {
-        $conn->respond($retriever->($e, $_->{"id"}, $options))
-            foreach (grep { $_->{"id"} } @$results);
+        $conn->respond($retriever->($e, $_, $options)) foreach @id_list;
     }
 
     $e->disconnect;
index a308ab1..f8f3781 100644 (file)
@@ -74,5 +74,6 @@
     "PRINT" : "Print",
     "INVOICES" : "Invoices",
     "NUM_CLAIMS_EXISTING" : "Claims (${0} existing)",
-    "LOAD_TERMS_FIRST" : "You can't retrieve records until you've loaded a CSV file\nwith bibliographic IDs in the first column."
+    "LOAD_TERMS_FIRST" : "You can't retrieve records until you've loaded a CSV file\nwith bibliographic IDs in the first column.",
+    "SELECT_SEARCH_FIELD": "Select Search Field"
 }
index 9b279b2..91f8190 100644 (file)
@@ -72,7 +72,7 @@ function TermSelectorFactory(terms) {
             "disabled": "disabled",
             "selected": "selected",
             "value": "",
-            "innerHTML": "Select Search Field" // XXX i18n
+            "innerHTML": localeStrings.SELECT_SEARCH_FIELD
         })
     );
 
@@ -736,7 +736,7 @@ function ResultManager(liPager, poGrid, plGrid, invGrid) {
             "&c=" + dojo.byId("acq-unified-conjunction").getValue();
     };
 
-    this.search = function(search_object, termManager) {
+    this.search = function(uriManager, termManager) {
         var bib_search_string = null;
         this.count_results = 0;
         this.result_type = dojo.byId("acq-unified-result-type").getValue();
@@ -761,7 +761,9 @@ function ResultManager(liPager, poGrid, plGrid, invGrid) {
 
         this.params[
             dojo.byId("acq-unified-conjunction").getValue() == "and" ? 1 : 2
-        ] = search_object;
+        ] = uriManager.search_object;
+        if (uriManager.order_by)
+            this.params[4].order_by = uriManager.order_by;
 
         var interface = this.result_types[this.result_type].interface;
         interface.dataLoader = this._dataLoader;
@@ -800,7 +802,10 @@ function URIManager() {
                 ]
             },
             "result_type": "purchase_order",
-            "conjunction": "and"
+            "conjunction": "and",
+            "order_by": [
+                {"class": "acqpo", "field": "edit_time", "direction": "desc"}
+            ]
         },
         "pl": {
             "search_object": {
@@ -809,7 +814,10 @@ function URIManager() {
                 ]
             },
             "result_type": "picklist",
-            "conjunction": "and"
+            "conjunction": "and",
+            "order_by": [
+                {"class": "acqpl", "field": "edit_time", "direction": "desc"}
+            ]
         },
         "inv": {
             "search_object": {
@@ -819,7 +827,10 @@ function URIManager() {
                 ]
             },
             "result_type": "invoice",
-            "conjunction": "and"
+            "conjunction": "and",
+            "order_by": [
+                {"class": "acqinv", "field": "recv_date", "direction": "desc"}
+            ]
         }
     };
 
@@ -861,7 +872,7 @@ openils.Util.addOnLoad(
             hideForm();
             openils.Util.show("acq-unified-body");
             termManager.reflect(uriManager.search_object);
-            resultManager.search(uriManager.search_object, termManager);
+            resultManager.search(uriManager, termManager);
         } else {
             termManager.addRow();
             openils.Util.show("acq-unified-body");