callbackery_app/lib/App/GuiPlugin/SongForm.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 110 CC 5 SCAR 27.7
CC
5
Cov
0%
getAllFieldValues
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 16.0
1 package <%= ${class} %>::GuiPlugin::SongForm;
2 use Mojo::Base 'CallBackery::GuiPlugin::AbstractForm';
3 use CallBackery::Translate qw(trm);
4 use CallBackery::Exception qw(mkerror);
5 use Mojo::JSON qw(true false);
6  
7 use POSIX qw(strftime);
8  
9 =head1 NAME
10  
11 <%= ${class} %>::GuiPlugin::SongForm - Song Edit Form
12  
13 =head1 SYNOPSIS
14  
15 use <%= ${class} %>::GuiPlugin::SongForm;
16  
17 =head1 DESCRIPTION
18  
19 The Song Edit Form
20  
21 =cut
22  
23 =head1 METHODS
24  
25 All the methods of L<CallBackery::GuiPlugin::AbstractForm> plus:
26  
27 =cut
28  
29 =head2 formCfg
30  
31 Returns a Configuration Structure for the Song Entry Form.
32  
33 =cut
34  
35  
36 my $voices = [
37 S => 'Soprano',
38 A => 'Alto',
39 T => 'Tenor',
40 B => 'Bass',
41 ];
42  
43 my $decades = [
44 { tilte => ' ', key => '0' }, # The entry with key `0` appears first
45 { title => 'Sixites', key => '60s' },
46 { title => 'Eighties', key => '80s' },
47 { title => 'Nineties', key => '90s' },
48 { title => 'Post Vinyl', key => '20+' },
49 ];
50  
51 my %VOICE;
52 my @VOICES;
53  
54 while (@$voices){
55 my $key = shift @$voices;
56 my $name = shift @$voices;
57 $VOICE{$key} = $name;
58 push @VOICES, $key;
59 }
60  
61 has formCfg => sub {
62 my $self = shift;
63 my $db = $self->user->db;
64  
65 return [
66 $self->config->{type} eq 'edit' ? {
67 key => 'song_id',
68 label => trm('SongId'),
69 widget => 'hiddenText',
70 set => {
71 readOnly => $self->true,
72 },
73 } : (),
74  
75 {
76 key => 'song_title',
77 label => trm('Title'),
78 widget => 'text',
79 set => {
80 required => $self->true,
81 },
82 },
83 {
84 widget => 'header',
85 label => trm('Song Details'),
86 note => trm('Use the following fields to write down some extra information about the song.')
87 },
88 (map {
89 {
90 key => 'song_voice_'.$_,
91 label => $VOICE{$_}.' Voice',
92 widget => 'checkBox',
93 set => {
94 label => trm('Song has a '.$VOICE{$_}.' Part'),
95 }
96 }
97 } @VOICES),
98 {
99 key => 'song_decade',
100 label => trm('Song Decade'),
101 widget => 'selectBox',
102 triggerFormReset => true,
103 set => {
104 required => false
105 },
106 cfg => {
107 structure => $decades
108 }
109 },
110 {
111 key => 'song_decade_addional',
112 label => trm('Addional Decade info'),
113 widget => 'text',
114 reloadOnFormReset => true,
115 set => $self->args->{currentFormData}{song_decade} ? {
116 readOnly => false,
117 placeholder => 'Type something extra',
118 } : {
119 readOnly => true,
120 placeholder => 'This field becomes available as soon you select a decade',
121 },
122 },
123 {
124 key => 'song_composer',
125 label => trm('Composer'),
126 widget => 'text',
127 },
128 {
129 key => 'song_page',
130 label => trm('Song Page'),
131 widget => 'text',
132 validator => sub {
133 my $value = shift;
134 if ($value ne int($value)){
135 return "Expected a page Number";
136 }
137 return "";
138 }
139 },
140 {
141 key => 'song_note',
142 label => trm('Note'),
143 widget => 'textArea',
144 note => trm('Use this area to write down additional notes on the particular song.'),
145 set => {
146 placeholder => 'some extra information about this song',
147 }
148 },
149 ];
150 };
151  
152 has actionCfg => sub {
153 my $self = shift;
154 my $type = $self->config->{type} // 'new';
155  
156 my $handler = sub {
157 my $self = shift;
158 my $args = shift;
159  
160 $args->{song_voices} = join "", map { $args->{'song_voice_'.$_} ? ($_) : () } @VOICES;
161  
162 my @fields = qw(title voices decade decade_addional composer page note);
163  
164 my $db = $self->user->db;
165  
166 my $id = $db->updateOrInsertData('song',{
167 map { $_ => $args->{'song_'.$_} } @fields
168 },$args->{song_id} ? { id => int($args->{song_id}) } : ());
169 return {
170 action => 'dataSaved'
171 };
172 };
173  
174 return [
175 {
176 label => $type eq 'edit'
177 ? trm('Save Changes')
178 : trm('Add Tree Node'),
179 action => 'submit',
180 key => 'save',
181 actionHandler => $handler
182 }
183 ];
184 };
185  
186 has grammar => sub {
187 my $self = shift;
188 $self->mergeGrammar(
189 $self->SUPER::grammar,
190 {
191 _doc => "Tree Node Configuration",
192 _vars => [ qw(type) ],
193 type => {
194 _doc => 'type of form to show: edit, add',
195 _re => '(edit|add)'
196 },
197 },
198 );
199 };
200  
201 sub getAllFieldValues {
202 my $self = shift;
203 my $args = shift;
204 my $formData = shift;
205  
206 # we can also update field values based on the current form data
207 if ($formData->{currentFormData}{song_decade} eq '20+' && !$formData->{currentFormData}{song_decade_addional}) {
208 return {song_decade_addional => 'We most certainly need more info here!'};
209 }
210
211 return {} if $self->config->{type} ne 'edit';
212 my $id = $args->{selection}{song_id};
213 return {} unless $id;
214  
215 my $db = $self->user->db;
216 my $data = $db->fetchRow('song',{id => $id});
217 my $voices = $data->{song_voices};
218 for my $v (@VOICES){
219 $data->{'song_voice_'.$v} = $voices =~ /$v/ ? $self->true : $self->false;
220 ;
221 }
222 return $data;
223 }
224  
225 has checkAccess => sub {
226 my $self = shift;
227 return $self->user->may('write');
228 };
229  
230 1;
231 __END__