<?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 VtemItem extends ObjectModel
{
	public $id;
	public $id_vtfooter_block_item;
	public $id_vtfooter_block;
	public $type;
	public $link = '';
	public $linktype;
	public $link_content;
	public $module_name;
	public $hook_name;
	public $latitude;
	public $longitude;
	public $addthis;
	public $show_title = 1;
	public $target;
	public $position;
	public $title;
	public $text;
	public static $definition = array(
		'table' => 'vtfooter_block_item',
		'primary' => 'id_vtfooter_block_item',
		'multilang' => true,
		'fields' => array(
			'id_vtfooter_block' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
			'type' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
			'link' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
			'linktype' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
			'link_content' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
			'module_name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
			'hook_name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
			'latitude' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
			'longitude' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
			'addthis' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'show_title' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'target' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
			'position' => array('type' => self::TYPE_INT),
			'title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 255),
			'text' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString')
		));
	public function add($autodate = true, $null_values = false)
	{
		$context = Context::getContext();
		$id_shop = $context->shop->id;
		$this->position = VtemItem::getLastPosition((int)$this->id_vtfooter_block, $id_shop);
		$res = parent::add($autodate, $null_values);
		$res &= Db::getInstance()->execute('
			INSERT INTO `'._DB_PREFIX_.'vtfooter_block_item_shop` (`id_vtfooter_block_item`, `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_item_shop`
			WHERE `id_vtfooter_block_item` = '.(int)$this->id);
		$id_vtfooter_block = $this->id_vtfooter_block;
		$res &= parent::delete();
		if ($res)
			return $this->cleanPositions($id_vtfooter_block);
		return false;
	}
	/**
	 * Delete several categories from database
	 *
	 * return boolean Deletion result
	 */
	public function deleteSelection($customfields)
	{
		$return = 1;
		foreach ($customfields as $id_vtfooter_block_item)
		{
			$customfield = new VtemItem((int)$id_vtfooter_block_item);
			$return &= $customfield->delete();
		}
		return $return;
	}
	public static function getLastPosition($id_vtfooter_block, $id_shop = null)
	{
		if (!$id_shop)
		{
			$context = Context::getContext();
			$id_shop = $context->shop->id;
		}
		return (Db::getInstance()->getValue('SELECT MAX(i.`position`)+1 
			FROM `'._DB_PREFIX_.'vtfooter_block_item` i, `'._DB_PREFIX_.'vtfooter_block_item_shop` lbis
			WHERE i.`id_vtfooter_block` = '.(int)$id_vtfooter_block.' AND lbis.`id_shop` = '.(int)$id_shop));
	}
	public function updatePosition($way, $position)
	{
		$context = Context::getContext();
		$id_shop = $context->shop->id;
		if (!$res = Db::getInstance()->ExecuteS('
			SELECT cp.`id_vtfooter_block_item`, cp.`position`, cp.`id_vtfooter_block` 
			FROM `'._DB_PREFIX_.'vtfooter_block_item` cp
			JOIN `'._DB_PREFIX_.'vtfooter_block_item_shop` cps ON(cp.`id_vtfooter_block_item` = cps.`id_vtfooter_block_item`
			AND cps.`id_shop` = '.(int)$id_shop.')
			WHERE cp.`id_vtfooter_block` = '.(int)$this->id_vtfooter_block.' 
			ORDER BY cp.`position` ASC'
		))
			return false;
		$ids = array();
		foreach ($res as $custom_field)
			if ((int)$custom_field['id_vtfooter_block_item'] == (int)$this->id)
				$moved_field = $custom_field;
			else
				$ids[] = (int)$custom_field['id_vtfooter_block_item'];
		if (!isset($moved_field) || !isset($position))
			return false;
		return (Db::getInstance()->Execute('
			UPDATE `'._DB_PREFIX_.'vtfooter_block_item`
			SET `position`= `position` '.($way ? '- 1' : '+ 1').'
			WHERE `position` 
			'.($way
				? '> '.(int)$moved_field['position'].' AND `position` <= '.(int)$position
				: '< '.(int)$moved_field['position'].' AND `position` >= '.(int)$position).'
			AND `id_vtfooter_block`='.(int)$moved_field['id_vtfooter_block'].' AND `id_vtfooter_block_item` IN ('.implode(',', $ids).')')
		&& Db::getInstance()->Execute('
			UPDATE `'._DB_PREFIX_.'vtfooter_block_item`
			SET `position` = '.(int)$position.'
			WHERE `id_vtfooter_block_item` = '.(int)$moved_field['id_vtfooter_block_item'].'
			AND `id_vtfooter_block`='.(int)$moved_field['id_vtfooter_block']));
	}
	public static function cleanPositions($id_vtfooter_block, $id_shop = null)
	{
		if (!$id_shop)
		{
			$context = Context::getContext();
			$id_shop = $context->shop->id;
		}
		$result = Db::getInstance()->ExecuteS('
		SELECT cp.`id_vtfooter_block_item`
		FROM `'._DB_PREFIX_.'vtfooter_block_item` cp
		JOIN `'._DB_PREFIX_.'vtfooter_block_item_shop` cps ON(cp.`id_vtfooter_block_item` = cps.`id_vtfooter_block_item`
		AND cps.`id_shop` = '.(int)$id_shop.')
		WHERE cp.`id_vtfooter_block` = '.(int)$id_vtfooter_block.'
		ORDER BY cp.`position`');
		$sizeof = count($result);
		for ($i = 0; $i < $sizeof; ++$i)
		{
			$sql = '
			UPDATE `'._DB_PREFIX_.'vtfooter_block_item`
			SET `position` = '.(int)$i.'
			WHERE `id_vtfooter_block` = '.(int)$id_vtfooter_block.'
			AND `id_vtfooter_block_item` = '.(int)$result[$i]['id_vtfooter_block_item'];
			Db::getInstance()->Execute($sql);
		}
		return true;
	}
	public static function getFooterItems($id_vtfooter_block_item = false, $id_vtfooter_block = false, $active = true)
	{
		$context = Context::getContext();
		$id_shop = $context->shop->id;
		$sql = 'SELECT value
				FROM `'._DB_PREFIX_.'vtfooter_block_item` ll
				JOIN `'._DB_PREFIX_.'vtfooter_block_item_shop` lis ON(ll.`id_vtfooter_block_item` = lis.`id_vtfooter_block_item`
				AND lis.`id_shop` = '.(int)$id_shop.')
				LEFT JOIN `'._DB_PREFIX_.'vtfooter_block_item_lang` lll ON (ll.`id_vtfooter_block_item` = lll.`id_vtfooter_block_item`)
				WHERE 1 '.($id_vtfooter_block_item ? ' AND ll.`id_vtfooter_block_item` = '.
				(int)$id_vtfooter_block_item : '').($id_vtfooter_block ? ' AND ll.`id_vtfooter_block` = '.(int)$id_vtfooter_block : '').
				($active ? ' AND ll.`active`='.(int)$active : '');
		return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);
	}
}
?>