# $Id: /local/CPAN/Handel/lib/Catalyst/Helper/Controller/Handel/Order.pm 1058 2007-08-23T02:05:31.088429Z claco $ ## no critic (ProhibitCaptureWithoutTest) package Catalyst::Helper::Controller::Handel::Order; use strict; use warnings; BEGIN { use Catalyst 5.7001; use Catalyst::Utils; use Path::Class; }; =head1 NAME Catalyst::Helper::Controller::Handel::Order - Helper for Handel::Order Controllers =head1 SYNOPSIS script/create.pl controller Handel::Order [] script/create.pl controller Orders Handel::Order OrderModel =head1 DESCRIPTION A Helper for creating controllers based on Handel::Order objects. If no modelclass is specified, ::M::Orders is assumed. The modelclass argument tries to do the right thing with the names given to it. For example, you can pass the shortened class name without the MyApp::M/C, or pass the fully qualified package name: MyApp::M::OrderModel MyApp::Model::OrderModel OrderModel In all three cases everything before M{odel)|C(ontroller) will be stripped and the class OrderModel will be used. B =head1 METHODS =head2 mk_compclass Makes a Handel::Order Controller class and template files for you. =cut sub mk_compclass { my ($self, $helper, $model) = @_; my $file = $helper->{'file'}; my $dir = dir($helper->{'base'}, 'root', $helper->{'uri'}); $model ||= 'Order'; $model =~ /^(.*::M(odel)?::)?(.*)$/i; $model = $3 ? $3 : 'Order'; $helper->{'model'} = $model; $helper->{'action'} = Catalyst::Utils::class2prefix($helper->{'class'}); $helper->mk_dir($dir); $helper->render_file('controller', $file); $helper->render_file('default', file($dir, 'default')); $helper->render_file('view', file($dir, 'view')); $helper->render_file('errors', file($dir, 'errors')); $helper->render_file('profiles', file($dir, 'profiles.yml')); $helper->render_file('messages', file($dir, 'messages.yml')); return 1; }; =head2 mk_comptest Makes a Handel::Order Controller test for you. =cut sub mk_comptest { my ($self, $helper) = @_; my $test = $helper->{'test'}; $helper->render_file('test', $test); return 1; }; =head1 SEE ALSO L, L, L =head1 AUTHOR Christopher H. Laco CPAN ID: CLACO claco@chrislaco.com http://today.icantfocus.com/blog/ =cut 1; __DATA__ =begin pod_to_ignore __controller__ package [% class %]; use strict; use warnings; BEGIN { use base qw/Catalyst::Controller/; use Handel::Constants qw/:order/; use FormValidator::Simple 0.17; use YAML 0.65; }; =head1 NAME [% class %] - Catalyst Controller =head1 DESCRIPTION Catalyst Controller. =head1 METHODS =head2 COMPONENT =cut sub COMPONENT { my $self = shift->NEXT::COMPONENT(@_); $self->{'validator'} = FormValidator::Simple->new; $self->{'validator'}->set_messages( $_[0]->path_to('root', '[% action %]', 'messages.yml') ); $self->{'profiles'} = YAML::LoadFile($_[0]->path_to('root', '[% action %]', 'profiles.yml')); return $self; }; =head2 default Default action when browsing to [% uri %]/ that lists the saved orders for the current shopper. If no session exists, or the shopper id isn't set, no orders will be loaded. This keeps non-shoppers like Google and others from wasting sessions and order records for no good reason. =cut sub default : Private { my ($self, $c) = @_; $c->stash->{'template'} = '[% action %]/default'; if ($c->sessionid && $c->session->{'shopper'}) { my $orders = $c->model('[% model %]')->search({ shopper => $c->session->{'shopper'}, type => ORDER_TYPE_SAVED }); $c->stash->{'orders'} = $orders; }; return; }; =head2 create Creates a new order from the current shopping cart. my $order = $c->forward('create'); =cut sub create : Private { my ($self, $c, $cart) = @_; if ($c->sessionid && $c->session->{'shopper'}) { return $c->model('[% model %]')->create({ shopper => $c->session->{'shopper'}, type => ORDER_TYPE_TEMP, cart => $cart }); }; return; }; =head2 load Loads the current temporary order for the current shopper. my $order = $c->forward('load'); =cut sub load : Private { my ($self, $c) = @_; if ($c->sessionid && $c->session->{'shopper'}) { return $c->model('[% model %]')->search({ shopper => $c->session->{'shopper'}, type => ORDER_TYPE_TEMP })->first; }; return; }; =head2 view =over =item Parameters: id =back Loads the specified order and displays its details during a GET operation. [% uri %]/view/$id =cut sub view : Local { my ($self, $c, $id) = @_; $c->stash->{'template'} = '[% action %]/view'; if ($id && $c->sessionid && $c->session->{'shopper'}) { if ($c->forward('validate', [{id => $id}])) { my $order = $c->model('[% model %]')->search({ shopper => $c->session->{'shopper'}, id => $id, type => ORDER_TYPE_SAVED })->first; if ($order) { $c->stash->{'order'} = $order; $c->stash->{'items'} = $order->items; }; }; }; return; }; =head2 validate Validates the current form parameters using the profile in profiles.yml that matches the current action. if ($c->forward('validate')) { }; =cut sub validate : Private { my ($self, $c, $query) = @_; $query ||= $c->req; $self->{'validator'}->results->clear; my $results = $self->{'validator'}->check( $query, $self->{'profiles'}->{$c->action} ); if ($results->success) { return $results; } else { $c->stash->{'errors'} = $results->messages($c->action); }; return; }; =head1 AUTHOR A clever guy =head1 LICENSE This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1; __test__ use Test::More tests => 3; use strict; use warnings; use_ok('Catalyst::Test', '[% app %]'); use_ok('[% class %]'); ok(request('[% uri %]')->is_success, 'Request should succeed'); __default__ [% TAGS [- -] -%] [% USE HTML %]

