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.004001'; # VERSION
4  
5 1 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶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 blasdsdasdsa->import;
48 }
49  
50 unshift @sass_path,
51 catdir rel2abs(curdir),
52 qw/lib XTaTIK public sass fake-company/
53 unless $ENV{XTATIK_COMPANY}
54 and -r catfile $ENV{XTATIK_COMPANY},
55 qw/public sass bootstrap company-variables.scss/;
56  
57 my $silo_path = $ENV{XTATIK_SITE_ROOT}
58 // catfile 'silo', $self->config('site');
59  
60 $self->config('_silo_path', $silo_path);
61  
62 unshift @{ $self->renderer->paths },
63 catdir $silo_path, 'templates';
64  
65 unshift @{ $self->static->paths },
66 catdir $silo_path, 'public';
67  
68 unshift @sass_path,
69 catdir $silo_path, 'public', 'sass';
70  
71 lib->import( catdir $silo_path, 'lib' );
72  
73 unshift @sass_path,
74 catdir rel2abs(curdir), qw/lib XTaTIK public sass fake-site/
75 unless -r catfile $silo_path,
76 qw/public sass bootstrap site-variables.scss/;
77  
78 $ENV{SASS_PATH} = join ':', @sass_path;
79  
80 $self->log->debug('*** XTaTIK LOADED: ***');
81 $self->log->debug('Site: ' . $self->config('site') );
82 $self->log->debug("XTATIK_COMPANY: $ENV{XTATIK_COMPANY}");
83 $self->log->debug("XTATIK_SITE_ROOT: $ENV{XTATIK_SITE_ROOT}");
84 $self->log->debug("Silo path: $silo_path");
85 $self->log->debug("SASS path: $ENV{SASS_PATH}");
86 $self->log->debug("Database: "
87 . (($self->config('pg_url') =~ m{^(?:postgresql:///)?(\w+)}i)[0] //'')
88 );
89 $self->log->debug('**********************');
90  
91 $self->secrets([ $self->config('mojo_secrets') ]);
92  
93 $self->config( hypnotoad => {listen => ['http://*:3005'], proxy => 1} );
94  
95 $self->plugin('AntiSpamMailTo');
96 $self->plugin('FormChecker' => error_class => 'foo');
97 $self->plugin('IP2Location');
98 $self->plugin('bootstrap3');
99  
100 $self->asset(
101 'app.css' => qw{
102 /sass/reset.scss
103 /sass/bs-callout.scss
104 /sass/bootstrap-extras.scss
105 /sass/main.scss
106 },
107 (
108 sort map s{^\Q$silo_path\E[\\/]?public[\\/]}{}r,
109 File::Find::Rule->name('*.scss')
110 ->in( catdir $silo_path, qw/public sass user/),
111 ),
112 (
113 $ENV{XTATIK_COMPANY}
114 ? (
115 sort map s{^\Q$ENV{XTATIK_COMPANY}\E[\\/]?public[\\/]}{}r,
116 File::Find::Rule->name('*.css', '*.scss')
117 ->in( catdir $ENV{XTATIK_COMPANY}, qw/public sass user/ )
118 ) : ()
119 )
120 );
121  
122 $self->asset(
123 'app.js' => qw{
124 /JS/ie10-viewport-bug-workaround.js
125 /JS/main.js
126 },
127 (
128 map s{^\Q$silo_path\E[\\/]public[\\/]}{}r,
129 File::Find::Rule->name('*.js')
130 ->in( catfile($silo_path, 'public', 'JS') ),
131 ),
132 (
133 $ENV{XTATIK_COMPANY}
134 ? (
135 map s{^\Q$ENV{XTATIK_COMPANY}\E[\\/]public[\\/]}{}r,
136 File::Find::Rule->name('*.js')
137 ->in( catfile($ENV{XTATIK_COMPANY}, 'public', 'JS') )
138 ) : ()
139 )
140 );
141  
142 my $mconf = {
143 how => $self->config('mail')->{how},
144 howargs => $self->config('mail')->{howargs},
145 };
146 $self->plugin(mail => $mconf);
147  
148 # Initialize globals (this is probably a stupid way to do things)
149 $PG = Mojo::Pg->new( $self->config('pg_url') );
150  
151 $self->session( expiration => 60*60*24*7 );
152  
153 $self->helper( xtext => \&_helper_xtext );
154 $self->helper( xvar => \&_helper_xvar );
155 $self->helper( users => \&_helper_users );
156 $self->helper( products => \&_helper_products );
157 $self->helper( quotes => \&_helper_quotes );
158 $self->helper( cart => \&_helper_cart );
159 $self->helper( cart_dollars => \&_helper_cart_dollars );
160 $self->helper( cart_cents => \&_helper_cart_cents );
161 $self->helper( product_search => $self->_gen_helper_product_search );
162 $self->helper(
163 blog => sub {
164 state $blog = XTaTIK::Model::Blog->new(
165 blog_root => $ENV{XTATIK_BLOG_SRC}
166 // catfile $silo_path, 'blog_src'
167 );
168 }
169 );
170 $self->helper( active_page => sub {
171 my ( $c, $name ) = @_;
172 my $active = $c->stash('active_page') // '';
173 return $active eq $name ? ' class="active"' : '';
174 });
175 $self->helper( items_in => sub {
176 my ( $c, $what ) = @_;
177 return unless defined $what;
178 $what = $c->stash($what) // [] unless ref $what;
179 return @$what;
180 });
181  
182  
183 # use Acme::Dump::And::Dumper;
184 # die DnD [ grep -e catfile($_, 'content-pics', 'nav-logo.png'),
185 # @{ $self->static->paths } ];
186 $self->config('text')->{show_nav_logo}
187 //= $self->static->file('content-pics/nav-logo.png');
188  
189 my $r = $self->routes;
190 { # Root routes
191 $r->get('/' )->to('root#index' );
192 $r->get('/contact' )->to('root#contact' );
193 $r->get('/about' )->to('root#about' );
194 $r->get('/search' )->to('search#search' );
195 $r->get('/history' )->to('root#history' );
196 $r->get('/login' )->to('root#login' );
197 $r->post('/contact')->to('root#contact_post' );
198 $r->get('/feedback')->to('root#feedback' );
199 $r->get('/sitemap' )->to('root#sitemap' );
200 $r->get('/robots' )->to('root#robots' );
201 $r->post('/feedback')->to('root#feedback_post');
202 $r->get('/product/(*url)')->to('root#product');
203 $r->get('/products(*category)')
204 ->to('root#products_category', { category => '' });
205 $r->get('/privacy-policy')
206 ->to('root#privacy_policy');
207 $r->get('/terms-and-conditions')
208 ->to('root#terms_and_conditions');
209 }
210  
211 { # Cart routes
212 my $rc = $r->under('/cart');
213 $rc->get( '/' )->to('cart#index' );
214 $rc->post('/add' )->to('cart#add' );
215  
216 for my $plug ( $self->config('checkout_system')->@* ) {
217 my ( $name, $conf ) = ref $plug ? ( @$plug ) : ( $plug );
218  
219 my $handler = "XTaTIK::Plugin::Cart::$name";
220 eval "require $handler"
221 or die "Failed to load cart handler $handler: $@";
222  
223 my $h = $handler->new( $conf ? ( conf => $conf ) : () );
224 $h->_add_routes( $rc );
225 }
226 }
227  
228 unless ( $self->config('no_blog') ) {
229 { # Blog routes
230 my $rb = $r->under('/blog');
231 $rb->get('/' )->to('blog#index');
232 $rb->get('/*post')->to('blog#read');
233 }
234 }
235  
236 { # User section routes
237 $r->post('/login' )->to('user#login' );
238 $r->any( '/logout')->to('user#logout');
239  
240 my $ru = $r->under('/user')->to('user#is_logged_in');
241 $ru->get('/')->to('user#index')->name('user/index');
242 $ru->get('/site-products')->to('user#site_products');
243 $ru->post('/site-products')->to('user#site_products');
244 $ru->get('/master-products-database')
245 ->to('user#master_products_database')
246 ->name('user/master_products_database');
247 $ru->post('/master-products-database')
248 ->to('user#master_products_database_post');
249 $ru->post('/master-products-database/update')
250 ->to('user#master_products_database_update');
251 $ru->post('/master-products-database/delete')
252 ->to('user#master_products_database_delete');
253 $ru->get('/manage-users')->to('user#manage_users');
254 $ru->post('/manage-users/add')->to('user#add_user');
255 $ru->post('/manage-users/update')->to('user#update_users');
256 $ru->post('/manage-users/delete')->to('user#delete_users');
257 $ru->get('/hot-products')->to('user#hot_products');
258 $ru->post('/hot-products')->to('user#hot_products_post');
259 $ru->get('/quotes')->to('user#quotes_handler');
260 }
261 }
262  
263 #### HELPERS
264  
265 sub _helper_xtext {
266 my ( $c, $var, $v ) = @_;
267 $c->config('text')->{ $var } = $v
268 if @_ == 3;
269  
270 return $c->config('text')->{ $var };
271 }
272  
273 sub _helper_xvar {
274 my ( undef, $var, $value ) = @_;
275 state $xvars = XTaTIK::Model::XVars->new(pg => $PG);
276  
277 if ( defined $value ) {
278 $xvars->set($var, $value);
279 }
280 else {
281 return $xvars->get($var);
282 }
283 };
284  
285 sub _helper_users {
286 state $users = XTaTIK::Model::Users->new(
287 pg => $PG,
288 );
289 };
290  
291 sub _gen_helper_product_search {
292 my $self = shift;
293  
294 # Create search dir and touch index files, unless we already have them
295 my $dir = catdir $self->config('_silo_path'), 'search_index';
296 unless ( -d $dir ) {
297 mkdir $dir
298 or croak "Failed to create search_index directory $dir: $!";
299 }
300  
301 for ( map catfile($dir, $_), qw/ixd.bdb ixp.bdb ixw.bdb/ ) {
302 next if -f and -r;
303 open my $fh, '>', $_
304 or croak "Failed to create search_index file $_: $!";
305 }
306  
307 return sub {
308 state $search = XTaTIK::Model::ProductSearch->new( dir => $dir );
309 };
310 }
311  
312 sub _helper_products {
313 my $c = shift;
314 state $products = XTaTIK::Model::Products->new(
315 pg => $PG,
316 custom_cat_sorting => $c->config('custom_cat_sorting'),
317 site => $c->config('site'),
318 );
319  
320 $products->pricing_region( $c->geoip_region );
321 return $products;
322 };
323  
324 sub _helper_quotes {
325 # my $c = shift;
326  
327 state $quotes = XTaTIK::Model::Quotes->new( pg => $PG );
328 return $quotes;
329 };
330  
331 sub _helper_cart {
332 my $c = shift;
333  
334 return $c->stash('__cart') if $c->stash('__cart');
335  
336 my $cart = XTaTIK::Model::Cart->new(
337 pg => $PG,
338 products => $c->products,
339 );
340  
341 if ( my $id = $c->session('cart_id') ) {
342 $cart->id( $id );
343 }
344 else {
345 $c->session( cart_id => $cart->new_cart );
346 }
347  
348 $cart->load;
349  
350 $c->stash( __cart => $cart );
351 return $cart;
352 };
353  
354 sub _helper_cart_dollars {
355 my $c = shift;
356 my $is_refresh = shift;
357 my $dollars = $is_refresh
358 ? $c->cart->dollars
359 : $c->session('cart_dollars') // $c->cart->dollars;
360 $c->session( cart_dollars => $dollars );
361 return $dollars;
362 };
363  
364 sub _helper_cart_cents {
365 my $c = shift;
366 my $is_refresh = shift;
367 my $cents = $is_refresh
368 ? $c->cart->cents
369 : $c->session('cart_cents') // $c->cart->cents;
370 $c->session( cart_cents => $cents);
371 return $cents;
372 };
373  
374 1;
375  
376 __END__
377  
378 =encoding utf8
379  
380 =for stopwords eCommerce Analytics GeoIP Perlbrew PostgreSQL deployable jQuery Cassi
381  
382  
383 =head1 NAME
384  
385 XTaTIK - Rapidly deployable, multi-website eCommerce website base
386  
387 =head1 WARNING
388  
389 =for html <div style="display: table; height: 91px; background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/section-warning.png) no-repeat left; padding-left: 120px;" ><div style="display: table-cell; vertical-align: middle;">
390  
391 This software is currently EXPERIMENTAL and the way it works might
392 be changed. The first stable release is currently expected to appear
393 before the end of 2015. Please consult
394 L<milestones on GitHub|https://github.com/XTaTIK/XTaTIK/milestones>
395 for what's to come I<(Note: milestone dates might be moved)>.
396  
397 =for html </div></div>
398  
399 =head1 SYNOPSIS
400  
401 The following code in your C<XTaTIK.conf> is all you
402 need to launch a new eCommerce website with the default theme.
403 You'd then log in as the admin and tell the site which products from
404 the master product database to display on it.
405  
406 {
407 site => 'awesomewidgets',
408 text => {
409 market => 'Awesome Widgets',
410 website_domain => 'example.com',
411 google_analytics => 'UA-00000000-00',
412 paypal_custom => 'AW: $promo_code',
413 order_number => 'AWX%06d',
414 quote_number => 'AWQ%06d',
415 market_tag => 'The Most Awesome Widgets In The World!',
416 },
417 mojo_secrets => 'b4q34qgfdxw35t#$@!',
418 };
419  
420 =head1 DEMO
421  
422 You can view a demo website running on XTaTIK at L<http://demo.xtatik.org/>
423  
424 =head1 DESCRIPTION
425  
426 XTaTIK is a typical eCommerce website base that can be used to run
427 a single website, but its true power shows when you want to run many
428 separate websites that belong to one company and offer overlapping
429 sets of products (for example, single company serving several markets).
430  
431 XTaTIK Core
432 │
433 ├── Single database for products and customer data
434 │
435 └── Company Silo────── Single product images directory
436 │
437 ├── Site 1 Silo
438 ├── Site 2 Silo
439 ├── Site 3 Silo
440 └── Site 4 Silo
441  
442 I<"Silo" is just a term for a directory with a subset of website files.>
443  
444 The idea, depicted above, is that core eCommerce functionality, like
445 "About us" pages, product search, purchasing a product or adding one
446 to a quote, and check out pages are all handled by XTaTIK core.
447  
448 Company Silo offers any configuration that is to be shared among all the
449 websites. This would be your company's branch addresses, maps,
450 email addresses, etc. You can also override default XTaTIK's pages
451 and even business logic. This is also where you keep all the product images.
452  
453 In Site Silos, you setup anything that you can't specify in your Company
454 Silo. This would be market name and Google Analytics tracking code,
455 among others. As with the Company Silo, in Site Silos you can override
456 any config, files, and business logic provided by the Company Silo or
457 the XTaTIK core.
458  
459 The end result is you don't duplicate anything that you don't need to.
460 Each site is using the same master products database and master
461 product pictures directory, so any changes done in one place are propagated
462 to all of your sites. Same goes for any config you specify in the Company
463 Silo.
464  
465 =head1 INSTALLATION AND USE
466  
467 To learn how to install and use XTaTIK, please see L<XTaTIK::Docs>
468  
469 =head1 FEATURE LIST
470  
471 I<Note: many of the features can be disabled>
472  
473 =over 4
474  
475 =item * Offering products for online purchases
476  
477 =item * Offering products for quote requests
478  
479 =item * Plugin-based checkout systems (currently only PayPal is implemented)
480  
481 =item * GeoIP based pricing
482  
483 =item * Product search
484  
485 =item * Blog
486  
487 =item * Site feedback
488  
489 =item * I<Home>, I<About Us>, I<Company History>, I<Contact Us>,
490 I<Terms and Conditions>, and I<Privacy Policy> pages
491  
492 =back
493  
494 =head1 VERSION SUPPORT
495  
496 XTaTIK is tested in and supports the current and previous major releases of
497 Internet Explorer, Firefox, and Safari, as well as the current
498 version of Google Chrome. There's unofficial support for IE8, IE9, and
499 IE10, which will most likely end in 2016.
500  
501 XTaTIK supports the current and previous major releases of Perl
502 (not counting developer releases) and
503 likely no attempt will be made to provide support for earlier versions.
504 You can use L<Perlbrew|http://perlbrew.pl/> to obtain the latest
505 versions of Perl, if you're currently lacking one.
506  
507 =head1 TECHNOLOGIES USED
508  
509 XTaTIK relies on technologies listed below.
510 Depending on how much customization
511 you desire, you may need to be familiar with some of them:
512  
513 =over 4
514  
515 =item * L<Bootstrap|http://getbootstrap.com>
516  
517 =item * L<SASS|http://sass-lang.com/>
518  
519 =item * L<CSS3|http://www.w3.org/Style/CSS/current-work.en.html>
520  
521 =item * L<HTML5|http://www.w3.org/TR/html5/>
522  
523 =item * L<jQuery|https://jquery.com/>
524  
525 =item * L<PostgreSQL|http://www.postgresql.org/>
526  
527 =item * L<Mojolicious> (also, see
528 L<www.mojolicio.us|http://www.mojolicio.us/>)
529  
530 =item * And of course, the lovely L<Perl 5|https://www.perl.org/>
531  
532 =back
533  
534 =head1 CONNECT WITH XTaTIK USERS
535  
536 =over 4
537  
538 =item * Like XTaTIK on Facebook: L<https://www.facebook.com/XTaTIKPerl>
539  
540 =item * Star and Watch XTaTIK GitHub repo:
541 L<https://github.com/XTaTIK/XTaTIK>
542  
543 =back
544  
545 =head1 SEE ALSO
546  
547 L<http://www.XTaTIK.org>, L<Mojolicious>, L<Mojo::Pg>
548  
549 =for html <div style="background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/hr.png);height: 18px;"></div>
550  
551 =head1 REPOSITORY
552  
553 =for html <div style="display: table; height: 91px; background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/section-github.png) no-repeat left; padding-left: 120px;" ><div style="display: table-cell; vertical-align: middle;">
554  
555 Fork this module on GitHub:
556 L<https://github.com/XTaTIK/XTaTIK>
557  
558 =for html </div></div>
559  
560 =head1 BUGS
561  
562 =for html <div style="display: table; height: 91px; background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/section-bugs.png) no-repeat left; padding-left: 120px;" ><div style="display: table-cell; vertical-align: middle;">
563  
564 To report bugs or request features, please use
565 L<https://github.com/XTaTIK/XTaTIK/issues>
566  
567 If you can't access GitHub, you can email your request
568 to C<bug-XTaTIK at rt.cpan.org>
569  
570 =for html </div></div>
571  
572 =head1 AUTHOR
573  
574 =for html <div style="display: table; height: 91px; background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/section-author.png) no-repeat left; padding-left: 120px;" ><div style="display: table-cell; vertical-align: middle;">
575  
576 =for html <span style="display: inline-block; text-align: center;"> <a href="http://metacpan.org/author/ZOFFIX"> <img src="http://www.gravatar.com/avatar/328e658ab6b08dfb5c106266a4a5d065?d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2F627d83ef9879f31bdabf448e666a32d5" alt="ZOFFIX" style="display: block; margin: 0 3px 5px 0!important; border: 1px solid #666; border-radius: 3px; "> <span style="color: #333; font-weight: bold;">ZOFFIX</span> </a> </span>
577  
578 =over 4
579  
580 =item * Cassi Bryant (L<https://github.com/cassi42>)
581  
582 =back
583  
584 =for html </div></div>
585  
586 =head1 LICENSE
587  
588 Copyright © 2015, Zoffix Znet
589  
590 You can use and distribute this module under
591 The Artistic License 2.0.
592 See the C<LICENSE> file included in this distribution for complete
593 details.
594  
595 B<Note:> for convenience of distribution, this module includes works
596 created by other organizations. They retain their copyright and their
597 original licenses:
598  
599 =head2 Bootstrap3
600  
601 Copyright © 2011-2015, Twitter, Inc
602  
603 L<http://getbootstrap.com/>. Licensed under the L<MIT License|https://github.com/twbs/bootstrap/blob/master/LICENSE>
604  
605 =head2 jQuery
606  
607 Copyright © 2005, 2015 jQuery Foundation, Inc.
608  
609 L<https://jquery.org>. Licensed under the L<MIT License|https://tldrlegal.com/license/mit-license>
610  
611 =cut
« XTaTIK/Controller/Search.pm ↑ Summary XTaTIK/Model/Cart.pm »