]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/org_tree_js.pl
Fix up the import script.
[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 $tree = $e->retrieve_all_actor_org_unit;
28 my $types = $e->retrieve_all_actor_org_unit_type;
29
30
31 sub val {
32     my $v = shift;
33     return 'null' unless defined $v;
34
35     # required for JS code this is checking truthness 
36     # without using isTrue() (1/0 vs. t/f)
37     return 1 if $v eq 't';
38     return 0 if $v eq 'f';
39
40     return "\"$v\"";
41 }
42
43 my $pile = "var _l = [";
44
45 my @array;
46 for my $o (@$tree) {
47         my ($i,$t,$p,$n,$v) = ($o->id,$o->ou_type,val($o->parent_ou),$o->name,val($o->opac_visible));
48     $p ||= 'null';
49         push @array, "[$i,$t,$p,\"$n\",$v]";
50 }
51
52 $pile .= join ',', @array;
53 $pile .= "];\n";
54
55
56 $pile .= 'globalOrgTypes = [';
57 for my $t (@$types) {
58     $pile .= 'new aout([null,null,null,null,'.
59         val($t->can_have_users).','.
60         val($t->can_have_vols).','.
61         val($t->depth).','.
62         val($t->id).','.
63         val($t->name).','.
64         val($t->opac_label).','.
65         val($t->parent).']), ';
66 }
67 $pile =~ s/, $//; # remove trailing comma
68 $pile .= '];';
69
70 print "$pile\n";
71
72