<?php
/**
* ---------------------------------------------------------------------------------
*
* This file is part of the 'giftonordermodule' module feature (main || related module)
* Developped for Prestashop platform.
* You are not allowed to use it on several site
* You are not allowed to redistribute this module
* This header must not be removed
*
* @category  giftonordermodule
* @author    OleaCorner <contact@oleacorner.com> <www.oleacorner.com>
* @copyright OleaCorner
* @version   4.3
* @license   XXX
*         
* ---------------------------------------------------------------------------------
*/

class GiftonordermodulepreselectajaxModuleFrontController extends ModuleFrontController

{

	/**
	 *
	 * @see FrontController::initContent()
	 */
	public function initContent()
	{
		parent::initContent ();
		
		include_once (_PS_MODULE_DIR_.'/giftonordermodule/Giftonorder.php');
		if (Context::getContext ()->cart->id == 0)
		{
			Context::getContext ()->cart->save ();
			Context::getContext ()->cookie->id_cart = Context::getContext ()->cart->id;
		}
		
		$submit_id_product = 0;
		$submit_id_giftonorder = 0;
		$submit_id_gift = 0;
		$select_gift_submitted = Tools::getValue ('giftonorder_select_gift');
		if ($select_gift_submitted != '')
		{
			list ($submit_id_giftonorder, $submit_id_gift) = explode ('_', $select_gift_submitted);
			$submit_id_giftonorder = (int)$submit_id_giftonorder;
			$submit_id_gift = (int)$submit_id_gift;
		}
		
		if ($submit_id_giftonorder != 0 && $submit_id_gift != 0)
		{
			$giftonorder = new Giftonorder ($submit_id_giftonorder);
			if (in_array ($submit_id_gift, explode (',', $giftonorder->gift_id_product_list)))
			{
				$preselectedgifts = Giftonorder::get_preselected_gifts (Context::getContext ()->cart);
				$preselectedgifts[$submit_id_giftonorder] = $submit_id_gift;
				Giftonorder::save_preselected_gifts (Context::getContext ()->cart, $preselectedgifts);
			}
		}
	}
}