#!/usr/bin/perl
-# turns the orgTree and orgTypes into a static HTML option list
+# for each supported locale, turn the orgTree and orgTypes into a static HTML option list
use OpenSRF::AppSession;
use OpenSRF::System;
use OpenILS::Utils::Fieldmapper;
use OpenSRF::Utils::SettingsClient;
+use OpenILS::Application::AppUtils;
use Unicode::Normalize;
use Data::Dumper;
+use File::Spec;
-die "usage: perl org_tree_html_options.pl <bootstrap_config> <output_file>" unless $ARGV[1];
+die "usage: perl org_tree_html_options.pl <bootstrap_config> <output_path> <output_file>" unless $ARGV[2];
OpenSRF::System->bootstrap_client(config_file => $ARGV[0]);
-open FILE, ">$ARGV[1]";
-
-Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
-
-my $ses = OpenSRF::AppSession->create("open-ils.actor");
-my $tree = $ses->request("open-ils.actor.org_tree.retrieve")->gather(1);
+my $path = $ARGV[1];
+my $filename = $ARGV[2];
my @types;
-my $aout = $ses->request("open-ils.actor.org_types.retrieve")->gather(1);
-foreach my $type (@$aout) {
- $types[int($type->id)] = $type;
-}
-print_option($tree);
+Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
+
+#Get our list of locales
+my $session = OpenSRF::AppSession->create("open-ils.cstore");
+my $locales = $session->request("open-ils.cstore.direct.config.i18n_locale.search.atomic", {"code" => {"!=" => undef}}, {"order_by" => {"i18n_l" => "name"}})->gather();
+$session->disconnect();
-$ses->disconnect();
-close FILE;
+foreach my $locale (@$locales) {
+ my $ses = OpenSRF::AppSession->create("open-ils.actor");
+ $ses->session_locale($locale->code);
+ my $tree = $ses->request("open-ils.actor.org_tree.retrieve")->gather(1);
+ my $aout = $ses->request("open-ils.actor.org_types.retrieve")->gather(1);
+ foreach my $type (@$aout) {
+ $types[int($type->id)] = $type;
+ }
+ my $dir = File::Spec->catdir($path, $locale->code);
+ if (!-d $dir) {
+ mkdir($dir) or die "Could not create output directory: $dir $!\n";
+ }
+ my @org_tree_html;
+ print_option($tree, \@org_tree_html);
+ $ses->disconnect();
+ open(FILE, '>', File::Spec->catfile($dir, $filename)) or die $!;
+ print FILE @org_tree_html;
+ close FILE;
+}
sub print_option {
my $node = shift;
+ my $org_tree_html = shift;
+
return unless ($node->opac_visible =~ /^[y1t]+/i);
my $depth = $types[$node->ou_type]->depth;
- my $sname = entityize($node->shortname);
- my $name = entityize($node->name);
+ my $sname = OpenILS::Application::AppUtils->entityize($node->shortname);
+ my $name = OpenILS::Application::AppUtils->entityize($node->name);
my $kids = $node->children;
- print FILE "<option value='$sname'>" . '   'x$depth . "$name</option>\n";
- print_option($_) for (@$kids);
-}
-
-sub entityize {
- my $stuff = shift || return "";
- $stuff =~ s/\</</og;
- $stuff =~ s/\>/>/og;
- $stuff =~ s/\&/&/og;
- $stuff = NFD($stuff);
- $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
- return $stuff;
+ push @$org_tree_html, "<option value='$sname'>" . '   'x$depth . "$name</option>\n";
+ print_option($_, $org_tree_html) for (@$kids);
}
use OpenILS::Utils::Fieldmapper;
use OpenSRF::Utils::SettingsClient;
use OpenSRF::Utils::Cache;
+use File::Spec;
-die "usage: perl org_tree_js.pl <bootstrap_config>" unless $ARGV[0];
+die "usage: perl org_tree_js.pl <bootstrap_config> <path> <filename>" unless $ARGV[2];
OpenSRF::System->bootstrap_client(config_file => $ARGV[0]);
-my $locale = $ARGV[1] || '';
+my $path = $ARGV[1];
+my $filename = $ARGV[2];
Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
# must be loaded after the IDL is parsed
require OpenILS::Utils::CStoreEditor;
-warn "removing OrgTree from the cache...\n";
-my $cache = OpenSRF::Utils::Cache->new;
-$cache->delete_cache("orgtree.$locale");
-
-# fetch the org_unit's and org_unit_type's
-my $e = OpenILS::Utils::CStoreEditor->new;
-$e->session->session_locale($locale) if ($locale);
-
-my $types = $e->retrieve_all_actor_org_unit_type;
-my $tree = $e->request(
- 'open-ils.cstore.direct.actor.org_unit.search.atomic',
- {id => {"!=" => undef}},
- {order_by => {aou => 'name'}, no_i18n => $locale ? 0 : 1 }
-);
+# Get our list of locales
+my $session = OpenSRF::AppSession->create("open-ils.cstore");
+my $locales = $session->request("open-ils.cstore.direct.config.i18n_locale.search.atomic", {"code" => {"!=" => undef}}, {"order_by" => {"i18n_l" => "name"}})->gather();
+$session->disconnect();
+
+foreach my $locale (@$locales) {
+ warn "removing OrgTree from the cache for locale " . $locale->code . "...\n";
+ my $cache = OpenSRF::Utils::Cache->new;
+ $cache->delete_cache("orgtree.$locale->code");
+
+ # fetch the org_unit's and org_unit_type's
+ my $e = OpenILS::Utils::CStoreEditor->new;
+ $e->session->session_locale($locale->code) if ($locale->code);
+
+ my $types = $e->retrieve_all_actor_org_unit_type;
+ my $tree = $e->request(
+ 'open-ils.cstore.direct.actor.org_unit.search.atomic',
+ {id => {"!=" => undef}},
+ {order_by => {aou => 'name'}, no_i18n => $locale->code ? 0 : 1 }
+ );
+ my $dir = File::Spec->catdir($path, $locale->code);
+ if (!-d $dir) {
+ mkdir($dir);
+ }
+ build_tree_js($types, $tree, File::Spec->catfile($dir, $filename));
+}
sub val {
return "\"$v\"";
}
-my $pile = "var _l = [";
-
-my @array;
-for my $o (@$tree) {
- 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));
- $p ||= 'null';
- push @array, "[$i,$t,$p,$n,$v,$s]";
-}
-
-$pile .= join ',', @array;
-$pile .= "]; /* Org Units */ \n";
-
-
-$pile .= 'var globalOrgTypes = [';
-for my $t (@$types) {
- 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);
- $p ||= 'null';
- $pile .= "new aout([null,null,null,null,$u,$v,$d,$i,$n,$o,$p]), ";
+sub build_tree_js {
+ my $types = shift;
+ my $tree = shift;
+ my $outfile = shift;
+
+ my $pile = "var _l = [";
+
+ my @array;
+ for my $o (@$tree) {
+ 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));
+ $p ||= 'null';
+ push @array, "[$i,$t,$p,$n,$v,$s]";
+ }
+
+ $pile .= join ',', @array;
+ $pile .= "]; /* Org Units */ \n";
+
+
+ $pile .= 'var globalOrgTypes = [';
+ for my $t (@$types) {
+ 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);
+ $p ||= 'null';
+ $pile .= "new aout([null,null,null,null,$u,$v,$d,$i,$n,$o,$p]), ";
+ }
+ $pile =~ s/, $//; # remove trailing comma
+ $pile .= ']; /* OU Types */';
+ open(OUTFH, '>', $outfile) or die "Could not open $outfile : $!";
+ print OUTFH "$pile\n";
+ close(OUTFH);
}
-$pile =~ s/, $//; # remove trailing comma
-$pile .= ']; /* OU Types */';
-print "$pile\n";