lib/Strehler/Meta/Category.pm

filter: Statementstmt 78.2% 169 / 216 Branchbran 62.1% 46 / 74 Conditioncond 50.0% 8 / 16 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 92.0% 23 / 25 Podpod 45.0% 9 / 20 totaltotal 71.3% 256 / 359 CC 51 SCAR 18.9
CC
51
Cov
73%
get_form_data
20.0
get_list
16.0
save_form
8.6
CRAP 6.6
« blib/lib/Strehler/FormFu/Element/EntityTypeSelect.pm ↑ Summary lib/Strehler/FormFu/Validator/CategoryUnique.pm »
1 package Strehler::Meta::Category;
2 12 $Strehler::Meta::Category::VERSION = '1.6.8';
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Dancer2;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Dancer2::Plugin::DBIC;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Strehler::Helpers;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 has row => (
10 is => 'ro',
11 );
12  
13 157 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=6 SCAR=18.0
Pod undocumented
14 157 my ( $class, @args ) = @_;
15 157 my $category = undef;
16  
17 # Create category using category table ID
18 157 ▶ if($#args == 0)
Branch: 2 branches
BranchTrueFalseTotal
if ($#args == 0) { } 94 63 157
elsif ($#args == 1) { } 3 60 63
19 {
20 94 my $id = shift @args;
21 94 $category = $class->get_schema()->resultset('Category')->find($id);
22 }
23 # Create category giving a row retrived using DBIx::Class
24 elsif($#args == 1)
25 {
26 3 ▶ if($args[0] eq 'row')
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] eq "row") 3 0 3
27 {
28 3 $category = $args[1];
29 }
30 }
31 else
32 {
33 # Create category using name as $parent/$category (used by explode_name)
34 60 my %hash_args = @args;
35 60 ▶ if($hash_args{'parent'})
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_args{'parent'}) { } 14 46 60
36 {
37 14 my $main = $class->get_schema()->resultset('Category')->find($hash_args{'parent'});
38 14 ▶ if($main)
Branch: 1 branch
BranchTrueFalseTotal
if ($main) { } 14 0 14
39 {
40 14 $category = $main->categories->find({ category => $hash_args{'category'}});
41 }
42 else
43 {
44 0 $category = undef;
45 }
46 }
47 else
48 {
49 46 $category = $class->get_schema()->resultset('Category')->find({ category => $hash_args{'category'}, parent => undef });
50 }
51 }
52 157 return { row => $category };
53 };
54  
55 sub get_schema
56 189 ▶{
Subroutine get_schema: called CC=2 SCAR=8.1
Pod undocumented
57 189 ▶ if(config->{'Strehler'}->{'schema'})
Branch: 1 branch
BranchTrueFalseTotal
if (config()->{'Strehler'}{'schema'}) { } 0 189 189
58 {
59 0 return schema config->{'Strehler'}->{'schema'};
60 }
61 else
62 {
63 189 return schema;
64 }
65 }
66  
67 sub subcategories
68 4 ▶{
Subroutine subcategories: called CC=5 SCAR=17.4
Pod documented
69 4 my $self = shift;
70 4 my $deep = shift;
71 4 my $already_collected = shift;
72 4 my @subs;
73 4 ▶ if($already_collected)
Branch: 1 branch
BranchTrueFalseTotal
if ($already_collected) 0 4 4
74 {
75 0 @subs = @{$already_collected};
76 }
77  
78 4 ▶ return @subs if ! $self->row;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->row 0 4 4
79 4 for($self->row->categories)
80 {
81 3 my $category = Strehler::Meta::Category->new('row', $_);
82 3 push @subs, $category;
83 3 ▶ if($deep)
Branch: 1 branch
BranchTrueFalseTotal
if ($deep) 0 3 3
84 {
85 0 @subs = $category->subcategories(1, \@subs);
86 }
87 }
88 4 return @subs;
89 }
90  
91  
92 sub get_basic_data
93 1 ▶{
Subroutine get_basic_data: called CC=1 SCAR=0.0
Pod documented
94 1 my $self = shift;
95 1 my %data;
96 1 $data{'id'} = $self->get_attr('id');
97 1 $data{'name'} = $self->get_attr('category');
98 1 $data{'title'} = $self->get_attr('category');
99 1 $data{'ext_name'} = $self->ext_name;
100 1 $data{'parent'} = $self->get_attr('parent');
101 1 return %data;
102 }
103  
104 sub has_elements
105 6 ▶{
Subroutine has_elements: called CC=3 SCAR=11.0
Pod documented
106 6 my $self = shift;
107 6 my $category_row = $self->row;
108 6 for my $e (Strehler::Helpers::get_categorized_entities())
109 {
110 18 my $cl = Strehler::Helpers::class_from_entity($e);
111 18 my $accessor = $cl->category_accessor($category_row);
112 18 ▶ return 1 if($category_row->$accessor->count() > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $category_row->$accessor->count > 0 2 16 18
113 }
114 4 return 0;
115 }
116 sub is_parent
117 12 ▶{
Subroutine is_parent: called CC=1 SCAR=0.0
Pod documented
118 12 my $self = shift;
119 12 return $self->row->categories->count() > 0;
120 }
121 sub no_categories
122 19 ▶{
Subroutine no_categories: called CC=1 SCAR=0.0
Pod undocumented
123 19 my $self = shift;
124 19 my $how_many = $self->get_schema()->resultset('Category')->search({});
125 19 return $how_many == 0;
126 }
127  
128 sub max_article_order
129 0 ▶{
Subroutine max_article_order: not called CC=1 SCAR=6.9
Pod undocumented
130 0 my $self = shift;
131 0 my $max = $self->row->articles->search()->get_column('display_order')->max();
132 0 return $max;
133 }
134  
135 sub delete
136 6 ▶{
Subroutine delete: called CC=3 SCAR=11.0
Pod documented
137 6 my $self = shift;
138 6 ▶ return -1 if($self->has_elements());
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_elements 2 4 6
139 4 ▶ return -2 if($self->is_parent());
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_parent 0 4 4
140 4 $self->row->configured_tags->delete_all();
141 4 $self->row->delete();
142 4 return 0;
143 }
144  
145 sub get_attr
146 69 ▶{
Subroutine get_attr: called CC=2 SCAR=7.0
Pod documented
147 69 my $self = shift;
148 69 my $attr = shift;
149 69 ▶ return undef if(! $self->exists);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists 0 69 69
150 69 return $self->row->get_column($attr);
151 }
152  
153  
154 sub make_select
155 1 ▶{
Subroutine make_select: called CC=6 SCAR=19.5
Pod undocumented
156 1 my $self = shift;
157 1 my $parent = shift;
158 1 my $option = shift;
159 1 my @category_values = $self->get_schema()->resultset('Category')->search({ parent => $parent });
160 1 my @category_values_for_select;
161 my @base_select;
162 1 push @base_select, { value => undef, label => "-- select --" };
163 1 ▶ if($parent && $option && $option eq 'ancestor')
Branch: 1 branch
BranchTrueFalseTotal
if ($parent and $option and $option eq "ancestor") 0 1 1
Condition33%2/6
$parent and $option and $option eq "ancestor"
!ll&&!rl&&r
100
$parent and $option
!ll&&!rl&&r
100
MC/DC0%0/3
$parent and $option and $option eq "ancestor"
$parent $option $option eq "ancestor"
Truth table25%1/4
A and B and C
A= $parentB= $optionC= $option eq "ancestor"
ABCresult
0XX0
10X0
1100
1111
164 {
165 0 push @category_values_for_select, { value => 'anc:'.$parent, label => "-- all --" };
166 }
167 1 my $category_count = 0;
168 1 for(@category_values)
169 {
170 4 $category_count++;
171 4 push @category_values_for_select, { value => $_->id, label => $_->category }
172 }
173 1 ▶ if($category_count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($category_count > 0) { } 1 0 1
174 {
175 1 @category_values_for_select = (@base_select, @category_values_for_select);
176 1 return \@category_values_for_select;
177 }
178 else
179 {
180 0 return \@base_select;
181 }
182 }
183  
184 sub get_list
185 1 ▶{
Subroutine get_list: called CC=8 SCAR=27.7
Pod documented
186 1 my $self = shift;
187 1 my $params = shift;
188 1 my %args;
189 1 ▶ if($params)
Branch: 1 branch
BranchTrueFalseTotal
if ($params) { } 0 1 1
190 {
191 0 %args = %{ $params };
192 }
193 else
194 {
195 1 %args = ();
196 }
197 1 ▶ $args{'order'} ||= 'desc';
Condition50%1/2
$args{"order"} ||= "desc"
l!l
01
MC/DC0%0/1
$args{"order"} ||= "desc"
$args{"order"}
Truth table50%1/2
A
A= $args{"order"}
Aresult
11
00
198 1 ▶ $args{'order_by'} ||= 'id';
Condition50%1/2
$args{"order_by"} ||= "id"
l!l
01
MC/DC0%0/1
$args{"order_by"} ||= "id"
$args{"order_by"}
Truth table50%1/2
A
A= $args{"order_by"}
Aresult
11
00
199 1 ▶ $args{'parent'} ||= undef;
Condition50%1/2
$args{"parent"} ||= undef
l!l
01
MC/DC0%0/1
$args{"parent"} ||= undef
$args{"parent"}
Truth table50%1/2
A
A= $args{"parent"}
Aresult
11
00
200 1 ▶ $args{'depth'} ||= 0;
Condition50%1/2
$args{"depth"} ||= 0
l!l
01
MC/DC0%0/1
$args{"depth"} ||= 0
$args{"depth"}
Truth table50%1/2
A
A= $args{"depth"}
Aresult
11
00
201 1 my $search_criteria = undef;
202  
203 1 my @to_view;
204 1 my $rs = $self->get_schema()->resultset('Category')->search({parent => $args{'parent'}}, { order_by => { '-' . $args{'order'} => $args{'order_by'} }});
205 1 for($rs->all())
206 {
207 0 my $cat = Strehler::Meta::Category->new($_->id);
208 0 my %el = $cat->get_basic_data();
209 0 $el{'depth'} = $args{'depth'};
210 0 ▶ if($args{'depth'} > 5)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'depth'} > 5) { } 0 0 0
211 {
212 0 $el{'display_name'} = $el{'ext_name'};
213 0 $el{'display_name'} =~ s/^.*\/(.*\/.*)$/\.\.\.\/$1/;
214 }
215 else
216 {
217 0 $el{'display_name'} = $el{'name'};
218 }
219 0 push @to_view, \%el;
220 0 my @subs = $self->get_list({ parent => $_->id, depth => $args{'depth'} + 1});
221 0 push @to_view, @subs;
222 }
223 1 return @to_view;
224 }
225 sub explode_name
226 46 ▶{
Subroutine explode_name: called CC=3 SCAR=11.0
Pod undocumented
227 46 my $self = shift;
228 46 my $category_path = shift;
229 46 ▶ return Strehler::Meta::Category->new(-1) if(! $category_path);
Branch: 1 branch
BranchTrueFalseTotal
unless $category_path 0 46 46
230 46 my $cat_id = $self->name_crawler($category_path, undef);
231 46 ▶ if($cat_id)
Branch: 1 branch
BranchTrueFalseTotal
if ($cat_id) { } 29 17 46
232 {
233 29 return Strehler::Meta::Category->new($cat_id);
234 }
235 else
236 {
237 17 return Strehler::Meta::Category->new(-1);
238 }
239 }
240 sub name_crawler
241 60 ▶{
Subroutine name_crawler: called CC=4 SCAR=13.9
Pod undocumented
242 60 my $self = shift;
243 60 my $name = shift;
244 60 my $parent = shift;
245  
246 60 my @subnames = split '/', $name;
247 60 my $cat = shift @subnames;
248 60 my $cat_obj = Strehler::Meta::Category->new(category => $cat, parent => $parent);
249 60 ▶ $parent ||= '';
Condition100%2/2
$parent ||= ""
l!l
1446
MC/DC100%1/1
$parent ||= ""
$parent
Truth table100%2/2
A
A= $parent
Aresult
11
00
250 60 ▶ if($cat_obj->exists())
Branch: 1 branch
BranchTrueFalseTotal
if ($cat_obj->exists) { } 43 17 60
251 {
252 43 ▶ if(@subnames)
Branch: 1 branch
BranchTrueFalseTotal
if (@subnames) { } 14 29 43
253 {
254 14 return $self->name_crawler(join('/', @subnames), $cat_obj->get_attr('id'));
255 }
256 else
257 {
258 29 return $cat_obj->get_attr('id');
259 }
260 }
261 else
262 {
263 17 return undef;
264 }
265  
266 }
267  
268 sub exists
269 206 ▶{
Subroutine exists: called CC=2 SCAR=6.9
Pod undocumented
270 206 my $self = shift;
271 206 ▶ if($self->row)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->row) { } 172 34 206
272 {
273 172 return 1;
274 }
275 else
276 {
277 34 return 0;
278 }
279 }
280  
281 sub get_form_data
282 0 ▶{
Subroutine get_form_data: not called CC=4 SCAR=30.0
Pod documented
283 0 my $self = shift;
284 0 my $ents = shift;
285 0 my @entities = @{$ents};
286 0 push @entities, 'all';
287 0 my $row = $self->row;
288 0 my $data;
289 0 $data->{'category'} = $row->category;
290 0 ▶ if($row->parent)
Branch: 1 branch
BranchTrueFalseTotal
if ($row->parent) { } 0 0 0
291 {
292 0 $data->{'parent'} = $row->parent->id;
293 }
294 else
295 {
296 0 $data->{'parent'} = undef;
297 }
298 0 my $configured_tags = Strehler::Meta::Tag->get_configured_tags($self->ext_name(), \@entities);
299 0 for(@entities)
300 {
301 0 my $e = $_;
302 0 ▶ if($configured_tags->{$e})
Branch: 1 branch
BranchTrueFalseTotal
if ($configured_tags->{$e}) 0 0 0
303 {
304 0 $data->{'tags-' . $e} = $configured_tags->{$e};
305 0 $data->{'default-' . $e} = $configured_tags->{'default-' . $e};
306 }
307 }
308 0 return $data;
309 }
310  
311 sub save_form
312 14 ▶{
Subroutine save_form: called CC=8 SCAR=21.5
Pod documented
313 14 my $self = shift;
314 14 my $id = shift;
315 14 my $form = shift;
316 14 my $ents = shift;
317 14 my @entities = @{$ents};
318 14 my $new_category;
319 14 ▶ if($id)
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 0 14 14
320 {
321 0 $new_category = $self->get_schema()->resultset('Category')->find($id);
322 0 ▶ if($form->param_value('parent'))
Branch: 1 branch
BranchTrueFalseTotal
if ($form->param_value('parent')) { } 0 0 0
323 {
324 0 $new_category->update({category => $form->param_value('category'), parent => $form->param_value('parent')});
325 }
326 else
327 {
328 0 $new_category->update({category => $form->param_value('category')});
329 }
330 }
331 else
332 {
333 14 ▶ if($form->param_value('parent'))
Branch: 1 branch
BranchTrueFalseTotal
if ($form->param_value('parent')) { } 4 10 14
334 {
335 4 $new_category = $self->get_schema()->resultset('Category')->create({category => $form->param_value('category'), parent => $form->param_value('parent')});
336 }
337 else
338 {
339 10 $new_category = $self->get_schema()->resultset('Category')->create({category => $form->param_value('category')});
340 }
341 }
342 14 ▶ if($form->param_value('tags-all'))
Branch: 1 branch
BranchTrueFalseTotal
if ($form->param_value('tags-all')) { } 8 6 14
343 {
344 8 Strehler::Meta::Tag->clean_configured_tags($new_category->id);
345 8 Strehler::Meta::Tag->save_configured_tags($form->param_value('tags-all'), $form->param_value('default-all'), $new_category->id, 'all');
346 }
347 else
348 {
349 6 my $cleaned = 0;
350 6 for(@entities)
351 {
352 24 my $e = $_;
353 24 ▶ if($form->param_value('tags-' . $e))
Branch: 1 branch
BranchTrueFalseTotal
if ($form->param_value('tags-' . $e)) 6 18 24
354 {
355 6 ▶ if(! $cleaned)
Branch: 1 branch
BranchTrueFalseTotal
unless ($cleaned) 6 0 6
356 {
357 6 Strehler::Meta::Tag->clean_configured_tags($new_category->id);
358 6 $cleaned = 1;
359 }
360 6 Strehler::Meta::Tag->save_configured_tags($form->param_value('tags-' . $e), $form->param_value('default-' . $e), $new_category->id, $e);
361 }
362 }
363 }
364 14 return $new_category->id;
365 }
366  
367 sub ext_name
368 40 ▶{
Subroutine ext_name: called CC=3 SCAR=11.0
Pod undocumented
369 40 my $self = shift;
370 40 ▶ return undef if(! $self->exists());
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists 0 40 40
371 40 my $category = $self->row->category;
372 40 my $parent = $self->row->parent;
373 40 ▶ while($parent)
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 11 40 51
374 {
375 11 $category = $parent->category . '/' . $category;
376 11 $parent = $parent->parent;
377 }
378 40 return $category;
379 }
380  
381 sub check_role
382 22 ▶{
Subroutine check_role: called CC=2 SCAR=6.9
Pod undocumented
383 22 my $self = shift;
384 22 my $role = shift;
385 22 ▶ if(! config->{Strehler}->{admin_secured})
Branch: 1 branch
BranchTrueFalseTotal
unless (config()->{"Strehler"}{"admin_secured"}) 21 1 22
386 {
387 21 return 1;
388 }
389 1 return ($role eq 'admin');
390 }
391  
392 sub error_message
393 2 ▶{
Subroutine error_message: called CC=5 SCAR=19.7
Pod undocumented
394 2 my $self = shift;
395 2 my $action = shift;
396 2 my $code = shift;
397 2 ▶ if($code == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 0) { } 0 2 2
398 {
399 0 return "OK";
400 }
401 else
402 {
403 2 ▶ if($action eq 'delete')
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "delete") 2 0 2
404 {
405 2 ▶ if($code == -1)
Branch: 2 branches
BranchTrueFalseTotal
if ($code == -1) { } 2 0 2
elsif ($code == -2) { } 0 0 0
406 {
407 2 return "Category " . $self->get_attr('category') . " is not empty! Deletion is impossible.";
408 }
409 elsif($code == -2)
410 {
411 0 return "Category " . $self->get_attr('category') . " has subcategories! Deletion is impossible.";
412 }
413 }
414 0 return "An error has occurred";
415 }
416 }
417  
418  
419 12 1;
420  
421  
422  
423  
424  
425  
426  
« blib/lib/Strehler/FormFu/Element/EntityTypeSelect.pm ↑ Summary lib/Strehler/FormFu/Validator/CategoryUnique.pm »