| 1 | package CGI::Application::Plugin::PageLookup::Value; | ||||||||||||||||||||
| 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 | |||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | CGI::Application::Plugin::PageLookup::Value - Manage values scattered across a website | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | Version 1.8 | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 3 | our $VERSION = '1.8'; | |||||||||||||||||||
| 17 | 3 | our $AUTOLOAD; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | This module allows the management of template variable instantiation across a website. | ||||||||||||||||||||
| 22 | You can specialise a default value for a parameter (without requiring it to be used on every page) | ||||||||||||||||||||
| 23 | and override that value for specific pages. Or you can merely set the value for individual pages. | ||||||||||||||||||||
| 24 | This depends on L<CGI::Application::Plugin::PageLookup>. For loops see L<CGI::Application::Plugin::PageLookup::Loop>. | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | In the template you can do things like <TMPL_VAR NAME="values.hope">, <TMPL_VAR NAME="values.faith"> and <TMPL_VAR NAME="values.charity">. | ||||||||||||||||||||
| 29 | You must register the "values" parameter as a CGI::Application::Plugin::PageLookup::Value object as follows: | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | use CGI::Application; | ||||||||||||||||||||
| 32 | use CGI::Application::Plugin::PageLookup qw(:all); | ||||||||||||||||||||
| 33 | use CGI::Application::Plugin::PageLookup::Value; | ||||||||||||||||||||
| 34 | use HTML::Template::Pluggable; | ||||||||||||||||||||
| 35 | use HTML::Template::Plugin::Dot; | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | sub cgiapp_init { | ||||||||||||||||||||
| 38 | my $self = shift; | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # pagelookup depends CGI::Application::DBH; | ||||||||||||||||||||
| 41 | $self->dbh_config(......); # whatever arguments are appropriate | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | $self->html_tmpl_class('HTML::Template::Pluggable'); | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | $self->pagelookup_config( | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # load smart dot-notation objects | ||||||||||||||||||||
| 48 | objects => | ||||||||||||||||||||
| 49 | ( | ||||||||||||||||||||
| 50 | # Register the 'values' parameter | ||||||||||||||||||||
| 51 | values => 'CGI::Application::Plugin::PageLookup::Value, | ||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | ... | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | After that all that remains is to populate the cgiapp_values table with the appropriate values. Notice that the code does | ||||||||||||||||||||
| 60 | not need to know what comes after the dot in the templates. So if you want to set "values.hope" to "disappointment" in all English | ||||||||||||||||||||
| 61 | pages you would run | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | INSERT INTO cgiapp_values (lang, param, value) VALUES ('en', 'hope', 'disappointment') | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | On the other hand if you wanted set "values.hope" to "a glimmer of light" on page 7 but "disappointment" everywhere else, then you would | ||||||||||||||||||||
| 66 | run | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | INSERT INTO cgiapp_values (lang, param, value) VALUES ('en', 'hope', 'disappointment') | ||||||||||||||||||||
| 69 | INSERT INTO cgiapp_values (lang, internalId, param, value) VALUES ('en', 7, 'hope', 'a glimmer of light') | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head1 DATABASE | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | This module depends on only one extra table: cgiapp_values. The lang and internalId columns join against | ||||||||||||||||||||
| 75 | the cgiapp_table. However the internalId column can null, making the parameter available to all pages | ||||||||||||||||||||
| 76 | in the same language. The lang, internalId and param columns form the key of the table. | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =over | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =item Table: cgiapp_values | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Field Type Null Key Default Extra | ||||||||||||||||||||
| 83 | ------------ ------------------------------------------------------------------- ---- ---- ------- ----- | ||||||||||||||||||||
| 84 | lang varchar(2) NO UNI NULL | ||||||||||||||||||||
| 85 | internalId unsigned numeric(10,0) YES UNI NULL | ||||||||||||||||||||
| 86 | param varchar(20) NO UNI NULL | ||||||||||||||||||||
| 87 | value text NO NULL | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =back | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =head1 FUNCTIONS | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =head2 new | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | A constructor following the requirements set out in L<CGI::Application::Plugin::PageLookup>. | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 18 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 100 | 18 | my $class = shift; | |||||||||||||||||||
| 101 | 18 | my $self = {}; | |||||||||||||||||||
| 102 | 18 | $self->{cgiapp} = shift; | |||||||||||||||||||
| 103 | 18 | $self->{page_id} = shift; | |||||||||||||||||||
| 104 | 18 | $self->{template} = shift; | |||||||||||||||||||
| 105 | 18 | $self->{name} = shift; | |||||||||||||||||||
| 106 | 18 | my %args = @_; | |||||||||||||||||||
| 107 | 18 | $self->{config} = \%args; | |||||||||||||||||||
| 108 | 18 | bless $self, $class; | |||||||||||||||||||
| 109 | 18 | return $self; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =head2 can | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | We need to autoload methods so that the template writer can use variables without needing to know | ||||||||||||||||||||
| 115 | where the variables will be used. Thus 'can' must return a true value in all cases to avoid breaking | ||||||||||||||||||||
| 116 | L<HTML::Template::Plugin::Dot>. Also 'can' is supposed to either return undef or a CODE ref. This seems the cleanest | ||||||||||||||||||||
| 117 | way of meeting all requirements. | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 36 | ▶ | sub can { | ||||||||||||||||||
|
Subroutine
can: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||
| 122 | 36 | my $self = shift; | |||||||||||||||||||
| 123 | 36 | my $param = shift; | |||||||||||||||||||
| 124 | 18 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.4
| |||||||||||||||||||||
| 125 | 18 | my $self = shift; | |||||||||||||||||||
| 126 | 18 | my $prefix = $self->{cgiapp}->pagelookup_prefix(%{$self->{config}}); | |||||||||||||||||||
| 127 | 18 | my $page_id = $self->{page_id}; | |||||||||||||||||||
| 128 | 18 | my $dbh = $self->{cgiapp}->dbh; | |||||||||||||||||||
| 129 | 18 | my @sql = ( | |||||||||||||||||||
| 130 | "SELECT v.value FROM ${prefix}values v, ${prefix}pages p WHERE v.internalId = p.internalId AND v.param = '$param' AND v.lang = p.lang AND p.pageId = '$page_id'", | ||||||||||||||||||||
| 131 | "SELECT v.value FROM ${prefix}values v, ${prefix}pages p WHERE v.internalId IS NULL AND v.param = '$param' AND v.lang = p.lang AND p.pageId = '$page_id'"); | ||||||||||||||||||||
| 132 | 18 | foreach my $s (@sql) { | |||||||||||||||||||
| 133 | 34 | ▶ | my $sth = $dbh->prepare($s) || croak $dbh->errstr; | ||||||||||||||||||
|
Condition33%1/3
$dbh->prepare($s) || $dbh->croak->errstr
MC/DC0%0/2
$dbh->prepare($s) || $dbh->croak->errstr
$dbh->prepare($s)
$dbh->croak->errstr
Truth table33%1/3
A || B
A= $dbh->prepare($s)B= $dbh->croak->errstr
| |||||||||||||||||||||
| 134 | 34 | ▶ | $sth->execute || croak $dbh->errstr; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | 34 | my $hash_ref = $sth->fetchrow_hashref; | |||||||||||||||||||
| 136 | 34 | ▶ | if ($hash_ref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | 18 | $sth->finish; | |||||||||||||||||||
| 138 | 18 | return $hash_ref->{value}; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | 16 | ▶ | croak $sth->errstr if $sth->err; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 16 | $sth->finish; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | 0 | return undef; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 36 | }; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =head2 AUTOLOAD | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | We need to autoload methods so that the template writer can use variables without needing to know | ||||||||||||||||||||
| 151 | where the variables will be used. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 18 | ▶ | sub AUTOLOAD { | ||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 156 | 18 | my $self = shift; | |||||||||||||||||||
| 157 | 18 | my @method = split /::/, $AUTOLOAD; | |||||||||||||||||||
| 158 | 18 | my $param = pop @method; | |||||||||||||||||||
| 159 | 18 | my $c = $self->can($param); | |||||||||||||||||||
| 160 | 18 | ▶ | return &$c($self) if $c; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 0 | return undef; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 DESTROY | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | We have to define DESTROY, because an autoloaded version would be bad. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =head1 AUTHOR | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | Nicholas Bamber, C<< <nicholas at periapt.co.uk> >> | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =head1 BUGS | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | Please report any bugs or feature requests to C<bug-cgi-application-plugin-pagelookup at rt.cpan.org>, or through | ||||||||||||||||||||
| 180 | 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 | ||||||||||||||||||||
| 181 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | =head2 AUTOLOAD | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | AUTOLOAD is quite a fraught subject. There is probably no perfect solution. See http://www.perlmonks.org/?node_id=342804 for a sample of the issues. | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =head1 SUPPORT | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | perldoc CGI::Application::Plugin::PageLookup::Value | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | You can also look for information at: | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =over 4 | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =item * RT: CPAN's request tracker | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Application-Plugin-PageLookup> | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | L<http://annocpan.org/dist/CGI-Application-Plugin-PageLookup> | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | =item * CPAN Ratings | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | L<http://cpanratings.perl.org/d/CGI-Application-Plugin-PageLookup> | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | =item * Search CPAN | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | L<http://search.cpan.org/dist/CGI-Application-Plugin-PageLookup/> | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =back | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | Copyright 2009 Nicholas Bamber. | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 225 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||
| 226 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | 3 | 1; # End of CGI::Application::Plugin::PageLookup::Value | |||||||||||||||||||