<?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
*/

include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');
include(dirname(__FILE__).'/libs/vtemBlock.php');
include(dirname(__FILE__).'/vtemfooter.php');
$module = new vtemfooter();
$get_vtemajax = Tools::getValue('vtemajax');
$get_vtemtask = Tools::getValue('task');
if (isset($get_vtemajax) && ($get_vtemtask == 'gethook'))
{
	$module_name = Tools::getValue('module_name');
	$hook_name = Tools::getValue('hook_name');
	$content = $module->getParamHooks( $module_name, $hook_name );
	die( $content );
}
if (isset($get_vtemajax) && ($get_vtemtask == 'positionItem') && Tools::getValue('action') == 'dnd')
{
	$blocks = VtemBlock::getBlocks(false, Context::getContext()->language->id);
	if ($blocks)
	{
		foreach ($blocks as $b)
		{
			if (Tools::getValue('vtemtable-'.$b['id_vtfooter_block']))
				$table = Tools::getValue('vtemtable-'.$b['id_vtfooter_block']);
		}
	}
	if (isset($table))
	{
		$pos = 0;
		foreach ($table as $key => $row)
		{
			$ids = explode('_', $row);
			Db::getInstance()->Execute('
			UPDATE `'._DB_PREFIX_.'vtfooter_block_item` 
			SET `position` = '.(int)$pos.' 
			WHERE `id_vtfooter_block_item` = '.(int)$ids[2].' AND `id_vtfooter_block` = '.(int)$ids[1]);
			$pos++;
		}
	}
}
if (isset($get_vtemajax) && ($get_vtemtask == 'deleteItem'))
{
	$json_data = array();
	$json_data['result'] = 1;
	if ($table = Tools::getValue('class_tr'))
	{
		$ids = explode('_', $table);
		if (Validate::isLoadedObject($obj = new vtemItem($ids[2])))
		{
			if (!$obj->delete())
			{
				$json_data['result'] = 0;
				$json_data['error'] = $module->l('can\'t delete');
			}
		}
		else
		{
			$json_data['result'] = 0;
			$json_data['error'] = $module->l('can\'t create object');
		}
	}
	die(Tools::jsonEncode($json_data));
}
if (isset($get_vtemajax) && ($get_vtemtask == 'updateBlock'))
{
	$json_data = array();
	$json_data['result'] = 1;
	$default_language = new Language((int)$module->default_form_language);
	if (Validate::isLoadedObject($obj = new VtemBlock(Tools::getValue('id_vtfooter_block'))))
	{
		$errors = array();
		$titles = array();
		foreach ($module->languages as $language)
			$titles[$language['id_lang']] = Tools::getValue('title_'.$language['id_lang']);
		if (!Tools::getValue('title_'.$module->default_form_language))
			$errors[] = $module->l('the field Title is required at least in '.$default_language->name);
		foreach ($titles as $key => $val)
			if (!Validate::isGenericName($val))
				$errors[] = $module->l('the field Title is invalid');
		if (Tools::getValue('width') && !Validate::isFloat(Tools::getValue('width')))
			$errors[] = $module->l('the field Width is invalid');
		$obj->title = $titles;
		$obj->width = Tools::getValue('width');
		$obj->show_title = Tools::getValue('show_title');
		if (!count($errors))
		{
			if (!$obj->update())
			{
				$json_data['result'] = 0;
				$json_data['error'] = $module->l('can\'t update object');
			}
		}
		else
		{
			$json_data['result'] = 0;
			$json_data['error'] = implode(',', $errors);
		}
	}
	else
	{
		$json_data['result'] = 0;
		$json_data['error'] = $module->l('can\'t create object');
	}
	die(Tools::jsonEncode($json_data));
}
?>