File Coverage

blib/lib/XAO/DO/Web/Condition.pm
Criterion Covered Total %
statement 91 109 83.4
branch 56 72 77.7
condition 10 19 52.6
subroutine 6 6 100.0
pod 1 2 50.0
total 164 208 78.8


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             XAO::DO::Web::Condition - allows to check various conditions
4              
5             =head1 SYNOPSIS
6              
7             Only useful in XAO::Web context.
8              
9             =head1 DESCRIPTION
10              
11             Example usage would be:
12              
13             <%Condition NAME1.value="<%CgiParam param=test%>"
14             NAME1.path="/bits/test-is-set"
15             NAME2.cgiparam="foo"
16             NAME2.path="/bits/foo-is-set"
17             NAME3.siteconfig="product_list"
18             NAME3.template="product_list exists in siteconfig"
19             default.objname="Error"
20             default.template="No required parameter set"
21             %>
22              
23             Which means to execute /bits/test-is-set if CGI has `test'
24             parameter, otherwise execute /bits/foo-is-set if `foo' parameter
25             is set and finally, if there is no foo and no test - execute
26             /bits/nothing-set. For `foo' shortcut is used, because most of the
27             time you will check for CGI parameters anyway.
28              
29             Default object to be substituted is Page. Another object may be
30             specified with objname. All arguments after NAMEx. are just passed
31             into object without any processing.
32              
33             NAME1 and NAME2 may be anything, they sorted alphabetycally before
34             checking. So, usually if there is only one check and default - then
35             something meaningful is used for the name. For multiple choices just
36             numbers are better for names.
37              
38             Condition checked in perl style - '0' and empty string is false.
39              
40             Hides itself from object it executes - makes parent and parent_args
41             pointing to Condition's parent.
42              
43             Supports the following conditions:
44              
45             =over
46              
47             =item value
48              
49             Just constant value, usually substituted in template itself.
50              
51             =item cgiparam
52              
53             Parameter in CGI.
54              
55             =item arg
56              
57             Parent object argument.
58              
59             =item siteconf
60              
61             Site configuration parameter.
62              
63             =item cookie
64              
65             Cookie value (including cookie values set earlier in the same render).
66              
67             =item secure
68              
69             True if the the current page is being transferred over a secure
70             connection (the url prefix is https://). Value is not used.
71              
72             =back
73              
74             All values are treated as booleans only, no comparision is implemented
75             yet.
76              
77             =cut
78              
79             ###############################################################################
80             package XAO::DO::Web::Condition;
81 4     4   2881 use strict;
  4         10  
  4         113  
82 4     4   17 use XAO::Utils;
  4         7  
  4         277  
83 4     4   21 use XAO::Objects;
  4         7  
  4         111  
84 4     4   19 use base XAO::Objects->load(objname => 'Web::Page');
  4         5  
  4         22  
85              
86             our $VERSION='2.009';
87              
88             ###############################################################################
89              
90             sub check_target ($$$) {
91 207     207 0 376 my ($pvalue,$target,$targop)=@_;
92              
93 207 100 66     543 if(defined $target && defined $pvalue) {
94 23 100       103 if($targop eq '=') { return ($pvalue eq $target); }
  17 100       45  
    100          
    50          
95 2         10 elsif($targop eq '!') { return ($pvalue ne $target); }
96 2         8 elsif($targop eq '<') { return ($pvalue < $target); }
97 2         6 elsif($targop eq '>') { return ($pvalue > $target); }
98             }
99             else {
100 184         398 return $pvalue;
101             }
102             }
103              
104             ###############################################################################
105              
106             sub display ($;%)
107 210     210 1 316 { my $self=shift;
108 210 50       266 my %args=%{get_args(\@_) || {}};
  210         370  
109 210         2270 my $config=$self->siteconfig;
110              
111             ##
112             # First going through the list of conditions and checking them.
113             #
114 210         289 my $name;
115 210         896 foreach my $a (sort keys %args)
116 273 100       1450 { next unless $a =~ /^(\w+)\.(number|value|arg|cgiparam|length|siteconf|siteconfig|cookie|secure|clipboard)$/;
117 215 50 100     970 if($2 eq 'cgiparam')
    100          
    100          
    100          
    100          
    50          
    50          
    100          
    100          
118 0         0 { my $param=$args{$a};
119 0         0 my $cname=$1;
120 0         0 my ($target,$targop);
121 0 0       0 if($param =~ /^\s*(.*?)\s*(=|>|<|\!)\s*(.*?)\s*$/)
122 0         0 { $param=$1;
123 0         0 $targop=$2;
124 0         0 $target=$3;
125             }
126 0         0 my $pvalue=$config->cgi->param($param);
127 0 0       0 if(check_target($pvalue,$target,$targop))
128 0         0 { $name=$cname;
129 0         0 last;
130             }
131             }
132             elsif($2 eq 'length')
133 3         5 { my $param=$args{$a};
134 3 100 66     13 if(defined($param) && length($param))
135 2         4 { $name=$1;
136 2         5 last;
137             }
138             }
139             elsif($2 eq 'arg')
140 194         303 { my $param=$args{$a};
141 194         324 my $cname=$1;
142 194         253 my ($target,$targop);
143 194 100       782 if($param =~ /^\s*(.*?)\s*(=|>|<|\!)\s*(.*?)\s*$/)
144 16         35 { $param=$1;
145 16         56 $targop=$2;
146 16         24 $target=$3;
147             }
148 194 50       404 if($self->{'parent'})
149 194         339 { my $pvalue=$self->{'parent'}->{'args'}->{$param};
150 194         213 my $matches;
151 194 100       362 if(check_target($pvalue,$target,$targop))
152 175         289 { $name=$cname;
153 175         336 last;
154             }
155             }
156             }
157             elsif($2 eq 'siteconf' || $2 eq 'siteconfig')
158 5         13 { my $param=$args{$a};
159 5         9 my $cname=$1;
160 5         8 my ($target,$targop);
161 5 100       40 if($param =~ /^\s*(.*?)\s*(=|>|<|\!)\s*(.*?)\s*$/)
162 2         5 { $param=$1;
163 2         7 $targop=$2;
164 2         3 $target=$3;
165             }
166 5         97 my $pvalue=$config->get($param);
167 5 100       179 if(check_target($pvalue,$target,$targop))
168 4         7 { $name=$cname;
169 4         9 last;
170             }
171             }
172             elsif($2 eq 'cookie')
173 3         6 { my $param=$args{$a};
174 3         7 my $cname=$1;
175 3         6 my ($target,$targop);
176 3 100       20 if($param =~ /^\s*(.*?)\s*(=|>|<|\!)\s*(.*?)\s*$/)
177 2         4 { $param=$1;
178 2         5 $targop=$2;
179 2         4 $target=$3;
180             }
181 3         65 my $pvalue=$config->get_cookie($param);
182 3 100       9 if(check_target($pvalue,$target,$targop))
183 2         4 { $name=$cname;
184 2         4 last;
185             }
186             }
187             elsif($2 eq 'number')
188 0 0 0     0 { if(($args{$a} || 0)+0)
189 0         0 { $name=$1;
190 0         0 last;
191             }
192             }
193             elsif($2 eq 'secure')
194 0 0       0 { if($self->is_secure)
195 0         0 { $name=$1;
196 0         0 last;
197             }
198             }
199             elsif($2 eq 'clipboard')
200 5         10 { my $param=$args{$a};
201 5         10 my $cname=$1;
202 5         7 my ($target,$targop);
203 5 100       29 if($param =~ /^\s*(.*?)\s*(=|>|<|\!)\s*(.*?)\s*$/)
204 3         6 { $param=$1;
205 3         5 $targop=$2;
206 3         6 $target=$3;
207             }
208 5         15 my $pvalue=$self->clipboard->get($param);
209 5 100       166 if(check_target($pvalue,$target,$targop))
210 3         8 { $name=$cname;
211 3         8 last;
212             }
213             }
214             elsif($args{$a}) # value
215 2         5 { $name=$1;
216 2         5 last;
217             }
218             }
219 210 100       437 $name="default" unless defined $name;
220              
221             # Building object arguments now.
222             #
223 210         266 my %objargs;
224 210         470 foreach my $a (keys %args) {
225 745 50 33     6539 if($self->{'parent'} && $self->{'parent'}->{'args'}
    100 33        
    100          
226             && $a =~ /^$name\.pass\.(.*)$/) {
227 0         0 $objargs{$1}=$self->{'parent'}->{'args'}->{$1};
228             }
229             elsif($a eq "$name.pass") {
230             # See below
231             }
232             elsif($a =~ /^$name\.(\w.*)$/) {
233 398         1308 $objargs{$1}=$args{$a};
234             }
235             }
236 210 50       439 return unless %objargs;
237              
238             # Now getting the object
239             #
240 210   50     836 my $obj=$self->object(objname => $objargs{'objname'} || "Page");
241 210         11715 delete $objargs{'objname'};
242              
243             # If we were asked to pass complete set of arguments then merging.
244             #
245 210 100       511 if($args{"$name.pass"}) {
246 122         414 $obj->display($self->pass_args($args{"$name.pass"},\%objargs));
247             }
248             else {
249 88         254 $obj->display(\%objargs);
250             }
251             }
252              
253             ###############################################################################
254             1;
255             __END__