]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/extras/org_tree_js.pl
Executable scripts
[working/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 use File::Spec;
13
14 die "usage: perl org_tree_js.pl <bootstrap_config> <path> <filename>" unless $ARGV[2];
15 OpenSRF::System->bootstrap_client(config_file => $ARGV[0]);
16
17 my $path = $ARGV[1];
18 my $filename = $ARGV[2];
19
20 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
21
22 # must be loaded after the IDL is parsed
23 require OpenILS::Utils::CStoreEditor;
24
25 # Get our list of locales
26 my $session = OpenSRF::AppSession->create("open-ils.cstore");
27 my $locales = $session->request("open-ils.cstore.direct.config.i18n_locale.search.atomic", {"code" => {"!=" => undef}}, {"order_by" => {"i18n_l" => "name"}})->gather();
28 $session->disconnect();
29
30 foreach my $locale (@$locales) {
31         warn "removing OrgTree from the cache for locale " . $locale->code . "...\n";
32         my $cache = OpenSRF::Utils::Cache->new;
33         $cache->delete_cache("orgtree.$locale->code");
34
35         # fetch the org_unit's and org_unit_type's
36         my $e = OpenILS::Utils::CStoreEditor->new;
37         $e->session->session_locale($locale->code) if ($locale->code);
38
39         my $types = $e->retrieve_all_actor_org_unit_type;
40         my $tree = $e->request(
41                 'open-ils.cstore.direct.actor.org_unit.search.atomic',
42                 {id => {"!=" => undef}},
43                 {order_by => {aou => 'name'}, no_i18n => $locale->code ? 0 : 1 }
44         );
45         my $dir = File::Spec->catdir($path, $locale->code);
46         if (!-d $dir) {
47                 mkdir($dir);
48         }
49         build_tree_js($types, $tree, File::Spec->catfile($dir, $filename));
50 }
51
52
53 sub val {
54     my $v = shift;
55     return 'null' unless defined $v;
56
57     # required for JS code this is checking truthness 
58     # without using isTrue() (1/0 vs. t/f)
59     return 1 if $v eq 't';
60     return 0 if $v eq 'f';
61
62     $v =~ s/([\x{0080}-\x{fffd}])/sprintf('\u%04x',ord($1))/sgoe;
63
64     return "\"$v\"";
65 }
66
67 sub build_tree_js {
68         my $types = shift;
69         my $tree = shift;
70         my $outfile = shift;
71
72         my $pile = "var _l = [";
73
74         my @array;
75         for my $o (@$tree) {
76                 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));
77                 $p ||= 'null';
78                 push @array, "[$i,$t,$p,$n,$v,$s]";
79         }
80
81         $pile .= join ',', @array;
82         $pile .= "]; /* Org Units */ \n";
83
84
85         $pile .= 'var globalOrgTypes = [';
86         for my $t (@$types) {
87                 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);
88                 $p ||= 'null';
89                 $pile .= "new aout([null,$u,$v,$d,$i,$n,$o,$p]), ";
90         }
91         $pile =~ s/, $//; # remove trailing comma
92                 $pile .= ']; /* OU Types */';
93         open(OUTFH, '>', $outfile) or die "Could not open $outfile : $!";
94         print OUTFH "$pile\n";
95         close(OUTFH);
96 }
97
98
99