<?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 GiftonordermoduleAllgiftsModuleFrontController extends ModuleFrontController
{	
	/**
	 *
	 * @see FrontController::initContent()
	 */
	public function initContent()
	{
		if ((int)Configuration::get('OLEA_GIFTONORDER_CMSID') > 0)
			Tools::redirect(Context::getContext()->link->getCMSLink((int)Configuration::get('OLEA_GIFTONORDER_CMSID')));
		
		parent::initContent ();
		
		include_once (_PS_MODULE_DIR_.'/giftonordermodule/Giftonorder.php');
		
		$allgifts = Giftonorder::getPotentialGift ($this->context->customer->id, $this->context->cookie->id_lang, true);
		$nbProducts = count ($allgifts);
		
		self::oleaAddObjectTableAssociation ('giftonorder');
		
		$prod = new Product ();
		$allgiftsfull = array();
		if (isset ($allgifts) && count ($allgifts))
			foreach ($allgifts as $gift)
			{
				if ($gift['gift_id_product_list'] != '')
				{
					$sqlReq = '
						SELECT p.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, p.`ean13`,
						i.`id_image`, il.`legend`, m.`name` as manufacturer_name
						FROM `'._DB_PREFIX_.'product` p 
						'.Shop::addSqlAssociation ('product', 'p').'
						LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.$this->context->cookie->id_lang.')
						'.Shop::addSqlRestrictionOnLang ('pl').'
						LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
						'.Shop::addSqlAssociation ('image', 'i', false, 'image_shop.cover=1').'
						LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.$this->context->cookie->id_lang.')
						LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (p.`id_manufacturer`= m.`id_manufacturer`)
						WHERE p.`id_product` IN ('.$gift['gift_id_product_list'].')
						AND p.`active` = 1';
					$result = Db::getInstance ()->ExecuteS ($sqlReq);
					if ($result)
						$allgiftsfull[] = array_merge ($gift, array(
							'gift_products' => $prod->getProductsProperties ($this->context->cookie->id_lang, $result)
						));
					else
						$allgiftsfull[] = $gift;
				}
			}
		
		$this->context->smarty->assign (array(
			'nbProducts' => $nbProducts,
			'allgifts' => $allgiftsfull,
			'message_allgifts' => Configuration::get ('GIFTONORDER_MESSAGE_ALLGIFTS', $this->context->cookie->id_lang)
		));
		
		if ((int)Configuration::get ('OLEA_GIFTONORDER_ALLGIFTTPL'))
			$this->setTemplate ('/all_giftonorderfull.tpl');
		else
			$this->setTemplate ('/all_giftonorder.tpl');
	}

	public static function oleaAddObjectTableAssociation($table_name)
	{
		Shop::getAssoTable ($table_name); // To force init
		return Shop::addTableAssociation ($table_name, array(
			'type' => 'shop'
		));
	}
}
