| 1 | package WWW::Garden::Design::Controller::AutoComplete; | ||
| 2 | |||
| 3 | use Mojo::Base 'Mojolicious::Controller'; | ||
| 4 | |||
| 5 | use Moo; | ||
| 6 | |||
| 7 | our $VERSION = '0.97'; | ||
| 8 | |||
| 9 | # ----------------------------------------------- | ||
| 10 | |||
| 11 | sub display | ||
| 12 | { | ||
| 13 | my($self) = @_; | ||
| 14 | my($key) = $self -> param('term') || ''; # jQuery forces use of 'term'. | ||
| 15 | my($type) = $self -> param('type') || ''; | ||
| 16 | |||
| 17 | $self -> app -> log -> debug("AutoComplete.display(key: $key, type: $type)"); | ||
| 18 | |||
| 19 | my(%context) = | ||
| 20 | ( # Form field Table column Table name. | ||
| 21 | design_flower => ['*', 'flowers'], | ||
| 22 | design_feature => ['*', 'features'], | ||
| 23 | feature_name => ['name', 'features'], | ||
| 24 | garden_name => ['name', 'gardens'], | ||
| 25 | property_name => ['name', 'properties'], | ||
| 26 | ); | ||
| 27 | my(%want_single_item) = | ||
| 28 | ( | ||
| 29 | garden_name => 1, | ||
| 30 | property_name => 1, | ||
| 31 | ); | ||
| 32 | |||
| 33 | my($defaults) = $self -> app -> defaults; | ||
| 34 | |||
| 35 | # In the case of $type being 'design_flower', we're called from line 501 in homepage.html.ep, | ||
| 36 | # meaning we're on the Design garden tab. In this case we can't assume the string the user typed | ||
| 37 | # petains to just one column of the flower database, so we search these 3 columns in the | ||
| 38 | # 'flowers' table: scientific_name, common_name and aliases. | ||
| 39 | # Warning: This use '*' in %context above means the methods in Database.pm which search %context | ||
| 40 | # must skip it. See Database.autocomplete_item() and Database.autocomplete_list(). | ||
| 41 | # Likewise for 'design_feature'. | ||
| 42 | |||
| 43 | if ($type eq 'design_flower') | ||
| 44 | { | ||
| 45 | $self -> render(json => $$defaults{db} -> autocomplete_flower_list(uc $key) ); | ||
| 46 | } | ||
| 47 | elsif ($type eq 'design_feature') | ||
| 48 | { | ||
| 49 | $self -> render(json => $$defaults{db} -> autocomplete_feature_list(uc $key) ); | ||
| 50 | } | ||
| 51 | elsif ($want_single_item{$type}) | ||
| 52 | { | ||
| 53 | $self -> render(json => $$defaults{db} -> autocomplete_item(\%context, uc $key, $type) ); | ||
| 54 | } | ||
| 55 | else | ||
| 56 | { | ||
| 57 | $self -> render(json => $$defaults{db} -> autocomplete_list(\%context, uc $key, $type) ); | ||
| 58 | } | ||
| 59 | |||
| 60 | } # End of display. | ||
| 61 | |||
| 62 | # ----------------------------------------------- | ||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | =pod | ||
| 67 | |||
| 68 | =head1 NAME | ||
| 69 | |||
| 70 | WWW::Garden::Design::Controller::AutoComplete - Manage the 'flowers' database | ||
| 71 | |||
| 72 | =head1 Machine-Readable Change Log | ||
| 73 | |||
| 74 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 75 | |||
| 76 | =head1 Version Numbers | ||
| 77 | |||
| 78 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 79 | |||
| 80 | =head1 Repository | ||
| 81 | |||
| 82 | L<https://github.com/ronsavage/WWW-Garden-Design> | ||
| 83 | |||
| 84 | =head1 Support | ||
| 85 | |||
| 86 | Email the author, or log a bug on RT: | ||
| 87 | |||
| 88 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=WWW::Garden::Design>. | ||
| 89 | |||
| 90 | =head1 Author | ||
| 91 | |||
| 92 | L<WWW::Garden::Design> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2014. | ||
| 93 | |||
| 94 | My homepage: L<https://savage.net.au/>. | ||
| 95 | |||
| 96 | =head1 Copyright | ||
| 97 | |||
| 98 | Australian copyright (c) 2018, Ron Savage. | ||
| 99 | |||
| 100 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 101 | you can redistribute them and/or modify them under the terms of | ||
| 102 | The Perl License, a copy of which is available at: | ||
| 103 | http://dev.perl.org/licenses/ | ||
| 104 | |||
| 105 | =cut |