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   4196 use strict;
  4         10  
  4         171  
82 4     4   26 use XAO::Utils;
  4         8  
  4         376  
83 4     4   25 use XAO::Objects;
  4         8  
  4         200  
84 4     4   32 use base XAO::Objects->load(objname => 'Web::Page');
  4         6  
  4         21  
85              
86             our $VERSION='2.009';
87              
88             ###############################################################################
89              
90             sub check_target ($$$) {
91 207     207 0 617 my ($pvalue,$target,$targop)=@_;
92              
93 207 100 66     631 if(defined $target && defined $pvalue) {
94 23 100       67 if($targop eq '=') { return ($pvalue eq $target); }
  17 100       98  
    100          
    50          
95 2         10 elsif($targop eq '!') { return ($pvalue ne $target); }
96 2         11 elsif($targop eq '<') { return ($pvalue < $target); }
97 2         9 elsif($targop eq '>') { return ($pvalue > $target); }
98             }
99             else {
100 184         625 return $pvalue;
101             }
102             }
103              
104             ###############################################################################
105              
106             sub display ($;%)
107 210     210 1 379 { my $self=shift;
108 210 50       324 my %args=%{get_args(\@_) || {}};
  210         536  
109 210         3031 my $config=$self->siteconfig;
110              
111             ##
112             # First going through the list of conditions and checking them.
113             #
114 210         393 my $name;
115 210         1097 foreach my $a (sort keys %args)
116 273 100       1836 { next unless $a =~ /^(\w+)\.(number|value|arg|cgiparam|length|siteconf|siteconfig|cookie|secure|clipboard)$/;
117 215 50 100     1319 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         9 { my $param=$args{$a};
134 3 100 66     19 if(defined($param) && length($param))
135 2         6 { $name=$1;
136 2         6 last;
137             }
138             }
139             elsif($2 eq 'arg')
140 194         472 { my $param=$args{$a};
141 194         487 my $cname=$1;
142 194         312 my ($target,$targop);
143 194 100       1198 if($param =~ /^\s*(.*?)\s*(=|>|<|\!)\s*(.*?)\s*$/)
144 16         41 { $param=$1;
145 16         34 $targop=$2;
146 16         60 $target=$3;
147             }
148 194 50       539 if($self->{'parent'})
149 194         450 { my $pvalue=$self->{'parent'}->{'args'}->{$param};
150 194         269 my $matches;
151 194 100       641 if(check_target($pvalue,$target,$targop))
152 175         327 { $name=$cname;
153 175         417 last;
154             }
155             }
156             }
157             elsif($2 eq 'siteconf' || $2 eq 'siteconfig')
158 5         12 { my $param=$args{$a};
159 5         14 my $cname=$1;
160 5         10 my ($target,$targop);
161 5 100       75 if($param =~ /^\s*(.*?)\s*(=|>|<|\!)\s*(.*?)\s*$/)
162 2         5 { $param=$1;
163 2         6 $targop=$2;
164 2         7 $target=$3;
165             }
166 5         192 my $pvalue=$config->get($param);
167 5 100       277 if(check_target($pvalue,$target,$targop))
168 4         8 { $name=$cname;
169 4         10 last;
170             }
171             }
172             elsif($2 eq 'cookie')
173 3         9 { my $param=$args{$a};
174 3         10 my $cname=$1;
175 3         6 my ($target,$targop);
176 3 100       28 if($param =~ /^\s*(.*?)\s*(=|>|<|\!)\s*(.*?)\s*$/)
177 2         5 { $param=$1;
178 2         5 $targop=$2;
179 2         7 $target=$3;
180             }
181 3         151 my $pvalue=$config->get_cookie($param);
182 3 100       10 if(check_target($pvalue,$target,$targop))
183 2         5 { $name=$cname;
184 2         6 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         11 { my $param=$args{$a};
201 5         11 my $cname=$1;
202 5         13 my ($target,$targop);
203 5 100       39 if($param =~ /^\s*(.*?)\s*(=|>|<|\!)\s*(.*?)\s*$/)
204 3         9 { $param=$1;
205 3         6 $targop=$2;
206 3         8 $target=$3;
207             }
208 5         23 my $pvalue=$self->clipboard->get($param);
209 5 100       267 if(check_target($pvalue,$target,$targop))
210 3         7 { $name=$cname;
211 3         7 last;
212             }
213             }
214             elsif($args{$a}) # value
215 2         8 { $name=$1;
216 2         6 last;
217             }
218             }
219 210 100       585 $name="default" unless defined $name;
220              
221             # Building object arguments now.
222             #
223 210         323 my %objargs;
224 210         505 foreach my $a (keys %args) {
225 745 50 33     12377 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         1518 $objargs{$1}=$args{$a};
234             }
235             }
236 210 50       599 return unless %objargs;
237              
238             # Now getting the object
239             #
240 210   50     1090 my $obj=$self->object(objname => $objargs{'objname'} || "Page");
241 210         15898 delete $objargs{'objname'};
242              
243             # If we were asked to pass complete set of arguments then merging.
244             #
245 210 100       621 if($args{"$name.pass"}) {
246 122         489 $obj->display($self->pass_args($args{"$name.pass"},\%objargs));
247             }
248             else {
249 88         358 $obj->display(\%objargs);
250             }
251             }
252              
253             ###############################################################################
254             1;
255             __END__