| 1 | # ------------------------------------------------------------------------------------- | ||
| 2 | # flo::plugin::Shop::Basket::Add | ||
| 3 | # ------------------------------------------------------------------------------------- | ||
| 4 | # Author : Jean-Michel Hiver <jhiver@mkdoc.com>. | ||
| 5 | # Copyright : (c) MKDoc Holdings Ltd, 2003 | ||
| 6 | # | ||
| 7 | # This MKDoc plugin adds an item into 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::Add; | ||
| 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 | |||
| 22 | use base qw /flo::plugin::Shop::Basket/; | ||
| 23 | |||
| 24 | |||
| 25 | sub http_post | ||
| 26 | { | ||
| 27 | my $self = shift; | ||
| 28 | my $cgi = flo::Standard::cgi(); | ||
| 29 | |||
| 30 | # if there is no session present, redirect to a help | ||
| 31 | # page about cookies, user accounts and stuff | ||
| 32 | my $session = MKDoc::Session->load() || do { | ||
| 33 | my $help_p = new flo::plugin::Shop::Basket::Help(); | ||
| 34 | print $cgi->redirect ($help_p->uri()); | ||
| 35 | return 'TERMINATE'; | ||
| 36 | }; | ||
| 37 | |||
| 38 | # if the session holds no basket object, create one | ||
| 39 | $session->{basket} ||= new MKDoc::ECommerce::Basket(); | ||
| 40 | |||
| 41 | my $item = new MKDoc::ECommerce::Item ( | ||
| 42 | reference => $cgi->param ('reference'), | ||
| 43 | description => $cgi->param ('description'), | ||
| 44 | unit_price => $cgi->param ('unit_price'), | ||
| 45 | quantity => $cgi->param ('quantity'), | ||
| 46 | signature => $cgi->param ('signature'), | ||
| 47 | ); | ||
| 48 | |||
| 49 | $item and do { | ||
| 50 | $session->{basket}->add ($item); | ||
| 51 | $session->save(); | ||
| 52 | }; | ||
| 53 | |||
| 54 | return $self->redirect(); | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | sub redirect | ||
| 59 | { | ||
| 60 | my $self = shift; | ||
| 61 | my $cgi = flo::Standard::cgi(); | ||
| 62 | |||
| 63 | # redirect the user to whereever s?he comes from and terminate | ||
| 64 | my $from = $cgi->param ('from') || $ENV{HTTP_REFERER} || do { | ||
| 65 | my $cgix = flo::Standard::cgi()->new(); | ||
| 66 | for ($cgix->param()) { $cgix->delete ($_) } | ||
| 67 | $cgix->path_info ('/'); | ||
| 68 | $cgix->self_url; | ||
| 69 | }; | ||
| 70 | |||
| 71 | print $cgi->redirect ($from); | ||
| 72 | return 'TERMINATE'; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | 1; | ||
| 77 | |||
| 78 | |||
| 79 | __END__ |