| 1 | package WWW::Garden::Design::Controller::Search; | ||
| 2 | |||
| 3 | use Mojo::Base 'Mojolicious::Controller'; | ||
| 4 | |||
| 5 | use boolean; | ||
| 6 | |||
| 7 | use Moo; | ||
| 8 | |||
| 9 | our $VERSION = '0.97'; | ||
| 10 | |||
| 11 | # ----------------------------------------------- | ||
| 12 | |||
| 13 | sub display | ||
| 14 | { | ||
| 15 | my($self) = @_; | ||
| 16 | my($defaults) = $self -> app -> defaults; | ||
| 17 | my($db) = $$defaults{db}; | ||
| 18 | my($csrf_token) = $self -> param('csrf_token') || ''; | ||
| 19 | my($search_text) = $db -> trim($self -> param('search_text') || ''); | ||
| 20 | my($ids) = $$defaults{search_attribute_ids}; | ||
| 21 | my(%search_attributes) = map{($_ => ($self -> param($_) || '') )} map{@$_} @$ids; | ||
| 22 | my($search_attributes) = join('', values %search_attributes); | ||
| 23 | my($must_have) = ( (length($search_text) > 0) || ($search_attributes =~ /true/) ) ? true : false; | ||
| 24 | |||
| 25 | if ($must_have -> isTrue) | ||
| 26 | { | ||
| 27 | my($constants_table) = $$defaults{constants_table}; | ||
| 28 | my($search_result, $request) = $db -> search($defaults, $constants_table, \%search_attributes, $search_text); | ||
| 29 | my($match_count) = 0; | ||
| 30 | my($result_html) = ''; | ||
| 31 | |||
| 32 | my($message); | ||
| 33 | |||
| 34 | if ($$request{text_is_clean} -> isTrue) | ||
| 35 | { | ||
| 36 | ($match_count, $result_html) = $self -> format($constants_table, $db, $search_result); | ||
| 37 | } | ||
| 38 | else | ||
| 39 | { | ||
| 40 | $message = "The search text '$search_text' can't be used!"; | ||
| 41 | $result_html = ''; | ||
| 42 | |||
| 43 | $self -> app -> log -> error($message); | ||
| 44 | } | ||
| 45 | |||
| 46 | $self -> stash(elapsed_time => sprintf('%.2f', $$request{time_taken}) ); | ||
| 47 | $self -> stash(error => $message); | ||
| 48 | $self -> stash(match_count => $match_count); | ||
| 49 | $self -> stash(result_html => $result_html); | ||
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | my($message) = 'Please supply some search text or select some attributes'; | ||
| 54 | |||
| 55 | $self -> stash(elapsed_time => 0); | ||
| 56 | $self -> stash(error => $message); | ||
| 57 | $self -> stash(match_count => 0); | ||
| 58 | $self -> stash(result_html => undef); | ||
| 59 | $self -> app -> log -> error($message); | ||
| 60 | } | ||
| 61 | |||
| 62 | $self -> render; | ||
| 63 | |||
| 64 | } # End of display. | ||
| 65 | |||
| 66 | # ----------------------------------------------- | ||
| 67 | |||
| 68 | sub format | ||
| 69 | { | ||
| 70 | my($self, $constants_table, $db, $search_results) = @_; | ||
| 71 | my($count) = 0; | ||
| 72 | my($html) = ''; | ||
| 73 | |||
| 74 | my($attribute); | ||
| 75 | my($native); | ||
| 76 | |||
| 77 | for my $item (@$search_results) | ||
| 78 | { | ||
| 79 | $count++; | ||
| 80 | |||
| 81 | # Find which of the flower's attributes is 'Native'. | ||
| 82 | |||
| 83 | my($native) = 'N/A'; | ||
| 84 | |||
| 85 | for $attribute (@{$$item{attributes} }) | ||
| 86 | { | ||
| 87 | $native = $$attribute{range} if ($$attribute{name} eq 'Native'); | ||
| 88 | } | ||
| 89 | |||
| 90 | # Note: Every time you add a column, you must update: | ||
| 91 | # o templates/initialize/homepage.html.ep. | ||
| 92 | # o templates/search/display.html.ep. | ||
| 93 | |||
| 94 | $html .= <<EOS; | ||
| 95 | <tr> | ||
| 96 | <td>$count</td> | ||
| 97 | <td>$native</td> | ||
| 98 | <td>$$item{scientific_name}</td> | ||
| 99 | <td>$$item{common_name}</td> | ||
| 100 | <td>$$item{aliases}</td> | ||
| 101 | <td>$$item{hxw}</td> | ||
| 102 | <td>$$item{publish}</td> | ||
| 103 | <td> | ||
| 104 | <button class = 'button' onClick='populate_details($$item{id})'> | ||
| 105 | <img src = '$$item{thumbnail_url}' width = '$$constants_table{cell_width}' height = '$$constants_table{cell_height}'/> | ||
| 106 | </button> | ||
| 107 | </td> | ||
| 108 | </tr> | ||
| 109 | EOS | ||
| 110 | } | ||
| 111 | |||
| 112 | return ($count, $html); | ||
| 113 | |||
| 114 | } # End of format. | ||
| 115 | |||
| 116 | # ----------------------------------------------- | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | =pod | ||
| 121 | |||
| 122 | =head1 NAME | ||
| 123 | |||
| 124 | WWW::Garden::Design::Controller::Search - Manage the 'flowers' database | ||
| 125 | |||
| 126 | =head1 Machine-Readable Change Log | ||
| 127 | |||
| 128 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 129 | |||
| 130 | =head1 Version Numbers | ||
| 131 | |||
| 132 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 133 | |||
| 134 | =head1 Repository | ||
| 135 | |||
| 136 | L<https://github.com/ronsavage/WWW-Garden-Design> | ||
| 137 | |||
| 138 | =head1 Support | ||
| 139 | |||
| 140 | Email the author, or log a bug on RT: | ||
| 141 | |||
| 142 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=WWW::Garden::Design>. | ||
| 143 | |||
| 144 | =head1 Author | ||
| 145 | |||
| 146 | L<WWW::Garden::Design> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2014. | ||
| 147 | |||
| 148 | My homepage: L<https://savage.net.au/>. | ||
| 149 | |||
| 150 | =head1 Copyright | ||
| 151 | |||
| 152 | Australian copyright (c) 2018, Ron Savage. | ||
| 153 | |||
| 154 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 155 | you can redistribute them and/or modify them under the terms of | ||
| 156 | The Perl License, a copy of which is available at: | ||
| 157 | http://dev.perl.org/licenses/ | ||
| 158 | |||
| 159 | =cut |