| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package OpenGuides::Template; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 92 |  |  | 92 |  | 16293 | use strict; | 
|  | 92 |  |  |  |  | 104 |  | 
|  | 92 |  |  |  |  | 2221 |  | 
| 4 | 92 |  |  | 92 |  | 274 | use vars qw( $VERSION ); | 
|  | 92 |  |  |  |  | 108 |  | 
|  | 92 |  |  |  |  | 3439 |  | 
| 5 |  |  |  |  |  |  | $VERSION = '0.21'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 92 |  |  | 92 |  | 301 | use Carp qw( croak ); | 
|  | 92 |  |  |  |  | 95 |  | 
|  | 92 |  |  |  |  | 3302 |  | 
| 8 | 92 |  |  | 92 |  | 958 | use CGI; # want to get rid of this and put the burden on the templates | 
|  | 92 |  |  |  |  | 20091 |  | 
|  | 92 |  |  |  |  | 454 |  | 
| 9 | 92 |  |  | 92 |  | 11697 | use OpenGuides; # for $VERSION for template variable | 
|  | 92 |  |  |  |  | 115 |  | 
|  | 92 |  |  |  |  | 1927 |  | 
| 10 | 92 |  |  | 92 |  | 309 | use OpenGuides::CGI; | 
|  | 92 |  |  |  |  | 91 |  | 
|  | 92 |  |  |  |  | 1364 |  | 
| 11 | 92 |  |  | 92 |  | 39339 | use Template; | 
|  | 92 |  |  |  |  | 1251222 |  | 
|  | 92 |  |  |  |  | 2121 |  | 
| 12 | 92 |  |  | 92 |  | 456 | use URI::Escape; | 
|  | 92 |  |  |  |  | 110 |  | 
|  | 92 |  |  |  |  | 4226 |  | 
| 13 | 92 |  |  | 92 |  | 32591 | use Data::Validate::URI qw( is_web_uri ); | 
|  | 92 |  |  |  |  | 2867271 |  | 
|  | 92 |  |  |  |  | 180757 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 NAME | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | OpenGuides::Template - Do Template Toolkit related stuff for OpenGuides applications. | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | Does all the Template Toolkit stuff for OpenGuides.  Distributed and | 
| 22 |  |  |  |  |  |  | installed as part of the OpenGuides project, not intended for | 
| 23 |  |  |  |  |  |  | independent installation.  This documentation is probably only useful | 
| 24 |  |  |  |  |  |  | to OpenGuides developers. | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | use OpenGuides::Config; | 
| 29 |  |  |  |  |  |  | use OpenGuides::Utils; | 
| 30 |  |  |  |  |  |  | use OpenGuides::Template; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | my $config = OpenGuides::Config->new( file => "wiki.conf" ); | 
| 33 |  |  |  |  |  |  | my $wiki = OpenGuides::Utils->make_wiki_object( config => $config ); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | print OpenGuides::Template->output( wiki     => $wiki, | 
| 36 |  |  |  |  |  |  | config   => $config, | 
| 37 |  |  |  |  |  |  | template => "node.tt", | 
| 38 |  |  |  |  |  |  | vars     => { foo => "bar" } | 
| 39 |  |  |  |  |  |  | ); | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | =head1 METHODS | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =over 4 | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | =item B | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | print OpenGuides::Template->output( wiki         => $wiki, | 
| 48 |  |  |  |  |  |  | config       => $config, | 
| 49 |  |  |  |  |  |  | template     => "node.tt", | 
| 50 |  |  |  |  |  |  | content_type => "text/html", | 
| 51 |  |  |  |  |  |  | cookies      => $cookie, | 
| 52 |  |  |  |  |  |  | vars         => {foo => "bar"}, | 
| 53 |  |  |  |  |  |  | noheaders    => 1 | 
| 54 |  |  |  |  |  |  | ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | Returns everything you need to send to STDOUT, including the | 
| 57 |  |  |  |  |  |  | Content-Type: header. Croaks unless C is supplied. | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | The config object and variables supplied in C are passed through | 
| 60 |  |  |  |  |  |  | to the template specified.  Additional Template Toolkit variables are | 
| 61 |  |  |  |  |  |  | automatically set and passed through as well, as described below. | 
| 62 |  |  |  |  |  |  | B variables set in C will over-ride any variables of the | 
| 63 |  |  |  |  |  |  | same name in the config object or the user cookies. | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | =over | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =item * C | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | =item * C | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =item * C | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =item * C | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =item * C (gets set to true or false - defaults to false) | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =item * C | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =item * C | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =item * C | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =item * C (unless C is set in user cookie) | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =item * C | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =item * C | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =item * C | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | =item * C | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =item * C | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | =item * C | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =item * C (the preferences from the user cookie) | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =back | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | If C is supplied: | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =over | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =item * C | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =item * C (the node name escaped for use in URLs) | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =back | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | Content-Type: defaults to C and is omitted if the | 
| 112 |  |  |  |  |  |  | C arg is explicitly set to the blank string. | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | However, what you more often need is the C option, | 
| 115 |  |  |  |  |  |  | which suppresses all HTTP headers, not just the Content-Type. | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | The HTTP response code may be explictly set with the C arg. | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =cut | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | sub output { | 
| 122 | 257 |  |  | 257 | 1 | 16626 | my ($class, %args) = @_; | 
| 123 | 257 | 100 |  |  |  | 952 | croak "No template supplied" unless $args{template}; | 
| 124 | 256 | 50 |  |  |  | 804 | my $config = $args{config} or croak "No config supplied"; | 
| 125 | 256 |  |  |  |  | 945 | my $template_path = $config->template_path; | 
| 126 | 256 |  | 100 |  |  | 2280 | my $custom_template_path = $config->custom_template_path || ""; | 
| 127 | 256 |  |  |  |  | 4084 | my $tt = Template->new( | 
| 128 |  |  |  |  |  |  | { INCLUDE_PATH => "$custom_template_path:$template_path" } ); | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 256 |  |  |  |  | 524556 | my $script_name  = $config->script_name; | 
| 131 | 256 |  |  |  |  | 2590 | my $script_url   = $config->script_url; | 
| 132 | 256 |  |  |  |  | 917 | my $default_city = $config->default_city; | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | # Check cookie to see if we need to set the formatting_rules_link. | 
| 135 | 256 |  |  |  |  | 1477 | my ($formatting_rules_link, $omit_help_links); | 
| 136 | 256 |  |  |  |  | 865 | my $formatting_rules_node = $config->formatting_rules_node; | 
| 137 | 256 |  |  |  |  | 1778 | $formatting_rules_link = $config->formatting_rules_link; | 
| 138 |  |  |  |  |  |  | my %cookie_data = OpenGuides::CGI->get_prefs_from_cookie( | 
| 139 |  |  |  |  |  |  | config  => $config, | 
| 140 |  |  |  |  |  |  | cookies => $args{cookies}, | 
| 141 | 256 |  |  |  |  | 2618 | ); | 
| 142 | 256 | 100 |  |  |  | 1003 | if ( $cookie_data{omit_help_links} ) { | 
| 143 | 14 |  |  |  |  | 25 | $omit_help_links = 1; | 
| 144 |  |  |  |  |  |  | } else { | 
| 145 | 242 | 100 | 66 |  |  | 1372 | if (( $formatting_rules_node ) and !( $formatting_rules_link )){ | 
| 146 |  |  |  |  |  |  | $formatting_rules_link = $script_url . $script_name . "?" | 
| 147 | 8 |  |  |  |  | 30 | . uri_escape($args{wiki}->formatter->node_name_to_node_param($formatting_rules_node)); | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 256 |  |  |  |  | 583 | my $enable_page_deletion = 0; | 
| 152 | 256 | 100 | 100 |  |  | 890 | if ( $config->enable_page_deletion | 
|  |  |  | 66 |  |  |  |  | 
| 153 |  |  |  |  |  |  | and ( lc($config->enable_page_deletion) eq "y" | 
| 154 |  |  |  |  |  |  | or $config->enable_page_deletion eq "1" ) | 
| 155 |  |  |  |  |  |  | ) { | 
| 156 | 8 |  |  |  |  | 180 | $enable_page_deletion = 1; | 
| 157 |  |  |  |  |  |  | } | 
| 158 | 256 |  |  |  |  | 2067 | my $is_admin = 0; | 
| 159 | 256 | 100 |  |  |  | 780 | if ( $cookie_data{is_admin} ) { | 
| 160 | 17 |  |  |  |  | 21 | $is_admin = 1; | 
| 161 |  |  |  |  |  |  | } | 
| 162 |  |  |  |  |  |  |  | 
| 163 | 256 |  |  |  |  | 759 | my $tt_vars = { | 
| 164 |  |  |  |  |  |  | config                => $config, | 
| 165 |  |  |  |  |  |  | prefs                 => \%cookie_data, | 
| 166 |  |  |  |  |  |  | site_name             => $config->site_name, | 
| 167 |  |  |  |  |  |  | cgi_url               => $script_name, | 
| 168 |  |  |  |  |  |  | script_url            => $script_url, | 
| 169 |  |  |  |  |  |  | full_cgi_url          => $script_url . $script_name, | 
| 170 |  |  |  |  |  |  | contact_email         => $config->contact_email, | 
| 171 |  |  |  |  |  |  | stylesheet            => $config->stylesheet_url, | 
| 172 |  |  |  |  |  |  | home_link             => $script_url . $script_name, | 
| 173 |  |  |  |  |  |  | home_name             => $config->home_name, | 
| 174 |  |  |  |  |  |  | navbar_on_home_page   => $config->navbar_on_home_page, | 
| 175 |  |  |  |  |  |  | omit_help_links       => $omit_help_links, | 
| 176 |  |  |  |  |  |  | is_admin              => $is_admin, | 
| 177 |  |  |  |  |  |  | formatting_rules_link => $formatting_rules_link, | 
| 178 |  |  |  |  |  |  | formatting_rules_node => $formatting_rules_node, | 
| 179 |  |  |  |  |  |  | openguides_version    => $OpenGuides::VERSION, | 
| 180 |  |  |  |  |  |  | enable_page_deletion  => $enable_page_deletion, | 
| 181 |  |  |  |  |  |  | language              => $config->default_language, | 
| 182 |  |  |  |  |  |  | http_charset          => $config->http_charset, | 
| 183 |  |  |  |  |  |  | default_city          => $default_city, | 
| 184 |  |  |  |  |  |  | gmaps_api_key         => $config->gmaps_api_key, | 
| 185 |  |  |  |  |  |  | licence_name          => $config->licence_name, | 
| 186 |  |  |  |  |  |  | licence_url           => $config->licence_url, | 
| 187 |  |  |  |  |  |  | licence_info_url      => $config->licence_info_url, | 
| 188 |  |  |  |  |  |  | responsive            => $config->responsive, | 
| 189 |  |  |  |  |  |  | }; | 
| 190 |  |  |  |  |  |  |  | 
| 191 | 256 | 100 |  |  |  | 17335 | if ($args{node}) { | 
| 192 | 144 |  |  |  |  | 825 | $tt_vars->{node_name} = CGI->escapeHTML($args{node}); | 
| 193 | 144 |  |  |  |  | 11610 | $tt_vars->{node_param} = CGI->escape($args{wiki}->formatter->node_name_to_node_param($args{node})); | 
| 194 |  |  |  |  |  |  | } | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | # Now set further TT variables if explicitly supplied - do this after the | 
| 197 |  |  |  |  |  |  | # above auto-setting as these override auto-set ones. | 
| 198 | 256 | 100 |  |  |  | 6358 | $tt_vars = { %$tt_vars, %{ $args{vars} || {} } }; | 
|  | 256 |  |  |  |  | 4761 |  | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | # Finally, dig out the username from the cookie if we haven't already | 
| 201 |  |  |  |  |  |  | # been sent it in vars. | 
| 202 | 256 | 100 |  |  |  | 1691 | if ( !$tt_vars->{username} ) { | 
| 203 | 250 |  |  |  |  | 858 | my %prefs = OpenGuides::CGI->get_prefs_from_cookie(config => $config); | 
| 204 |  |  |  |  |  |  | # If there's nothing in there, it defaults to "Anonymous". | 
| 205 | 250 | 100 |  |  |  | 1210 | if ( $prefs{username} ne "Anonymous" ) { | 
| 206 | 45 |  |  |  |  | 129 | $tt_vars->{username} = $prefs{username}; | 
| 207 |  |  |  |  |  |  | } | 
| 208 |  |  |  |  |  |  | } | 
| 209 |  |  |  |  |  |  |  | 
| 210 | 256 |  |  |  |  | 406 | my $header = ""; | 
| 211 |  |  |  |  |  |  |  | 
| 212 | 256 | 100 |  |  |  | 761 | unless ( $args{noheaders} ) { | 
| 213 | 188 |  |  |  |  | 243 | my %cgi_header_args; | 
| 214 |  |  |  |  |  |  |  | 
| 215 | 188 | 100 | 100 |  |  | 695 | if ( defined $args{content_type} and $args{content_type} eq "" ) { | 
| 216 | 1 |  |  |  |  | 2 | $cgi_header_args{'-type'} = ''; | 
| 217 |  |  |  |  |  |  | } else { | 
| 218 | 187 | 100 |  |  |  | 383 | if ( $args{content_type} ) { | 
| 219 | 4 |  |  |  |  | 8 | $cgi_header_args{'-type'} = $args{content_type}; | 
| 220 |  |  |  |  |  |  | } else { | 
| 221 | 183 |  |  |  |  | 383 | $cgi_header_args{'-type'} = "text/html"; | 
| 222 |  |  |  |  |  |  | } | 
| 223 |  |  |  |  |  |  | } | 
| 224 |  |  |  |  |  |  |  | 
| 225 | 188 | 100 |  |  |  | 471 | if ( $tt_vars->{http_charset} ) { | 
| 226 | 2 |  |  |  |  | 4 | $cgi_header_args{'-type'} .= "; charset=".$tt_vars->{http_charset}; | 
| 227 |  |  |  |  |  |  | } | 
| 228 | 188 |  |  |  |  | 366 | $cgi_header_args{'-cookie'} = $args{cookies}; | 
| 229 |  |  |  |  |  |  |  | 
| 230 | 188 | 100 |  |  |  | 443 | if ( $args{http_status} ) { | 
| 231 | 8 |  |  |  |  | 17 | $cgi_header_args{'-status'} = $args{http_status}; | 
| 232 |  |  |  |  |  |  | } | 
| 233 |  |  |  |  |  |  |  | 
| 234 | 188 |  |  |  |  | 809 | $header = CGI::header( %cgi_header_args ); | 
| 235 |  |  |  |  |  |  | } | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | # vile hack | 
| 238 |  |  |  |  |  |  | my %field_vars = OpenGuides::Template->extract_metadata_vars( | 
| 239 |  |  |  |  |  |  | wiki                 => $args{wiki}, | 
| 240 | 256 |  |  |  |  | 43286 | config               => $config, | 
| 241 |  |  |  |  |  |  | set_coord_field_vars => 1, | 
| 242 |  |  |  |  |  |  | metadata => {}, | 
| 243 |  |  |  |  |  |  | ); | 
| 244 |  |  |  |  |  |  |  | 
| 245 | 256 |  |  |  |  | 6136 | $tt_vars = { %field_vars, %$tt_vars }; | 
| 246 |  |  |  |  |  |  |  | 
| 247 | 256 |  |  |  |  | 1527 | my $output; | 
| 248 | 256 |  |  |  |  | 1204 | $tt->process( $args{template}, $tt_vars, \$output ); | 
| 249 |  |  |  |  |  |  |  | 
| 250 | 256 |  |  |  |  | 118798 | my $contact_email = $config->contact_email; | 
| 251 |  |  |  |  |  |  |  | 
| 252 | 256 |  | 66 |  |  | 2639 | $output ||= qq(ERROR   | 
| 253 |  |  |  |  |  |  | Sorry!  Something went wrong.  Please contact the site administrator | 
| 254 |  |  |  |  |  |  | at $contact_email and quote the | 
| 255 |  |  |  |  |  |  | following error message: Failed to process template: )  | 
| 256 |  |  |  |  |  |  | . $tt->error | 
| 257 |  |  |  |  |  |  | . qq(); | 
| 258 |  |  |  |  |  |  |  | 
| 259 | 256 |  |  |  |  | 7740 | return $header . $output; | 
| 260 |  |  |  |  |  |  | } | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | =item B | 
| 263 |  |  |  |  |  |  |  | 
| 264 |  |  |  |  |  |  | my %node_data = $wiki->retrieve_node( "Home Page" ); | 
| 265 |  |  |  |  |  |  |  | 
| 266 |  |  |  |  |  |  | my %metadata_vars = OpenGuides::Template->extract_metadata_vars( | 
| 267 |  |  |  |  |  |  | wiki     => $wiki, | 
| 268 |  |  |  |  |  |  | config   => $config, | 
| 269 |  |  |  |  |  |  | metadata => $node_data{metadata} | 
| 270 |  |  |  |  |  |  | ); | 
| 271 |  |  |  |  |  |  |  | 
| 272 |  |  |  |  |  |  | # -- or -- | 
| 273 |  |  |  |  |  |  |  | 
| 274 |  |  |  |  |  |  | my %metadata_vars = OpenGuides::Template->extract_metadata_vars( | 
| 275 |  |  |  |  |  |  | wiki     => $wiki, | 
| 276 |  |  |  |  |  |  | config   => $config, | 
| 277 |  |  |  |  |  |  | cgi_obj  => $q | 
| 278 |  |  |  |  |  |  | ); | 
| 279 |  |  |  |  |  |  |  | 
| 280 |  |  |  |  |  |  | # -- then -- | 
| 281 |  |  |  |  |  |  |  | 
| 282 |  |  |  |  |  |  | print OpenGuides::Template->output( | 
| 283 |  |  |  |  |  |  | wiki     => $wiki, | 
| 284 |  |  |  |  |  |  | config   => $config, | 
| 285 |  |  |  |  |  |  | template => "node.tt", | 
| 286 |  |  |  |  |  |  | vars     => { foo => "bar", | 
| 287 |  |  |  |  |  |  | %metadata_vars } | 
| 288 |  |  |  |  |  |  | ); | 
| 289 |  |  |  |  |  |  |  | 
| 290 |  |  |  |  |  |  | Picks out things like categories, locales, phone number etc from | 
| 291 |  |  |  |  |  |  | EITHER the metadata hash returned by L OR the query | 
| 292 |  |  |  |  |  |  | parameters in a L object, and packages them nicely for passing to | 
| 293 |  |  |  |  |  |  | templates or storing in L datastore.  If you supply both | 
| 294 |  |  |  |  |  |  | C and C then C will take precedence, but | 
| 295 |  |  |  |  |  |  | don't do that. | 
| 296 |  |  |  |  |  |  |  | 
| 297 |  |  |  |  |  |  | The variables C, C, C, | 
| 298 |  |  |  |  |  |  | C, C, C, and | 
| 299 |  |  |  |  |  |  | C, which are used to create various forms, will | 
| 300 |  |  |  |  |  |  | only be set if I C is supplied I | 
| 301 |  |  |  |  |  |  | C is true, to prevent these values from being | 
| 302 |  |  |  |  |  |  | stored in the database on a node commit. | 
| 303 |  |  |  |  |  |  |  | 
| 304 |  |  |  |  |  |  | =cut | 
| 305 |  |  |  |  |  |  |  | 
| 306 |  |  |  |  |  |  | sub extract_metadata_vars { | 
| 307 | 732 |  |  | 732 | 1 | 6961 | my ($class, %args) = @_; | 
| 308 | 732 | 100 |  |  |  | 1001 | my %metadata = %{$args{metadata} || {} }; | 
|  | 732 |  |  |  |  | 4265 |  | 
| 309 | 732 |  |  |  |  | 1370 | my $q = $args{cgi_obj}; | 
| 310 | 732 |  |  |  |  | 941 | my $wiki = $args{wiki}; | 
| 311 | 732 |  |  |  |  | 2122 | my $formatter = $wiki->formatter; | 
| 312 | 732 |  |  |  |  | 2628 | my $config = $args{config}; | 
| 313 | 732 |  |  |  |  | 2022 | my $script_name = $config->script_name; | 
| 314 |  |  |  |  |  |  |  | 
| 315 |  |  |  |  |  |  | # Categories and locales are displayed as links in the page footer. | 
| 316 |  |  |  |  |  |  | # We return these twice, as eg 'category' being a simple array of | 
| 317 |  |  |  |  |  |  | # category names, but 'categories' being an array of hashrefs including | 
| 318 |  |  |  |  |  |  | # a URL too.  This is ick. | 
| 319 | 732 |  |  |  |  | 5102 | my (@catlist, @loclist); | 
| 320 | 732 | 100 |  |  |  | 1714 | if ( $args{metadata} ) { | 
| 321 | 373 | 100 |  |  |  | 447 | @catlist = sort @{ $metadata{category} || [] }; | 
|  | 373 |  |  |  |  | 2053 |  | 
| 322 | 373 | 100 |  |  |  | 542 | @loclist = sort @{ $metadata{locale}   || [] }; | 
|  | 373 |  |  |  |  | 1580 |  | 
| 323 |  |  |  |  |  |  | } else { | 
| 324 | 359 |  |  |  |  | 990 | my $categories_text = $q->param('categories'); | 
| 325 | 359 |  |  |  |  | 5123 | my $locales_text    = $q->param('locales'); | 
| 326 |  |  |  |  |  |  |  | 
| 327 |  |  |  |  |  |  | # Basic sanity-checking. Probably lives elsewhere. | 
| 328 | 359 |  |  |  |  | 4431 | foreach ( $categories_text, $locales_text ) { | 
| 329 | 718 |  |  |  |  | 913 | s/</g; | 
| 330 | 718 |  |  |  |  | 901 | s/>/>/g; | 
| 331 |  |  |  |  |  |  | } | 
| 332 |  |  |  |  |  |  |  | 
| 333 |  |  |  |  |  |  | # Trim leading and trailing spaces, collapse internal whitespace. | 
| 334 | 359 |  |  |  |  | 1392 | @catlist = sort grep { s/^\s+//; s/\s+$//; s/\s+/ /g; $_; } | 
|  | 114 |  |  |  |  | 321 |  | 
|  | 114 |  |  |  |  | 191 |  | 
|  | 114 |  |  |  |  | 175 |  | 
|  | 114 |  |  |  |  | 334 |  | 
| 335 |  |  |  |  |  |  | split("\r\n", $categories_text); | 
| 336 | 359 |  |  |  |  | 1024 | @loclist = sort grep { s/^\s+//; s/\s+$//; s/\s+/ /g; $_; } | 
|  | 56 |  |  |  |  | 156 |  | 
|  | 56 |  |  |  |  | 102 |  | 
|  | 56 |  |  |  |  | 100 |  | 
|  | 56 |  |  |  |  | 146 |  | 
| 337 |  |  |  |  |  |  | split("\r\n", $locales_text); | 
| 338 |  |  |  |  |  |  | } | 
| 339 |  |  |  |  |  |  |  | 
| 340 |  |  |  |  |  |  | # Some stuff here is copied from OpenGuides->_autoCreateCategoryLocale | 
| 341 |  |  |  |  |  |  | # - we should rationalise this. | 
| 342 |  |  |  |  |  |  | my @categories = map { | 
| 343 | 732 |  |  |  |  | 1502 | my $param = $formatter->node_name_to_node_param( $_ ); | 
|  | 144 |  |  |  |  | 13461 |  | 
| 344 | 144 |  |  |  |  | 2540 | my $name = $_; | 
| 345 | 144 |  |  |  |  | 987 | $name =~ s/(.*)/\u$1/; | 
| 346 | 144 |  |  |  |  | 380 | $name = $wiki->formatter->_do_freeupper( "Category $name" ); | 
| 347 |  |  |  |  |  |  | { | 
| 348 | 144 | 100 |  |  |  | 1453 | name => $_, | 
| 349 |  |  |  |  |  |  | url  => $wiki->node_exists( $name ) | 
| 350 |  |  |  |  |  |  | ? "$script_name?Category_" . uri_escape( $param ) | 
| 351 |  |  |  |  |  |  | : "", | 
| 352 |  |  |  |  |  |  | }; | 
| 353 |  |  |  |  |  |  | } @catlist; | 
| 354 |  |  |  |  |  |  |  | 
| 355 |  |  |  |  |  |  | my @locales = map { | 
| 356 | 732 |  |  |  |  | 68515 | my $param = $formatter->node_name_to_node_param( $_ ); | 
|  | 85 |  |  |  |  | 11171 |  | 
| 357 | 85 |  |  |  |  | 1465 | my $name = $_; | 
| 358 | 85 |  |  |  |  | 558 | $name =~ s/(.*)/\u$1/; | 
| 359 | 85 |  |  |  |  | 266 | $name = $wiki->formatter->_do_freeupper( "Locale $name" ); | 
| 360 |  |  |  |  |  |  | { | 
| 361 | 85 | 100 |  |  |  | 879 | name => $_, | 
| 362 |  |  |  |  |  |  | url  => $wiki->node_exists( $name ) | 
| 363 |  |  |  |  |  |  | ? "$script_name?Locale_" . uri_escape( $param ) | 
| 364 |  |  |  |  |  |  | : "", | 
| 365 |  |  |  |  |  |  | }; | 
| 366 |  |  |  |  |  |  | } @loclist; | 
| 367 |  |  |  |  |  |  |  | 
| 368 | 732 | 100 |  |  |  | 34125 | my $website = $args{metadata} ? $metadata{website}[0] | 
| 369 |  |  |  |  |  |  | : $q->param("website"); | 
| 370 |  |  |  |  |  |  | # Do truncation for website name display.  Max length of field is set in | 
| 371 |  |  |  |  |  |  | # conf file (website_link_max_chars).  Leading http:// and www. if present | 
| 372 |  |  |  |  |  |  | # is stripped; trailing / is also stripped if it's the only / in the URL. | 
| 373 | 732 |  |  |  |  | 5214 | my $formatted_website_text = ""; | 
| 374 | 732 | 50 | 66 |  |  | 2538 | if ( $website && $website ne "http://" && is_web_uri( $website ) ) { | 
|  |  |  | 66 |  |  |  |  | 
| 375 | 13 |  |  |  |  | 9054 | my $maxlen = $config->website_link_max_chars; | 
| 376 | 13 |  |  |  |  | 93 | my $trunc_website = $website; | 
| 377 | 13 |  |  |  |  | 64 | $trunc_website =~ s|http://(www.)?||; | 
| 378 | 13 | 100 |  |  |  | 46 | if ( $trunc_website =~ tr|/|| == 1 ) { | 
| 379 | 6 |  |  |  |  | 12 | $trunc_website =~ s|/$||; | 
| 380 |  |  |  |  |  |  | } | 
| 381 | 13 | 100 |  |  |  | 37 | if ( length( $trunc_website ) > $maxlen ) { | 
| 382 | 2 |  |  |  |  | 6 | $trunc_website = substr( $trunc_website, 0, $maxlen - 3 ) . "..."; | 
| 383 |  |  |  |  |  |  | } | 
| 384 | 13 |  |  |  |  | 46 | $formatted_website_text = '' | 
| 385 |  |  |  |  |  |  | . $trunc_website . ''; | 
| 386 |  |  |  |  |  |  | } | 
| 387 |  |  |  |  |  |  |  | 
| 388 | 732 | 100 |  |  |  | 2254 | my $hours_text = $args{metadata} ? $metadata{opening_hours_text}[0] | 
| 389 |  |  |  |  |  |  | : $q->param("hours_text"); | 
| 390 |  |  |  |  |  |  |  | 
| 391 | 732 | 100 |  |  |  | 6384 | my $summary = $args{metadata} ? $metadata{summary}[0] | 
| 392 |  |  |  |  |  |  | : $q->param("summary"); | 
| 393 |  |  |  |  |  |  |  | 
| 394 | 732 |  |  |  |  | 8182 | my %vars = ( | 
| 395 |  |  |  |  |  |  | categories             => \@categories, | 
| 396 |  |  |  |  |  |  | locales                => \@locales, | 
| 397 |  |  |  |  |  |  | category               => \@catlist, | 
| 398 |  |  |  |  |  |  | locale                 => \@loclist, | 
| 399 |  |  |  |  |  |  | formatted_website_text => $formatted_website_text, | 
| 400 |  |  |  |  |  |  | hours_text             => $hours_text, | 
| 401 |  |  |  |  |  |  | summary                => $summary, | 
| 402 |  |  |  |  |  |  | ); | 
| 403 |  |  |  |  |  |  |  | 
| 404 | 732 | 100 |  |  |  | 2540 | if ($config->enable_node_image ) { | 
| 405 | 730 |  |  |  |  | 6045 | foreach my $key ( qw( node_image node_image_licence node_image_url | 
| 406 |  |  |  |  |  |  | node_image_copyright ) ) { | 
| 407 | 2920 | 100 |  |  |  | 5844 | my $value = $args{metadata} ? $metadata{$key}[0] | 
| 408 |  |  |  |  |  |  | : $q->param( $key ); | 
| 409 | 2920 | 100 |  |  |  | 18174 | if ( $value ) { | 
| 410 | 37 |  |  |  |  | 81 | $value =~ s/^\s+//g; | 
| 411 | 37 |  |  |  |  | 74 | $value =~ s/\s+$//g; | 
| 412 |  |  |  |  |  |  | } | 
| 413 | 2920 | 100 |  |  |  | 4702 | $vars{$key} = $value if $value; | 
| 414 |  |  |  |  |  |  | } | 
| 415 |  |  |  |  |  |  | } | 
| 416 |  |  |  |  |  |  |  | 
| 417 | 732 | 100 |  |  |  | 1843 | if (exists $metadata{source}) { | 
| 418 | 1 |  |  |  |  | 10 | ($vars{source_site}) = $metadata{source}[0] =~ /^(.*?)(?:\?|$)/; | 
| 419 |  |  |  |  |  |  | } | 
| 420 |  |  |  |  |  |  |  | 
| 421 | 732 | 100 |  |  |  | 1437 | if ( $args{metadata} ) { | 
| 422 | 373 |  |  |  |  | 767 | foreach my $var ( qw( phone fax address postcode os_x os_y osie_x | 
| 423 |  |  |  |  |  |  | osie_y latitude longitude map_link website | 
| 424 |  |  |  |  |  |  | summary) ) { | 
| 425 | 4849 |  |  |  |  | 6073 | $vars{$var} = $metadata{$var}[0]; | 
| 426 |  |  |  |  |  |  | } | 
| 427 |  |  |  |  |  |  | # Data for the distance search forms on the node display. | 
| 428 | 373 |  |  |  |  | 997 | my $geo_handler = $config->geo_handler; | 
| 429 | 373 | 100 |  |  |  | 2435 | if ( $geo_handler == 1 ) { | 
|  |  | 100 |  |  |  |  |  | 
| 430 |  |  |  |  |  |  | %vars = ( | 
| 431 |  |  |  |  |  |  | %vars, | 
| 432 |  |  |  |  |  |  | coord_field_1       => "os_x", | 
| 433 |  |  |  |  |  |  | coord_field_2       => "os_y", | 
| 434 |  |  |  |  |  |  | dist_field          => "os_dist", | 
| 435 |  |  |  |  |  |  | coord_field_1_name  => "OS X coordinate", | 
| 436 |  |  |  |  |  |  | coord_field_2_name  => "OS Y coordinate", | 
| 437 |  |  |  |  |  |  | coord_field_1_value => $metadata{os_x}[0], | 
| 438 | 343 |  |  |  |  | 4766 | coord_field_2_value => $metadata{os_y}[0], | 
| 439 |  |  |  |  |  |  | ); | 
| 440 |  |  |  |  |  |  | } elsif ( $geo_handler == 2 ) { | 
| 441 |  |  |  |  |  |  | %vars = ( | 
| 442 |  |  |  |  |  |  | %vars, | 
| 443 |  |  |  |  |  |  | coord_field_1       => "osie_x", | 
| 444 |  |  |  |  |  |  | coord_field_2       => "osie_y", | 
| 445 |  |  |  |  |  |  | dist_field          => "osie_dist", | 
| 446 |  |  |  |  |  |  | coord_field_1_name | 
| 447 |  |  |  |  |  |  | => "Irish National Grid X coordinate", | 
| 448 |  |  |  |  |  |  | coord_field_2_name | 
| 449 |  |  |  |  |  |  | =>"Irish National Grid Y coordinate", | 
| 450 |  |  |  |  |  |  | coord_field_1_value => $metadata{osie_x}[0], | 
| 451 | 11 |  |  |  |  | 176 | coord_field_2_value => $metadata{osie_y}[0], | 
| 452 |  |  |  |  |  |  | ); | 
| 453 |  |  |  |  |  |  | } else { | 
| 454 | 19 |  |  |  |  | 52 | my $lat_text = "Latitude (" . $config->ellipsoid . " decimal)"; | 
| 455 | 19 |  |  |  |  | 147 | my $long_text = "Longitude (" . $config->ellipsoid . " decimal)"; | 
| 456 |  |  |  |  |  |  | %vars = ( | 
| 457 |  |  |  |  |  |  | %vars, | 
| 458 |  |  |  |  |  |  | coord_field_1       => "latitude", | 
| 459 |  |  |  |  |  |  | coord_field_2       => "longitude", | 
| 460 |  |  |  |  |  |  | dist_field          => "latlong_dist", | 
| 461 |  |  |  |  |  |  | coord_field_1_name  => $lat_text, | 
| 462 |  |  |  |  |  |  | coord_field_2_name  => $long_text, | 
| 463 |  |  |  |  |  |  | coord_field_1_value => $metadata{latitude}[0], | 
| 464 | 19 |  |  |  |  | 376 | coord_field_2_value => $metadata{longitude}[0], | 
| 465 |  |  |  |  |  |  | ); | 
| 466 |  |  |  |  |  |  | } | 
| 467 |  |  |  |  |  |  | } else { | 
| 468 | 359 |  |  |  |  | 878 | foreach my $var ( qw( phone fax address postcode map_link website | 
| 469 |  |  |  |  |  |  | summary) ) { | 
| 470 | 2513 |  |  |  |  | 24868 | $vars{$var} = $q->param($var); | 
| 471 |  |  |  |  |  |  | } | 
| 472 |  |  |  |  |  |  |  | 
| 473 |  |  |  |  |  |  | # Trim leading and trailing whitespace from the fax field - some | 
| 474 |  |  |  |  |  |  | # guides use this to store the Twitter username, so whitespace will | 
| 475 |  |  |  |  |  |  | # mess things up when this is turned into a URL. | 
| 476 | 359 |  |  |  |  | 4193 | $vars{fax} =~ s/^\s+//g; | 
| 477 | 359 |  |  |  |  | 558 | $vars{fax} =~ s/\s+$//g; | 
| 478 |  |  |  |  |  |  |  | 
| 479 | 359 |  |  |  |  | 1134 | my $geo_handler = $config->geo_handler; | 
| 480 | 359 | 100 |  |  |  | 2854 | if ( $geo_handler == 1 ) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 481 | 233 |  |  |  |  | 364314 | require Geo::Coordinates::OSGB; | 
| 482 |  |  |  |  |  |  |  | 
| 483 | 233 |  |  |  |  | 393702 | my $os_x   = $q->param("os_x"); | 
| 484 | 233 |  |  |  |  | 4156 | my $os_y   = $q->param("os_y"); | 
| 485 | 233 |  |  |  |  | 2879 | my $lat    = $q->param("latitude"); | 
| 486 | 233 |  |  |  |  | 2707 | my $long   = $q->param("longitude"); | 
| 487 |  |  |  |  |  |  |  | 
| 488 |  |  |  |  |  |  | # Trim whitespace - trailing whitespace buggers up the | 
| 489 |  |  |  |  |  |  | # integerification by postgres and it's an easy mistake to | 
| 490 |  |  |  |  |  |  | # make when typing into a form. | 
| 491 | 233 |  |  |  |  | 2676 | $os_x =~ s/\s+//g; | 
| 492 | 233 |  |  |  |  | 388 | $os_y =~ s/\s+//g; | 
| 493 |  |  |  |  |  |  |  | 
| 494 |  |  |  |  |  |  | # If we were sent x and y, work out lat/long; and vice versa. | 
| 495 | 233 | 100 | 66 |  |  | 3565 | if ( defined $os_x && length $os_x | 
|  |  | 100 | 66 |  |  |  |  | 
|  |  |  | 100 |  |  |  |  | 
|  |  |  | 100 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
| 496 |  |  |  |  |  |  | && defined $os_y && length $os_y ) { | 
| 497 | 63 |  |  |  |  | 267 | ( $lat, $long ) = Geo::Coordinates::OSGB::grid_to_ll( | 
| 498 |  |  |  |  |  |  | $os_x, $os_y ); | 
| 499 | 63 |  |  |  |  | 13700 | $lat  = sprintf( "%.6f", $lat ); | 
| 500 | 63 |  |  |  |  | 251 | $long = sprintf( "%.6f", $long ); | 
| 501 |  |  |  |  |  |  | } elsif ( defined $lat && length $lat | 
| 502 |  |  |  |  |  |  | && defined $long && length $long ) { | 
| 503 | 17 |  |  |  |  | 74 | ( $os_x, $os_y ) = Geo::Coordinates::OSGB::ll_to_grid( | 
| 504 |  |  |  |  |  |  | $lat, $long ); | 
| 505 | 17 |  |  |  |  | 2988 | $os_x = sprintf( "%d", $os_x ); | 
| 506 | 17 |  |  |  |  | 53 | $os_y = sprintf( "%d", $os_y ); | 
| 507 |  |  |  |  |  |  | } | 
| 508 |  |  |  |  |  |  |  | 
| 509 | 233 | 100 | 66 |  |  | 1722 | if ( defined $os_x && length $os_x | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 100 |  |  |  |  | 
| 510 |  |  |  |  |  |  | && defined $os_y && length $os_y ) { | 
| 511 | 80 |  |  |  |  | 1066 | %vars = ( | 
| 512 |  |  |  |  |  |  | %vars, | 
| 513 |  |  |  |  |  |  | latitude  => $lat, | 
| 514 |  |  |  |  |  |  | longitude => $long, | 
| 515 |  |  |  |  |  |  | os_x      => $os_x, | 
| 516 |  |  |  |  |  |  | os_y      => $os_y, | 
| 517 |  |  |  |  |  |  | ); | 
| 518 |  |  |  |  |  |  | } | 
| 519 | 233 | 100 |  |  |  | 928 | if ( $args{set_coord_field_vars} ) { | 
| 520 | 1 |  |  |  |  | 20 | %vars = ( | 
| 521 |  |  |  |  |  |  | %vars, | 
| 522 |  |  |  |  |  |  | coord_field_1       => "os_x", | 
| 523 |  |  |  |  |  |  | coord_field_2       => "os_y", | 
| 524 |  |  |  |  |  |  | dist_field          => "os_dist", | 
| 525 |  |  |  |  |  |  | coord_field_1_name  => "OS X coordinate", | 
| 526 |  |  |  |  |  |  | coord_field_2_name  => "OS Y coordinate", | 
| 527 |  |  |  |  |  |  | coord_field_1_value => $os_x, | 
| 528 |  |  |  |  |  |  | coord_field_2_value => $os_y, | 
| 529 |  |  |  |  |  |  | ); | 
| 530 |  |  |  |  |  |  | } | 
| 531 |  |  |  |  |  |  | } elsif ( $geo_handler == 2 ) { | 
| 532 | 57 |  |  |  |  | 3980 | require Geo::Coordinates::ITM; | 
| 533 |  |  |  |  |  |  |  | 
| 534 | 57 |  |  |  |  | 65481 | my $osie_x = $q->param("osie_x"); | 
| 535 | 57 |  |  |  |  | 829 | my $osie_y = $q->param("osie_y"); | 
| 536 | 57 |  |  |  |  | 667 | my $lat    = $q->param("latitude"); | 
| 537 | 57 |  |  |  |  | 694 | my $long   = $q->param("longitude"); | 
| 538 |  |  |  |  |  |  |  | 
| 539 |  |  |  |  |  |  | # Trim whitespace. | 
| 540 | 57 |  |  |  |  | 751 | $osie_x =~ s/\s+//g; | 
| 541 | 57 |  |  |  |  | 122 | $osie_y =~ s/\s+//g; | 
| 542 |  |  |  |  |  |  |  | 
| 543 |  |  |  |  |  |  | # If we were sent x and y, work out lat/long; and vice versa. | 
| 544 | 57 | 100 | 66 |  |  | 735 | if ( defined $osie_x && length $osie_x | 
|  |  | 100 | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
| 545 |  |  |  |  |  |  | && defined $osie_y && length $osie_y ) { | 
| 546 | 55 |  |  |  |  | 253 | ( $lat, $long ) = Geo::Coordinates::ITM::grid_to_ll( | 
| 547 |  |  |  |  |  |  | $osie_x, $osie_y ); | 
| 548 | 55 |  |  |  |  | 12975 | $lat  = sprintf( "%.6f", $lat ); | 
| 549 | 55 |  |  |  |  | 230 | $long = sprintf( "%.6f", $long ); | 
| 550 |  |  |  |  |  |  | } elsif ( defined $lat && length $lat && defined $long | 
| 551 |  |  |  |  |  |  | && length $long ) { | 
| 552 | 1 |  |  |  |  | 8 | ( $osie_x, $osie_y ) = Geo::Coordinates::ITM::ll_to_grid( | 
| 553 |  |  |  |  |  |  | $lat, $long ); | 
| 554 | 1 |  |  |  |  | 185 | $osie_x = sprintf( "%d", $osie_x ); | 
| 555 | 1 |  |  |  |  | 3 | $osie_y = sprintf( "%d", $osie_y ); | 
| 556 |  |  |  |  |  |  | } | 
| 557 | 57 | 50 | 66 |  |  | 612 | if ( defined $osie_x && length $osie_x | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
| 558 |  |  |  |  |  |  | && defined $osie_y && length $osie_y ) { | 
| 559 | 56 |  |  |  |  | 740 | %vars = ( | 
| 560 |  |  |  |  |  |  | %vars, | 
| 561 |  |  |  |  |  |  | latitude  => $lat, | 
| 562 |  |  |  |  |  |  | longitude => $long, | 
| 563 |  |  |  |  |  |  | osie_x    => $osie_x, | 
| 564 |  |  |  |  |  |  | osie_y    => $osie_y, | 
| 565 |  |  |  |  |  |  | ); | 
| 566 |  |  |  |  |  |  | } | 
| 567 | 57 | 50 |  |  |  | 324 | if ( $args{set_coord_field_vars} ) { | 
| 568 | 0 |  |  |  |  | 0 | %vars = ( | 
| 569 |  |  |  |  |  |  | %vars, | 
| 570 |  |  |  |  |  |  | coord_field_1       => "osie_x", | 
| 571 |  |  |  |  |  |  | coord_field_2       => "osie_y", | 
| 572 |  |  |  |  |  |  | dist_field          => "osie_dist", | 
| 573 |  |  |  |  |  |  | coord_field_1_name | 
| 574 |  |  |  |  |  |  | => "Irish National Grid X coordinate", | 
| 575 |  |  |  |  |  |  | coord_field_2_name | 
| 576 |  |  |  |  |  |  | => "Irish National Grid Y coordinate", | 
| 577 |  |  |  |  |  |  | coord_field_1_value => $osie_x, | 
| 578 |  |  |  |  |  |  | coord_field_2_value => $osie_y, | 
| 579 |  |  |  |  |  |  | ); | 
| 580 |  |  |  |  |  |  | } | 
| 581 |  |  |  |  |  |  | } elsif ( $geo_handler == 3 ) { | 
| 582 | 69 |  |  |  |  | 6643 | require Geo::Coordinates::UTM; | 
| 583 | 69 |  |  |  |  | 78012 | my $lat    = $q->param("latitude"); | 
| 584 | 69 |  |  |  |  | 1030 | my $long   = $q->param("longitude"); | 
| 585 |  |  |  |  |  |  |  | 
| 586 | 69 | 50 | 66 |  |  | 1975 | if ( defined $lat && length $lat | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
| 587 |  |  |  |  |  |  | && defined $long && length $long ) { | 
| 588 |  |  |  |  |  |  | # Trim whitespace. | 
| 589 | 66 |  |  |  |  | 234 | $lat =~ s/\s+//g; | 
| 590 | 66 |  |  |  |  | 154 | $long =~ s/\s+//g; | 
| 591 | 66 |  |  |  |  | 224 | my ($zone, $easting, $northing) = | 
| 592 |  |  |  |  |  |  | Geo::Coordinates::UTM::latlon_to_utm( $config->ellipsoid, | 
| 593 |  |  |  |  |  |  | $lat, $long ); | 
| 594 | 66 |  |  |  |  | 6327 | $easting  =~ s/\..*//; # chop off decimal places | 
| 595 | 66 |  |  |  |  | 475 | $northing =~ s/\..*//; # - metre accuracy enough | 
| 596 | 66 |  |  |  |  | 826 | %vars = ( | 
| 597 |  |  |  |  |  |  | %vars, | 
| 598 |  |  |  |  |  |  | latitude  => $lat, | 
| 599 |  |  |  |  |  |  | longitude => $long, | 
| 600 |  |  |  |  |  |  | easting   => $easting, | 
| 601 |  |  |  |  |  |  | northing  => $northing, | 
| 602 |  |  |  |  |  |  | ); | 
| 603 |  |  |  |  |  |  | } | 
| 604 | 69 | 50 |  |  |  | 328 | if ( $args{set_coord_field_vars} ) { | 
| 605 | 0 |  |  |  |  | 0 | %vars = ( | 
| 606 |  |  |  |  |  |  | %vars, | 
| 607 |  |  |  |  |  |  | coord_field_1       => "latitude", | 
| 608 |  |  |  |  |  |  | coord_field_2       => "longitude", | 
| 609 |  |  |  |  |  |  | dist_field          => "latlong_dist", | 
| 610 |  |  |  |  |  |  | coord_field_1_name  => "Latitude (decimal)", | 
| 611 |  |  |  |  |  |  | coord_field_2_name  => "Longitude (decimal)", | 
| 612 |  |  |  |  |  |  | coord_field_1_value => $lat, | 
| 613 |  |  |  |  |  |  | coord_field_2_value => $long, | 
| 614 |  |  |  |  |  |  | ); | 
| 615 |  |  |  |  |  |  | } | 
| 616 |  |  |  |  |  |  | } | 
| 617 |  |  |  |  |  |  | } | 
| 618 |  |  |  |  |  |  |  | 
| 619 |  |  |  |  |  |  | # Check whether we need to munge lat and long. | 
| 620 |  |  |  |  |  |  | # Store them unmunged as well so commit_node can get hold of them. | 
| 621 | 732 |  |  |  |  | 5495 | my %prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config ); | 
| 622 | 732 | 100 |  |  |  | 2616 | if ( $prefs{latlong_traditional} ) { | 
| 623 | 6 |  |  |  |  | 9 | foreach my $var ( qw( latitude longitude ) ) { | 
| 624 | 12 | 100 | 66 |  |  | 43 | next unless defined $vars{$var} && length $vars{$var}; | 
| 625 | 4 |  |  |  |  | 8 | $vars{$var."_unmunged"} = $vars{$var}; | 
| 626 | 4 |  |  |  |  | 7 | $vars{$var} = _deg2string($vars{$var}); | 
| 627 |  |  |  |  |  |  | } | 
| 628 |  |  |  |  |  |  | } | 
| 629 |  |  |  |  |  |  |  | 
| 630 | 732 |  |  |  |  | 9205 | return %vars; | 
| 631 |  |  |  |  |  |  | } | 
| 632 |  |  |  |  |  |  |  | 
| 633 |  |  |  |  |  |  | # Slightly modified from the no-longer-available Geography::NationalGrid | 
| 634 |  |  |  |  |  |  | # module, which was written by P Kent and distributed under the Artistic | 
| 635 |  |  |  |  |  |  | # Licence. | 
| 636 |  |  |  |  |  |  | sub _deg2string { | 
| 637 | 4 |  |  | 4 |  | 5 | my $degrees = shift; | 
| 638 |  |  |  |  |  |  |  | 
| 639 |  |  |  |  |  |  | # make positive | 
| 640 | 4 |  |  |  |  | 4 | my $isneg = 0; | 
| 641 | 4 | 100 |  |  |  | 14 | if ($degrees < 0) { | 
|  |  | 50 |  |  |  |  |  | 
| 642 | 2 |  |  |  |  | 2 | $isneg = 1; | 
| 643 | 2 |  |  |  |  | 3 | $degrees = abs( $degrees ); | 
| 644 |  |  |  |  |  |  | } elsif ($degrees == 0) { | 
| 645 | 0 |  |  |  |  | 0 | return '0d 0m 0s'; | 
| 646 |  |  |  |  |  |  | } | 
| 647 |  |  |  |  |  |  |  | 
| 648 | 4 |  |  |  |  | 5 | my $d = int( $degrees ); | 
| 649 | 4 |  |  |  |  | 4 | $degrees -= $d; | 
| 650 | 4 |  |  |  |  | 4 | $degrees *= 60; | 
| 651 | 4 |  |  |  |  | 4 | my $m = int( $degrees ); | 
| 652 | 4 |  |  |  |  | 2 | $degrees -= $m; | 
| 653 | 4 |  |  |  |  | 3 | my $s = $degrees * 60; | 
| 654 |  |  |  |  |  |  |  | 
| 655 | 4 | 100 |  |  |  | 30 | return sprintf("%s%dd %um %.2fs", ($isneg?'-':''), $d, $m, $s); | 
| 656 |  |  |  |  |  |  | } | 
| 657 |  |  |  |  |  |  |  | 
| 658 |  |  |  |  |  |  | =back | 
| 659 |  |  |  |  |  |  |  | 
| 660 |  |  |  |  |  |  | =head1 AUTHOR | 
| 661 |  |  |  |  |  |  |  | 
| 662 |  |  |  |  |  |  | The OpenGuides Project (openguides-dev@lists.openguides.org) | 
| 663 |  |  |  |  |  |  |  | 
| 664 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 665 |  |  |  |  |  |  |  | 
| 666 |  |  |  |  |  |  | Copyright (C) 2003-2013 The OpenGuides Project.  All Rights Reserved. | 
| 667 |  |  |  |  |  |  |  | 
| 668 |  |  |  |  |  |  | This module is free software; you can redistribute it and/or modify it | 
| 669 |  |  |  |  |  |  | under the same terms as Perl itself. | 
| 670 |  |  |  |  |  |  |  | 
| 671 |  |  |  |  |  |  | =cut | 
| 672 |  |  |  |  |  |  |  | 
| 673 |  |  |  |  |  |  | 1; |