blib/lib/OpusVL/AppKit/RolesFor/Controller/GUI.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 189 CC 10 SCAR 18.8
CC
10
Cov
0%
application_action_list
12.0
flag_callback_error_ex
12.0
_sorted_filtered_actions
6.0
CRAP 6.5
1 package OpusVL::AppKit::RolesFor::Controller::GUI;
2  
3  
4 ##################################################################################################################################
5 # use lines.
6 ##################################################################################################################################
7 use strict;
8 use Moose::Role;
9  
10 ##################################################################################################################################
11 # moose calls.
12 ##################################################################################################################################
13  
14 has appkit => ( is => 'ro', isa => 'Int', default => 1 );
15 has appkit_name => ( is => 'rw', isa => 'Str', default => 'AppKit' );
16 has appkit_myclass => ( is => 'ro', isa => 'Str', );
17 has appkit_shared_module => ( is => 'rw', isa => 'Str');
18 has navigation_items_merged => ( is => 'rw', isa => 'Bool', default => 0 );
19 has appkit_method_group_order => ( is => 'rw', isa => 'Int', default => 0);
20 has appkit_method_group => ( is => 'rw', isa => 'Str', default => '');
21 has appkit_order => ( is => 'rw', isa => 'Int', default => 0);
22  
23 has _default_order => ( is => 'rw', isa => 'Int', default => 0);
24  
25  
26 has home_action => ( is => 'rw', isa => 'HashRef' );
27  
28  
29 has navigation_actions => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
30  
31  
32 has navigation_actions_grouped => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
33  
34  
35 has portlet_actions => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
36  
37  
38 has search_actions => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
39  
40  
41 before create_action => sub
42 {
43 my $self = shift;
44 my %args = @_;
45  
46 # add any ActionClass's to this action.. so when it is called, some extra code is excuted....
47 if ( defined $args{attributes}{AppKitForm} ) { push @{ $args{attributes}{ActionClass} }, "OpusVL::AppKit::Action::AppKitForm"; }
48  
49 if ( defined $args{attributes}{NavigationHome} )
50 {
51 # This action has been identified as a Home action...
52 $self->home_action
53 (
54 {
55 actionpath => $args{reverse},
56 actionname => $args{name},
57 }
58 );
59 }
60  
61 if ( defined $args{attributes}{NavigationName} )
62 {
63 # This action has been identified as a Navigation item..
64 my $array = $self->navigation_actions;
65 $array = [] unless defined $array;
66 $self->_default_order($self->_default_order+1);
67 my $order;
68 if(defined $args{attributes}{NavigationOrder})
69 {
70 $order = $args{attributes}{NavigationOrder}->[0]
71 }
72 else
73 {
74 $order = $self->_default_order;
75 }
76  
77 my $hide = {};
78 if ($args{attributes}{Hide}) {
79 my ($key, $value) = split(':', $args{attributes}{Hide}->[0]);
80 if ($key && $value) {
81 $hide = {
82 hidden => 1,
83 as => $key,
84 with => $value
85 };
86 }
87 }
88  
89 push
90 (
91 @$array,
92 {
93 value => $args{attributes}{NavigationName}->[0],
94 actionpath => $args{reverse},
95 actionname => $args{name},
96 title => $args{attributes}{Description}->[0],
97 controller => $self,
98 sort_index => $order,
99 hide_as => $hide->{as}||0,
100 hide_with => $hide->{with}||0,
101 }
102 );
103  
104 $self->navigation_actions( $array );
105 }
106
107 if ( defined $args{attributes}{PortletName} )
108 {
109 # This action has been identified as a Portlet action...
110 my $array = $self->portlet_actions;
111 $array = [] unless defined $array;
112 push
113 (
114 @$array,
115 {
116 value => $args{attributes}{PortletName}->[0],
117 actionpath => $args{reverse},
118 actionname => $args{name},
119 }
120 );
121 $self->portlet_actions ( $array );
122 }
123  
124 if ( defined $args{attributes}{SearchName} )
125 {
126 # This action has been identified as a Search action...
127 my $array = $self->search_actions;
128 $array = [] unless defined $array;
129 push
130 (
131 @$array,
132 {
133 value => $args{attributes}{SearchName}->[0],
134 actionpath => $args{reverse},
135 actionname => $args{name},
136 }
137 );
138 $self->search_actions ( $array );
139 }
140 };
141  
142 sub intranet_action_list
143 {
144 my $self = shift;
145 my $c = shift;
146  
147 my $actions = $self->navigation_actions;
148 return $self->_sorted_filtered_actions($c, $actions);
149 }
150  
151 sub _sorted_filtered_actions
152 {
153 my $self = shift;
154 my $c = shift;
155 my $actions = shift;
156  
157 return [] if !$actions;
158 my @actions = sort { $a->{sort_index} <=> $b->{sort_index} }
159 grep { $c->can_access($_->{controller}->action_for($_->{actionname})) } @$actions;
160 return \@actions;
161 }
162  
163 sub application_action_list
164 {
165 # this list includes groups too.
166 my $self = shift;
167 my $c = shift;
168  
169 my $grouped_actions = $self->navigation_actions_grouped;
170 return [] if !$grouped_actions;
171 my @groups;
172 for my $group (@$grouped_actions)
173 {
174 my $filtered = $self->_sorted_filtered_actions($c, $group->{actions});
175 push @groups, { group => $group->{group}, actions => $filtered } if @$filtered;
176 }
177 return \@groups;
178 }
179  
180  
181 ##################################################################################################################################
182 # controller actions.
183 ##################################################################################################################################
184  
185 sub date_long
186 {
187 my ($self, $dt) = @_;
188
189 return if !$dt;
190 return join '',
191 $dt->day_name,
192 ', ',
193 sprintf("%02d ",$dt->day),
194 $dt->month_name,
195 ' ',
196 $dt->year;
197 }
198  
199 sub date_short
200 {
201 my ($self, $dt) = @_;
202 return if !$dt;
203 return join '',
204 sprintf("%02d", $dt->day),
205 '-',
206 $dt->month_abbr,
207 '-',
208 $dt->year;
209 }
210  
211 sub time_long
212 {
213 my ($self, $dt) = @_;
214 return if !$dt;
215  
216 return join '',
217 sprintf('%02d', $dt->hour),
218 ':',
219 sprintf('%02d', $dt->minute),
220 ':',
221 sprintf('%02d', $dt->second);
222  
223 }
224  
225 sub time_short
226 {
227 my ($self, $dt) = @_;
228 return if !$dt;
229  
230 return join '',
231 sprintf('%02d', $dt->hour),
232 ':',
233 sprintf('%02d', $dt->minute);
234  
235 }
236  
237  
238 sub add_breadcrumb
239 {
240 my $self = shift;
241 my $c = shift;
242 my $args = shift;
243 push @{$c->stash->{breadcrumbs}}, $args;
244 }
245  
246  
247 sub add_final_crumb
248 {
249 my $self = shift;
250 my $c = shift;
251 my $title = shift;
252 push @{$c->stash->{breadcrumbs}}, { name => $title, url => $c->req->uri };
253 }
254  
255  
256 sub flag_callback_error
257 {
258 my ($self, $c, $field_name, $message) = @_;
259 return $self->flag_callback_error_ex($c, $field_name, { message => $message });
260 }
261  
262 sub flag_callback_error_ex
263 {
264 my ($self, $c, $field_name, $args) = @_;
265  
266 $args //= {};
267 my $message = $args->{message};
268 my $no_detach = $args->{no_detach};
269  
270 my $form = $c->stash->{form};
271 my $constraint = $form->get_field($field_name)->get_constraint({ type => 'Callback' });
272 $constraint->callback(sub { 0});
273 $constraint->message($message) if $message;
274 $form->process;
275 $c->detach unless $no_detach;
276 }
277  
278 ##
279 1;
280  
281 __END__
282  
283 =pod
284  
285 =encoding UTF-8
286  
287 =head1 NAME
288  
289 OpusVL::AppKit::RolesFor::Controller::GUI
290  
291 =head1 VERSION
292  
293 version 2.24
294  
295 =head1 SYNOPSIS
296  
297 package MyApp::Controller::SomeFunkyThing;
298 use Moose;
299 BEGIN{ extends 'Catalyst::Controller' };
300 with 'OpusVL::AppKit::RolesFor::Controller::GUI';
301  
302 __PACKAGE__->config( appkit_name => 'My Funky App' );
303 __PACKAGE__->config( appkit_icon => 'static/funkster/me.gif' );
304 __PACKAGE__->config( appkit_myclass => 'MyApp' );
305
306 sub index
307 :Path
308 :Args(0)
309 :NavigationHome
310 :NavigationName("Funky Home")
311 :PortletName("Funky Portlet")
312 :AppKitForm
313 {
314 # .. do some funky stuff ..
315 }
316  
317 =head1 DESCRIPTION
318  
319 If you use this Moose::Role with a controller it can be intergrated into the OpusVL::AppKit.
320  
321 You can just do:
322 use Moose;
323 with 'OpusVL::AppKit::RolesFor::Controller::GUI';
324  
325 Give your Controller a name within the GUI:
326 __PACKAGE__->config( appkit_name => 'Some Name' );
327  
328 To make use of the additional features you will have to use one of the following
329 action method attributes:
330  
331 NavigationHome
332 This tells the GUI this action is the 'Home' action for this controller.
333  
334 NavigationName
335 Tells the GUI this action is a navigation item and what its name should be.
336  
337 PortletName
338 Tells the GUI this action is a portlet action, so calling is only garented to fill
339 out the 'portlet' stash key.
340  
341 AppKitForm
342 Behaves like FormConfig option in FormFu Controller, except it loads form from the
343 ShareDir of namespace passed in 'appkit_myclass'
344
345 SearchName
346 Tells the GUI this action is a search action and what its name should be
347  
348 =head1 NAME
349  
350 OpusVL::AppKit::RolesFor::Controller::GUI - Role for Controllers wanting to interact with AppKit
351  
352 =head1 METHODS
353  
354 =head2 home_action
355  
356 This should be the hash of action details that pertain the the 'home action' of a controller.
357 If there is none defined for a controller, it should be undef.
358  
359 =head2 navigation_actions
360  
361 This should be an Array Ref of HashRef's pertaining the actions that make up the navigation
362  
363 =head2 navigation_actions_grouped
364  
365 This should be an Array Ref of HashRef's pertaining the actions that make up the navigation
366 grouped by appkit_method_group.
367  
368 =head2 portlet_actions
369  
370 This should be an Array Ref of HashRef's pertaining the actions that are Portlet's
371  
372 =head2 search_actions
373  
374 This should be an Array Ref of HashRef's pertaining the actions that are Portlet's
375  
376 =head2 create_action
377  
378 Hook into the creation of the actions.
379 Here we read the action attributes and act accordingly.
380  
381 =head2 intranet_action_list
382  
383 Returns a sorted list of actions for the menu filtered by what the user can access.
384  
385 =head2 application_action_list
386  
387 Returns a sorted list of actions for the menu filtered by what the user can access.
388  
389 It returns a list of hashes containing two keys, group (the group name) and actions, a list of
390 the actions for that group.
391  
392 =head2 date_long
393  
394 Provides a standard L<DateTime> formatting function that is also mirrored (and called) from TT using
395 the date_long() function.
396  
397 Monday, 10 May 2010
398  
399 =head2 date_short
400  
401 Provides a short date format function for DD-MM-YYYY display.
402  
403 =head2 time_long
404  
405 Provides a long time format function, HH:MM:SS
406  
407 =head2 time_short
408  
409 Provides a short time format function, HH:MM
410  
411 =head2 add_breadcrumb
412  
413 Adds the a breadcrumb on your breadcrumb trial. Pass it the context object and the breadcumb info,
414  
415 $self->add_breadcrumb($c, { name => 'Title', url => $search_url });
416  
417 =head2 add_final_crumb
418  
419 Adds the final breadcrumb on your trial. Simply pass it the title of the breadcrumb.
420  
421 $self->add_final_crumb($c, 'Title');
422  
423 =head2 flag_callback_error
424  
425 Flags an HTML::FormFu callback error.
426  
427 Setup a callback constraint on your form,
428  
429 - type: Text
430 name: project
431 label: Project
432 constraints:
433 - type: Callback
434 message: Project is invalid
435  
436 Then within your controller you can do,
437  
438 $self->flag_callback_error($c, 'project');
439  
440 This will terminate the processing of the action too, by doing a $c->detach;
441  
442 =head1 SEE ALSO
443  
444 L<CatalystX::AppBuilder>,
445 L<OpusVL::AppKit>,
446 L<Catalyst>
447  
448 =head1 AUTHOR
449  
450 OpusVL - www.opusvl.com
451  
452 =head1 COPYRIGHT AND LICENSE
453  
454 This software is copyright (c) 2010 by OpusVL - www.opusvl.com.
455  
456 This is free software; you can redistribute it and/or modify it under
457 the same terms as the Perl 5 programming language system itself.
458  
459 =cut