File Coverage

blib/lib/CGI/Application/Plugin/PageLookup/Value.pm
Criterion Covered Total %
statement 39 42 92.8
branch 5 8 62.5
condition 1 3 33.3
subroutine 6 7 85.7
pod 2 2 100.0
total 53 62 85.4


line stmt bran cond sub pod time code
1             package CGI::Application::Plugin::PageLookup::Value;
2              
3 2     2   1247 use warnings;
  2         5  
  2         57  
4 2     2   9 use strict;
  2         3  
  2         992  
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             our $VERSION = '1.8';
17             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. For loops see L.
25              
26             =head1 SYNOPSIS
27              
28             In the template you can do things like , and .
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.
96              
97             =cut
98              
99             sub new {
100 18     18 1 26 my $class = shift;
101 18         37 my $self = {};
102 18         44 $self->{cgiapp} = shift;
103 18         31 $self->{page_id} = shift;
104 18         28 $self->{template} = shift;
105 18         40 $self->{name} = shift;
106 18         40 my %args = @_;
107 18         36 $self->{config} = \%args;
108 18         88 bless $self, $class;
109 18         66 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. 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             sub can {
122 36     36 1 6338 my $self = shift;
123 36         57 my $param = shift;
124             return sub {
125 18     18   26 my $self = shift;
126 18         32 my $prefix = $self->{cgiapp}->pagelookup_prefix(%{$self->{config}});
  18         93  
127 18         37 my $page_id = $self->{page_id};
128 18         81 my $dbh = $self->{cgiapp}->dbh;
129 18         1138 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         36 foreach my $s (@sql) {
133 34   33     165 my $sth = $dbh->prepare($s) || croak $dbh->errstr;
134 34 50       7057 $sth->execute || croak $dbh->errstr;
135 34         796 my $hash_ref = $sth->fetchrow_hashref;
136 34 100       151 if ($hash_ref) {
137 18         66 $sth->finish;
138 18         464 return $hash_ref->{value};
139             }
140 16 50       84 croak $sth->errstr if $sth->err;
141 16         238 $sth->finish;
142             }
143 0         0 return undef;
144              
145 36         209 };
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             sub AUTOLOAD {
156 18     18   236 my $self = shift;
157 18         87 my @method = split /::/, $AUTOLOAD;
158 18         32 my $param = pop @method;
159 18         35 my $c = $self->can($param);
160 18 50       64 return &$c($self) if $c;
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     0     sub DESTROY {
171             }
172              
173             =head1 AUTHOR
174              
175             Nicholas Bamber, C<< >>
176              
177             =head1 BUGS
178              
179             Please report any bugs or feature requests to C, or through
180             the web interface at L. 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
201              
202             =item * AnnoCPAN: Annotated CPAN documentation
203              
204             L
205              
206             =item * CPAN Ratings
207              
208             L
209              
210             =item * Search CPAN
211              
212             L
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             1; # End of CGI::Application::Plugin::PageLookup::Value