XTaTIK.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« XTaTIK/Controller/Search.pm ↑ Summary XTaTIK/Model/Cart.pm »
1 package XTaTIK;
2  
3 our $VERSION = '0.005003'; # VERSION
4  
5 1 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 0 ▶use XTaTIK::Model::Cart;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use XTaTIK::Model::Quotes;
9 use XTaTIK::Model::Products;
10 use XTaTIK::Model::Users;
11 use XTaTIK::Model::Blog;
12 use XTaTIK::Model::ProductSearch;
13 use XTaTIK::Model::XVars;
14 use File::Find::Rule;
15 use File::Basename 'dirname';
16 use File::Spec::Functions qw/catfile curdir catdir rel2abs/;
17 use Carp qw/croak/;
18 use HTML::Entities;
19 use Mojo::Pg;
20 use experimental qw/postderef/;
21 use lib;
22  
23 my $PG;
24  
25 sub startup {
26 my $self = shift;
27 $self->moniker('XTaTIK');
28 $self->home->parse(catdir(dirname(__FILE__), 'XTaTIK'));
29 $self->plugin('Config');
30 $self->static->paths->[0] = $self->home->rel_dir('public');
31 $self->renderer->paths->[0] = $self->home->rel_dir('templates');
32  
33 my @sass_path = split /:/, $ENV{SASS_PATH}//'';
34  
35 if ( $ENV{XTATIK_COMPANY} ) {
36 unshift @{ $self->renderer->paths },
37 catdir $ENV{XTATIK_COMPANY}, 'templates';
38  
39 unshift @{ $self->static->paths },
40 catdir $ENV{XTATIK_COMPANY}, 'public';
41  
42 unshift @sass_path,
43 catdir $ENV{XTATIK_COMPANY}, 'public', 'sass';
44  
45 lib->import( catdir $ENV{XTATIK_COMPANY}, 'lib' );
46 }
47  
48 unshift @sass_path,
49 catdir rel2abs(curdir),
50 qw/lib XTaTIK public sass fake-company/
51 unless $ENV{XTATIK_COMPANY}
52 and -r catfile $ENV{XTATIK_COMPANY},
53 qw/public sass bootstrap company-variables.scss/;
54  
55 my $silo_path = $ENV{XTATIK_SITE_ROOT}
56 // catfile 'silo', $self->config('site');
57  
58 $self->config('_silo_path', $silo_path);
59  
60 unshift @{ $self->renderer->paths },
61 catdir $silo_path, 'templates';
62  
63 unshift @{ $self->static->paths },
64 catdir $silo_path, 'public';
65  
66 unshift @sass_path,
67 catdir $silo_path, 'public', 'sass';
68  
69 lib->import( catdir $silo_path, 'lib' );
70  
71 unshift @sass_path,
72 catdir rel2abs(curdir), qw/lib XTaTIK public sass fake-site/
73 unless -r catfile $silo_path,
74 qw/public sass bootstrap site-variables.scss/;
75  
76 $ENV{SASS_PATH} = join ':', @sass_path;
77  
78 $self->log->debug('*** XTaTIK LOADED: ***');
79 $self->log->debug('Site: ' . $self->config('site') );
80 $self->log->debug("XTATIK_COMPANY: $ENV{XTATIK_COMPANY}");
81 $self->log->debug("XTATIK_SITE_ROOT: $ENV{XTATIK_SITE_ROOT}");
82 $self->log->debug("Silo path: $silo_path");
83 $self->log->debug("SASS path: $ENV{SASS_PATH}");
84 $self->log->debug("Database: "
85 . (($self->config('pg_url') =~ m{^(?:postgresql:///)?(\w+)}i)[0] //'')
86 );
87 $self->log->debug('**********************');
88  
89 $self->secrets([ $self->config('mojo_secrets') ]);
90  
91 $self->plugin('AntiSpamMailTo');
92 $self->plugin('FormChecker' => error_class => 'foo');
93 $self->plugin('IP2Location');
94 $self->_setup_bootstrap_assets;
95  
96 $self->asset(
97 'app.css' => qw{
98 /sass/reset.scss
99 /sass/bs-callout.scss
100 /sass/bootstrap-extras.scss
101 /sass/main.scss
102 },
103 (
104 sort map s{^\Q$silo_path\E[\\/]?public[\\/]}{}r,
105 File::Find::Rule->name('*.scss')
106 ->in( catdir $silo_path, qw/public sass user/),
107 ),
108 (
109 $ENV{XTATIK_COMPANY}
110 ? (
111 sort map s{^\Q$ENV{XTATIK_COMPANY}\E[\\/]?public[\\/]}{}r,
112 File::Find::Rule->name('*.css', '*.scss')
113 ->in( catdir $ENV{XTATIK_COMPANY}, qw/public sass user/ )
114 ) : ()
115 )
116 );
117  
118 $self->asset(
119 'app.js' => qw{
120 /JS/ie10-viewport-bug-workaround.js
121 /JS/main.js
122 },
123 (
124 map s{^\Q$silo_path\E[\\/]public[\\/]}{}r,
125 File::Find::Rule->name('*.js')
126 ->in( catfile($silo_path, 'public', 'JS') ),
127 ),
128 (
129 $ENV{XTATIK_COMPANY}
130 ? (
131 map s{^\Q$ENV{XTATIK_COMPANY}\E[\\/]public[\\/]}{}r,
132 File::Find::Rule->name('*.js')
133 ->in( catfile($ENV{XTATIK_COMPANY}, 'public', 'JS') )
134 ) : ()
135 )
136 );
137  
138 my $mconf = {
139 how => $self->config('mail')->{how},
140 howargs => $self->config('mail')->{howargs},
141 };
142 $self->plugin(mail => $mconf);
143  
144 # Initialize globals (this is probably a stupid way to do things)
145 $PG = Mojo::Pg->new( $self->config('pg_url') );
146  
147 $self->session( expiration => 60*60*24*7 );
148  
149 $self->helper( xtext => \&_helper_xtext );
150 $self->helper( xvar => \&_helper_xvar );
151 $self->helper( users => \&_helper_users );
152 $self->helper( products => \&_helper_products );
153 $self->helper( quotes => \&_helper_quotes );
154 $self->helper( cart => \&_helper_cart );
155 $self->helper( cart_dollars => \&_helper_cart_dollars );
156 $self->helper( cart_cents => \&_helper_cart_cents );
157 $self->helper( product_search => $self->_gen_helper_product_search );
158 $self->helper(
159 blog => sub {
160 state $blog = XTaTIK::Model::Blog->new(
161 blog_root => $ENV{XTATIK_BLOG_SRC}
162 // catfile $silo_path, 'blog_src'
163 );
164 }
165 );
166 $self->helper( active_page => sub {
167 my ( $c, $name ) = @_;
168 my $active = $c->stash('active_page') // '';
169 return $active eq $name ? ' class="active"' : '';
170 });
171 $self->helper( items_in => sub {
172 my ( $c, $what ) = @_;
173 return unless defined $what;
174 $what = $c->stash($what) // [] unless ref $what;
175 return @$what;
176 });
177  
178  
179 # use Acme::Dump::And::Dumper;
180 # die DnD [ grep -e catfile($_, 'content-pics', 'nav-logo.png'),
181 # @{ $self->static->paths } ];
182 $self->config('text')->{show_nav_logo}
183 //= $self->static->file('content-pics/nav-logo.png');
184  
185 my $r = $self->routes;
186 { # Root routes
187 $r->get('/' )->to('root#index' );
188 $r->get('/contact' )->to('root#contact' );
189 $r->get('/about' )->to('root#about' );
190 $r->get('/search' )->to('search#search' );
191 $r->get('/history' )->to('root#history' );
192 $r->get('/login' )->to('root#login' );
193 $r->post('/contact')->to('root#contact_post' );
194 $r->get('/feedback')->to('root#feedback' );
195 $r->get('/sitemap' )->to('root#sitemap' );
196 $r->get('/robots' )->to('root#robots' );
197 $r->post('/feedback')->to('root#feedback_post');
198 $r->get('/product/(*url)')->to('root#product');
199 $r->get('/products(*category)')
200 ->to('root#products_category', { category => '' });
201 $r->get('/privacy-policy')
202 ->to('root#privacy_policy');
203 $r->get('/terms-and-conditions')
204 ->to('root#terms_and_conditions');
205 }
206  
207 { # Cart routes
208 my $rc = $r->under('/cart');
209 $rc->get( '/' )->to('cart#index' );
210 $rc->post('/add' )->to('cart#add' );
211  
212 for my $plug ( $self->config('checkout_system')->@* ) {
213 my ( $name, $conf ) = ref $plug ? ( @$plug ) : ( $plug );
214  
215 my $handler = "XTaTIK::Plugin::Cart::$name";
216 eval "require $handler"
217 or die "Failed to load cart handler $handler: $@";
218  
219 my $h = $handler->new( $conf ? ( conf => $conf ) : () );
220 $h->_add_routes( $rc );
221 }
222 }
223  
224 unless ( $self->config('no_blog') ) {
225 { # Blog routes
226 my $rb = $r->under('/blog');
227 $rb->get('/' )->to('blog#index');
228 $rb->get('/*post')->to('blog#read');
229 }
230 }
231  
232 { # User section routes
233 $r->post('/login' )->to('user#login' );
234 $r->any( '/logout')->to('user#logout');
235  
236 my $ru = $r->under('/user')->to('user#is_logged_in');
237 $ru->get('/')->to('user#index')->name('user/index');
238 $ru->get('/site-products')->to('user#site_products');
239 $ru->post('/site-products')->to('user#site_products');
240 $ru->get('/master-products-database')
241 ->to('user#master_products_database')
242 ->name('user/master_products_database');
243 $ru->post('/master-products-database')
244 ->to('user#master_products_database_post');
245 $ru->post('/master-products-database/update')
246 ->to('user#master_products_database_update');
247 $ru->post('/master-products-database/delete')
248 ->to('user#master_products_database_delete');
249 $ru->get('/manage-users')->to('user#manage_users');
250 $ru->post('/manage-users/add')->to('user#add_user');
251 $ru->post('/manage-users/update')->to('user#update_users');
252 $ru->post('/manage-users/delete')->to('user#delete_users');
253 $ru->get('/hot-products')->to('user#hot_products');
254 $ru->post('/hot-products')->to('user#hot_products_post');
255 $ru->get('/quotes')->to('user#quotes_handler');
256 }
257 }
258  
259 # Registers the "bootstrap.css" and "bootstrap.js" AssetPack assets that our
260 # templates load via `%= asset "bootstrap.css"` / `%= asset "bootstrap.js"`.
261 #
262 # This used to be provided by Mojolicious::Plugin::Bootstrap3, which bundled
263 # Bootstrap 3's Sass sources, JavaScript, and jQuery and registered these two
264 # assets. That plugin is no longer available on CPAN, so we ship the same
265 # Bootstrap 3.3.5 sources under lib/XTaTIK/public/ and register the assets
266 # ourselves, faithfully mirroring what the plugin's register() did.
267 sub _setup_bootstrap_assets {
268 my $self = shift;
269  
270 # The plugin loaded AssetPack itself (unless already loaded); do the same,
271 # since our own $self->asset(...) calls below rely on it.
272 $self->plugin('AssetPack') unless eval { $self->asset; 1 };
273  
274 # Make our bundled Bootstrap Sass partials (lib/XTaTIK/public/sass/bootstrap/*)
275 # resolvable via @import "bootstrap/..." in our bootstrap.scss.
276 my $sass_dir = catdir dirname(__FILE__), qw/XTaTIK public sass/;
277 $ENV{SASS_PATH} = length($ENV{SASS_PATH}//'')
278 ? "$ENV{SASS_PATH}:$sass_dir" : $sass_dir;
279  
280 # Same asset topics and JavaScript file list the plugin registered by default
281 # (jQuery first, then the individual Bootstrap components).
282 $self->asset('bootstrap.css' => '/sass/bootstrap.scss');
283 $self->asset(
284 'bootstrap.js' =>
285 '/JS/jquery-1.11.0.min.js',
286 map "/JS/bootstrap/$_", qw/
287 transition.js alert.js button.js carousel.js
288 collapse.js dropdown.js modal.js tooltip.js
289 popover.js scrollspy.js tab.js affix.js
290 /,
291 );
292 }
293  
294 #### HELPERS
295  
296 sub _helper_xtext {
297 my ( $c, $var, $v ) = @_;
298 $c->config('text')->{ $var } = $v
299 if @_ == 3;
300  
301 return $c->config('text')->{ $var };
302 }
303  
304 sub _helper_xvar {
305 my ( undef, $var, $value ) = @_;
306 state $xvars = XTaTIK::Model::XVars->new(pg => $PG);
307  
308 if ( defined $value ) {
309 $xvars->set($var, $value);
310 }
311 else {
312 return $xvars->get($var);
313 }
314 };
315  
316 sub _helper_users {
317 state $users = XTaTIK::Model::Users->new(
318 pg => $PG,
319 );
320 };
321  
322 sub _gen_helper_product_search {
323 my $self = shift;
324  
325 # Create search dir and touch index files, unless we already have them
326 my $dir = catdir $self->config('_silo_path'), 'search_index';
327 unless ( -d $dir ) {
328 mkdir $dir
329 or croak "Failed to create search_index directory $dir: $!";
330 }
331  
332 for ( map catfile($dir, $_), qw/ixd.bdb ixp.bdb ixw.bdb/ ) {
333 next if -f and -r;
334 open my $fh, '>', $_
335 or croak "Failed to create search_index file $_: $!";
336 }
337  
338 return sub {
339 state $search = XTaTIK::Model::ProductSearch->new( dir => $dir );
340 };
341 }
342  
343 sub _helper_products {
344 my $c = shift;
345 state $products = XTaTIK::Model::Products->new(
346 pg => $PG,
347 custom_cat_sorting => $c->config('custom_cat_sorting'),
348 site => $c->config('site'),
349 );
350  
351 $products->pricing_region( $c->geoip_region );
352 return $products;
353 };
354  
355 sub _helper_quotes {
356 # my $c = shift;
357  
358 state $quotes = XTaTIK::Model::Quotes->new( pg => $PG );
359 return $quotes;
360 };
361  
362 sub _helper_cart {
363 my $c = shift;
364  
365 return $c->stash('__cart') if $c->stash('__cart');
366  
367 my $cart = XTaTIK::Model::Cart->new(
368 pg => $PG,
369 products => $c->products,
370 );
371  
372 if ( my $id = $c->session('cart_id') ) {
373 $cart->id( $id );
374 }
375 else {
376 $c->session( cart_id => $cart->new_cart );
377 }
378  
379 $cart->load;
380  
381 $c->stash( __cart => $cart );
382 return $cart;
383 };
384  
385 sub _helper_cart_dollars {
386 my $c = shift;
387 my $is_refresh = shift;
388 my $dollars = $is_refresh
389 ? $c->cart->dollars
390 : $c->session('cart_dollars') // $c->cart->dollars;
391 $c->session( cart_dollars => $dollars );
392 return $dollars;
393 };
394  
395 sub _helper_cart_cents {
396 my $c = shift;
397 my $is_refresh = shift;
398 my $cents = $is_refresh
399 ? $c->cart->cents
400 : $c->session('cart_cents') // $c->cart->cents;
401 $c->session( cart_cents => $cents);
402 return $cents;
403 };
404  
405 1;
406  
407 __END__
« XTaTIK/Controller/Search.pm ↑ Summary XTaTIK/Model/Cart.pm »