Your Order History

[% IF orders.count %] [% WHILE (order = orders.next) %] [% END %]
Order# Created
[% HTML.escape(order.number) %] [% HTML.escape(order.updated) %]
[% ELSE %]

You have no orders.

[% END %] __view__ [% TAGS [- -] -%] [% USE HTML %]

Order Details

[% INCLUDE [- action -]/errors %] [% IF order %]
Billing Shipping
 
Order Number: [% HTML.escape(order.number) %]
Order Created: [% HTML.escape(order.updated) %]
 
First Name: [% HTML.escape(order.billtofirstname) %] First Name: [% HTML.escape(order.shiptofirstname) %]
Last Name: [% HTML.escape(order.billtolastname) %] Last Name: [% HTML.escape(order.shiptolastname) %]
 
Address: [% HTML.escape(order.billtoaddress1) %] Address: [% HTML.escape(order.shiptoaddress1) %]
[% HTML.escape(order.billtoaddress2) %] [% HTML.escape(order.shiptoaddress2) %]
[% HTML.escape(order.billtoaddress3) %] [% HTML.escape(order.shiptoaddress3) %]
City: [% HTML.escape(order.billtocity) %] City: [% HTML.escape(order.shiptocity) %]
State/Province: [% HTML.escape(order.billtostate) %] State/Province: [% HTML.escape(order.shiptostate) %]
Zip/Postal Code: [% HTML.escape(order.billtozip) %] Zip/Postal Code: [% HTML.escape(order.shiptozip) %]
Country: [% HTML.escape(order.billtocountry) %] Country: [% HTML.escape(order.shiptocountry) %]
Day Phone: [% HTML.escape(order.billtodayphone) %] Day Phone: [% HTML.escape(order.shiptodayphone) %]
Night Phone: [% HTML.escape(order.billtonightphone) %] Night Phone: [% HTML.escape(order.shiptonightphone) %]
Fax: [% HTML.escape(order.billtofax) %] Fax: [% HTML.escape(order.shiptofax) %]
Email: [% HTML.escape(order.billtoemail) %] Email: [% HTML.escape(order.shiptoemail) %]
 
Comments: [% HTML.escape(order.comments) %]
 
[% WHILE (item = items.next) %] [% END %]
SKU Description Price Quantity Total
[% HTML.escape(item.sku) %] [% HTML.escape(item.description) %] [% HTML.escape(item.price.as_string('FMT_SYMBOL')) %] [% HTML.escape(item.quantity) %] [% HTML.escape(item.total.as_string('FMT_SYMBOL')) %]
Subtotal: [% HTML.escape(order.subtotal.as_string('FMT_SYMBOL')) %]
Tax: [% HTML.escape(order.tax.as_string('FMT_SYMBOL')) %]
Shipping: [% HTML.escape(order.shipping.as_string('FMT_SYMBOL')) %]
Handling: [% HTML.escape(order.handling.as_string('FMT_SYMBOL')) %]
Total: [% HTML.escape(order.total.as_string('FMT_SYMBOL')) %]
[% ELSE %]

The order requested could not be found.

[% END %] [% TAGS [- -] -%] __errors__ [% TAGS [- -] -%] [% IF errors %]
    [% FOREACH error IN errors %]
  • [% HTML.escape(error) %]
  • [% END %]
[% END %] __messages__ [% action %]/view: id: REGEX: The id field is in the wrong format. __profiles__ [% action %]/view: - id - - - REGEX - !!perl/regexp (?i-xsm:^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$) __END__