blib/lib/CGI/Application/Plugin/PageLookup/Href.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 45 | 53 | 84.9 |
branch | 6 | 16 | 37.5 |
condition | 3 | 8 | 37.5 |
subroutine | 6 | 6 | 100.0 |
pod | 3 | 3 | 100.0 |
total | 63 | 86 | 73.2 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package CGI::Application::Plugin::PageLookup::Href; | ||||||
2 | |||||||
3 | 2 | 2 | 4083 | use warnings; | |||
2 | 5 | ||||||
2 | 76 | ||||||
4 | 2 | 2 | 10 | use strict; | |||
2 | 4 | ||||||
2 | 71 | ||||||
5 | 2 | 2 | 11 | use Carp; | |||
2 | 6 | ||||||
2 | 1662 | ||||||
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 | our $VERSION = '1.8'; | ||||||
18 | 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 |
||||||
24 | work. For example a menu is typically implemented in HTML as
|
||||||
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 |
||||||
28 | |||||||
29 | =head1 SYNOPSIS | ||||||
30 | |||||||
31 | In the template you might define a number of links as follows: | ||||||
32 | |||||||
33 | This page in other languages: |
||||||
34 | |
||||||
35 | |
||||||
36 | |
||||||
37 | |
||||||
38 | |||||||
39 | |||||||
40 | Some other pages that may be of interest |
||||||
41 | |
||||||
42 | |
||||||
43 | |
||||||
44 | |
||||||
45 | |||||||
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 |
||||||
84 | |||||||
85 | =cut | ||||||
86 | |||||||
87 | sub new { | ||||||
88 | 3 | 3 | 1 | 7 | my $class = shift; | ||
89 | 3 | 7 | my $self = {}; | ||||
90 | 3 | 8 | $self->{cgiapp} = shift; | ||||
91 | 3 | 9 | $self->{page_id} = shift; | ||||
92 | 3 | 6 | $self->{template} = shift; | ||||
93 | 3 | 9 | $self->{name} = shift; | ||||
94 | 3 | 7 | my %args = @_; | ||||
95 | 3 | 9 | $self->{config} = \%args; | ||||
96 | |||||||
97 | 3 | 17 | bless $self, $class; | ||||
98 | 3 | 15 | 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 | sub translate { | ||||||
109 | 9 | 9 | 1 | 6013 | my $self = shift; | ||
110 | 9 | 16 | my $language = shift; | ||||
111 | 9 | 18 | my $prefix = $self->{cgiapp}->pagelookup_prefix(%{$self->{config}}); | ||||
9 | 48 | ||||||
112 | 9 | 18 | my $page_id = $self->{page_id}; | ||||
113 | 9 | 39 | my $dbh = $self->{cgiapp}->dbh; | ||||
114 | |||||||
115 | # First one pass over the loop | ||||||
116 | 9 | 576 | 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 | 33 | 48 | my $sth = $dbh->prepare($sql) || croak $dbh->errstr; | |||
118 | 9 | 50 | 2132 | $sth->execute || croak $dbh->errstr; | |||
119 | 9 | 363 | my $hash_ref = $sth->fetchrow_hashref; | ||||
120 | 9 | 50 | 44 | if ($hash_ref) { | |||
121 | 9 | 38 | $sth->finish; | ||||
122 | 9 | 50 | 170 | return $hash_ref->{pageId} if exists $hash_ref->{pageId}; | |||
123 | 0 | 0 | croak "could not translate $page_id to $language"; | ||||
124 | } | ||||||
125 | 0 | 0 | 0 | croak $sth->errstr if $sth->err; | |||
126 | 0 | 0 | $sth->finish; | ||||
127 | 0 | 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 | sub refer { | ||||||
138 | 9 | 9 | 1 | 5308 | my $self = shift; | ||
139 | 9 | 50 | 32 | my $internalid = shift || 0; | |||
140 | 9 | 15 | my $prefix = $self->{cgiapp}->pagelookup_prefix(%{$self->{config}}); | ||||
9 | 44 | ||||||
141 | 9 | 20 | my $page_id = $self->{page_id}; | ||||
142 | 9 | 35 | my $dbh = $self->{cgiapp}->dbh; | ||||
143 | |||||||
144 | # First one pass over the loop | ||||||
145 | 9 | 538 | 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 | 33 | 45 | my $sth = $dbh->prepare($sql) || croak $dbh->errstr; | |||
147 | 9 | 50 | 1953 | $sth->execute || croak $dbh->errstr; | |||
148 | 9 | 292 | my $hash_ref = $sth->fetchrow_hashref; | ||||
149 | 9 | 50 | 40 | if ($hash_ref) { | |||
150 | 9 | 36 | $sth->finish; | ||||
151 | 9 | 50 | 163 | return $hash_ref->{pageId} if exists $hash_ref->{pageId}; | |||
152 | 0 | croak "could not find $internalid page"; | |||||
153 | } | ||||||
154 | 0 | 0 | croak $sth->errstr if $sth->err; | ||||
155 | 0 | $sth->finish; | |||||
156 | 0 | croak "could not find $internalid page"; | |||||
157 | } | ||||||
158 | |||||||
159 | =head1 AUTHOR | ||||||
160 | |||||||
161 | Nicholas Bamber, C<< |
||||||
162 | |||||||
163 | =head1 BUGS | ||||||
164 | |||||||
165 | Please report any bugs or feature requests to C |
||||||
166 | the web interface at L |
||||||
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 |
||||||
183 | |||||||
184 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||
185 | |||||||
186 | L |
||||||
187 | |||||||
188 | =item * CPAN Ratings | ||||||
189 | |||||||
190 | L |
||||||
191 | |||||||
192 | =item * Search CPAN | ||||||
193 | |||||||
194 | L |
||||||
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 | 1; # End of CGI::Application::Plugin::PageLookup::Href |