<?php
/**
* 2007-2014 PrestAddon
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestaddon.com for more information.
*
*  @author    PrestAddon SA <admin@prestaddon.com>
*  @copyright 2007-2014 PrestAddon
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

class VtemBlock extends ObjectModel
{
	public $id;
	public $id_vtfooter_block;
	public $width;
	public $show_title = 1;
	public $id_position;
	public $title;
	public static $definition = array(
		'table' => 'vtfooter_block',
		'primary' => 'id_vtfooter_block',
		'multilang' => true,
		'fields' => array(
			'width' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'),
			'show_title' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'id_position' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
			'title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 255)
			));
	public function add($autodate = true, $null_values = false)
	{
	$context = Context::getContext();
	$id_shop = $context->shop->id;
	$res = parent::add($autodate, $null_values);
	$res &= Db::getInstance()->execute('
		INSERT INTO `'._DB_PREFIX_.'vtfooter_block_shop` (`id_vtfooter_block`, `id_shop`)
		VALUES('.(int)$this->id.', '.(int)$id_shop.')');
		return $res;
	}
	/*public function update($null_values = false)
	{
		return parent::update($null_values);
	}*/
	public function delete()
	{
		$res = Db::getInstance()->execute('
			DELETE FROM `'._DB_PREFIX_.'vtfooter_block_shop`
			WHERE `id_vtfooter_block` = '.(int)$this->id
		);
		$id_vtfooter_block = $this->id;
		$res &= parent::delete();
		$return = true;
		if ($res)
		{
			$items = self::getItems($id_vtfooter_block, $this->context->language->id);
			if ($items)
			{
				foreach ($items as $i)
				{
					$obj = new VtemItem($i['id_vtfooter_block_item']);
					$return &= $obj->delete();
				}
			}
		}
		else
			$return &= false;
		return $return;
	}
	public static function getBlocks($id_position = false, $id_lang, $id_shop = null)
	{
		if (!$id_shop)
		{
			$context = Context::getContext();
			$id_shop = $context->shop->id;
		}
		$res = Db::getInstance()->ExecuteS('
		SELECT fl.*, fll.`title` 
		FROM `'._DB_PREFIX_.'vtfooter_block` fl
		JOIN `'._DB_PREFIX_.'vtfooter_block_shop` lbs ON(fl.`id_vtfooter_block` = lbs.`id_vtfooter_block` AND lbs.`id_shop` = '.(int)$id_shop.')
		LEFT JOIN `'._DB_PREFIX_.'vtfooter_block_lang` fll ON(fll.id_vtfooter_block = fl.id_vtfooter_block AND fll.`id_lang` = '.(int)$id_lang.')
		WHERE 1 '.($id_position ? ' AND fl.`id_position` = '.(int)$id_position : '').' 
		ORDER BY fl.`id_vtfooter_block` ASC' );
		return $res;
	}
	public static function getItems($id_vtfooter_block, $id_lang, $id_shop = null)
	{
		if (!$id_shop)
		{
			$context = Context::getContext();
			$id_shop = $context->shop->id;
		}
		$results = Db::getInstance()->ExecuteS('
		SELECT *
		FROM `'._DB_PREFIX_.'vtfooter_block_item` bi
		JOIN `'._DB_PREFIX_.'vtfooter_block_item_shop` lbis ON(bi.`id_vtfooter_block_item` = lbis.`id_vtfooter_block_item`
		AND lbis.`id_shop` = '.(int)$id_shop.')
		LEFT JOIN `'._DB_PREFIX_.'vtfooter_block_item_lang` bil ON(bi.`id_vtfooter_block_item` = bil.`id_vtfooter_block_item`
		AND bil.`id_lang` = '.(int)$id_lang.')
		WHERE bi.`id_vtfooter_block`='.(int)$id_vtfooter_block.'
		ORDER BY bi.`position` ASC ');
		return $results;
	}
}
