]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/org_tree_js.pl
added org name sorting to org list retrieval
[Evergreen.git] / Open-ILS / src / extras / org_tree_js.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3
4 # ------------------------------------------------------------
5 # turns the orgTree and orgTypes into js files
6 # ------------------------------------------------------------
7
8 use OpenSRF::System;
9 use OpenILS::Utils::Fieldmapper;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenSRF::Utils::Cache;
12
13 die "usage: perl org_tree_js.pl <bootstrap_config>" unless $ARGV[0];
14 OpenSRF::System->bootstrap_client(config_file => $ARGV[0]);
15
16 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
17
18 # must be loaded after the IDL is parsed
19 require OpenILS::Utils::CStoreEditor;
20
21 warn "removing OrgTree from the cache...\n";
22 my $cache = OpenSRF::Utils::Cache->new;
23 $cache->delete_cache('orgtree');
24
25 # fetch the org_unit's and org_unit_type's
26 my $e = OpenILS::Utils::CStoreEditor->new;
27 my $types = $e->retrieve_all_actor_org_unit_type;
28 my $tree = $e->request(
29     'open-ils.cstore.direct.actor.org_unit.search.atomic',
30     {id => {"!=" => undef}},
31     {order_by => {aou => 'name'}}
32 );
33
34
35 sub val {
36     my $v = shift;
37     return 'null' unless defined $v;
38
39     # required for JS code this is checking truthness 
40     # without using isTrue() (1/0 vs. t/f)
41     return 1 if $v eq 't';
42     return 0 if $v eq 'f';
43
44     $v =~ s/([\x{0080}-\x{fffd}])/sprintf('\u%04x',ord($1))/sgoe;
45
46     return "\"$v\"";
47 }
48
49 my $pile = "var _l = [";
50
51 my @array;
52 for my $o (@$tree) {
53         my ($i,$t,$p,$n,$v) = ($o->id,$o->ou_type,$o->parent_ou,val($o->name),val($o->opac_visible));
54     $p ||= 'null';
55         push @array, "[$i,$t,$p,$n,$v]";
56 }
57
58 $pile .= join ',', @array;
59 $pile .= "]; /* Org Units */ \n";
60
61
62 $pile .= 'globalOrgTypes = [';
63 for my $t (@$types) {
64     my ($u,$v,$d,$i,$n,$o,$p) = (val($t->can_have_users),val($t->can_have_vols),$t->depth,$t->id,val($t->name),val($t->opac_label),$t->parent);
65     $p ||= 'null';
66     $pile .= "new aout([null,null,null,null,$u,$v,$d,$i,$n,$o,$p]), ";
67 }
68 $pile =~ s/, $//; # remove trailing comma
69 $pile .= ']; /* OU Types */';
70
71 print "$pile\n";
72
73