]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/org_tree_js.pl
2bed194f9f1dd70f781d2943b00681a601cffa8b
[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 my $locale = $ARGV[1] || '';
17
18 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
19
20 # must be loaded after the IDL is parsed
21 require OpenILS::Utils::CStoreEditor;
22
23 warn "removing OrgTree from the cache...\n";
24 my $cache = OpenSRF::Utils::Cache->new;
25 $cache->delete_cache("orgtree.$locale");
26
27 # fetch the org_unit's and org_unit_type's
28 my $e = OpenILS::Utils::CStoreEditor->new;
29 $e->session->session_locale($locale) if ($locale);
30
31 my $types = $e->retrieve_all_actor_org_unit_type;
32 my $tree = $e->request(
33     'open-ils.cstore.direct.actor.org_unit.search.atomic',
34     {id => {"!=" => undef}},
35     {order_by => {aou => 'name'}, no_i18n => $locale ? 0 : 1 }
36 );
37
38
39 sub val {
40     my $v = shift;
41     return 'null' unless defined $v;
42
43     # required for JS code this is checking truthness 
44     # without using isTrue() (1/0 vs. t/f)
45     return 1 if $v eq 't';
46     return 0 if $v eq 'f';
47
48     $v =~ s/([\x{0080}-\x{fffd}])/sprintf('\u%04x',ord($1))/sgoe;
49
50     return "\"$v\"";
51 }
52
53 my $pile = "var _l = [";
54
55 my @array;
56 for my $o (@$tree) {
57         my ($i,$t,$p,$n,$v,$s) = ($o->id,$o->ou_type,$o->parent_ou,val($o->name),val($o->opac_visible),val($o->shortname));
58     $p ||= 'null';
59         push @array, "[$i,$t,$p,$n,$v,$s]";
60 }
61
62 $pile .= join ',', @array;
63 $pile .= "]; /* Org Units */ \n";
64
65
66 $pile .= 'var globalOrgTypes = [';
67 for my $t (@$types) {
68     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);
69     $p ||= 'null';
70     $pile .= "new aout([null,null,null,null,$u,$v,$d,$i,$n,$o,$p]), ";
71 }
72 $pile =~ s/, $//; # remove trailing comma
73 $pile .= ']; /* OU Types */';
74
75 print "$pile\n";
76
77