| 1 | package CGI::Application::Plugin::PageLookup::Href; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | =head1 NAME | ||||||||||||
| 8 | |||||||||||||
| 9 | CGI::Application::Plugin::PageLookup::Href - Manage internal URLs | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 VERSION | ||||||||||||
| 12 | |||||||||||||
| 13 | Version 1.8 | ||||||||||||
| 14 | |||||||||||||
| 15 | =cut | ||||||||||||
| 16 | |||||||||||||
| 17 | 3 | our $VERSION = '1.8'; | |||||||||||
| 18 | 3 | our $AUTOLOAD; | |||||||||||
| 19 | |||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||
| 21 | |||||||||||||
| 22 | This module manages the instantiation of list style template parameters across a website; | ||||||||||||
| 23 | for example TMPL_LOOP in L<HTML::Template>, though one must use L<HTML::Template::Pluggable> for it to | ||||||||||||
| 24 | work. For example a menu is typically implemented in HTML as <ul>....</ul>. Using this module | ||||||||||||
| 25 | the menu can be instantiated from the database and the same data used to instantiate a human-readable | ||||||||||||
| 26 | sitemap page. On the other hand the staff page will have list data that is only required on that page. | ||||||||||||
| 27 | This module depends on L<CGI::Application::Plugin::PageLookup>. | ||||||||||||
| 28 | |||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||
| 30 | |||||||||||||
| 31 | In the template you might define a number of links as follows: | ||||||||||||
| 32 | |||||||||||||
| 33 | <p>This page in other languages:</p> | ||||||||||||
| 34 | <ul> | ||||||||||||
| 35 | <li><a href="<TMPL_VAR NAME="href.translate('en')">">English</a></li> | ||||||||||||
| 36 | <li><a href="<TMPL_VAR NAME="href.translate('de')">">German</a></li> | ||||||||||||
| 37 | <li><a href="<TMPL_VAR NAME="href.translate('fr')">">French</a></li> | ||||||||||||
| 38 | </ul> | ||||||||||||
| 39 | |||||||||||||
| 40 | <p>Some other pages that may be of interest</p> | ||||||||||||
| 41 | <ul> | ||||||||||||
| 42 | <li><a href="<TMPL_VAR NAME="href.refer(1)">">My first page</a></li> | ||||||||||||
| 43 | <li><a href="<TMPL_VAR NAME="href.refer(2)">">My second and more exciting page</a></li> | ||||||||||||
| 44 | <li><a href="<TMPL_VAR NAME="href.refer(3)">">My last will and testament</a></li> | ||||||||||||
| 45 | </ul> | ||||||||||||
| 46 | |||||||||||||
| 47 | You must register the "href" parameter as a CGI::Application::Plugin::PageLookup::Href object as follows: | ||||||||||||
| 48 | |||||||||||||
| 49 | use CGI::Application; | ||||||||||||
| 50 | use CGI::Application::Plugin::PageLookup qw(:all); | ||||||||||||
| 51 | use CGI::Application::Plugin::PageLookup::Href; | ||||||||||||
| 52 | use HTML::Template::Pluggable; | ||||||||||||
| 53 | use HTML::Template::Plugin::Dot; | ||||||||||||
| 54 | |||||||||||||
| 55 | sub cgiapp_init { | ||||||||||||
| 56 | my $self = shift; | ||||||||||||
| 57 | |||||||||||||
| 58 | # pagelookup depends CGI::Application::DBH; | ||||||||||||
| 59 | $self->dbh_config(......); # whatever arguments are appropriate | ||||||||||||
| 60 | |||||||||||||
| 61 | $self->html_tmpl_class('HTML::Template::Pluggable'); | ||||||||||||
| 62 | |||||||||||||
| 63 | $self->pagelookup_config( | ||||||||||||
| 64 | |||||||||||||
| 65 | # load smart dot-notation objects | ||||||||||||
| 66 | objects => | ||||||||||||
| 67 | { | ||||||||||||
| 68 | # Register the 'href' parameter | ||||||||||||
| 69 | href => 'CGI::Application::Plugin::PageLookup::Href', | ||||||||||||
| 70 | }, | ||||||||||||
| 71 | |||||||||||||
| 72 | |||||||||||||
| 73 | ); | ||||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | |||||||||||||
| 77 | ... | ||||||||||||
| 78 | |||||||||||||
| 79 | =head1 FUNCTIONS | ||||||||||||
| 80 | |||||||||||||
| 81 | =head2 new | ||||||||||||
| 82 | |||||||||||||
| 83 | A constructor following the requirements set out in L<CGI::Application::Plugin::PageLookup>. | ||||||||||||
| 84 | |||||||||||||
| 85 | =cut | ||||||||||||
| 86 | |||||||||||||
| 87 | 3 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 88 | 3 | my $class = shift; | |||||||||||
| 89 | 3 | my $self = {}; | |||||||||||
| 90 | 3 | $self->{cgiapp} = shift; | |||||||||||
| 91 | 3 | $self->{page_id} = shift; | |||||||||||
| 92 | 3 | $self->{template} = shift; | |||||||||||
| 93 | 3 | $self->{name} = shift; | |||||||||||
| 94 | 3 | my %args = @_; | |||||||||||
| 95 | 3 | $self->{config} = \%args; | |||||||||||
| 96 | |||||||||||||
| 97 | 3 | bless $self, $class; | |||||||||||
| 98 | 3 | return $self; | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | =head2 translate | ||||||||||||
| 102 | |||||||||||||
| 103 | This function takes the existing page id and translates it into another specified language. This way every | ||||||||||||
| 104 | page can link to its cognate page in another languages. | ||||||||||||
| 105 | |||||||||||||
| 106 | =cut | ||||||||||||
| 107 | |||||||||||||
| 108 | 9 | ▶ | sub translate { | ||||||||||
|
Subroutine
translate: called CC=6 SCAR=20.6
Pod
documented
| |||||||||||||
| 109 | 9 | my $self = shift; | |||||||||||
| 110 | 9 | my $language = shift; | |||||||||||
| 111 | 9 | my $prefix = $self->{cgiapp}->pagelookup_prefix(%{$self->{config}}); | |||||||||||
| 112 | 9 | my $page_id = $self->{page_id}; | |||||||||||
| 113 | 9 | my $dbh = $self->{cgiapp}->dbh; | |||||||||||
| 114 | |||||||||||||
| 115 | # First one pass over the loop | ||||||||||||
| 116 | 9 | my $sql = "SELECT p1.pageId FROM ${prefix}pages p1, ${prefix}pages p2 WHERE p1.internalId = p2.internalId AND p1.lang = '$language' AND p2.pageId = '$page_id'"; | |||||||||||
| 117 | 9 | ▶ | my $sth = $dbh->prepare($sql) || croak $dbh->errstr; | ||||||||||
|
Condition50%1/2
$dbh->prepare($sql) || croak($dbh->errstr)
MC/DC0%0/1
$dbh->prepare($sql) || croak($dbh->errstr)
$dbh->prepare($sql)
Truth table50%1/2
A
A= $dbh->prepare($sql)
| |||||||||||||
| 118 | 9 | ▶ | $sth->execute || croak $dbh->errstr; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | 9 | my $hash_ref = $sth->fetchrow_hashref; | |||||||||||
| 120 | 9 | ▶ | if ($hash_ref) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 9 | $sth->finish; | |||||||||||
| 122 | 9 | ▶ | return $hash_ref->{pageId} if exists $hash_ref->{pageId}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | 0 | croak "could not translate $page_id to $language"; | |||||||||||
| 124 | } | ||||||||||||
| 125 | 0 | ▶ | croak $sth->errstr if $sth->err; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 126 | 0 | $sth->finish; | |||||||||||
| 127 | 0 | croak "could not translate $page_id to $language"; | |||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | =head2 refer | ||||||||||||
| 131 | |||||||||||||
| 132 | This function takes an internal id and translated that into the corresponding page but in the same language as the current page. | ||||||||||||
| 133 | This way URLs can be search engine friendly irrespective of language. | ||||||||||||
| 134 | |||||||||||||
| 135 | =cut | ||||||||||||
| 136 | |||||||||||||
| 137 | 9 | ▶ | sub refer { | ||||||||||
|
Subroutine
refer: called CC=7 SCAR=22.7
Pod
documented
| |||||||||||||
| 138 | 9 | my $self = shift; | |||||||||||
| 139 | 9 | ▶ | my $internalid = shift || 0; | ||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 140 | 9 | my $prefix = $self->{cgiapp}->pagelookup_prefix(%{$self->{config}}); | |||||||||||
| 141 | 9 | my $page_id = $self->{page_id}; | |||||||||||
| 142 | 9 | my $dbh = $self->{cgiapp}->dbh; | |||||||||||
| 143 | |||||||||||||
| 144 | # First one pass over the loop | ||||||||||||
| 145 | 9 | my $sql = "SELECT p1.pageId FROM ${prefix}pages p1, ${prefix}pages p2 WHERE p1.internalId = $internalid AND p1.lang = p2.lang AND p2.pageId = '$page_id'"; | |||||||||||
| 146 | 9 | ▶ | my $sth = $dbh->prepare($sql) || croak $dbh->errstr; | ||||||||||
|
Condition50%1/2
$dbh->prepare($sql) || croak($dbh->errstr)
MC/DC0%0/1
$dbh->prepare($sql) || croak($dbh->errstr)
$dbh->prepare($sql)
Truth table50%1/2
A
A= $dbh->prepare($sql)
| |||||||||||||
| 147 | 9 | ▶ | $sth->execute || croak $dbh->errstr; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 9 | my $hash_ref = $sth->fetchrow_hashref; | |||||||||||
| 149 | 9 | ▶ | if ($hash_ref) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | 9 | $sth->finish; | |||||||||||
| 151 | 9 | ▶ | return $hash_ref->{pageId} if exists $hash_ref->{pageId}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 152 | 0 | croak "could not find $internalid page"; | |||||||||||
| 153 | } | ||||||||||||
| 154 | 0 | ▶ | croak $sth->errstr if $sth->err; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 155 | 0 | $sth->finish; | |||||||||||
| 156 | 0 | croak "could not find $internalid page"; | |||||||||||
| 157 | } | ||||||||||||
| 158 | |||||||||||||
| 159 | =head1 AUTHOR | ||||||||||||
| 160 | |||||||||||||
| 161 | Nicholas Bamber, C<< <nicholas at periapt.co.uk> >> | ||||||||||||
| 162 | |||||||||||||
| 163 | =head1 BUGS | ||||||||||||
| 164 | |||||||||||||
| 165 | Please report any bugs or feature requests to C<bug-cgi-application-plugin-pagelookup at rt.cpan.org>, or through | ||||||||||||
| 166 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-PageLookup>. I will be notified, and then you'll | ||||||||||||
| 167 | automatically be notified of progress on your bug as I make changes. | ||||||||||||
| 168 | |||||||||||||
| 169 | =head1 SUPPORT | ||||||||||||
| 170 | |||||||||||||
| 171 | You can find documentation for this module with the perldoc command. | ||||||||||||
| 172 | |||||||||||||
| 173 | perldoc CGI::Application::Plugin::PageLookup::Href | ||||||||||||
| 174 | |||||||||||||
| 175 | |||||||||||||
| 176 | You can also look for information at: | ||||||||||||
| 177 | |||||||||||||
| 178 | =over 4 | ||||||||||||
| 179 | |||||||||||||
| 180 | =item * RT: CPAN's request tracker | ||||||||||||
| 181 | |||||||||||||
| 182 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Application-Plugin-PageLookup> | ||||||||||||
| 183 | |||||||||||||
| 184 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 185 | |||||||||||||
| 186 | L<http://annocpan.org/dist/CGI-Application-Plugin-PageLookup> | ||||||||||||
| 187 | |||||||||||||
| 188 | =item * CPAN Ratings | ||||||||||||
| 189 | |||||||||||||
| 190 | L<http://cpanratings.perl.org/d/CGI-Application-Plugin-PageLookup> | ||||||||||||
| 191 | |||||||||||||
| 192 | =item * Search CPAN | ||||||||||||
| 193 | |||||||||||||
| 194 | L<http://search.cpan.org/dist/CGI-Application-Plugin-PageLookup/> | ||||||||||||
| 195 | |||||||||||||
| 196 | =back | ||||||||||||
| 197 | |||||||||||||
| 198 | |||||||||||||
| 199 | =head1 ACKNOWLEDGEMENTS | ||||||||||||
| 200 | |||||||||||||
| 201 | |||||||||||||
| 202 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 203 | |||||||||||||
| 204 | Copyright 2009 Nicholas Bamber. | ||||||||||||
| 205 | |||||||||||||
| 206 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 207 | under the terms of either: the GNU General Public License as published | ||||||||||||
| 208 | by the Free Software Foundation; or the Artistic License. | ||||||||||||
| 209 | |||||||||||||
| 210 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||
| 211 | |||||||||||||
| 212 | |||||||||||||
| 213 | =cut | ||||||||||||
| 214 | |||||||||||||
| 215 | 3 | 1; # End of CGI::Application::Plugin::PageLookup::Href | |||||||||||