added untested org tree searching method
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 7 Jul 2006 17:04:19 +0000 (17:04 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 7 Jul 2006 17:04:19 +0000 (17:04 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@4932 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Circ/ScriptBuilder.pm

index 8dd4cb1..d7e1a82 100644 (file)
@@ -13,6 +13,8 @@ my @COPY_STATUSES;
 my @COPY_LOCATIONS;
 my %GROUP_SET;
 my $GROUP_TREE;
+my $ORG_TREE;
+my @ORG_LIST;
 
 
 # -----------------------------------------------------------------------
@@ -52,7 +54,8 @@ sub build {
 sub build_runner {
        my $editor      = shift;
        my $ctx         = shift;
-       my $runner      = OpenILS::Utils::ScriptRunner->new;
+
+       my $runner = OpenILS::Utils::ScriptRunner->new;
 
        $runner->insert( "$evt.groupTree",      $GROUP_TREE, 1);
 
@@ -70,6 +73,9 @@ sub build_runner {
        $runner->insert("$evt.$_", $ctx->{_direct}->{$_}) for keys %{$ctx->{_direct}};
 
        $ctx->{runner} = $runner;
+
+       insert_org_methods( $editor, $ctx );
+
        return $runner;
 }
 
@@ -171,16 +177,12 @@ sub fetch_user_data {
                        ]
                )->[0];
 
-               _flatten_groups($GROUP_TREE);
+               flatten_groups($GROUP_TREE);
        }
 
        $patron->profile( $GROUP_SET{$patron->profile} )
                unless ref $patron->profile;
 
-#      $patron->profile( 
-#              grep { $_->id == $patron->profile } @GROUP_LIST ) 
-#              unless ref $patron->profile;
-
        $patron->card($e->retrieve_actor_card($patron->card));
 
        $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
@@ -215,16 +217,65 @@ sub fetch_user_data {
 }
 
 
-sub _flatten_groups {
+sub flatten_groups {
        my $tree = shift;
        return undef unless $tree;
        $GROUP_SET{$tree->id} = $tree;
        if( $tree->children ) {
-               _flatten_groups($_) for @{$tree->children};
+               flatten_groups($_) for @{$tree->children};
+       }
+}
+
+sub flatten_org_tree {
+       my $tree = shift;
+       return undef unless $tree;
+       push( @ORG_LIST, $tree );
+       if( $tree->children ) {
+               flatten_org_tree($_) for @{$tree->children};
+       }
+}
+
+
+
+sub insert_org_methods {
+       my ( $editor, $ctx ) = @_;
+       my $runner = $ctx->{runner};
+
+
+       if(!$ORG_TREE) {
+               $ORG_TREE = $editor->search_actor_org_unit(
+                       [
+                               {"parent_ou" => undef },
+                               {
+                                       flesh                           => 2,
+                                       flesh_fields    => { aou =>  ['children'] },
+                                       order_by                        => { aou => 'name'}
+                               }
+                       ]
+               )->[0];
+               flatten_org_tree($ORG_TREE);
        }
+
+       $runner->insert("$evt.__OILS_FUNC_isOrgDescendent", 
+               sub {
+                       my( $sname, $id ) = @_;
+                       my ($parent)    = grep { $_->shortname eq $sname } @ORG_LIST;
+                       my ($child)             = grep { $_->id == $id } @ORG_LIST;
+                       return is_org_descendent( $parent, $child );
+               }
+       );
 }
 
 
+sub is_org_descendent {
+       my( $parent, $child ) = @_;
+       return 0 unless $parent and $child;
+       do {
+               return 1 if $parent->id == $child->id;
+       } while( ($child) = grep { $_->id == $child->parent_ou } @ORG_LIST );
+       return 0;
+}
+
 1;