lib/Strehler/Helpers.pm

filter: Statementstmt 74.8% 92 / 123 Branchbran 59.0% 26 / 44 Conditioncond 32.0% 8 / 25 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 88.2% 15 / 17 Podpod 90.9% 10 / 11 totaltotal 64.9% 152 / 234 CC 38 SCAR 28.8
CC
38
Cov
66%
top_bars
42.0
list_parameters_init
38.3
check_statics
12.0
CRAP 17.8
« lib/Strehler/Admin.pm ↑ Summary lib/Strehler/Dancer2/Plugin/Admin.pm »
1 package Strehler::Helpers;
2 12 $Strehler::Helpers::VERSION = '1.6.8';
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Dancer2 0.166001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Strehler;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Text::Unidecode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #Barely copied from http://stackoverflow.com/questions/4009281/how-can-i-generate-url-slugs-in-perl
11 sub slugify
12 22 ▶{
Subroutine slugify: called CC=1 SCAR=0.0
Pod documented
13 22 my $input = shift;
14 22 $input = NFKD($input); # Normalize the Unicode string
15 22 $input = unidecode($input); # make accented letters not accented
16 22 $input =~ tr/\000-\177//cd; # Strip non-ASCII characters (>127)
17 22 $input =~ s/[^\w\s-]//g; # Remove all characters that are not word characters (includes _), spaces, or hyphens
18 22 $input =~ s/^\s+|\s+$//g; # Trim whitespace from both ends
19 22 $input = lc($input);
20 22 $input =~ s/[-\s]+/-/g; # Replace all occurrences of spaces and hyphens with a single hyphen
21 22 return $input;
22 }
23  
24 sub get_categorized_entities
25 37 ▶{
Subroutine get_categorized_entities: called CC=3 SCAR=11.0
Pod documented
26 37 my @entities = entities_list();
27 37 my @out;
28 37 for(@entities)
29 {
30 259 my $cl = class_from_entity($_);
31 259 ▶ push @out, $_ if $cl->categorized();
Branch: 1 branch
BranchTrueFalseTotal
if $cl->categorized 148 111 259
32 }
33 37 return @out;
34 }
35 sub standard_entities
36 37 ▶{
Subroutine standard_entities: called CC=1 SCAR=0.0
Pod documented
37 37 return ('article', 'image', 'user', 'log');
38 }
39 sub entities_list
40 37 ▶{
Subroutine entities_list: called CC=3 SCAR=11.0
Pod documented
41 37 my @entities = standard_entities();
42 37 my @configured_entities = keys %{config->{'Strehler'}->{'extra_menu'}};
43 37 foreach my $e (@configured_entities)
44 {
45 111 ▶ push @entities, $e if(! grep {$_ eq $e} @entities);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $e;} @entities 111 0 111
46 }
47 37 return @entities;
48 }
49 sub top_bars
50 0 ▶{
Subroutine top_bars: not called CC=6 SCAR=37.4
Pod documented
51 0 my @entities = entities_list();
52 0 my @editor_menu;
53 my @admin_menu;
54 0 foreach my $e (@entities)
55 {
56 0 my $c = class_from_entity($e);
57 0 ▶ if($c && $c->visible())
Branch: 1 branch
BranchTrueFalseTotal
if ($c and $c->visible) 0 0 0
Condition0%0/3
$c and $c->visible
!ll&&!rl&&r
000
MC/DC0%0/2
$c and $c->visible
$c $c->visible
Truth table0%0/3
A and B
A= $cB= $c->visible
ABresult
0X0
100
111
58 {
59 0 ▶ if($c->allowed_role() && $c->allowed_role() eq "admin")
Branch: 1 branch
BranchTrueFalseTotal
if ($c->allowed_role and $c->allowed_role eq 'admin') { } 0 0 0
Condition0%0/3
$c->allowed_role and $c->allowed_role eq "admin"
!ll&&!rl&&r
000
MC/DC0%0/2
$c->allowed_role and $c->allowed_role eq "admin"
$c->allowed_role $c->allowed_role eq "admin"
Truth table0%0/3
A and B
A= $c->allowed_roleB= $c->allowed_role eq "admin"
ABresult
0X0
100
111
60 {
61 0 push @admin_menu, { name => $e, label => $c->label() };
62 }
63 else
64 {
65 0 push @editor_menu, { name => $e, label => $c->label() };
66 }
67 }
68 }
69 0 return \@editor_menu, \@admin_menu;
70 }
71  
72  
73 sub class_from_entity
74 326 ▶{
Subroutine class_from_entity: called CC=6 SCAR=17.9
Pod documented
75 326 my $entity = shift;
76 326 my $class;
77 326 ▶ if($entity eq 'article')
Branch: 4 branches
BranchTrueFalseTotal
if ($entity eq 'article') { } 79 247 326
elsif ($entity eq 'image') { } 48 199 247
elsif ($entity eq 'user') { } 40 159 199
elsif ($entity eq 'log') { } 37 122 159
78 {
79 79 $class = "Strehler::Element::Article";
80 }
81 elsif($entity eq 'image')
82 {
83 48 $class = "Strehler::Element::Image";
84 }
85 elsif($entity eq 'user')
86 {
87 40 $class = "Strehler::Element::User";
88 }
89 elsif($entity eq 'log')
90 {
91 37 $class = "Strehler::Element::Log";
92 }
93 else
94 {
95 122 $class = config->{'Strehler'}->{'extra_menu'}->{$entity}->{class};
96 }
97 326 ▶ if($class)
Branch: 1 branch
BranchTrueFalseTotal
if ($class) 326 0 326
98 {
99 326 load $class;
100 326 return $class;
101 }
102 }
103  
104 sub class_from_plural
105 6 ▶{
Subroutine class_from_plural: called CC=5 SCAR=16.1
Pod documented
106 6 my $plural = shift;
107 6 foreach my $class ('Strehler::Element::Article', 'Strehler::Element::Image', 'Strehler::Element::User', 'Strehler::Element::Log')
108 {
109 9 load $class;
110 9 ▶ if($class->plural() eq $plural)
Branch: 1 branch
BranchTrueFalseTotal
if ($class->plural eq $plural) 5 4 9
111 {
112 5 return $class;
113 }
114 }
115 1 foreach my $entity (keys %{config->{'Strehler'}->{'extra_menu'}})
116 {
117 2 my $entity_class = config->{'Strehler'}->{'extra_menu'}->{$entity}->{'class'};
118 2 load $entity_class;
119 2 ▶ if($entity_class->plural() eq $plural)
Branch: 1 branch
BranchTrueFalseTotal
if ($entity_class->plural eq $plural) 1 1 2
120 {
121 1 return $entity_class;
122 }
123 }
124 0 return undef;
125 }
126  
127 sub public_directory
128 1 ▶{
Subroutine public_directory: called CC=2 SCAR=8.1
Pod documented
129 1 ▶ my $public_directory = app->config->{public} || path( app->config_location, 'public' );
Condition33%1/3
app()->config->{'public'} || path(app()->config_location, 'public')
l!l&&r!l&&!r
100
MC/DC0%0/2
app()->config->{'public'} || path(app()->config_location, 'public')
app()->config->{'public'} path(app()->config_location, 'public')
Truth table33%1/3
A || B
A= app()->config->{'public'}B= path(app()->config_location, 'public')
ABresult
1X1
011
000
130 1 return $public_directory;
131 }
132  
133 sub check_statics
134 0 ▶{
Subroutine check_statics: not called CC=3 SCAR=24.8
Pod documented
135 0 my $public_directory = public_directory();
136 0 my $version_file;
137 0 ▶ open($version_file, '<', "$public_directory/strehler/VERSION") or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open $version_file, "<", "$public_directory/strehler/VERSION" or return 0 0 0 0
138 0 my $data = <$version_file>;
139 0 chomp $data;
140 0 ▶ return $data eq $Strehler::STATICS_VERSION ? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$data eq $Strehler::STATICS_VERSION ? : 0 0 0
141 }
142  
143 sub list_parameters_names
144 24 ▶{
Subroutine list_parameters_names: called CC=3 SCAR=11.0
Pod undocumented
145 24 my $type = shift;
146 24 ▶ if($type eq 'session')
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'session') { } 18 6 24
elsif ($type eq 'extra') { } 6 0 6
147 {
148 18 return ('page', 'category-input', 'order', 'order-by', 'search', 'language');
149 }
150 elsif($type eq 'extra')
151 {
152 6 return ('strehl-from', 'reset-filter', 'reset-search', 'strehl-catname');
153 }
154 }
155  
156  
157 sub list_parameters_init
158 6 ▶{
Subroutine list_parameters_init: called CC=15 SCAR=36.5
Pod documented
159 6 my $entity = shift;
160 6 my $session = shift;
161 6 my $params = shift;
162  
163 6 my %output;
164  
165 # Params init
166 6 foreach my $p (list_parameters_names('session'))
167 {
168 36 ▶ $output{$p} = exists $params->{$p} ? $params->{$p} : $session->{$p};
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{$p} ? : 4 32 36
169 }
170 #Used by template but not managed by session
171 6 $output{'backlink'} = $params->{'strehl-from'};
172  
173 #reset management
174 6 ▶ if(exists $params->{'reset-filter'})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"reset-filter"}) 0 6 6
175 {
176 0 $output{'page'} = undef;
177 0 $output{'category'} = undef;
178 0 $output{'language'} = undef;
179 0 return %output;
180 }
181 6 ▶ if(exists $params->{'reset-search'})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"reset-search"}) 0 6 6
182 {
183 0 $output{'page'} = undef;
184 0 $output{'search'} = undef;
185 0 return %output;
186 }
187  
188 #strehl-catname converted to category id (override category-input)
189 6 ▶ if(exists $params->{'strehl-catname'})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{'strehl-catname'}) { } 0 6 6
190 {
191 0 my $wanted_cat = Strehler::Meta::Category->explode_name($params->{'strehl-catname'});
192 0 ▶ if(! $wanted_cat->exists())
Branch: 1 branch
BranchTrueFalseTotal
unless ($wanted_cat->exists) 0 0 0
193 {
194 0 $output{'error'} = "1";
195 0 return %output;
196 }
197 0 $output{'category'} = $wanted_cat->get_attr('id');
198 }
199 else
200 {
201 6 ▶ if($output{'category-input'} && $output{'category-input'} =~ m/anc:([0-9]+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($output{'category-input'} and $output{'category-input'} =~ /anc:([0-9]+)/) { } 0 6 6
Condition33%1/3
$output{"category-input"} and $output{"category-input"} =~ /anc:([0-9]+)/
!ll&&!rl&&r
600
MC/DC0%0/2
$output{"category-input"} and $output{"category-input"} =~ /anc:([0-9]+)/
$output{"category-input"} $output{"category-input"} =~ /anc:([0-9]+)/
Truth table33%1/3
A and B
A= $output{"category-input"}B= $output{"category-input"} =~ /anc:([0-9]+)/
ABresult
0X0
100
111
202 {
203 0 $output{'category'} = undef;
204 0 $output{'ancestor'} = $1;
205 }
206 else
207 {
208 6 $output{'category'} = $output{'category-input'};
209 6 $output{'ancestor'} = undef;
210 }
211 }
212
213 #Some default values
214 6 ▶ $output{'page'} ||= 1;
Condition50%1/2
$output{"page"} ||= 1
l!l
06
MC/DC0%0/1
$output{"page"} ||= 1
$output{"page"}
Truth table50%1/2
A
A= $output{"page"}
Aresult
11
00
215 6 ▶ $output{'order'} ||= 'desc';
Condition100%2/2
$output{"order"} ||= "desc"
l!l
24
MC/DC100%1/1
$output{"order"} ||= "desc"
$output{"order"}
Truth table100%2/2
A
A= $output{"order"}
Aresult
11
00
216 6 ▶ if($output{'category'} || $output{'ancestor'} || $output{'language'} || $output{'search'})
Branch: 1 branch
BranchTrueFalseTotal
if ($output{'category'} or $output{'ancestor'} or $output{'language'} or $output{'search'}) { } 0 6 6
Condition33%3/9
$output{"category"} or $output{"ancestor"} or $output{"language"} or $output{"search"}
l!l&&r!l&&!r
006
$output{"category"} or $output{"ancestor"} or $output{"language"}
l!l&&r!l&&!r
006
$output{"category"} or $output{"ancestor"}
l!l&&r!l&&!r
006
MC/DC0%0/4
$output{"category"} or $output{"ancestor"} or $output{"language"} or $output{"search"}
$output{"category"} $output{"ancestor"} $output{"language"} $output{"search"}
Truth table20%1/5
A or B or C or D
A= $output{"category"}B= $output{"ancestor"}C= $output{"language"}D= $output{"search"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
217 {
218 0 $output{'filtered'} = 1;
219 }
220 else
221 {
222 6 $output{'filtered'} = 0;
223 }
224  
225 #Some parameters have different names in different contexts.
226 #I'll do some duplication here, dirty but easy.
227 6 $output{'order_by'} = $output{'order-by'};
228 6 $output{'category_id'} = $output{'category'};
229 6 $output{'cat_filter'} = $output{'category'};
230 6 return %output;
231 }
232  
233 =encoding utf8
234  
235 =head1 NAME
236  
237 Strehler::Helpers - Helpers
238  
239 =head1 DESCRIPTION
240  
241 Just few methods used in Strehler that could come useful throughtout in the application and also while developing on top of Strehler.
242  
243 =head1 FUNCTIONS
244  
245 =over 4
246  
247 =item slugify
248  
249 arguments: $string
250  
251 return value: $slugified
252  
253 This method take a string and return the slugified version of it. Used to retrieve articles.
254  
255 =item class_from_entity
256  
257 arguments: entity
258  
259 return value $class
260  
261 Return the $class related to the given entity
262  
263 =item get_categorized_entities
264  
265 return value @entities
266  
267 Return all the entities using categories
268  
269 =item standard_entities
270  
271 return value @entities
272  
273 Return standard entities defined in Strehler core.
274  
275 =item entities_list
276  
277 return value @entities
278  
279 Return all the entities managed by the system
280  
281 =item top_bars
282  
283 return value \@editor_menu, \@admin_menu
284  
285 Used to build the structure of the top bar, considering access level
286  
287 =item class_from_plural
288  
289 argument: $class_plural
290  
291 return value $class
292  
293 Return a class from its plural name.
294  
295 =item public_directory
296  
297 return value $dir
298  
299 Return public directory as configured by Dancer2, using Dancer2 explicit configuration, but with a fallback calculated on configuration directory.
300  
301 =item check_statics
302  
303 return value $checked
304  
305 Return true is static version configured in VERSION file under public directory is the same configured in Strehler module.
306  
307 =item list_parameters_init
308  
309 arguments: $entity, $session, $params
310  
311 return value @parametes
312  
313 Take entity, session values and parameters from request and calculate all the parameters that will be used for list page.
314  
315 Parameters considered are listed by the function list_parameters_names
316  
317 =back
318  
319 =cut
320  
321 12 1;
322  
323  
« lib/Strehler/Admin.pm ↑ Summary lib/Strehler/Dancer2/Plugin/Admin.pm »