From 97bd3a0480cabba17db12c3c58670400525cdacc Mon Sep 17 00:00:00 2001 From: Dan Scott Date: Mon, 15 Aug 2011 18:03:37 -0400 Subject: [PATCH] Revamp autogen.sh to call functions from a Perl module This cut moves all of the logic from the individual scripts that autogen.sh used to call into a self-contained Perl module. We use OpenILS::Utils::Cronscript to avoid much of the init dupe code. Note that calling Perl functions from a bash script is not crazy pretty, but perhaps in the long run autogen.sh becomes something else. This is just a step towards consolidating those functions in a single module while maintaining the same interface as before. Signed-off-by: Dan Scott Signed-off-by: Thomas Berezansky --- Open-ILS/src/extras/autogen.sh | 18 +- Open-ILS/src/extras/facet_types_js.pl | 97 ------ Open-ILS/src/extras/fieldmapper.pl | 67 ---- Open-ILS/src/extras/locale_html_options.pl | 38 --- Open-ILS/src/extras/org_lasso_js.pl | 41 --- Open-ILS/src/extras/org_tree_html_options.pl | 64 ---- Open-ILS/src/extras/org_tree_js.pl | 99 ------ Open-ILS/src/extras/org_tree_proximity.pl | 39 --- .../src/perlmods/lib/OpenILS/Utils/Configure.pm | 358 +++++++++++++++++++++ 9 files changed, 367 insertions(+), 454 deletions(-) delete mode 100755 Open-ILS/src/extras/facet_types_js.pl delete mode 100755 Open-ILS/src/extras/fieldmapper.pl delete mode 100755 Open-ILS/src/extras/locale_html_options.pl delete mode 100755 Open-ILS/src/extras/org_lasso_js.pl delete mode 100755 Open-ILS/src/extras/org_tree_html_options.pl delete mode 100755 Open-ILS/src/extras/org_tree_js.pl delete mode 100755 Open-ILS/src/extras/org_tree_proximity.pl create mode 100644 Open-ILS/src/perlmods/lib/OpenILS/Utils/Configure.pm diff --git a/Open-ILS/src/extras/autogen.sh b/Open-ILS/src/extras/autogen.sh index 0c36edbfec..0e47148e2a 100755 --- a/Open-ILS/src/extras/autogen.sh +++ b/Open-ILS/src/extras/autogen.sh @@ -43,7 +43,7 @@ function usage { echo "Examples:"; echo ""; echo " Update organization tree and fieldmapper IDL:"; - echo " $0 -c SYSCONFDIR/opensrf_core.xml"; + echo " $0"; echo ""; echo " Update organization tree and refresh proximity:"; echo " $0 -u -c SYSCONFDIR/opensrf_core.xml"; @@ -97,41 +97,41 @@ echo "Updating Evergreen organization tree and IDL using '$CONFIG'" echo "" echo "Updating fieldmapper"; -perl fieldmapper.pl "$CONFIG" > "$JSDIR/fmall.js"; +perl -MOpenILS::Utils::Configure -e 'print OpenILS::Utils::Configure::fieldmapper();' > "$JSDIR/fmall.js"; cp "$JSDIR/fmall.js" "$FMDOJODIR/" echo " -> $JSDIR/fmall.js"; echo "Updating web_fieldmapper"; -perl fieldmapper.pl "$CONFIG" "web_core" > "$JSDIR/fmcore.js"; +perl -MOpenILS::Utils::Configure -e 'print OpenILS::Utils::Configure::fieldmapper("web_core");' > "$JSDIR/fmcore.js"; echo " -> $JSDIR/fmcore.js"; echo "Updating OrgTree"; -perl org_tree_js.pl "$CONFIG" "$JSDIR" "OrgTree.js"; +perl -MOpenILS::Utils::Configure -e "OpenILS::Utils::Configure::org_tree_js('$JSDIR', 'OrgTree.js');' cp "$JSDIR/en-US/OrgTree.js" "$FMDOJODIR/" echo " -> $JSDIR/*/OrgTree.js"; echo "Updating OrgTree HTML"; -perl org_tree_html_options.pl "$CONFIG" "$SLIMPACDIR" "lib_list.inc"; +perl -MOpenILS::Utils::Configure -e "OpenILS::Utils::Configure::org_tree_html_options('$SLIMPACDIR', 'lib_list.inc');" echo " -> $SLIMPACDIR/*/lib_list.inc"; echo "Updating locales selection HTML"; -perl locale_html_options.pl "$CONFIG" "$SLIMPACDIR/locales.inc"; +perl -MOpenILS::Utils::Configure -e "print OpenILS::Utils::Configure::locale_html_options();" > "$SLIMPACDIR/locales.inc" echo " -> $SLIMPACDIR/*/locales.inc"; echo "Updating Search Groups"; -perl org_lasso_js.pl "$CONFIG" > "$JSDIR/OrgLasso.js"; +perl -MOpenILS::Utils::Configure -e "print OpenILS::Utils::Configure::org_lasso();" > "$JSDIR/OrgLasso.js"; cp "$JSDIR/OrgLasso.js" "$FMDOJODIR/" echo " -> $JSDIR/OrgLasso.js"; echo "Updating Facet Definitions"; -perl facet_types_js.pl "$CONFIG" "$JSDIR" "FacetDefs.js"; +perl -MOpenILS::Utils::Configure -e "OpenILS::Utils::Configure::facet_types('$JSDIR', 'FacetDefs.js');" cp "$JSDIR/en-US/FacetDefs.js" "$FMDOJODIR/" echo " -> $JSDIR/*/FacetDefs.js"; if [ ! -z "$PROXIMITY" ] then echo "Refreshing proximity of org units"; - perl org_tree_proximity.pl "$CONFIG"; + perl -MOpenILS::Utils::Configure -e "OpenILS::Utils::Configure::org_tree_proximity();" fi echo "Creating combined JS..." diff --git a/Open-ILS/src/extras/facet_types_js.pl b/Open-ILS/src/extras/facet_types_js.pl deleted file mode 100755 index 3bae8ea283..0000000000 --- a/Open-ILS/src/extras/facet_types_js.pl +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/perl -use strict; use warnings; - -# ------------------------------------------------------------ -# turns the facet fields defined on config.metabib_field into JS -# ------------------------------------------------------------ - -use OpenSRF::System; -use OpenILS::Utils::Fieldmapper; -use OpenSRF::Utils::SettingsClient; -use OpenSRF::Utils::Cache; -use File::Spec; - -die "usage: $0 " unless $ARGV[2]; -OpenSRF::System->bootstrap_client(config_file => $ARGV[0]); - -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; - -# 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 facet list from the cache for locale " . $locale->code . "...\n"; - my $cache = OpenSRF::Utils::Cache->new; - $cache->delete_cache("facet_definition.".$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.config.metabib_field.search.atomic', - { facet_field => 't' }, - { no_i18n => $locale->code ? 0 : 1, - flesh => 1, - flesh_fields => { cmf => [ 'field_class' ] } - } - ); - my $dir = File::Spec->catdir($path, $locale->code); - if (!-d $dir) { - mkdir($dir); - } - build_tree_js($tree, File::Spec->catfile($dir, $filename)); -} - - -sub val { - my $v = shift; - return 'null' unless defined $v; - - # required for JS code this is checking truthness - # without using isTrue() (1/0 vs. t/f) - return 1 if $v eq 't'; - return 0 if $v eq 'f'; - - $v =~ s/([\x{0080}-\x{fffd}])/sprintf('\u%04x',ord($1))/sgoe; - - return "\"$v\""; -} - -sub build_tree_js { - my $tree = shift; - my $outfile = shift; - - my $pile = "var globalFacets = {"; - my @array; - for my $o (@$tree) { - my %hash = ( - id => $o->id, - name => val($o->name), - label => val($o->label), - classname => val($o->field_class->name), - classlabel => val($o->field_class->label) - ); - - $pile .= $hash{id}.':{'.join(',', map { "$_:$hash{$_}" } keys %hash).'},'; - } - - $pile =~ s/,$//; # remove trailing comma - $pile .= "}; /* Facets */"; - - open(OUTFH, '>', $outfile) or die "Could not open $outfile : $!"; - print OUTFH "$pile\n"; - close(OUTFH); -} - - - diff --git a/Open-ILS/src/extras/fieldmapper.pl b/Open-ILS/src/extras/fieldmapper.pl deleted file mode 100755 index d365bad8f3..0000000000 --- a/Open-ILS/src/extras/fieldmapper.pl +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/perl -use strict; use warnings; -use Data::Dumper; -use OpenILS::Utils::Fieldmapper; -use OpenSRF::Utils::SettingsClient; - -OpenSRF::System->bootstrap_client(config_file => $ARGV[0]); -Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL")); - -my $map = $Fieldmapper::fieldmap; - -# if a true value is provided, we generate the web (light) version of the fieldmapper -my $web = $ARGV[0]; -if(!$web) { $web = ""; } - - -my @web_core = qw/ - aou au perm_ex ex aout - mvr ccs ahr aua ac actscecm cbreb acpl - cbrebi acpn acp acnn acn bren asc asce - clfm cifm citm cam ahtc - asv asva asvr asvq - circ ccs ahn bre mrd - crcd crmf crrf mbts aoc aus - mous mobts mb ancc cnct cnal - /; - -my @reports = qw/ perm_ex ex ao aou aout /; - - -print "var _c = {};\n"; - -for my $object (keys %$map) { - - my $hint = $map->{$object}->{hint}; - - if($web eq "web_core") { - next unless (grep { $_ eq $hint } @web_core ); - } - - if($web eq "reports") { - next unless (grep { $_ eq $hint } @web_core ); - } - - - my $short_name = $map->{$object}->{hint}; - - my @fields; - for my $field (keys %{$map->{$object}->{fields}}) { - my $position = $map->{$object}->{fields}->{$field}->{position}; - $fields[$position] = $field; - } - - print "_c[\"$short_name\"] = ["; - for my $f (@fields) { - next unless $f; - if( $f ne "isnew" and $f ne "ischanged" and $f ne "isdeleted" ) { - print "\"$f\","; - } - } - print "];\n"; - - -} - -print "var fmclasses = _c;\n"; - diff --git a/Open-ILS/src/extras/locale_html_options.pl b/Open-ILS/src/extras/locale_html_options.pl deleted file mode 100755 index 3675e23e81..0000000000 --- a/Open-ILS/src/extras/locale_html_options.pl +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/perl -use strict; -use warnings; -# Turns supported locales into a static HTML option list - -use OpenSRF::AppSession; -use OpenSRF::System; -use OpenILS::Utils::Fieldmapper; -use OpenSRF::Utils::SettingsClient; -use OpenILS::Application::AppUtils; - -die "usage: perl locale_html_options.pl " unless $ARGV[1]; -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.cstore"); -my $locales = $ses->request("open-ils.cstore.direct.config.i18n_locale.search.atomic", {"code" => {"!=" => undef}}, {"order_by" => {"i18n_l" => "name"}})->gather(); - -print_option($locales); - -$ses->disconnect(); -close FILE; - - -sub print_option { - my $locales = shift; - print FILE "\n"; -} - diff --git a/Open-ILS/src/extras/org_lasso_js.pl b/Open-ILS/src/extras/org_lasso_js.pl deleted file mode 100755 index c7cd693301..0000000000 --- a/Open-ILS/src/extras/org_lasso_js.pl +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/perl -use strict; use warnings; - -# ------------------------------------------------------------ -# turns the actor.org_lasso table into a js file -# ------------------------------------------------------------ - -use OpenSRF::System; -use OpenILS::Utils::Fieldmapper; -use OpenSRF::Utils::SettingsClient; -use OpenSRF::Utils::JSON; - -die "usage: perl org_tree_js.pl " unless $ARGV[0]; -OpenSRF::System->bootstrap_client(config_file => $ARGV[0]); - -Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL")); - -# must be loaded after the IDL is parsed -require OpenILS::Utils::CStoreEditor; - -# fetch the org_unit's and org_unit_type's -my $e = OpenILS::Utils::CStoreEditor->new; -my $lassos = $e->request( - 'open-ils.cstore.direct.actor.org_lasso.search.atomic', - {id => {"!=" => undef}}, - {order_by => {lasso => 'name'}} -); - -# We need at least one defined search group; otherwise, just generate an empty array -if (scalar(@$lassos) > 0) { - print - "var _lasso = [\n new lasso(" . - join( "),\n new lasso(", map { OpenSRF::Utils::JSON->perl2JSON( bless($_, 'ARRAY') ) } @$lassos ) . - ")\n]; /* Org Search Groups (Lassos) */ \n"; -} else { - print < " unless $ARGV[2]; -OpenSRF::System->bootstrap_client(config_file => $ARGV[0]); - -my $path = $ARGV[1]; -my $filename = $ARGV[2]; - -my @types; - -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(); - -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 = OpenILS::Application::AppUtils->entityize($node->shortname); - my $name = OpenILS::Application::AppUtils->entityize($node->name); - my $kids = $node->children; - - push @$org_tree_html, "\n"; - print_option($_, $org_tree_html) for (@$kids); -} - diff --git a/Open-ILS/src/extras/org_tree_js.pl b/Open-ILS/src/extras/org_tree_js.pl deleted file mode 100755 index 01a31c2cb7..0000000000 --- a/Open-ILS/src/extras/org_tree_js.pl +++ /dev/null @@ -1,99 +0,0 @@ -#!/usr/bin/perl -use strict; use warnings; - -# ------------------------------------------------------------ -# turns the orgTree and orgTypes into js files -# ------------------------------------------------------------ - -use OpenSRF::System; -use OpenILS::Utils::Fieldmapper; -use OpenSRF::Utils::SettingsClient; -use OpenSRF::Utils::Cache; -use File::Spec; - -die "usage: perl org_tree_js.pl " unless $ARGV[2]; -OpenSRF::System->bootstrap_client(config_file => $ARGV[0]); - -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; - -# 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 { - my $v = shift; - return 'null' unless defined $v; - - # required for JS code this is checking truthness - # without using isTrue() (1/0 vs. t/f) - return 1 if $v eq 't'; - return 0 if $v eq 'f'; - - $v =~ s/([\x{0080}-\x{fffd}])/sprintf('\u%04x',ord($1))/sgoe; - - return "\"$v\""; -} - -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,$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); -} - - - diff --git a/Open-ILS/src/extras/org_tree_proximity.pl b/Open-ILS/src/extras/org_tree_proximity.pl deleted file mode 100755 index 4cfbaae8bf..0000000000 --- a/Open-ILS/src/extras/org_tree_proximity.pl +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/perl -# vim:et:ts=4:sw=4 -# ----------------------------------------------------------------------- -# Copyright (C) 2008 Laurentian University -# Dan Scott -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# ----------------------------------------------------------------------- - -# calculate the proximity of organizations in the organization tree - -use OpenSRF::AppSession; -use OpenSRF::System; -use OpenILS::Utils::Fieldmapper; -use OpenSRF::Utils::SettingsClient; - -die "usage: perl org_tree_proximity.pl " unless $ARGV[0]; -OpenSRF::System->bootstrap_client(config_file => $ARGV[0]); - -Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL")); - -my $ses = OpenSRF::AppSession->create("open-ils.storage"); -my $result = $ses->request("open-ils.storage.actor.org_unit.refresh_proximity"); - -if ($result) { - print "Successfully updated the organization proximity\n"; -} else { - print "Failed to update the organization proximity\n"; -} - -$ses->disconnect(); diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Utils/Configure.pm b/Open-ILS/src/perlmods/lib/OpenILS/Utils/Configure.pm new file mode 100644 index 0000000000..b324ac29b1 --- /dev/null +++ b/Open-ILS/src/perlmods/lib/OpenILS/Utils/Configure.pm @@ -0,0 +1,358 @@ +package OpenILS::Utils::Configure; + +use strict; +use warnings; +use File::Spec; +use OpenILS::Utils::Cronscript; + +sub fieldmapper { + my $web = shift; + + my $core = OpenILS::Utils::Cronscript->new({nolockfile => 1}); + $core->bootstrap; + + my $output; + my $map = $Fieldmapper::fieldmap; + + # if a true value is provided, we generate the web (light) version of the fieldmapper + if(!$web) { $web = ""; } + + my @web_core = qw/ + aou au perm_ex ex aout + mvr ccs ahr aua ac actscecm cbreb acpl + cbrebi acpn acp acnn acn bren asc asce + clfm cifm citm cam ahtc + asv asva asvr asvq + circ ccs ahn bre mrd + crcd crmf crrf mbts aoc aus + mous mobts mb ancc cnct cnal + /; + + my @reports = qw/ perm_ex ex ao aou aout /; + + + $output = "var _c = {};\n"; + + for my $object (keys %$map) { + + my $hint = $map->{$object}->{hint}; + + if($web eq "web_core") { + next unless (grep { $_ eq $hint } @web_core ); + } + + if($web eq "reports") { + next unless (grep { $_ eq $hint } @web_core ); + } + + + my $short_name = $map->{$object}->{hint}; + + my @fields; + for my $field (keys %{$map->{$object}->{fields}}) { + my $position = $map->{$object}->{fields}->{$field}->{position}; + $fields[$position] = $field; + } + + $output .= "_c[\"$short_name\"] = ["; + for my $f (@fields) { + next unless $f; + if( $f ne "isnew" and $f ne "ischanged" and $f ne "isdeleted" ) { + $output .= "\"$f\","; + } + } + $output .= "];\n"; + + + } + + $output .= "var fmclasses = _c;\n"; + return $output; +} + +sub org_tree_js { + # ------------------------------------------------------------ + # turns the orgTree and orgTypes into js files + # ------------------------------------------------------------ + use OpenSRF::Utils::Cache; + + my $path = shift; + my $filename = shift; + + my $core = OpenILS::Utils::Cronscript->new({nolockfile => 1}); + $core->bootstrap; + + # must be loaded after the IDL is parsed + require OpenILS::Utils::CStoreEditor; + + # Get our list of locales + my $locales = get_locales(); + + 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->init(); + $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 { + my $v = shift; + return 'null' unless defined $v; + + # required for JS code this is checking truthness + # without using isTrue() (1/0 vs. t/f) + return 1 if $v eq 't'; + return 0 if $v eq 'f'; + + $v =~ s/([\x{0080}-\x{fffd}])/sprintf('\u%04x',ord($1))/sgoe; + + return "\"$v\""; +} + +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,$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); +} + +sub org_tree_html_options { + # for each supported locale, turn the orgTree and orgTypes into a static HTML option list + + use Unicode::Normalize; + use Data::Dumper; + + my $path = shift; + my $filename = shift; + + my @types; + + my $core = OpenILS::Utils::Cronscript->new({nolockfile => 1}); + $core->bootstrap; + + my $locales = get_locales(); + + 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_org_tree_html($tree, \@org_tree_html, \@types); + $ses->disconnect(); + + open(OUTFH, '>', File::Spec->catfile($dir, $filename)) or die $!; + print OUTFH @org_tree_html; + close OUTFH; + } + +} + +sub print_org_tree_html { + my $node = shift; + my $org_tree_html = shift; + my $types = shift; + + return unless ($node->opac_visible =~ /^[y1t]+/i); + + my $depth = $types->[$node->ou_type]->depth; + my $sname = OpenILS::Application::AppUtils->entityize($node->shortname); + my $name = OpenILS::Application::AppUtils->entityize($node->name); + my $kids = $node->children; + + push @$org_tree_html, "\n"; + print_org_tree_html($_, $org_tree_html, $types) for (@$kids); +} + +sub org_lasso { + # Renders a JavaScript version of the org unit search groups + + my $core = OpenILS::Utils::Cronscript->new({nolockfile => 1}); + $core->bootstrap; + + # must be loaded after the IDL is parsed + require OpenILS::Utils::CStoreEditor; + + my $output; + + # fetch the org_unit's and org_unit_type's + my $e = OpenILS::Utils::CStoreEditor->new; + $e->init(); + my $lassos = $e->request( + 'open-ils.cstore.direct.actor.org_lasso.search.atomic', + {id => {"!=" => undef}}, + {order_by => {lasso => 'name'}} + ); + + # We need at least one defined search group; otherwise, just generate an empty array + if (scalar(@$lassos) > 0) { + $output = + "var _lasso = [\n new lasso(" . + join( "),\n new lasso(", map { OpenSRF::Utils::JSON->perl2JSON( bless($_, 'ARRAY') ) } @$lassos ) . + ")\n]; /* Org Search Groups (Lassos) */ \n"; + } else { + $output = <entityize($locale->code); + my $name = OpenILS::Application::AppUtils->entityize($locale->name); + $output .= " \n"; + } + $output .= "\n"; + + return $output; +} + +sub facet_types { + # ------------------------------------------------------------ + # turns the facet fields defined on config.metabib_field into JS + # ------------------------------------------------------------ + + my $path = shift; + my $filename = shift; + # Get our list of locales + my $locales = get_locales(); + + foreach my $locale (@$locales) { + warn "removing facet list from the cache for locale " . $locale->code . "...\n"; + my $cache = OpenSRF::Utils::Cache->new; + $cache->delete_cache("facet_definition.".$locale->code); + + # fetch the org_unit's and org_unit_type's + my $e = OpenILS::Utils::CStoreEditor->new; + $e->init(); + $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.config.metabib_field.search.atomic', + { facet_field => 't' }, + { no_i18n => $locale->code ? 0 : 1, + flesh => 1, + flesh_fields => { cmf => [ 'field_class' ] } + } + ); + my $dir = File::Spec->catdir($path, $locale->code); + if (!-d $dir) { + mkdir($dir); + } + build_facet_type_js($tree, File::Spec->catfile($dir, $filename)); + } +} + +sub build_facet_type_js { + my $tree = shift; + my $outfile = shift; + + my $pile = "var globalFacets = {"; + my @array; + for my $o (@$tree) { + my %hash = ( + id => $o->id, + name => val($o->name), + label => val($o->label), + classname => val($o->field_class->name), + classlabel => val($o->field_class->label) + ); + + $pile .= $hash{id}.':{'.join(',', map { "$_:$hash{$_}" } keys %hash).'},'; + } + + $pile =~ s/,$//; # remove trailing comma + $pile .= "}; /* Facets */"; + + open(OUTFH, '>', $outfile) or die "Could not open $outfile : $!"; + print OUTFH "$pile\n"; + close(OUTFH); +} + +sub org_tree_proximity { + # calculate the proximity of organizations in the organization tree + + my $session = OpenILS::Utils::Cronscript->new({nolockfile => 1})->session('open-ils.storage'); + my $result = $session->request("open-ils.storage.actor.org_unit.refresh_proximity"); + + if ($result) { + print "Successfully updated the organization proximity\n"; + } else { + print "Failed to update the organization proximity\n"; + } + $session->disconnect(); +} + +sub get_locales { + # Get our list of locales + my $session = OpenILS::Utils::Cronscript->new({nolockfile => 1})->session("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(); + + return $locales; +} + +1; -- 2.11.0