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

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 85 CC 4 SCAR 17.0
CC
4
Cov
0%
getTableData
12.0
_getFilter
6.0
dbh
2.0
CRAP 5.5
1 % my $p = shift;
2 package <%= $p->{class} %>::GuiPlugin::Song;
3 use Mojo::Base 'CallBackery::GuiPlugin::AbstractTable';
4 use CallBackery::Translate qw(trm);
5 use CallBackery::Exception qw(mkerror);
6 use Mojo::JSON qw(true false);
7  
8 =head1 NAME
9  
10 <%= $p->{class} %>::GuiPlugin::Song - Song Table
11  
12 =head1 SYNOPSIS
13  
14 use <%= $p->{class} %>::GuiPlugin::Song;
15  
16 =head1 DESCRIPTION
17  
18 The Song Table Gui.
19  
20 =cut
21  
22  
23 =head1 METHODS
24  
25 All the methods of L<CallBackery::GuiPlugin::AbstractTable> plus:
26  
27 =cut
28  
29 has formCfg => sub {
30 my $self = shift;
31 my $db = $self->user->db;
32  
33 return [
34 {
35 widget => 'header',
36 label => trm('*'),
37 note => trm('Nice Start')
38 },
39 {
40 key => 'song_title',
41 widget => 'text',
42 note => 'Just type the title of a song',
43 label => 'Search',
44 set => {
45 placeholder => 'Song Title',
46 },
47 },
48 ]
49 };
50  
51 =head2 tableCfg
52  
53  
54 =cut
55  
56 has tableCfg => sub {
57 my $self = shift;
58 return [
59 {
60 label => trm('Id'),
61 type => 'number',
62 width => '1*',
63 key => 'song_id',
64 sortable => true,
65 primary => true
66 },
67 {
68 label => trm('Title'),
69 type => 'string',
70 width => '6*',
71 key => 'song_title',
72 sortable => true,
73 },
74 {
75 label => trm('Voices'),
76 type => 'string',
77 width => '1*',
78 key => 'song_voices',
79 sortable => true,
80 },
81 {
82 label => trm('Composer'),
83 type => 'string',
84 width => '2*',
85 key => 'song_composer',
86 sortable => true,
87 },
88 {
89 label => trm('Page'),
90 type => 'string',
91 width => '1*',
92 key => 'song_page',
93 sortable => true,
94 },
95 {
96 label => trm('Note'),
97 type => 'string',
98 width => '3*',
99 key => 'song_note',
100 sortable => true,
101 },
102 ]
103 };
104  
105 =head2 actionCfg
106  
107 Only users who can write get any actions presented.
108  
109 =cut
110  
111 has actionCfg => sub {
112 my $self = shift;
113 return [] if $self->user and not $self->user->may('write');
114  
115 return [
116 {
117 label => trm('Add Song'),
118 action => 'popup',
119 addToContextMenu => false,
120 name => 'songFormAdd',
121 popupTitle => trm('New Song'),
122 set => {
123 minHeight => 600,
124 minWidth => 500
125 },
126 backend => {
127 plugin => 'SongForm',
128 config => {
129 type => 'add'
130 }
131 }
132 },
133 {
134 action => 'separator'
135 },
136 {
137 label => trm('Edit Song'),
138 action => 'popup',
139 addToContextMenu => true,
140 defaultAction => true,
141 name => 'songFormEdit',
142 popupTitle => trm('Edit Song'),
143 backend => {
144 plugin => 'SongForm',
145 config => {
146 type => 'edit'
147 }
148 }
149 },
150 {
151 label => trm('Delete Song'),
152 action => 'submitVerify',
153 addToContextMenu => true,
154 question => trm('Do you really want to delete the selected Song '),
155 key => 'delete',
156 actionHandler => sub {
157 my $self = shift;
158 my $args = shift;
159 my $id = $args->{selection}{song_id};
160 die mkerror(4992,"You have to select a song first")
161 if not $id;
162 my $db = $self->user->db;
163 if ($db->deleteData('song',$id) == 1){
164 return {
165 action => 'reload',
166 };
167 }
168 die mkerror(4993,"Faild to remove song $id");
169 return {};
170 }
171 }
172 ];
173 };
174  
175 sub dbh {
176 shift->user->mojoSqlDb->dbh;
177 };
178  
179 sub _getFilter {
180 my $self = shift;
181 my $search = shift;
182 my $filter = '';
183 if ( $search ){
184 $filter = "WHERE song_title LIKE ".$self->dbh->quote('%'.$search);
185 }
186 return $filter;
187 }
188  
189 sub getTableRowCount {
190 my $self = shift;
191 my $args = shift;
192 my $filter = $self->_getFilter($args->{formData}{song_title});
193 return ($self->dbh->selectrow_array("SELECT count(song_id) FROM song $filter"))[0];
194 }
195  
196 sub getTableData {
197 my $self = shift;
198 my $args = shift;
199 my $filter = $self->_getFilter($args->{formData}{song_title});
200 my $SORT ='';
201 if ($args->{sortColumn}){
202 $SORT = 'ORDER BY '.$self->dbh->quote_identifier($args->{sortColumn});
203 $SORT .= $args->{sortDesc} ? ' DESC' : ' ASC';
204 }
205 return $self->dbh->selectall_arrayref(<<"SQL",{Slice => {}}, $args->{lastRow}-$args->{firstRow}+1,$args->{firstRow});
206 SELECT *
207 FROM song
208 $filter
209 $SORT
210 LIMIT ? OFFSET ?
211 SQL
212 }
213  
214 1;
215 __END__