ECommerce/Plugin/Basket/Modify.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 120 CC 3 SCAR 11.2
CC
3
Cov
0%
http_post
6.0
_extract_value
6.0
_extract_ref_id
2.0
CRAP 3.1
1 # -------------------------------------------------------------------------------------
2 # flo::plugin::Shop::Basket::Modify
3 # -------------------------------------------------------------------------------------
4 # Author : Jean-Michel Hiver <jhiver@mkdoc.com>.
5 # Copyright : (c) MKDoc Holdings Ltd, 2003
6 #
7 # This MKDoc plugin removes an item from someones' shopping basket.
8 # If the client sent a cookie, then this cookie is used to fetch the existing basket.
9 #
10 # Otherwise, this module creates a new shopping basket and sends a cookie to the
11 # client for future retrieval.
12 # -------------------------------------------------------------------------------------
13 package flo::plugin::Shop::Basket::Modify;
14 use strict;
15 use warnings;
16 use flo::Standard;
17 use flo::plugin::Shop::Basket::Help;
18 use MKDoc::Session;
19 use MKDoc::ECommerce::Item;
20 use MKDoc::ECommerce::Basket;
21 use URI;
22  
23 use base qw /flo::plugin::Shop::Basket/;
24  
25  
26 sub http_post
27 {
28 my $self = shift;
29 my $cgi = flo::Standard::cgi();
30
31 # if there is no session present, redirect to a help
32 # page about cookies, user accounts and stuff
33 my $session = MKDoc::Session->load() || do {
34 my $help_p = new flo::plugin::Shop::Basket::Help();
35 print $cgi->redirect ($help_p->uri());
36 return 'TERMINATE';
37 };
38
39 $session->{country} = $cgi->param ('country');
40
41 # change quantities
42 if ($cgi->param ('submit-change') || defined $cgi->param ('submit-change.x'))
43 {
44 foreach ($cgi->param())
45 {
46 /^submit-change-qty-/ and do {
47 my $ref_id = $_;
48 my $new_qty = $cgi->param ($_);
49 $ref_id = $self->_extract_ref_id ($ref_id);
50 $new_qty = $self->_extract_value ($new_qty);
51 $session->{basket}->set_quantity ($ref_id, $new_qty);
52 };
53 }
54 }
55
56 # delete items
57 for ($cgi->param ())
58 {
59 s/\.x$//;
60 /^submit-delete-id-/ and do {
61 my $ref_id = $self->_extract_ref_id ($_);
62 $session->{basket}->delete ($ref_id);
63 };
64 }
65
66 # save session
67 $session->save();
68
69 # redirect user to appropriate location and terminate
70 my $from = $cgi->param ('from') || $ENV{HTTP_REFERER} || do {
71 my $cgix = flo::Standard::cgi()->new();
72 for ($cgix->param()) { $cgix->delete ($_) }
73 $cgix->path_info ('/');
74 $cgix->self_url;
75 };
76 print $cgi->redirect ($from);
77 return 'TERMINATE';
78 }
79  
80  
81 sub _extract_ref_id
82 {
83 my $self = shift;
84 my $ref_id = shift;
85 $ref_id =~ s/^submit-change-qty-//;
86 $ref_id =~ s/^submit-delete-id-//;
87 return $ref_id;
88 }
89  
90  
91 sub _extract_value
92 {
93 my $self = shift;
94 my $value = shift;
95 no warnings;
96 {
97 $value = int ($value);
98 $value = 0 if ($value < 0);
99 }
100 return $value;
101 }
102  
103  
104 1;
105  
106  
107 __END__