| 1 | # ========================================================================== # | ||
| 2 | # lib/JPList/Request.pm - JPList Request parser module | ||
| 3 | # Copyright (C) 2017 Exceleron Software, LLC | ||
| 4 | # ========================================================================== # | ||
| 5 | |||
| 6 | package JPList::Request; | ||
| 7 | |||
| 8 | use Moose; | ||
| 9 | use URI::Escape; | ||
| 10 | use JSON; | ||
| 11 | |||
| 12 | with 'JPList::Controls::Filter'; | ||
| 13 | with 'JPList::Controls::Sort'; | ||
| 14 | |||
| 15 | # ========================================================================== # | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | JPList::Request - JPList Request parser module | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use JPList::Request; | ||
| 24 | my $jplist_req = JPList::Request->new(request_params => $self->request_params); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | The JPList::Request module allows you to decode the request params with all the controls | ||
| 29 | |||
| 30 | =head2 ATTRIBUTES | ||
| 31 | |||
| 32 | =over 4 | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | # ========================================================================== # | ||
| 37 | |||
| 38 | has 'request_params' => ( | ||
| 39 | is => 'rw', | ||
| 40 | isa => 'Str' | ||
| 41 | ); | ||
| 42 | |||
| 43 | has 'request_data' => ( | ||
| 44 | is => 'rw', | ||
| 45 | builder => '_decode_request_params', | ||
| 46 | lazy => 1, | ||
| 47 | clearer => 'clear_request_data' | ||
| 48 | ); | ||
| 49 | |||
| 50 | has 'filter_attrs' => ( | ||
| 51 | is => 'rw', | ||
| 52 | isa => 'HashRef' | ||
| 53 | ); | ||
| 54 | |||
| 55 | has 'filter_data' => ( | ||
| 56 | is => 'rw', | ||
| 57 | isa => 'ArrayRef' | ||
| 58 | ); | ||
| 59 | |||
| 60 | has 'sort_data' => ( | ||
| 61 | is => 'rw', | ||
| 62 | isa => 'ArrayRef' | ||
| 63 | ); | ||
| 64 | |||
| 65 | has 'pagination_data' => ( | ||
| 66 | is => 'rw', | ||
| 67 | isa => 'HashRef' | ||
| 68 | ); | ||
| 69 | |||
| 70 | has 'is_download' => ( | ||
| 71 | is => 'rw', | ||
| 72 | default => 0 | ||
| 73 | ); | ||
| 74 | |||
| 75 | # ========================================================================== # | ||
| 76 | |||
| 77 | =back | ||
| 78 | |||
| 79 | =head2 METHODS | ||
| 80 | |||
| 81 | =over 4 | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | # ========================================================================== # | ||
| 86 | |||
| 87 | =item C<decode_data> | ||
| 88 | |||
| 89 | Params : $statuses_request | ||
| 90 | |||
| 91 | Returns: NONE | ||
| 92 | |||
| 93 | Desc : decode the request params | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub decode_data | ||
| 98 | { | ||
| 99 | my ($self) = @_; | ||
| 100 | |||
| 101 | if (ref($self->request_data) eq 'ARRAY') { | ||
| 102 | |||
| 103 | $self->_store_status_list($self->request_data); | ||
| 104 | |||
| 105 | } | ||
| 106 | else { | ||
| 107 | |||
| 108 | return undef; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | # ========================================================================== # | ||
| 113 | |||
| 114 | =item C<_decode_request_params> | ||
| 115 | |||
| 116 | Params : $self->request_params | ||
| 117 | |||
| 118 | Returns: Data structure of formatted request data structure | ||
| 119 | |||
| 120 | Desc : decode params | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub _decode_request_params | ||
| 125 | { | ||
| 126 | |||
| 127 | my $self = shift; | ||
| 128 | |||
| 129 | return decode_json(uri_unescape($self->request_params)); | ||
| 130 | } | ||
| 131 | |||
| 132 | # ========================================================================== # | ||
| 133 | |||
| 134 | =item C<_store_status_list> | ||
| 135 | |||
| 136 | Params : Request Data | ||
| 137 | |||
| 138 | Returns: NONE | ||
| 139 | |||
| 140 | Desc : parses the list of unique actions and computes filter, sort and pagination data | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub _store_status_list | ||
| 145 | { | ||
| 146 | my ($self, $statuses_request) = @_; | ||
| 147 | |||
| 148 | foreach my $status_req (@$statuses_request) { | ||
| 149 | my $action = $status_req->{'action'}; | ||
| 150 | push(@{$self->{'status_list'}->{$action}}, $status_req); | ||
| 151 | } | ||
| 152 | |||
| 153 | $self->_get_filter_data(); | ||
| 154 | |||
| 155 | $self->_get_sort_data(); | ||
| 156 | |||
| 157 | $self->_get_pagination_data(); | ||
| 158 | |||
| 159 | ## Clear unwanted attributes | ||
| 160 | $self->clear_request_data(); | ||
| 161 | delete($self->{'status_list'}); | ||
| 162 | } | ||
| 163 | |||
| 164 | # ========================================================================== # | ||
| 165 | |||
| 166 | =item C<_get_filter_data> | ||
| 167 | |||
| 168 | Params : NONE | ||
| 169 | |||
| 170 | Returns: NONE | ||
| 171 | |||
| 172 | Desc : private function to parse filter data | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub _get_filter_data | ||
| 177 | { | ||
| 178 | my ($self) = @_; | ||
| 179 | |||
| 180 | my @filter_data; | ||
| 181 | |||
| 182 | # This is used to store the values of filter by column name | ||
| 183 | # Eg: $jplist->jplist_request->filter_attrs->{'ServiceType'} | ||
| 184 | my %filter_attrs; | ||
| 185 | |||
| 186 | foreach my $filter_vals (@{$self->{'status_list'}->{'filter'}}) { | ||
| 187 | if ($filter_vals->{'type'} eq 'textbox') { | ||
| 188 | my $filter_result = $self->textbox($filter_vals); | ||
| 189 | if (defined $filter_result) { | ||
| 190 | push(@filter_data, $filter_result); | ||
| 191 | $filter_attrs{$filter_result->{'column'}} = $filter_result->{'value'}; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | elsif ($filter_vals->{'type'} eq 'filter-drop-down') { | ||
| 195 | my $filter_result = $self->filterdropdown($filter_vals); | ||
| 196 | if (defined $filter_result) { | ||
| 197 | push(@filter_data, $filter_result); | ||
| 198 | $filter_attrs{$filter_result->{'column'}} = $filter_result->{'value'}; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | elsif ($filter_vals->{'type'} eq 'filter-select') { | ||
| 202 | my $filter_result = $self->filterselect($filter_vals); | ||
| 203 | if (defined $filter_result) { | ||
| 204 | push(@filter_data, $filter_result); | ||
| 205 | $filter_attrs{$filter_result->{'column'}} = $filter_result->{'value'}; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | elsif ($filter_vals->{'type'} eq 'date-picker-range-filter') { | ||
| 209 | my $filter_result = $self->filterdaterange($filter_vals); | ||
| 210 | if (defined $filter_result) { | ||
| 211 | push(@filter_data, $filter_result); | ||
| 212 | $filter_attrs{$filter_result->{'column'}} = $filter_result; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | elsif ($filter_vals->{'type'} eq 'date-picker-filter') { | ||
| 216 | my $filter_result = $self->filterdatepicker($filter_vals); | ||
| 217 | if (defined $filter_result) { | ||
| 218 | push(@filter_data, $filter_result); | ||
| 219 | $filter_attrs{$filter_result->{'column'}} = $filter_result->{'value'}; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | elsif ($filter_vals->{'type'} eq 'checkbox-group-filter') { | ||
| 223 | my $filter_result = $self->checkboxgroup($filter_vals); | ||
| 224 | if (defined $filter_result) { | ||
| 225 | push(@filter_data, $filter_result); | ||
| 226 | $filter_attrs{$filter_result->{'column'}} = $filter_result->{'values'}; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | elsif ($filter_vals->{'type'} eq 'button-filter' and $filter_vals->{'name'} eq 'download') { | ||
| 230 | print STDERR "[jplist info] download request filter called\n"; | ||
| 231 | my $data = $filter_vals->{'data'}; | ||
| 232 | if ( exists($data->{'filterType'}) | ||
| 233 | and ($data->{'filterType'} eq 'path') | ||
| 234 | and exists($data->{'path'}) | ||
| 235 | and ($data->{'path'} eq '.download')) | ||
| 236 | { | ||
| 237 | $self->is_download(1); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | $self->filter_data(\@filter_data); | ||
| 243 | $self->filter_attrs(\%filter_attrs); | ||
| 244 | } | ||
| 245 | |||
| 246 | # ========================================================================== # | ||
| 247 | |||
| 248 | =item C<_get_sort_data> | ||
| 249 | |||
| 250 | Params : NONE | ||
| 251 | |||
| 252 | Returns: NONE | ||
| 253 | |||
| 254 | Desc : private functiont to parse sort data | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | sub _get_sort_data | ||
| 259 | { | ||
| 260 | my ($self) = @_; | ||
| 261 | |||
| 262 | my @sort_data; | ||
| 263 | |||
| 264 | foreach my $sort_vals (@{$self->{'status_list'}->{'sort'}}) { | ||
| 265 | if ($sort_vals->{'type'} eq 'sort-drop-down') { | ||
| 266 | my $sort_result = $self->sortdropdown($sort_vals); | ||
| 267 | push(@sort_data, $sort_result); | ||
| 268 | } | ||
| 269 | elsif ($sort_vals->{'type'} eq 'sort-select') { | ||
| 270 | my $sort_result = $self->sortselect($sort_vals); | ||
| 271 | push(@sort_data, $sort_result); | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | $self->sort_data(\@sort_data); | ||
| 276 | } | ||
| 277 | |||
| 278 | # ========================================================================== # | ||
| 279 | |||
| 280 | =item C<_get_pagination_data> | ||
| 281 | |||
| 282 | Params : NONE | ||
| 283 | |||
| 284 | Returns: NONE | ||
| 285 | |||
| 286 | Desc : private functiont to parse pagination data | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub _get_pagination_data | ||
| 291 | { | ||
| 292 | my ($self) = @_; | ||
| 293 | |||
| 294 | my $pagination_data; | ||
| 295 | |||
| 296 | foreach my $paging_vals (@{$self->{'status_list'}->{'paging'}}) { | ||
| 297 | my $data = $paging_vals->{'data'}; | ||
| 298 | |||
| 299 | if ($data) { | ||
| 300 | if ($data->{'currentPage'}) { | ||
| 301 | $pagination_data->{'currentPage'} = $data->{'currentPage'}; | ||
| 302 | } | ||
| 303 | |||
| 304 | if ($data->{'number'}) { | ||
| 305 | $pagination_data->{'number'} = $data->{'number'}; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 | $self->pagination_data($pagination_data); | ||
| 311 | } | ||
| 312 | |||
| 313 | 1; | ||
| 314 | |||
| 315 | __END__ | ||
| 316 | |||
| 317 | =back | ||
| 318 | |||
| 319 | =head1 AUTHORS | ||
| 320 | |||
| 321 | Sheeju Alex, <sheeju@exceleron.com> | ||
| 322 | |||
| 323 | =head1 BUGS | ||
| 324 | |||
| 325 | https://github.com/sheeju/JPList/issues | ||
| 326 | |||
| 327 | =head1 SUPPORT | ||
| 328 | |||
| 329 | You can find documentation for this module with the perldoc command. | ||
| 330 | |||
| 331 | perldoc JPList | ||
| 332 | |||
| 333 | |||
| 334 | You can also look for information at: | ||
| 335 | |||
| 336 | =over 4 | ||
| 337 | |||
| 338 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 339 | |||
| 340 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=JPList> | ||
| 341 | |||
| 342 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 343 | |||
| 344 | L<http://annocpan.org/dist/JPList> | ||
| 345 | |||
| 346 | =item * CPAN Ratings | ||
| 347 | |||
| 348 | L<http://cpanratings.perl.org/d/JPList> | ||
| 349 | |||
| 350 | =item * Search CPAN | ||
| 351 | |||
| 352 | L<http://search.cpan.org/dist/JPList/> | ||
| 353 | |||
| 354 | =back | ||
| 355 | |||
| 356 | =head1 ACKNOWLEDGEMENTS | ||
| 357 | |||
| 358 | Development time supported by Exceleron L<www.exceleron.com|http://www.exceleron.com>. | ||
| 359 | |||
| 360 | =head1 LICENSE AND COPYRIGHT | ||
| 361 | |||
| 362 | Copyright (C) 2017 Exceleron Software, LLC | ||
| 363 | |||
| 364 | This program is free software; you can redistribute it and/or modify it | ||
| 365 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 366 | copy of the full license at: | ||
| 367 | |||
| 368 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 369 | |||
| 370 | Any use, modification, and distribution of the Standard or Modified | ||
| 371 | Versions is governed by this Artistic License. By using, modifying or | ||
| 372 | distributing the Package, you accept this license. Do not use, modify, | ||
| 373 | or distribute the Package, if you do not accept this license. | ||
| 374 | |||
| 375 | If your Modified Version has been derived from a Modified Version made | ||
| 376 | by someone other than you, you are nevertheless required to ensure that | ||
| 377 | your Modified Version complies with the requirements of this license. | ||
| 378 | |||
| 379 | This license does not grant you the right to use any trademark, service | ||
| 380 | mark, tradename, or logo of the Copyright Holder. | ||
| 381 | |||
| 382 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 383 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 384 | otherwise transfer the Package with respect to any patent claims | ||
| 385 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 386 | Package. If you institute patent litigation (including a cross-claim or | ||
| 387 | counterclaim) against any party alleging that the Package constitutes | ||
| 388 | direct or contributory patent infringement, then this Artistic License | ||
| 389 | to you shall terminate on the date that such litigation is filed. | ||
| 390 | |||
| 391 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 392 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 393 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 394 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 395 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 396 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 397 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 398 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 399 | |||
| 400 | =cut | ||
| 401 | |||
| 402 | # vim: ts=4 | ||
| 403 | # vim600: fdm=marker fdl=0 fdc=3 |