]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/extras/facet_types_js.pl
3bae8ea283a0d852d3d2d53f90543594a7e590fa
[working/Evergreen.git] / Open-ILS / src / extras / facet_types_js.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3
4 # ------------------------------------------------------------
5 # turns the facet fields defined on config.metabib_field into JS
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: $0 <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 facet list from the cache for locale " . $locale->code . "...\n";
32     my $cache = OpenSRF::Utils::Cache->new;
33     $cache->delete_cache("facet_definition.".$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.config.metabib_field.search.atomic',
42         {   facet_field     => 't' },
43         {   no_i18n         => $locale->code ? 0 : 1,
44             flesh           => 1,
45             flesh_fields    => { cmf => [ 'field_class' ] }
46         }
47     );
48     my $dir = File::Spec->catdir($path, $locale->code);
49     if (!-d $dir) {
50         mkdir($dir);
51     }
52     build_tree_js($tree, File::Spec->catfile($dir, $filename));
53 }
54
55
56 sub val {
57     my $v = shift;
58     return 'null' unless defined $v;
59
60     # required for JS code this is checking truthness 
61     # without using isTrue() (1/0 vs. t/f)
62     return 1 if $v eq 't';
63     return 0 if $v eq 'f';
64
65     $v =~ s/([\x{0080}-\x{fffd}])/sprintf('\u%04x',ord($1))/sgoe;
66
67     return "\"$v\"";
68 }
69
70 sub build_tree_js {
71     my $tree = shift;
72     my $outfile = shift;
73
74     my $pile = "var globalFacets = {";
75     my @array;
76     for my $o (@$tree) {
77         my %hash = (
78             id          => $o->id,
79             name        => val($o->name),
80             label       => val($o->label),
81             classname   => val($o->field_class->name),
82             classlabel  => val($o->field_class->label)
83         );
84
85         $pile .= $hash{id}.':{'.join(',', map { "$_:$hash{$_}" } keys %hash).'},';
86     }
87
88     $pile =~ s/,$//; # remove trailing comma
89     $pile .= "}; /* Facets */";
90
91     open(OUTFH, '>', $outfile) or die "Could not open $outfile : $!";
92     print OUTFH "$pile\n";
93     close(OUTFH);
94 }
95
96
97