Design.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Design/Import/SQLite.pm ↑ Summary Design/Database/Pg.pm »
1 package WWW::Garden::Design;
2  
3 1 ▶use utf8; # See $$defaults{joiner} below.
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Data::Dumper::Concise; # For Dumper().
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use WWW::Garden::Design::Database::Pg;
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 our $VERSION = '0.97';
14  
15 # -----------------------------------------------
16  
17 sub build_attribute_ids
18 {
19 my($self, $kind, $attribute_type_fields, $attribute_type_names) = @_;
20  
21 my($id);
22 my($name);
23  
24 return
25 [
26 map
27 {
28 $name = $$attribute_type_names[$_];
29 [
30 map
31 {
32 $id = "${kind}_${name}_$_" =~ s/ /_/gr;
33 $id =~ s/-/_/g;
34  
35 $id;
36 } @{$$attribute_type_fields{$name} }
37 ]
38 } 0 .. $#$attribute_type_names
39 ];
40  
41 } # End of build_attribute_ids.
42  
43 # -----------------------------------------------
44  
45 sub build_attribute_type_fields
46 {
47 my($self, $attribute_types_table) = @_;
48  
49 my(%attribute_type_fields);
50  
51 for (sort{$$a{sequence} <=> $$b{sequence} } @$attribute_types_table)
52 {
53 $attribute_type_fields{$$_{name} } = [split(/\s*,\s+/, $$_{range})];
54 }
55  
56 return \%attribute_type_fields;
57  
58 } # End of build_attribute_type_fields.
59  
60 # -----------------------------------------------
61  
62 sub build_attribute_type_names
63 {
64 my($self, $attribute_types_table) = @_;
65  
66 my(@fields);
67 my($name);
68  
69 return [map{$$_{name} } sort{$$a{sequence} <=> $$b{sequence} } @$attribute_types_table];
70  
71 } # End of build_attribute_type_names.
72  
73 # -----------------------------------------------
74 # attributes['Native'] = ['No', 'Yes', 'Unknown'];
75 # attributes['Sun tolerance'] = ['Full_sun', 'Part_shade', 'Shade', 'Unknown'];
76  
77 sub build_js_for_attributes
78 {
79 my($self, $type_names, $type_fields) = @_;
80 my($attribute_elements) = "\tvar attributes = new Object;\n\n";
81  
82 my($temp_name);
83  
84 for my $type_name (sort @$type_names)
85 {
86 $temp_name = $type_name =~ s/ /_/gr;
87 $attribute_elements .= "\tattributes['$temp_name'] = ["
88 . join(', ', map{"'$_'"} @{$$type_fields{$type_name} })
89 . "];\n";
90 }
91  
92 return $attribute_elements;
93  
94 } # End of build_js_for_attributes.
95  
96 # ------------------------------------------------
97  
98 sub initialize_defaults
99 {
100 my($self) = @_;
101 my($defaults) = {};
102 $$defaults{db} = WWW::Garden::Design::Database::Pg -> new(logger => $self -> app -> log);
103 $$defaults{constants_table} = $$defaults{db} -> read_constants_table; # Warning: Not read_table('constants').
104 $$defaults{attributes_table} = $$defaults{db} -> read_table('attributes');
105 $$defaults{attribute_types_table} = $$defaults{db} -> read_table('attribute_types');
106 $$defaults{attribute_type_names} = $self -> build_attribute_type_names($$defaults{attribute_types_table});
107 $$defaults{attribute_type_fields} = $self -> build_attribute_type_fields($$defaults{attribute_types_table});
108 $$defaults{attribute_attribute_ids} = $self -> build_attribute_ids('attribute', $$defaults{attribute_type_fields}, $$defaults{attribute_type_names});
109 $$defaults{attribute_elements} = $self -> build_js_for_attributes($$defaults{attribute_type_names}, $$defaults{attribute_type_fields});
110 $$defaults{features_table} = $$defaults{db} -> read_features_table; # Warning: Not read_table('features').
111 $$defaults{gardens_table} = $$defaults{db} -> read_gardens_table; # Warning: Not read_table('gardens').
112 $$defaults{joiner} = '«»'; # Used in homepage.html.ep.
113 $$defaults{properties_table} = $$defaults{db} -> read_table('properties');
114 $$defaults{search_attribute_ids} = $self -> build_attribute_ids('search', $$defaults{attribute_type_fields}, $$defaults{attribute_type_names});
115 my($attribute_elements) = $self -> build_js_for_attributes($$defaults{attribute_type_names}, $$defaults{attribute_type_fields});
116  
117 # Warning: build_gardens_property_menu() sets a value in the session read by build_garden_menu(),
118 # so it must be called first.
119 #
120 # A note on the property menus on the Gardens tab:
121 # o gardens_property_menu_1 => Properties which have gardens.
122 # o gardens_property_menu_2 => All properties, since any property is allowed to have gardens added.
123 # Also, when a new property is added, only the latter menu gets updated (on the Gardens page).
124 # And when a garden is added, the gardens_garden_menu menu is updated (on the Gardens page),
125 # as well as the garden menu on the Design page, i.e. the design_garden_menu.
126  
127 $$defaults{properties_property_menu} = $$defaults{db} -> build_properties_property_menu($$defaults{properties_table}, 'properties_property_menu', 0);
128 $$defaults{design_property_menu} = $$defaults{db} -> build_gardens_property_menu($self, $$defaults{gardens_table}, 'design_property_menu', 0);
129 $$defaults{design_garden_menu} = $$defaults{db} -> build_garden_menu($self, $$defaults{gardens_table}, 'design_garden_menu');
130 $$defaults{feature_menu} = $$defaults{db} -> build_feature_menu($$defaults{features_table}, 0);
131 $$defaults{gardens_property_menu_1} = $$defaults{db} -> build_gardens_property_menu($self, $$defaults{gardens_table}, 'gardens_property_menu_1', 0);
132 $$defaults{gardens_property_menu_2} = $$defaults{db} -> build_properties_property_menu($$defaults{properties_table}, 'gardens_property_menu_2', 0);
133 $$defaults{gardens_garden_menu} = $$defaults{db} -> build_garden_menu($self, $$defaults{gardens_table}, 'gardens_garden_menu');
134  
135 $self -> app -> defaults($defaults);
136  
137 # Find the id of the 1st property on the Properties tab.
138 # This is needed to initialize a JS variable of the same name,
139 # and the corresponding JS variable of the 2nd property menu on the Gardens tab.
140 # And we sort the properties because the property menu is sorted, and we default to the 1st item.
141  
142 @{$$defaults{properties_table} } = sort{$$a{name} cmp $$b{name} } @{$$defaults{properties_table} };
143 $$defaults{properties_current_property_id} = $$defaults{properties_table}[0]{id};
144  
145 # Find the id of the 1st garden on the Gardens tab. It is sorted in Database.read_gardens_table().
146  
147 $$defaults{gardens_current_garden_id} = $$defaults{gardens_table}[0]{id};
148 $$defaults{gardens_current_property_id_1} = $$defaults{gardens_table}[0]{property_id};
149  
150 # Find the id of the 1st feature on the Features tab.
151  
152 $$defaults{features_current_feature_id} = $$defaults{features_table}[0]{id};
153  
154 $self -> defaults($defaults);
155  
156 #$self -> app -> log -> debug("properties_current_property_id: $$defaults{properties_current_property_id}");
157 #$self -> app -> log -> debug("gardens_current_property_id_1: $$defaults{gardens_current_property_id_1}");
158 #$self -> app -> log -> debug("gardens_current_garden_id: $$defaults{gardens_current_garden_id}");
159 #$self -> app -> log -> debug("features_current_feature_id: $$defaults{features_current_feature_id}");
160  
161 } # End of initialize_defaults.
162  
163 # ------------------------------------------------
164 # This method will run once at server start.
165  
166 sub startup
167 {
168 my $self = shift;
169  
170 $self -> secrets(['757d76331dc264f21ae97b861189bd9d8aa74647']);
171  
172 # Log a special line to make the start of each request easy to find in the log.
173 # Of course, nothing is logged by this just because the server restarted.
174  
175 $self -> hook
176 (
177 before_dispatch =>
178 sub
179 {
180 $self -> app -> log -> info('-' x 30);
181 }
182 );
183  
184 # Documentation browser under '/perldoc'.
185  
186 $self -> plugin('ServerStatus' =>
187 {
188 allow => ['127.0.0.1'],
189 counterfile => '/tmp/mojolicious/counter.flowers.txt',
190 path => '/server-status',
191 scoreboard => '/tmp/mojolicious',
192 });
193 $self -> plugin('TagHelpers');
194 $self -> initialize_defaults; # For access inside all controllers.
195  
196 # Router.
197  
198 my($r) = $self -> routes;
199  
200 $r -> namespaces(['WWW::Garden::Design::Controller']);
201  
202 $r -> route('/') -> to('Initialize#homepage');
203 $r -> route('/AutoComplete') -> to('AutoComplete#display');
204 $r -> route('/Design') -> to('Design#process');
205 $r -> route('/Feature') -> to('Feature#process');
206 $r -> route('/Flower') -> to('Flower#process');
207 $r -> route('/Garden') -> to('Garden#process');
208 $r -> route('/GetFlowerDetails') -> to('GetFlowerDetails#display');
209 $r -> route('/GetTable/attribute_types') -> to('GetTable#attribute_types');
210 $r -> route('/GetTable/design_flower') -> to('GetTable#design_flower');
211 $r -> route('/GetTable/design_feature') -> to('GetTable#design_feature');
212 $r -> route('/GetTable/gardens') -> to('GetTable#gardens');
213 $r -> route('/GetTable/features') -> to('GetTable#features');
214 $r -> route('/GetTable/properties') -> to('GetTable#properties');
215 $r -> route('/Property') -> to('Property#process');
216 $r -> route('/Report/activity') -> to('Report#activity');
217 $r -> route('/Report/crosscheck') -> to('Report#crosscheck');
218 $r -> route('/Search') -> to('Search#display');
219  
220 } # End of startup.
221  
222 # ------------------------------------------------
223  
224 1;
225  
226 =pod
227  
228 =encoding utf8
229  
230 =head1 NAME
231  
232 WWW::Garden::Design - Flower Database, Search Engine and Garden Design
233  
234 =head1 Synopsis
235  
236 The Search Engine is started by the Mojolicious command scripts/start.sh:
237  
238 #!/bin/bash
239  
240 cp /dev/null log/development.log
241  
242 scripts/flowers daemon -clients 2 -listen http://*:3008 &
243  
244 Which runs scripts/hypnotoad.pl:
245  
246 #!/usr/bin/env perl
247  
248 use strict;
249 use warnings;
250  
251 use FindBin;
252 BEGIN { unshift @INC, "$FindBin::Bin/../lib" }
253  
254 # Start command line interface for application
255 require Mojolicious::Commands;
256 Mojolicious::Commands->start_app('WWW::Garden::Design');
257  
258 =head1 Description
259  
260 An L<article|https://savage.net.au/Flowers/html/Garden.Design.Software.html> about the package is
261 on-line.
262  
263 C<WWW::Garden::Design> provides:
264  
265 =over 4
266  
267 =item o A Flower Database stored in CSV files
268  
269 These are used for bootstrapping the system into the SQLite database (below).
270  
271 =item o An Import Package
272  
273 This reads the CSV files and populates the Flower Database.
274  
275 =item o A Flower Database managed by SQLite
276  
277 For testing, a copy ships in data/flowers.sqlite.
278  
279 =item o An Export Package
280  
281 There are a number of simple Perl scripts involved, and some bash scripts to tie them together.
282  
283 They generate:
284  
285 =over 4
286  
287 =item o A web page for every flower
288  
289 These are pointed to be clickable thumbnails on the 'Flower Catalog' (next) and the 'Garden Layouts'
290 (below).
291  
292 These pages consist of a set of details per flower:
293  
294 =over 4
295  
296 =item o Scientific name
297  
298 =item o Common name
299  
300 =item o Aliases
301  
302 =item o Attributes
303  
304 Details (so far) for: native, habit, edibleness and sub tolerance.
305  
306 =item o A set of images
307  
308 =item o A set of notes
309  
310 =item o A set of URLs
311  
312 =back
313  
314 =item o The 'Flower Catalog' as an HTML table
315  
316 This can be generated as a stand-alone page, or as a HTML table to be embedded in any web page.
317 Mine is L<online|https://savage.net.au/Flowers.html#the_flower_catalog>.
318  
319 Each row in the table displays:
320  
321 =over 4
322  
323 =item o A native (to Australia) flag (Yes or No)
324  
325 =item o The Scientific name
326  
327 =item o The Common name
328  
329 Actually, I've fiddled some of these to make flowers which have significantly different scientific
330 names end up on successive rows of the table. Likewise, flowers with very similar names are forced
331 to appear together in the table.
332  
333 =item o A list of the flower's aliases
334  
335 Search for 'pansy' to see a ridiculous list as a sample.
336  
337 =item o A clickable thumbnail
338  
339 Clicking opens up, in a new browser tab, a page dedicated to the flower whose thumbnail was clciked.
340  
341 =back
342  
343 =item o 'Garden Layouts' as SVG files
344  
345 One SVG file is created for each of your gardens.
346  
347 See my L<front garden layout|https://savage.net.au/Flowers/front.garden.layout.html> and
348 L<back garden layout|https://savage.net.au/Flowers/back.garden.layout.html>.
349  
350 =item o A set of updated CSV files
351  
352 For when you update the database either via the Search Engine or otherwise.
353  
354 =back
355  
356 =item o A Search Engine
357  
358 This is a L<Mojolicious>-based program.
359  
360 =back
361  
362 =head1 Distributions
363  
364 This module is available as a Unix-style distro (*.tgz).
365  
366 See L<https://savage.net.au/Perl-modules/html/installing-a-module.html>
367 for help on unpacking and installing distros.
368  
369 =head1 Installation
370  
371 Install C<WWW::Garden::Design> as you would any C<Perl> module:
372  
373 Run:
374  
375 cpanm WWW::Garden::Design
376  
377 or run:
378  
379 sudo cpan WWW::Garden::Design
380  
381 perl Makefile.PL
382 make (or dmake or nmake)
383 make test
384 make install
385  
386 =head1 References
387  
388 L<HumaneJS|http://wavded.github.com/humane-js/> - A simple, modern, browser notification system.
389  
390 =head1 Machine-Readable Change Log
391  
392 The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>.
393  
394 =head1 Version Numbers
395  
396 Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
397  
398 =head1 Repository
399  
400 L<https://github.com/ronsavage/WWW-Garden-Design>
401  
402 =head1 Support
403  
404 Email the author, or log a bug on RT:
405  
406 L<https://rt.cpan.org/Public/Dist/Display.html?Name=WWW::Garden::Design>.
407  
408 =head1 Author
409  
410 L<WWW::Garden::Design> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2014.
411  
412 My homepage: L<https://savage.net.au/>.
413  
414 =head1 Copyright
415  
416 Australian copyright (c) 2018, Ron Savage.
417  
418 All Programs of mine are 'OSI Certified Open Source Software';
419 you can redistribute them and/or modify them under the terms of
420 The Perl License, a copy of which is available at:
421 http://dev.perl.org/licenses/
422  
423 =cut
« Design/Import/SQLite.pm ↑ Summary Design/Database/Pg.pm »