From dee33cc9da90762dea9b9aa13190c65e80f07577 Mon Sep 17 00:00:00 2001
From: Bill Erickson <berick@esilibrary.com>
Date: Mon, 12 Mar 2012 14:11:47 -0400
Subject: [PATCH] Org unit sibling display sort order

Adds a new "sibling_order" column to actor.org_unit which specifies the
order in which an org unit should be sorted as compared to its sibling
org units in org unit trees.

This commit adds the IDL/DB components.  It also updates the tpac and
the generic get_org_tree API.  (Note, a follow-up commit will be pushed
to remove some deprecated / reduntant org tree retrievals).

Note that the sibling_order values do not have to be unique within each
set of siblings.  The org unit name is still used as a tie-breaker sort,
so if the sort order is left unset (i.e. all values are 0), org units
will fall back to name-only sorting.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
---
 Open-ILS/examples/fm_IDL.xml                                      | 1 +
 Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm         | 2 +-
 Open-ILS/src/sql/Pg/005.schema.actors.sql                         | 3 ++-
 .../sql/Pg/upgrade/XXXX.schema.org_unit_opac_vis_and_sorting.sql  | 8 ++++++++
 4 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml
index c9357baab5..8de132df5d 100644
--- a/Open-ILS/examples/fm_IDL.xml
+++ b/Open-ILS/examples/fm_IDL.xml
@@ -4944,6 +4944,7 @@ SELECT  usr,
 			<field reporter:label="Resource Attributes" name="rsrc_attrs" oils_persist:virtual="true" reporter:datatype="link"/>
 			<field reporter:label="Attribute Values" name="attr_vals" oils_persist:virtual="true" reporter:datatype="link"/>
 			<field reporter:label="Hours of Operation" name="hours_of_operation" oils_persist:virtual="true" reporter:datatype="link"/>
+			<field reporter:label="Sibling Sort Order" name="sibling_order" reporter:datatype="int"/>
 		</fields>
 		<links>
 			<link field="billing_address" reltype="has_a" key="id" map="" class="aoa"/>
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
index f46587f551..aa0be426f2 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
@@ -87,7 +87,7 @@ sub init_ro_object_cache {
 			    {   parent_ou => undef},
 			    {   flesh            => -1,
 				    flesh_fields    => {aou =>  ['children']},
-				    order_by        => {aou => 'name'}
+				    order_by        => {aou => 'sibling_order, name'}
 			    }
 		    ])->[0];
 
diff --git a/Open-ILS/src/sql/Pg/005.schema.actors.sql b/Open-ILS/src/sql/Pg/005.schema.actors.sql
index 4f9f39b49e..2a4429c4da 100644
--- a/Open-ILS/src/sql/Pg/005.schema.actors.sql
+++ b/Open-ILS/src/sql/Pg/005.schema.actors.sql
@@ -295,7 +295,8 @@ CREATE TABLE actor.org_unit (
 	email		TEXT,
 	phone		TEXT,
 	opac_visible	BOOL	NOT NULL DEFAULT TRUE,
-	fiscal_calendar INT     NOT NULL DEFAULT 1   -- foreign key constraint to be added later
+	fiscal_calendar INT     NOT NULL DEFAULT 1,   -- foreign key constraint to be added later
+	sibling_order   INT     NOT NULL DEFAULT 0 
 );
 CREATE INDEX actor_org_unit_parent_ou_idx ON actor.org_unit (parent_ou);
 CREATE INDEX actor_org_unit_ou_type_idx ON actor.org_unit (ou_type);
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.org_unit_opac_vis_and_sorting.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.org_unit_opac_vis_and_sorting.sql
index 115613ab6f..d38728035d 100644
--- a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.org_unit_opac_vis_and_sorting.sql
+++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.org_unit_opac_vis_and_sorting.sql
@@ -14,11 +14,19 @@ INSERT INTO config.global_flag (name, enabled, label)
         )
     );
 
+ALTER TABLE actor.org_unit ADD COLUMN 
+    sibling_order INTEGER NOT NULL DEFAULT 0; 
+
+ALTER TABLE auditor.actor_org_unit_history ADD COLUMN 
+    sibling_order INTEGER NOT NULL DEFAULT 0;
+
 COMMIT;
 
 /* UNDO
 BEGIN;
 DELETE FROM config.global_flag WHERE name = 'opac.org_unit.non_inheritied_visibility';
+ALTER TABLE actor.org_unit DROP COLUMN sibling_order;
+ALTER TABLE auditor.actor_org_unit_history DROP COLUMN sibling_order;
 COMMIT;
 */
 
-- 
2.11.0