2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides a order cart
|
|
|
|
*
|
2013-03-19 22:35:19 +00:00
|
|
|
* @package Cart
|
2010-11-29 22:41:08 +00:00
|
|
|
* @category Controllers
|
|
|
|
* @author Deon George
|
2013-03-19 22:35:19 +00:00
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
|
|
|
* @license http://dev.osbill.net/license.html
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
|
|
|
class Controller_Cart extends Controller_TemplateDefault {
|
|
|
|
/**
|
2013-01-15 06:07:54 +00:00
|
|
|
* List the cart contents
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
|
|
|
public function action_index() {
|
2013-01-15 06:07:54 +00:00
|
|
|
$output = '';
|
|
|
|
$co = Cart::instance();
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
// If the cart is empty, we'll return here.
|
2013-01-15 06:07:54 +00:00
|
|
|
if (! count($co->contents()))
|
2011-05-14 07:35:33 +00:00
|
|
|
Block::add(array(
|
2010-11-29 22:41:08 +00:00
|
|
|
'title'=>_('Empty Cart'),
|
|
|
|
'body'=>_('The cart is empty')
|
|
|
|
));
|
|
|
|
|
|
|
|
else {
|
2013-01-15 06:07:54 +00:00
|
|
|
Block::add(array(
|
|
|
|
'title'=>_('Cart Items'),
|
|
|
|
'body'=>Table::display(
|
|
|
|
$co->contents(),
|
|
|
|
NULL,
|
|
|
|
array(
|
|
|
|
'item()->q'=>array('label'=>'Quantity'),
|
|
|
|
'item()->i'=>array('label'=>'Item'),
|
|
|
|
'item()->t'=>array('label'=>'Total','class'=>'right'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'type'=>'list',
|
|
|
|
)
|
|
|
|
),
|
|
|
|
));
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
$checkout = ORM::factory('Checkout')->where_active()->find_all()->as_array();
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
foreach ($co->contents() as $cio)
|
|
|
|
$checkout = array_intersect($checkout,$cio->checkout()->as_array());
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
$payopt = array();
|
|
|
|
foreach ($checkout as $cko)
|
|
|
|
$payopt[$cko->id] = $cko->name;
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
$output .= _('Total amount due for payment').' '.$co->total(TRUE);
|
|
|
|
$output .= Form::open('checkout/before');
|
|
|
|
$output .= Form::select('checkout_id',$payopt);
|
|
|
|
$output .= Form::submit('submit',_('Checkout'));
|
2010-11-29 22:41:08 +00:00
|
|
|
$output .= Form::close();
|
|
|
|
|
2011-05-14 07:35:33 +00:00
|
|
|
Block::add(array(
|
2013-01-15 06:07:54 +00:00
|
|
|
'title'=>_('Payment'),
|
2010-11-29 22:41:08 +00:00
|
|
|
'body'=>$output,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an item to the cart
|
|
|
|
*/
|
|
|
|
public function action_add() {
|
2012-11-09 23:13:57 +00:00
|
|
|
$cart = ORM::factory('Cart');
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
$cart->session_id = Session::instance()->id();
|
|
|
|
|
2013-01-15 06:07:54 +00:00
|
|
|
if ($cart->values(Request::current()->post())->check())
|
2010-11-29 22:41:08 +00:00
|
|
|
$cart->save();
|
|
|
|
else
|
2013-01-15 06:07:54 +00:00
|
|
|
throw new Kohana_Exception('Unable to add to cart');
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
if ($cart->saved())
|
2012-11-09 23:13:57 +00:00
|
|
|
HTTP::redirect('cart/index');
|
2010-11-29 22:41:08 +00:00
|
|
|
else
|
|
|
|
throw new Kohana_Exception(_('There was a problem adding the item to the cart.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function action_empty() {
|
2013-01-15 06:07:54 +00:00
|
|
|
foreach (ORM::factory('Cart')->where('session_id','=',Session::instance()->id())->find_all() as $co)
|
|
|
|
$co->delete();
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
$this->template->content = _('Cart Emptied');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|