<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class provides a order cart
 *
 * @package    OSB
 * @subpackage Cart
 * @category   Controllers
 * @author     Deon George
 * @copyright  (c) 2010 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
class Controller_Cart extends Controller_TemplateDefault {
	/**
	 * Default action when called
	 */
	public function action_index() {
		return $this->action_list();
	}

	/**
	 * List items in the cart
	 */
	public function action_list() {
		// @todo - this should be a global config item
		$mediapath = Route::get('default/media');

		// If the cart is empty, we'll return here.
		if (! Cart::instance()->contents()->count_all())
			Block::add(array(
				'title'=>_('Empty Cart'),
				'body'=>_('The cart is empty')
			));

		else {
			Style::add(array(
				'type'=>'file',
				'data'=>'css/cart_contents.css',
				));

			$output = Form::open('checkout/noready');
			foreach (Cart::instance()->contents()->find_all() as $item) {
				$ppa = $item->product->get_price_array();
				$pdata = Period::details($item->recurr_schedule,$item->product->price_recurr_weekday,time(),TRUE);

				$price_box = View::factory('cart/list_pricebox')
					->set('price_recurring',Currency::display($item->quantity*$ppa[$item->recurr_schedule]['price_base']))
					->set('price_firstinvoice',Currency::display($item->quantity*$ppa[$item->recurr_schedule]['price_base']*$pdata['prorata']))
					->set('price_setup',Currency::display($item->quantity*$ppa[$item->recurr_schedule]['price_setup']))
					->set('item',$item)
					->set('mediapath',$mediapath);

				$output .= View::factory('cart/list_item')
					->set('price_box',$price_box)
					->set('service_start',$pdata['date'])
					->set('service_end',$pdata['end'])
					->set('price_recurring',Currency::display($item->quantity*$ppa[$item->recurr_schedule]['price_base']))
					->set('item',$item)
					->set('mediapath',$mediapath);

				// If we are a plugin product, we might need more information
				// @todo If an admin, show a system message if cart_info doesnt exist.
				if ($item->product->prod_plugin AND method_exists($item->product->prod_plugin_file,'product_cart') AND Kohana::find_file('views',sprintf('%s/cart_info',strtolower($item->product->prod_plugin_file)))) {
					$output .= View::factory(sprintf('%s/cart_info',strtolower($item->product->prod_plugin_file)));

					// @todo JS validation will need to verify data before submission
				}
			}
			$output .= '<div>'.Form::submit('submit',_('Checkout')).'</div>';
			$output .= Form::close();

			Block::add(array(
				'title'=>_('Your Items'),
				'body'=>$output,
			));
		}

		// Suppress our right hand tab
		$this->template->right = ' ';
	}

	/**
	 * Add an item to the cart
	 */
	public function action_add() {
		$cart = ORM::factory('cart');

		$cart->session_id = Session::instance()->id();

		if (Auth::instance()->logged_in())
			$cart->account_id = Auth::instance()->get_user()->id;

		if ($cart->values($_POST)->check())
			$cart->save();
		else
			echo Kohana::debug($cart->validate()->errors());

		if ($cart->saved())
			Request::current()->redirect('cart/index');
		else
			throw new Kohana_Exception(_('There was a problem adding the item to the cart.'));
	}

	public function action_empty() {
		$cart = ORM::factory('cart')
			->where('session_id','=',session_id());

		$cart->delete_all();

		$this->template->content = _('Cart Emptied');
	}
}
?>
