| Filename | /home/vagrant/kohaclone/C4/Templates.pm |
| Statements | Executed 0 statements in 0s |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 1.46ms | 17.7ms | C4::Templates::BEGIN@33 |
| 1 | 1 | 1 | 53µs | 75µs | C4::Templates::BEGIN@3 |
| 1 | 1 | 1 | 20µs | 53µs | C4::Templates::BEGIN@7 |
| 1 | 1 | 1 | 19µs | 32µs | C4::Templates::BEGIN@4 |
| 1 | 1 | 1 | 19µs | 130µs | C4::Templates::BEGIN@32 |
| 1 | 1 | 1 | 17µs | 55µs | C4::Templates::BEGIN@6 |
| 1 | 1 | 1 | 16µs | 83µs | C4::Templates::BEGIN@35 |
| 1 | 1 | 1 | 15µs | 204µs | C4::Templates::BEGIN@34 |
| 1 | 1 | 1 | 12µs | 67µs | C4::Templates::BEGIN@5 |
| 1 | 1 | 1 | 10µs | 13µs | C4::Templates::BEGIN@37 |
| 0 | 0 | 0 | 0s | 0s | C4::Templates::GetColumnDefs |
| 0 | 0 | 0 | 0s | 0s | C4::Templates::_get_template_file |
| 0 | 0 | 0 | 0s | 0s | C4::Templates::getlanguagecookie |
| 0 | 0 | 0 | 0s | 0s | C4::Templates::gettemplate |
| 0 | 0 | 0 | 0s | 0s | C4::Templates::new |
| 0 | 0 | 0 | 0s | 0s | C4::Templates::output |
| 0 | 0 | 0 | 0s | 0s | C4::Templates::param |
| 0 | 0 | 0 | 0s | 0s | C4::Templates::setlanguagecookie |
| 0 | 0 | 0 | 0s | 0s | C4::Templates::themelanguage |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package C4::Templates; | ||||
| 2 | |||||
| 3 | 2 | 96µs | # spent 75µs (53+22) within C4::Templates::BEGIN@3 which was called:
# once (53µs+22µs) by C4::Output::BEGIN@34 at line 3 # spent 75µs making 1 call to C4::Templates::BEGIN@3
# spent 22µs making 1 call to strict::import | ||
| 4 | 2 | 45µs | # spent 32µs (19+13) within C4::Templates::BEGIN@4 which was called:
# once (19µs+13µs) by C4::Output::BEGIN@34 at line 4 # spent 32µs making 1 call to C4::Templates::BEGIN@4
# spent 13µs making 1 call to warnings::import | ||
| 5 | 2 | 122µs | # spent 67µs (12+54) within C4::Templates::BEGIN@5 which was called:
# once (12µs+54µs) by C4::Output::BEGIN@34 at line 5 # spent 67µs making 1 call to C4::Templates::BEGIN@5
# spent 54µs making 1 call to Exporter::import | ||
| 6 | 2 | 93µs | # spent 55µs (17+38) within C4::Templates::BEGIN@6 which was called:
# once (17µs+38µs) by C4::Output::BEGIN@34 at line 6 # spent 55µs making 1 call to C4::Templates::BEGIN@6
# spent 38µs making 1 call to CGI::import | ||
| 7 | 2 | 87µs | # spent 53µs (20+34) within C4::Templates::BEGIN@7 which was called:
# once (20µs+34µs) by C4::Output::BEGIN@34 at line 7 # spent 53µs making 1 call to C4::Templates::BEGIN@7
# spent 34µs making 1 call to Exporter::import | ||
| 8 | |||||
| 9 | # Copyright 2009 Chris Cormack and The Koha Dev Team | ||||
| 10 | # | ||||
| 11 | # This file is part of Koha. | ||||
| 12 | # | ||||
| 13 | # Koha is free software; you can redistribute it and/or modify it | ||||
| 14 | # under the terms of the GNU General Public License as published by | ||||
| 15 | # the Free Software Foundation; either version 3 of the License, or | ||||
| 16 | # (at your option) any later version. | ||||
| 17 | # | ||||
| 18 | # Koha is distributed in the hope that it will be useful, but | ||||
| 19 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
| 21 | # GNU General Public License for more details. | ||||
| 22 | # | ||||
| 23 | # You should have received a copy of the GNU General Public License | ||||
| 24 | # along with Koha; if not, see <http://www.gnu.org/licenses>. | ||||
| 25 | |||||
| 26 | =head1 NAME | ||||
| 27 | |||||
| 28 | Koha::Templates - Object for manipulating templates for use with Koha | ||||
| 29 | |||||
| 30 | =cut | ||||
| 31 | |||||
| 32 | 2 | 242µs | # spent 130µs (19+112) within C4::Templates::BEGIN@32 which was called:
# once (19µs+112µs) by C4::Output::BEGIN@34 at line 32 # spent 130µs making 1 call to C4::Templates::BEGIN@32
# spent 112µs making 1 call to base::import | ||
| 33 | 1 | 17.7ms | # spent 17.7ms (1.46+16.2) within C4::Templates::BEGIN@33 which was called:
# once (1.46ms+16.2ms) by C4::Output::BEGIN@34 at line 33 # spent 17.7ms making 1 call to C4::Templates::BEGIN@33 | ||
| 34 | 2 | 393µs | # spent 204µs (15+189) within C4::Templates::BEGIN@34 which was called:
# once (15µs+189µs) by C4::Output::BEGIN@34 at line 34 # spent 204µs making 1 call to C4::Templates::BEGIN@34
# spent 189µs making 1 call to Exporter::import | ||
| 35 | 2 | 150µs | # spent 83µs (16+67) within C4::Templates::BEGIN@35 which was called:
# once (16µs+67µs) by C4::Output::BEGIN@34 at line 35 # spent 83µs making 1 call to C4::Templates::BEGIN@35
# spent 67µs making 1 call to Exporter::import | ||
| 36 | |||||
| 37 | 2 | 17µs | # spent 13µs (10+3) within C4::Templates::BEGIN@37 which was called:
# once (10µs+3µs) by C4::Output::BEGIN@34 at line 37 # spent 13µs making 1 call to C4::Templates::BEGIN@37
# spent 3µs making 1 call to C4::Context::import | ||
| 38 | |||||
| 39 | 1 | 171µs | __PACKAGE__->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars)); # spent 171µs making 1 call to Class::Accessor::mk_accessors | ||
| 40 | |||||
| - - | |||||
| 43 | sub new { | ||||
| 44 | my $class = shift; | ||||
| 45 | my $interface = shift; | ||||
| 46 | my $filename = shift; | ||||
| 47 | my $tmplbase = shift; | ||||
| 48 | my $query = @_? shift: undef; | ||||
| 49 | my $htdocs; | ||||
| 50 | if ( $interface ne "intranet" ) { | ||||
| 51 | $htdocs = C4::Context->config('opachtdocs'); | ||||
| 52 | } | ||||
| 53 | else { | ||||
| 54 | $htdocs = C4::Context->config('intrahtdocs'); | ||||
| 55 | } | ||||
| 56 | my ($theme, $lang, $activethemes)= themelanguage( $htdocs, $tmplbase, $interface, $query); | ||||
| 57 | my @includes; | ||||
| 58 | foreach (@$activethemes) { | ||||
| 59 | push @includes, "$htdocs/$_/$lang/includes"; | ||||
| 60 | push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en'; | ||||
| 61 | } | ||||
| 62 | # Do not use template cache if script is called from commandline | ||||
| 63 | my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE}; | ||||
| 64 | my $template = Template->new( | ||||
| 65 | { EVAL_PERL => 1, | ||||
| 66 | ABSOLUTE => 1, | ||||
| 67 | PLUGIN_BASE => 'Koha::Template::Plugin', | ||||
| 68 | COMPILE_EXT => $use_template_cache ? '.ttc' : '', | ||||
| 69 | COMPILE_DIR => $use_template_cache ? C4::Context->config('template_cache_dir') : '', | ||||
| 70 | INCLUDE_PATH => \@includes, | ||||
| 71 | FILTERS => {}, | ||||
| 72 | ENCODING => 'UTF-8', | ||||
| 73 | } | ||||
| 74 | ) or die Template->error(); | ||||
| 75 | my $self = { | ||||
| 76 | TEMPLATE => $template, | ||||
| 77 | VARS => {}, | ||||
| 78 | }; | ||||
| 79 | bless $self, $class; | ||||
| 80 | $self->theme($theme); | ||||
| 81 | $self->lang($lang); | ||||
| 82 | $self->activethemes($activethemes); | ||||
| 83 | $self->preferredtheme($activethemes->[0]); | ||||
| 84 | $self->filename($filename); | ||||
| 85 | $self->htdocs($htdocs); | ||||
| 86 | $self->interface($interface); | ||||
| 87 | $self->{VARS}->{"test"} = "value"; | ||||
| 88 | return $self; | ||||
| 89 | |||||
| 90 | } | ||||
| 91 | |||||
| 92 | sub output { | ||||
| 93 | my $self = shift; | ||||
| 94 | my $vars = shift; | ||||
| 95 | |||||
| 96 | # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename; | ||||
| 97 | my $template = $self->{TEMPLATE}; | ||||
| 98 | if ( $self->interface eq 'intranet' ) { | ||||
| 99 | $vars->{themelang} = '/intranet-tmpl'; | ||||
| 100 | } | ||||
| 101 | else { | ||||
| 102 | $vars->{themelang} = '/opac-tmpl'; | ||||
| 103 | } | ||||
| 104 | $vars->{lang} = $self->lang; | ||||
| 105 | $vars->{themelang} .= '/' . $self->preferredtheme . '/' . $self->lang; | ||||
| 106 | $vars->{interface} = | ||||
| 107 | ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ); | ||||
| 108 | $vars->{theme} = $self->theme; | ||||
| 109 | $vars->{OpacAdditionalStylesheet} = | ||||
| 110 | C4::Context->preference('OpacAdditionalStylesheet'); | ||||
| 111 | $vars->{opaclayoutstylesheet} = | ||||
| 112 | C4::Context->preference('opaclayoutstylesheet'); | ||||
| 113 | |||||
| 114 | # add variables set via param to $vars for processing | ||||
| 115 | for my $k ( keys %{ $self->{VARS} } ) { | ||||
| 116 | $vars->{$k} = $self->{VARS}->{$k}; | ||||
| 117 | } | ||||
| 118 | |||||
| 119 | my $data; | ||||
| 120 | binmode( STDOUT, ":utf8" ); | ||||
| 121 | $template->process( $self->filename, $vars, \$data ) | ||||
| 122 | || die "Template process failed: ", $template->error(); | ||||
| 123 | return $data; | ||||
| 124 | } | ||||
| 125 | |||||
| 126 | # wrapper method to allow easier transition from HTML template pro to Template Toolkit | ||||
| 127 | sub param { | ||||
| 128 | my $self = shift; | ||||
| 129 | while (@_) { | ||||
| 130 | my $key = shift; | ||||
| 131 | my $val = shift; | ||||
| 132 | if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; } | ||||
| 133 | elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; } | ||||
| 134 | if ( $key ) { | ||||
| 135 | $self->{VARS}->{$key} = $val; | ||||
| 136 | } else { | ||||
| 137 | warn "Problem = a value of $val has been passed to param without key"; | ||||
| 138 | } | ||||
| 139 | } | ||||
| 140 | } | ||||
| 141 | |||||
| 142 | |||||
| 143 | =head1 NAME | ||||
| 144 | |||||
| 145 | C4::Templates - Functions for managing templates | ||||
| 146 | |||||
| 147 | =head1 FUNCTIONS | ||||
| 148 | |||||
| 149 | =cut | ||||
| 150 | |||||
| 151 | # FIXME: this is a quick fix to stop rc1 installing broken | ||||
| 152 | # Still trying to figure out the correct fix. | ||||
| 153 | 1 | 16µs | my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/"; # spent 16µs making 1 call to C4::Context::config | ||
| 154 | |||||
| 155 | #--------------------------------------------------------------------------------------------------------- | ||||
| 156 | # FIXME - POD | ||||
| 157 | |||||
| 158 | sub _get_template_file { | ||||
| 159 | my ($tmplbase, $interface, $query) = @_; | ||||
| 160 | |||||
| 161 | my $is_intranet = $interface eq 'intranet'; | ||||
| 162 | my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs'); | ||||
| 163 | my ($theme, $lang, $availablethemes) = themelanguage($htdocs, $tmplbase, $interface, $query); | ||||
| 164 | $lang //= 'en'; | ||||
| 165 | my $filename = "$htdocs/$theme/$lang/modules/$tmplbase"; | ||||
| 166 | |||||
| 167 | return ($htdocs, $theme, $lang, $filename); | ||||
| 168 | } | ||||
| 169 | |||||
| 170 | |||||
| 171 | sub gettemplate { | ||||
| 172 | my ( $tmplbase, $interface, $query, $is_plugin ) = @_; | ||||
| 173 | ($query) or warn "no query in gettemplate"; | ||||
| 174 | my $path = C4::Context->preference('intranet_includes') || 'includes'; | ||||
| 175 | my ($htdocs, $theme, $lang, $filename) | ||||
| 176 | = _get_template_file($tmplbase, $interface, $query); | ||||
| 177 | $filename = $tmplbase if ( $is_plugin ); | ||||
| 178 | my $template = C4::Templates->new($interface, $filename, $tmplbase, $query); | ||||
| 179 | |||||
| 180 | # NOTE: Commenting these out rather than deleting them so that those who need | ||||
| 181 | # to know how we previously shimmed these directories will be able to understand. | ||||
| 182 | # my $is_intranet = $interface eq 'intranet'; | ||||
| 183 | # my $themelang = | ||||
| 184 | # ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') . | ||||
| 185 | # "/$theme/$lang"; | ||||
| 186 | # $template->param( | ||||
| 187 | # themelang => $themelang, | ||||
| 188 | # interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl', | ||||
| 189 | # theme => $theme, | ||||
| 190 | # lang => $lang | ||||
| 191 | # ); | ||||
| 192 | |||||
| 193 | # Bidirectionality, must be sent even if is the only language | ||||
| 194 | my $current_lang = regex_lang_subtags($lang); | ||||
| 195 | my $bidi; | ||||
| 196 | $bidi = get_bidi($current_lang->{script}) if $current_lang->{script}; | ||||
| 197 | $template->param( | ||||
| 198 | bidi => $bidi, | ||||
| 199 | ); | ||||
| 200 | # Languages | ||||
| 201 | my $languages_loop = getTranslatedLanguages($interface,$theme,$lang); | ||||
| 202 | my $num_languages_enabled = 0; | ||||
| 203 | foreach my $lang (@$languages_loop) { | ||||
| 204 | foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) { | ||||
| 205 | $num_languages_enabled++ if $sublang->{enabled}; | ||||
| 206 | } | ||||
| 207 | } | ||||
| 208 | my $one_language_enabled = ($num_languages_enabled <= 1) ? 1 : 0; # deal with zero enabled langs as well | ||||
| 209 | $template->param( | ||||
| 210 | languages_loop => $languages_loop, | ||||
| 211 | one_language_enabled => $one_language_enabled, | ||||
| 212 | ) unless $one_language_enabled; | ||||
| 213 | |||||
| 214 | return $template; | ||||
| 215 | } | ||||
| 216 | |||||
| 217 | |||||
| 218 | =head2 themelanguage | ||||
| 219 | |||||
| 220 | my ($theme,$lang,\@themes) = themelanguage($htdocs,$tmpl,$interface,query); | ||||
| 221 | |||||
| 222 | This function returns the theme and language to be used for rendering the UI. | ||||
| 223 | It also returns the list of themes that should be applied as a fallback. This is | ||||
| 224 | used for the theme overlay feature (i.e. if a file doesn't exist on the requested | ||||
| 225 | theme, fallback to the configured fallback). | ||||
| 226 | |||||
| 227 | Important: this function is used on the webinstaller too, so always consider | ||||
| 228 | the use case where the DB is not populated already when rewriting/fixing. | ||||
| 229 | |||||
| 230 | =cut | ||||
| 231 | |||||
| 232 | sub themelanguage { | ||||
| 233 | my ($htdocs, $tmpl, $interface, $query) = @_; | ||||
| 234 | ($query) or warn "no query in themelanguage"; | ||||
| 235 | |||||
| 236 | # Select a language based on cookie, syspref available languages & browser | ||||
| 237 | my $lang = C4::Languages::getlanguage($query); | ||||
| 238 | |||||
| 239 | # Get theme | ||||
| 240 | my @themes; | ||||
| 241 | my $theme_syspref = ($interface eq 'intranet') ? 'template' : 'opacthemes'; | ||||
| 242 | my $fallback_syspref = ($interface eq 'intranet') ? 'template' : 'OPACFallback'; | ||||
| 243 | # Yeah, hardcoded, last resort if the DB is not populated | ||||
| 244 | my $hardcoded_theme = ($interface eq 'intranet') ? 'prog' : 'bootstrap'; | ||||
| 245 | |||||
| 246 | # Configured theme is the first one | ||||
| 247 | push @themes, C4::Context->preference( $theme_syspref ) | ||||
| 248 | if C4::Context->preference( $theme_syspref ); | ||||
| 249 | # Configured fallback next | ||||
| 250 | push @themes, C4::Context->preference( $fallback_syspref ) | ||||
| 251 | if C4::Context->preference( $fallback_syspref ); | ||||
| 252 | # The hardcoded fallback theme is the last one | ||||
| 253 | push @themes, $hardcoded_theme; | ||||
| 254 | |||||
| 255 | # Try to find first theme for the selected theme/lang, then for fallback/lang | ||||
| 256 | my $where = $tmpl =~ /xsl$/ ? 'xslt' : 'modules'; | ||||
| 257 | for my $theme (@themes) { | ||||
| 258 | if ( -e "$htdocs/$theme/$lang/$where/$tmpl" ) { | ||||
| 259 | return ( $theme, $lang, uniq( \@themes ) ); | ||||
| 260 | } | ||||
| 261 | } | ||||
| 262 | # Otherwise return theme/'en', last resort fallback/'en' | ||||
| 263 | for my $theme (@themes) { | ||||
| 264 | if ( -e "$htdocs/$theme/en/$where/$tmpl" ) { | ||||
| 265 | return ( $theme, 'en', uniq( \@themes ) ); | ||||
| 266 | } | ||||
| 267 | } | ||||
| 268 | # tmpl is a full path, so this is a template for a plugin | ||||
| 269 | if ( $tmpl =~ /^\// && -e $tmpl ) { | ||||
| 270 | return ( $themes[0], $lang, uniq( \@themes ) ); | ||||
| 271 | } | ||||
| 272 | } | ||||
| 273 | |||||
| 274 | |||||
| 275 | sub setlanguagecookie { | ||||
| 276 | my ( $query, $language, $uri ) = @_; | ||||
| 277 | |||||
| 278 | my $cookie = $query->cookie( | ||||
| 279 | -name => 'KohaOpacLanguage', | ||||
| 280 | -value => $language, | ||||
| 281 | -HttpOnly => 1, | ||||
| 282 | -expires => '+3y' | ||||
| 283 | ); | ||||
| 284 | print $query->redirect( | ||||
| 285 | -uri => $uri, | ||||
| 286 | -cookie => $cookie | ||||
| 287 | ); | ||||
| 288 | } | ||||
| 289 | |||||
| 290 | =head2 getlanguagecookie | ||||
| 291 | |||||
| 292 | my $cookie = getlanguagecookie($query,$language); | ||||
| 293 | |||||
| 294 | Returns a cookie object containing the calculated language to be used. | ||||
| 295 | |||||
| 296 | =cut | ||||
| 297 | |||||
| 298 | sub getlanguagecookie { | ||||
| 299 | my ( $query, $language ) = @_; | ||||
| 300 | my $cookie = $query->cookie( | ||||
| 301 | -name => 'KohaOpacLanguage', | ||||
| 302 | -value => $language, | ||||
| 303 | -HttpOnly => 1, | ||||
| 304 | -expires => '+3y' | ||||
| 305 | ); | ||||
| 306 | |||||
| 307 | return $cookie; | ||||
| 308 | } | ||||
| 309 | |||||
| 310 | =head2 GetColumnDefs | ||||
| 311 | |||||
| 312 | my $columns = GetColumnDefs( $cgi ) | ||||
| 313 | |||||
| 314 | It is passed a CGI object and returns a hash of hashes containing | ||||
| 315 | the column names and descriptions for each table defined in the | ||||
| 316 | columns.def file corresponding to the CGI object. | ||||
| 317 | |||||
| 318 | =cut | ||||
| 319 | |||||
| 320 | sub GetColumnDefs { | ||||
| 321 | |||||
| 322 | my $query = shift; | ||||
| 323 | |||||
| 324 | my $columns = {}; | ||||
| 325 | |||||
| 326 | my $htdocs = C4::Context->config('intrahtdocs'); | ||||
| 327 | my $columns_file = 'columns.def'; | ||||
| 328 | |||||
| 329 | # Get theme and language to build the path to columns.def | ||||
| 330 | my ($theme, $lang, $availablethemes) = | ||||
| 331 | themelanguage($htdocs, 'about.tt', 'intranet', $query); | ||||
| 332 | # Build columns.def path | ||||
| 333 | my $path = "$htdocs/$theme/$lang/$columns_file"; | ||||
| 334 | my $fh; | ||||
| 335 | if ( ! open ( $fh, q{<:encoding(utf-8)}, $path ) ) { | ||||
| 336 | carp "Error opening $path. Check your templates."; | ||||
| 337 | return; | ||||
| 338 | } | ||||
| 339 | # Loop through the columns.def file | ||||
| 340 | while ( my $input = <$fh> ){ | ||||
| 341 | chomp $input; | ||||
| 342 | if ( $input =~ m|<field name="(.*)">(.*)</field>| ) { | ||||
| 343 | my ( $table, $column ) = split( '\.', $1); | ||||
| 344 | my $description = $2; | ||||
| 345 | # Initialize the table array if needed. | ||||
| 346 | @{$columns->{ $table }} = () if ! defined $columns->{ $table }; | ||||
| 347 | # Push field and description | ||||
| 348 | push @{$columns->{ $table }}, | ||||
| 349 | { field => $column, description => $description }; | ||||
| 350 | } | ||||
| 351 | } | ||||
| 352 | close $fh; | ||||
| 353 | |||||
| 354 | return $columns; | ||||
| 355 | } | ||||
| 356 | |||||
| 357 | 1; |