| 1 | package <%= ${class} %>::GuiPlugin::Song; | ||
| 2 | use Mojo::Base 'CallBackery::GuiPlugin::AbstractTable'; | ||
| 3 | use CallBackery::Translate qw(trm); | ||
| 4 | use CallBackery::Exception qw(mkerror); | ||
| 5 | use Mojo::JSON qw(true false); | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | <%= ${class} %>::GuiPlugin::Song - Song Table | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use <%= ${class} %>::GuiPlugin::Song; | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | The Song Table Gui. | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | |||
| 24 | All the methods of L<CallBackery::GuiPlugin::AbstractTable> plus: | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | |||
| 29 | has screenOpts => sub { | ||
| 30 | my $self = shift; | ||
| 31 | my $opts = $self->SUPER::screenOpts; | ||
| 32 | return { | ||
| 33 | %%$opts, | ||
| 34 | # an alternate layout for this screen | ||
| 35 | layout => { | ||
| 36 | class => 'qx.ui.layout.Dock', | ||
| 37 | set => {}, | ||
| 38 | }, | ||
| 39 | # and settings accordingly | ||
| 40 | container => { | ||
| 41 | set => { | ||
| 42 | # see https://www.qooxdoo.org/apps/apiviewer/#qx.ui.core.LayoutItem | ||
| 43 | # for inspiration in properties to set | ||
| 44 | maxWidth => 700, | ||
| 45 | maxHeight => 500, | ||
| 46 | alignX => 'left', | ||
| 47 | alignY => 'top', | ||
| 48 | }, | ||
| 49 | addProps => { | ||
| 50 | edge => 'west' | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | }; | ||
| 55 | |||
| 56 | has formCfg => sub { | ||
| 57 | my $self = shift; | ||
| 58 | my $db = $self->user->db; | ||
| 59 | |||
| 60 | return [ | ||
| 61 | { | ||
| 62 | widget => 'header', | ||
| 63 | label => trm('*'), | ||
| 64 | note => trm('Nice Start') | ||
| 65 | }, | ||
| 66 | { | ||
| 67 | key => 'song_title', | ||
| 68 | widget => 'text', | ||
| 69 | note => 'Just type the title of a song', | ||
| 70 | label => 'Search', | ||
| 71 | set => { | ||
| 72 | placeholder => 'Song Title', | ||
| 73 | }, | ||
| 74 | }, | ||
| 75 | ] | ||
| 76 | }; | ||
| 77 | |||
| 78 | =head2 tableCfg | ||
| 79 | |||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | has tableCfg => sub { | ||
| 84 | my $self = shift; | ||
| 85 | return [ | ||
| 86 | { | ||
| 87 | label => trm('Id'), | ||
| 88 | type => 'number', | ||
| 89 | width => '1*', | ||
| 90 | key => 'song_id', | ||
| 91 | sortable => true, | ||
| 92 | primary => true | ||
| 93 | }, | ||
| 94 | { | ||
| 95 | label => trm('Title'), | ||
| 96 | type => 'string', | ||
| 97 | width => '6*', | ||
| 98 | key => 'song_title', | ||
| 99 | sortable => true, | ||
| 100 | }, | ||
| 101 | { | ||
| 102 | label => trm('Voices'), | ||
| 103 | type => 'string', | ||
| 104 | width => '1*', | ||
| 105 | key => 'song_voices', | ||
| 106 | sortable => true, | ||
| 107 | }, | ||
| 108 | { | ||
| 109 | label => trm('Composer'), | ||
| 110 | type => 'string', | ||
| 111 | width => '2*', | ||
| 112 | key => 'song_composer', | ||
| 113 | sortable => true, | ||
| 114 | }, | ||
| 115 | { | ||
| 116 | label => trm('Page'), | ||
| 117 | type => 'number', | ||
| 118 | width => '1*', | ||
| 119 | key => 'song_page', | ||
| 120 | sortable => true, | ||
| 121 | }, | ||
| 122 | # { | ||
| 123 | # label => trm('Size'), | ||
| 124 | # type => 'number', | ||
| 125 | # format => { | ||
| 126 | # unitPrefix => 'metric', | ||
| 127 | # maximumFractionDigits => 2, | ||
| 128 | # postfix => 'Byte', | ||
| 129 | # locale => 'en' | ||
| 130 | # }, | ||
| 131 | # width => '1*', | ||
| 132 | # key => 'song_size', | ||
| 133 | # sortable => true, | ||
| 134 | # }, | ||
| 135 | { | ||
| 136 | label => trm('Note'), | ||
| 137 | type => 'string', | ||
| 138 | width => '3*', | ||
| 139 | key => 'song_note', | ||
| 140 | sortable => true, | ||
| 141 | }, | ||
| 142 | # { | ||
| 143 | # label => trm('Created'), | ||
| 144 | # type => 'date', | ||
| 145 | # format => 'yyyy-MM-dd HH:mm:ss Z', | ||
| 146 | # width => '3*', | ||
| 147 | # key => 'song_date', | ||
| 148 | # sortable => true, | ||
| 149 | # }, | ||
| 150 | |||
| 151 | ] | ||
| 152 | }; | ||
| 153 | |||
| 154 | =head2 actionCfg | ||
| 155 | |||
| 156 | Only users who can write get any actions presented. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | has actionCfg => sub { | ||
| 161 | my $self = shift; | ||
| 162 | return [] if $self->user and not $self->user->may('write'); | ||
| 163 | |||
| 164 | return [ | ||
| 165 | { | ||
| 166 | label => trm('Add Song'), | ||
| 167 | action => 'popup', | ||
| 168 | addToContextMenu => false, | ||
| 169 | name => 'songFormAdd', | ||
| 170 | key => 'add', | ||
| 171 | popupTitle => trm('New Song'), | ||
| 172 | set => { | ||
| 173 | minHeight => 600, | ||
| 174 | minWidth => 500 | ||
| 175 | }, | ||
| 176 | backend => { | ||
| 177 | plugin => 'SongForm', | ||
| 178 | config => { | ||
| 179 | type => 'add' | ||
| 180 | } | ||
| 181 | } | ||
| 182 | }, | ||
| 183 | { | ||
| 184 | action => 'separator' | ||
| 185 | }, | ||
| 186 | { | ||
| 187 | label => trm('Edit Song'), | ||
| 188 | action => 'popup', | ||
| 189 | addToContextMenu => true, | ||
| 190 | defaultAction => true, | ||
| 191 | key => 'edit', | ||
| 192 | name => 'songFormEdit', | ||
| 193 | buttonSet => { | ||
| 194 | enabled => false, | ||
| 195 | }, | ||
| 196 | popupTitle => trm('Edit Song'), | ||
| 197 | backend => { | ||
| 198 | plugin => 'SongForm', | ||
| 199 | config => { | ||
| 200 | type => 'edit' | ||
| 201 | } | ||
| 202 | } | ||
| 203 | }, | ||
| 204 | { | ||
| 205 | label => trm('Delete Song'), | ||
| 206 | action => 'submitVerify', | ||
| 207 | addToContextMenu => true, | ||
| 208 | question => trm('Do you really want to delete the selected Song '), | ||
| 209 | key => 'delete', | ||
| 210 | buttonSet => { | ||
| 211 | enabled => false, | ||
| 212 | }, | ||
| 213 | actionHandler => sub { | ||
| 214 | my $self = shift; | ||
| 215 | my $args = shift; | ||
| 216 | my $id = $args->{selection}{song_id}; | ||
| 217 | die mkerror(4992,"You have to select a song first") | ||
| 218 | if not $id; | ||
| 219 | my $db = $self->user->db; | ||
| 220 | if ($db->deleteData('song',$id) == 1){ | ||
| 221 | return { | ||
| 222 | action => 'reload', | ||
| 223 | }; | ||
| 224 | } | ||
| 225 | die mkerror(4993,"Faild to remove song $id"); | ||
| 226 | return {}; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | ]; | ||
| 230 | }; | ||
| 231 | |||
| 232 | sub db { | ||
| 233 | shift->user->mojoSqlDb; | ||
| 234 | }; | ||
| 235 | |||
| 236 | sub _getFilter { | ||
| 237 | my $self = shift; | ||
| 238 | my $search = shift; | ||
| 239 | my $filter = ''; | ||
| 240 | if ( $search ){ | ||
| 241 | $filter = "WHERE song_title LIKE ".$self->db->dbh->quote('%'.$search); | ||
| 242 | } | ||
| 243 | return $filter; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub getTableRowCount { | ||
| 247 | my $self = shift; | ||
| 248 | my $args = shift; | ||
| 249 | my $filter = $self->_getFilter($args->{formData}{song_title}); | ||
| 250 | return $self->db->query("SELECT count(song_id) AS count FROM song $filter")->hash->{count}; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub getTableData { | ||
| 254 | my $self = shift; | ||
| 255 | my $args = shift; | ||
| 256 | my $filter = $self->_getFilter($args->{formData}{song_title}); | ||
| 257 | my $SORT =''; | ||
| 258 | if ($args->{sortColumn}){ | ||
| 259 | $SORT = 'ORDER BY '.$self->dbh->quote_identifier($args->{sortColumn}); | ||
| 260 | $SORT .= $args->{sortDesc} ? ' DESC' : ' ASC'; | ||
| 261 | } | ||
| 262 | my $data = $self->db->query(<<"SQL",$args->{lastRow}-$args->{firstRow}+1,$args->{firstRow})->hashes; | ||
| 263 | SELECT * | ||
| 264 | FROM song | ||
| 265 | $filter | ||
| 266 | $SORT | ||
| 267 | LIMIT ? OFFSET ? | ||
| 268 | SQL | ||
| 269 | # this is just a silly example to show how to modify the action button properties | ||
| 270 | # based on the row selected | ||
| 271 | for my $row (@$data) { | ||
| 272 | my $ok = true; | ||
| 273 | if ($row->{song_note} =~ /protect/ and not $self->user->may('admin')){ | ||
| 274 | $ok = false; | ||
| 275 | } | ||
| 276 | $row->{_actionSet} = { | ||
| 277 | edit => { | ||
| 278 | enabled => $ok | ||
| 279 | }, | ||
| 280 | delete => { | ||
| 281 | enabled => $ok, | ||
| 282 | } | ||
| 283 | } | ||
| 284 | } | ||
| 285 | return $data; | ||
| 286 | } | ||
| 287 | |||
| 288 | 1; | ||
| 289 | __END__ |