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