<?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
*/

if (!class_exists('VtemFooterCustomParams', false))
{
class VtemFooterCustomParams
{
	public $name = '';
	protected $data = array();
	private $languages = null;
	private $default_form_language = null;
	public $hook_assign = array();
	public $context;
	public function __construct($name, $hook_assign = array())
	{
		$this->context = Context::getContext();
		$id_shop = $this->context->shop->id;
		$id_shop_group = $this->context->shop->id_shop_group;
		$this->name = $name;
		$this->hook_assign = $hook_assign;
		$id_lang = (int)$this->context->language->id;
		$result = Db::getInstance()->ExecuteS('
		SELECT c.name,IFNULL('.($id_lang ? 'cl' : 'c').'.`value`, c.`value`) AS value
		FROM `'._DB_PREFIX_.'configuration` c
		'.($id_lang ? ('LEFT JOIN `'._DB_PREFIX_.'configuration_lang` cl ON (c.`id_configuration` = cl.`id_configuration`
			AND cl.`id_lang` = '.(int)$id_lang.')') : '').'
		WHERE `name` LIKE \''.pSQL($name).'%\' AND (`id_shop_group` = NULL OR `id_shop_group` = '.(int)$id_shop_group.')
			AND (`id_shop` = NULL OR `id_shop` = '.(int)$id_shop.')');
		foreach ($result as $row)
			$this->data[$row['name']] = $row['value'];
		$this->languages();
	}
	public function get($name, $default = '')
	{
		if (isset($this->data[$this->name.'_'.$name]))
			return $this->data[$this->name.'_'.$name];
		else
		{
			if (Configuration::get($this->name.'_'.$name) != '')
			{
				$this->data[$this->name.'_'.$name] = Configuration::get($this->name.'_'.$name);
				return Configuration::get($this->name.'_'.$name);
			}
			elseif (isset($this->data[$name]))
				return $this->data[$name];
		}
		return $default;
	}
	public function set($key, $value)
	{
		$this->data[$key] = $value;
	}
	public function update($name)
	{
		Configuration::updateValue($this->name.'_'.$name, Tools::getValue($name), true);
	}
	public function batchUpdate($configurations = array())
	{
		foreach ($configurations as $config)
		Configuration::updateValue($this->name.'_'.$config, Tools::getValue($config), true);
	}
	public function fileTag($name, $file_option, $file_values, $file_lang_arr, $title, $attr, $li_atrr = '', $ul_attr = '')
	{
		$id = 'params_'.$name;
		$str = '<ul '.$ul_attr.'>
					<li class="vtem-config-left">
						<label for="'.$id.'">'.$title.'</label>
					</li>
					<li class="vtem-config-right">
						<div><label><b>'.$file_lang_arr['is_ena'].'</b></label> 
							<select name="'.$name.'-enable" id="vtem-group'.$name.'" '.$attr.' class="select-group">';
		foreach ($file_option['enable'] as $val => $text)
		{
			$is_selected = ($val == $file_values['enable'])?'selected="selected"':'';
			$str .= '<option '.$is_selected.' value="'.$val.'">'.$text.'</option>';
		}
		$str .= '</select>
					</div>
					<div id="vtem-groupfile1container" class="vtem-container '.$name.'-enable-1" style="display: block;">
					<fieldset class="vtem-fsi">
						<div class="vtem-cols"><label>'.$file_lang_arr['preview'].'</label>
						<input type="text" name="'.$name.'-preview" value="'.$file_values['preview'].'">
						</div>
						<div class="vtem-cols"><label>'.$file_lang_arr['link'].'</label>
						<input type="text" name="'.$name.'-link" value="'.$file_values['link'].'">
						</div>
						<div class="vtem-cols"><label>'.$file_lang_arr['title'].'</label>
						<input type="text" name="'.$name.'-title" value="'.$file_values['title'].'">
						</div>
						<textarea style="width:94%;min-height:60px" name="'.$name.'-desc">'.$file_values['desc'].'</textarea></b>
					</fieldset>
					</div>
                    </li>
                </ul>';
		if ($li_atrr)
			$str = '<li '.$li_atrr.'>'.$str.'</li>';
		return $str;
	}
	public function inputTag($name, $value, $title, $attr, $li_atrr = '', $ul_attr = '', $tooltip = '')
	{
		$id = 'params_'.$name;
		$str = '<ul '.$ul_attr.'>
					<li class="vtem-config-left">
						<label for="'.$id.'">'.$title.'</label>
					</li>
					<li class="vtem-config-right">
						<input name="'.$name.'" id="'.$id.'" value="'.$value.'" '.$attr.' type="text">';
		if ($tooltip)
			$str .= '<i class="clearfix">'.$tooltip.'</i>';
		$str .= '</li></ul>';
		if ($li_atrr)
			$str = '<li '.$li_atrr.'>'.$str.'</li>';
		return $str;
	}
	public function inputTagLang($div_lang_name, $name, $arrvalue, $title, $attr, $li_atrr = '', $ul_attr = '', $tooltip = '')
	{
		self::languages();
		$str_value = '';
		foreach ($this->languages as $language)
			$str_value .= '<div id="'.$name.'_'.$language['id_lang'].'" '.$attr.' style="display: '.
				($language['id_lang'] == $this->default_form_language ? 'block' : 'none').';float: left;width: 230px;">
			<input size="29" type="text" id="c'.$name.'_'.$language['id_lang'].'" name="'.$name.'_'.
				$language['id_lang'].'" value="'.$arrvalue[$language['id_lang']].'" />
			</div>';
		$str = '<ul '.$ul_attr.'>
			<li class="vtem-config-left">
			<label>'.$title.'</label>
			</li>
			<li class="vtem-config-right">'.$str_value.$this->displayFlags($this->languages, $this->default_form_language, $div_lang_name, $name);
		if ($tooltip)
			$str .= '<i class="clearfix">'.$tooltip.'</i>';
		$str .= '</li></ul>';
		if ($li_atrr)
			$str = '<li '.$li_atrr.'>'.$str.'</li>';
		return $str;
	}
	public function getCategory($name, $value, $title, $attr, $li_atrr = '', $ul_attr = '', $tooltip = '', $text_all_cat = '')
	{
		$children = $this->getIndexedCategories();
		$list = array();
		$this->treeCategory(1, $list, $children);
		$cat_array = explode(',', $value);
		$id = 'params_'.$name;
		$id = str_replace('[]', '', $id);
		$is_selected = (in_array('', $cat_array)) ? 'selected="selected"' : '';
		$options = '<option value="" onclick="vtemSelectAll(\'#params_category\');" '.$is_selected.'>-- '.$text_all_cat.'</option>';
		foreach ($list as $cat)
		{
			$is_selected = (in_array($cat['id_category'], $cat_array) || in_array('', $cat_array)) ? 'selected="selected"' : '';
			$options  .= '<option value="'.$cat['id_category'].'" '.$is_selected.'>---| '.$cat['tree'].$cat['name'].'</option>';
		}
		$str = '<ul '.$ul_attr.'>
			<li class="vtem-config-left">
			<label for="'.$id.'">'.$title.'</label>
			</li>
			<li class="vtem-config-right">
			<select '.$attr.' id="'.$id.'" name="'.$name.'">'.$options.'</select>';
		if ($tooltip)
			$str .= '<i class="clearfix">'.$tooltip.'</i>';
		$str .= '</li></ul>';
		if ($li_atrr)
			$str = '<li '.$li_atrr.'>'.$str.'</li>';
		return $str;
	}
	public function radioBooleanTag($name, $yes_no_lang, $value, $title, $attr, $li_atrr = '', $ul_attr = '', $tooltip = '')
	{
		$str = '<ul '.$ul_attr.'>
					<li class="vtem-config-left">
					<label>'.$title.'</label>
					</li>
					<li class="vtem-config-right">';
		foreach ($yes_no_lang as $key => $val)
		{
			$is_checked = ($key == $value) ? 'checked="checked"' : '';
			$str .= '<input type="radio" value="'.$key.'" id="params'.$name.$key.'" name="'.$name.'" '.$attr.' '.$is_checked.'>
						<label for="params'.$name.$key.'">'.$val.'</label>';
			$attr = '';
		}
		if ($tooltip)
			$str .= '<i class="clearfix">'.$tooltip.'</i>';
		$str .= '</li></ul>';
		if ($li_atrr)
			$str = '<li '.$li_atrr.'>'.$str.'</li>';
		return $str;
	}
	public function vtemGroupTag($title, $class, $li_atrr = '', $ul_attr = '')
	{
		$str = '<ul '.$ul_attr.'>
					<li class="vtem-config-left">&nbsp;</li>
					<li class="vtem-config-right">
					<div class="'.$class.'">'.$title.'</div>
					</li>
					</ul>';
		if ($li_atrr)
			$str = '<li '.$li_atrr.'>'.$str.'</li>';
		return $str;
	}
	public function vtemOverrideLinksTag($value, $title, $add_row_text, $li_atrr = '', $ul_attr = '', $tooltip = '')
	{
		$str = '<ul '.$ul_attr.'>
                    <li class="vtem-config-left">
                        <label>'.$title.'</label>
                    </li>
                    <li class="vtem-config-right">
                        <fieldset class="it-addrow-block">
                            <legend><span class="add" id="btna-override_links">'.$add_row_text.'</span></legend>';
		if ($value)
		{
			$link_array = explode(',', $value);
			foreach ($link_array as $key => $value)
			{
				$str .= '<div class="row">
						<span class="spantext">'.($key + 1).'</span>
						<input type="text" value="'.$value.'" name="override_links[]">
						<span class="remove"></span>
						</div>';
			}
		}
		$str .= '</fieldset>';
		if ($tooltip)
			$str .= '<i class="clearfix">'.$tooltip.'</i>';
		$str .= '</li></ul>';
		if ($li_atrr)
			$str = '<li '.$li_atrr.'>'.$str.'</li>';
		return $str;
	}
	public function selectTag($name, $values = array(), $value, $title, $attr, $li_atrr = '', $ul_attr = '', $tooltip = '')
	{
		$id = 'params_'.$name;
		$options = '';
		foreach ($values as $val => $text)
		{
			$is_selected = ($val == $value) ? 'selected="selected"' : '';
			$options .= '<option '.$is_selected.' value="'.$val.'">'.$text.'</option>';
		}
		$str = '<ul '.$ul_attr.'>
                    <li class="vtem-config-left">
                        <label for="'.$id.'">'.$title.'</label>
                    </li>
                    <li class="vtem-config-right">
                        <select '.$attr.' id="'.$id.'" name="'.$name.'">'.$options.'</select>';
		if ($tooltip)
			$str .= '<i class="clearfix">'.$tooltip.'</i>';
		$str .= '</li></ul>';
		if ($li_atrr)
			$str = '<li '.$li_atrr.'>'.$str.'</li>';
		return $str;
	}
	public static function treeCategory($id, &$list, $children, $tree = '')
	{
		if (isset($children[$id]))
		{
			if ($id != 0)
				$tree = $tree.' - ';
			foreach ($children[$id] as $v)
			{
				$v['tree'] = $tree;
				$list[] = $v;
				self::treeCategory($v['id_category'], $list, $children, $tree);
			}
		}
	}
	public function getIndexedCategories()
	{
		$id_lang = (int)$this->context->language->id;
		$join = 'JOIN `'._DB_PREFIX_.'category_shop` cs ON(c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)Context::getContext()->shop->id.')';
		$all_cat = Db::getInstance()->ExecuteS('
		SELECT c.*, cl.id_lang, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description
		FROM `'._DB_PREFIX_.'category` c
		'.$join.'
		LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.(int)$id_lang.')
		LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = c.`id_category`)
		WHERE `active` = 1
		GROUP BY c.`id_category`
		ORDER BY `id_category` ASC');
		$children = array();
		if ($all_cat)
		{
			foreach ($all_cat as $v)
			{
				$pt = $v['id_parent'];
				$list = isset($children[$pt]) ? $children[$pt] : array();
				array_push($list, $v);
				$children[$pt] = $list;
			}
			return $children;
		}
		return array();
	}
	public function getIndexedCMSCategories()
	{
		$id_lang = (int)$this->context->language->id;
		$all_cat = Db::getInstance()->ExecuteS('
		SELECT c.*, cl.id_lang, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description
		FROM `'._DB_PREFIX_.'cms_category` c
		LEFT JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON (c.`id_cms_category` = cl.`id_cms_category` AND `id_lang` = '.(int)$id_lang.')
		WHERE `active` = 1
		GROUP BY c.`id_cms_category`
		ORDER BY `name` ASC');
		$children = array();
		if ($all_cat)
		{
			foreach ($all_cat as $v)
			{
				$pt = $v['id_parent'];
				$list = $children[$pt] ? $children[$pt] : array();
				array_push($list, $v);
				$children[$pt] = $list;
			}
			return $children;
		}
		return array();
	}
	public static function treeCMSCategory($id, &$list, $children, $tree = '')
	{
		if (isset($children[$id]))
		{
			if ($id != 0)
				$tree = $tree.' - ';
			foreach ($children[$id] as $v)
			{
				$v['tree'] = $tree;
				$list[] = $v;
				self::treeCMSCategory($v['id_cms_category'], $list, $children, $tree);
			}
		}
	}
	public function getModules()
	{
		$not_module = array($this->name);
		$where = '';
		if (count($not_module) == 1)
			$where = 'WHERE m.`name` <> \''.$not_module[0].'\'';
		elseif (count($not_module) > 1)
			$where = 'WHERE m.`name` NOT IN (\''.implode("','", $not_module).'\')';
		$id_shop = $this->context->shop->id;
		return Db::getInstance()->ExecuteS('
		SELECT m.*
		FROM `'._DB_PREFIX_.'module` m
		JOIN `'._DB_PREFIX_.'module_shop` ms ON (m.`id_module` = ms.`id_module` AND ms.`id_shop` = '.(int)$id_shop.')
		'.$where );
	}
	public function getModulById($id_module)
	{
		return Db::getInstance()->getRow('
		SELECT m.*
		FROM `'._DB_PREFIX_.'module` m
		JOIN `'._DB_PREFIX_.'module_shop` ms ON (m.`id_module` = ms.`id_module` AND ms.`id_shop` = '.(int)$this->context->shop->id.')
		WHERE m.`id_module` = '.$id_module);
	}
	public function getHooksByModuleId($id_module)
	{
		$module = self::getModulById($id_module);
		$module_instance = Module::getInstanceByName($module['name']);
		$hooks = array();
		if ($this->hook_assign)
			foreach ($this->hook_assign as $hook)
			{
				$retro_hook_name = Hook::getRetroHookName($hook);
				if (is_callable(array($module_instance, 'hook'.$hook)) || is_callable(array($module_instance, 'hook'.$retro_hook_name)))
					$hooks[] = $retro_hook_name;
			}
		$results = self::getHookByArrName($hooks);
		return $results;
	}
	public static function getHookById($id_hook)
	{
		return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
		SELECT *
		FROM `'._DB_PREFIX_.'hook`
		WHERE `id_hook` = '.$id_hook);
	}
	public static function getHookByArrName($arr_name)
	{
		$result = Db::getInstance()->ExecuteS('
		SELECT `id_hook`, `name`
		FROM `'._DB_PREFIX_.'hook`
		WHERE `name` IN (\''.implode("','", $arr_name).'\')');
		return $result;
	}
	public function languages()
	{
		$allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
		if ($allow_employee_form_lang && !$this->context->cookie->employee_form_lang)
			$this->context->cookie->employee_form_lang = (int)Configuration::get('PS_LANG_DEFAULT');
		$use_lang_from_cookie = false;
		$this->languages = Language::getLanguages(false);
		if ($allow_employee_form_lang)
			foreach ($this->languages as $lang)
				if ($this->context->cookie->employee_form_lang == $lang['id_lang'])
					$use_lang_from_cookie = true;
		if (!$use_lang_from_cookie)
			$this->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
		else
			$this->default_form_language = (int)$this->context->cookie->employee_form_lang;
	}
	public function displayFlags($languages, $default_language, $ids, $id)
	{
		if (count($languages) == 1)
			return false;
		$output = '<div class="displayed_flag clearfix">
			<img src="../img/l/'.$default_language.'.jpg" class="pointer" id="language_current_'.$id.'" onclick="toggleLanguageFlags(this);" alt="" />
		</div>
		<div id="languages_'.$id.'" class="language_flags clearfix">
			'.$this->l('Choose language:').'';
		foreach ($languages as $language)
			$output .= '<img src="../img/l/'.(int)$language['id_lang'].'.jpg" class="pointer" alt="'.$language['name'].'" title="'.
							$language['name'].'" onclick="changeLanguage1(\''.$id.'\', \''.$ids.'\', '.$language['id_lang'].', \''.$language['iso_code'].'\');" /> ';
		$output .= '</div>';
			return $output;
	}
	public function getStrHook($module_name, $hook_name)
	{
		$module = Module::getInstanceByName($module_name);
		if (!$module->id)
			return '';
		$hooks = self::getHooksByModuleId($module->id);
		$options = '';
		if (!empty($hooks))
		{
			foreach ($hooks as $hook)
				$options .= '<option value="'.$hook['name'].'"'.
								(Tools::strtolower($hook['name']) == Tools::strtolower($hook_name) ? 'selected="selected"' : '').'>'.$hook['name'].'</option>';
		}
		return $options;
	}
	public function vtemGetCategories()
	{
		$children = $this->getIndexedCategories();
		$array_keys = array_keys($children);
		$first_key = array_shift($array_keys);
		$list = array();
		$this->treeCategory($first_key, $list, $children);
		return $list;
	}
}
}
?>