blib/lib/Mojolicious/Command/generate/callbackery_app/lib/App/GuiPlugin/SongForm.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 95 CC 4 SCAR 23.0
CC
4
Cov
0%
getAllFieldValues
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 10.0
1 % my $p = shift;
2 package <%= $p->{class} %>::GuiPlugin::SongForm;
3 use Mojo::Base 'CallBackery::GuiPlugin::AbstractForm';
4 use CallBackery::Translate qw(trm);
5 use CallBackery::Exception qw(mkerror);
6 use Mojo::JSON qw(true false);
7  
8 use POSIX qw(strftime);
9  
10 =head1 NAME
11  
12 <%= $p->{class} %>::GuiPlugin::SongForm - Song Edit Form
13  
14 =head1 SYNOPSIS
15  
16 use <%= $p->{class} %>::GuiPlugin::SongForm;
17  
18 =head1 DESCRIPTION
19  
20 The Song Edit Form
21  
22 =cut
23  
24 =head1 METHODS
25  
26 All the methods of L<CallBackery::GuiPlugin::AbstractForm> plus:
27  
28 =cut
29  
30 =head2 formCfg
31  
32 Returns a Configuration Structure for the Song Entry Form.
33  
34 =cut
35  
36  
37 my $voices = [
38 S => 'Soprano',
39 A => 'Alto',
40 T => 'Tenor',
41 B => 'Bass',
42 ];
43  
44 my %VOICE;
45 my @VOICES;
46  
47 while (@$voices){
48 my $key = shift @$voices;
49 my $name = shift @$voices;
50 $VOICE{$key} = $name;
51 push @VOICES, $key;
52 }
53  
54 has formCfg => sub {
55 my $self = shift;
56 my $db = $self->user->db;
57  
58 return [
59 $self->config->{type} eq 'edit' ? {
60 key => 'song_id',
61 label => trm('SongId'),
62 widget => 'hiddenText',
63 set => {
64 readOnly => $self->true,
65 },
66 } : (),
67  
68 {
69 key => 'song_title',
70 label => trm('Title'),
71 widget => 'text',
72 set => {
73 required => $self->true,
74 },
75 },
76 {
77 widget => 'header',
78 label => trm('Song Details'),
79 note => trm('Use the following fields to write down some extra information about the song.')
80 },
81 (map {
82 {
83 key => 'song_voice_'.$_,
84 label => $VOICE{$_}.' Voice',
85 widget => 'checkBox',
86 set => {
87 label => trm('Song has a '.$VOICE{$_}.' Part'),
88 }
89 }
90 } @VOICES),
91 {
92 key => 'song_composer',
93 label => trm('Composer'),
94 widget => 'text',
95 },
96 {
97 key => 'song_page',
98 label => trm('Song Page'),
99 widget => 'text',
100 validator => sub {
101 my $value = shift;
102 if ($value ne int($value)){
103 return "Expected a page Number";
104 }
105 return "";
106 }
107 },
108 {
109 key => 'song_note',
110 label => trm('Note'),
111 widget => 'textArea',
112 note => trm('Use this area to write down additional notes on the particular song.'),
113 set => {
114 placeholder => 'some extra information about this song',
115 }
116 },
117 ];
118 };
119  
120 has actionCfg => sub {
121 my $self = shift;
122 my $type = $self->config->{type} // 'new';
123  
124 my $handler = sub {
125 my $self = shift;
126 my $args = shift;
127  
128 $args->{song_voices} = join "", map { $args->{'song_voice_'.$_} ? ($_) : () } @VOICES;
129  
130 my @fields = qw(title voices composer page note);
131  
132 my $db = $self->user->db;
133  
134 my $id = $db->updateOrInsertData('song',{
135 map { $_ => $args->{'song_'.$_} } @fields
136 },$args->{song_id} ? { id => int($args->{song_id}) } : ());
137 return {
138 action => 'dataSaved'
139 };
140 };
141  
142 return [
143 {
144 label => $type eq 'edit'
145 ? trm('Save Changes')
146 : trm('Add Tree Node'),
147 action => 'submit',
148 key => 'save',
149 actionHandler => $handler
150 }
151 ];
152 };
153  
154 has grammar => sub {
155 my $self = shift;
156 $self->mergeGrammar(
157 $self->SUPER::grammar,
158 {
159 _doc => "Tree Node Configuration",
160 _vars => [ qw(type) ],
161 type => {
162 _doc => 'type of form to show: edit, add',
163 _re => '(edit|add)'
164 },
165 },
166 );
167 };
168  
169 sub getAllFieldValues {
170 my $self = shift;
171 my $args = shift;
172 return {} if $self->config->{type} ne 'edit';
173 my $id = $args->{selection}{song_id};
174 return {} unless $id;
175  
176 my $db = $self->user->db;
177 my $data = $db->fetchRow('song',{id => $id});
178 my $voices = $data->{song_voices};
179 for my $v (@VOICES){
180 $data->{'song_voice_'.$v} = $voices =~ /$v/ ? $self->true : $self->false;
181 ;
182 }
183 return $data;
184 }
185  
186 has checkAccess => sub {
187 my $self = shift;
188 return $self->user->may('write');
189 };
190  
191 1;
192 __END__