<?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 (!defined('_PS_VERSION_'))
	exit;
define('VTEM_FOOTER_LIMIT_BLOCK', 5);
class VtemFooter extends Module
{
	private $params = '';
	private $post_errors = array();
	public $languages = null;
	public $default_form_language = null;
	public $base_config_url;
	public $type;
	public $linktype;
	public $hook_assign = array();
	public $vtem_position;
	public function __construct()
	{
		$this->name = 'vtemfooter';
		parent::__construct();
		$this->tab = 'others';
		$this->version = '1.0';
		$this->bootstrap = true;
		$this->author = 'PrestAddon';
		$this->displayName = $this->l('VTEM Footer');
		$this->description = $this->l('Footer region is where you can show many important information across the site. It is very easy to use.');
		$this->vtem_position = array(1, 2, 3, 4);
		require_once(dirname(__FILE__).'/libs/params.php');
		require_once(dirname(__FILE__).'/libs/vtemBlock.php');
		require_once(dirname(__FILE__).'/libs/vtemItem.php');
		$this->secure_key = Tools::encrypt($this->name);
		$this->languages();
		$this->base_config_url = AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getValue('token');
		$this->type = array('link', 'custom_html', 'module', 'gmap', 'addthis');
		$this->linktype = array('product', 'category', 'cms', 'link', 'manufacturer', 'supplier');
		$this->hook_assign = array('rightcolumn', 'leftcolumn', 'home', 'top', 'footer');
		$this->params = new VtemFooterCustomParams($this->name, $this->hook_assign);
	}
	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 install()
	{
		if (!parent::install() || !$this->registerHook('footer') || !$this->registerHook('actionShopDataDuplication') || !$this->installTradDone())
			return false;
		return true;
	}
	public function uninstall()
	{
		if (!parent::uninstall())
			return false;
		if (!$this->uninstallTradDone())
			return false;
		return true;
	}
	/**
	* CREATE database
	*
	*/
	private function installTradDone()
	{
		$query = '';
		$error = true;
		require_once(dirname(__FILE__).'/libs/sql.tables.php');
		if (isset($query) && !empty($query))
		{
			$data = Db::getInstance()->ExecuteS("SHOW TABLES LIKE '"._DB_PREFIX_."vtfooter_block'");
			if (!$data)
			{
				$query = str_replace('_DB_PREFIX_', _DB_PREFIX_, $query);
				$query = str_replace('_MYSQL_ENGINE_', _MYSQL_ENGINE_, $query);
				$db_data_settings = preg_split("/;\s*[\r\n]+/", $query);
				foreach ($db_data_settings as $query)
				{
					$query = trim($query);
					if (!empty($query))
					{
						if (!Db::getInstance()->Execute($query))
							$error = false;
					}
				}
			}
		}
		else
			$error = false;
		if (!$error)
			return $error;
		$return = true;
		$id_shops = Shop::getShops(false, null, true);
		if (!($data = Db::getInstance()->ExecuteS( "SHOW TABLES LIKE '"._DB_PREFIX_."vtfooter_block_shop'" )))
		{
			$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'vtfooter_block_shop` (
				  `id_vtfooter_block` int(11) NOT NULL,
				  `id_shop` int(11) NOT NULL,
				  PRIMARY KEY (`id_vtfooter_block`,`id_shop`)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;';
			$result = Db::getInstance()->Execute($sql);
			if (!$result)
				return $result;
			$sql = 'SELECT id_vtfooter_block FROM `'._DB_PREFIX_.'vtfooter_block`';
			$id_blocks = Db::getInstance()->ExecuteS($sql);
			if ($id_blocks && $id_shops)
			{
				foreach ($id_shops as $id_shop)
					foreach ($id_blocks as $block)
						$return &= Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.
									'vtfooter_block_shop` (`id_vtfooter_block`, `id_shop`) VALUES('.(int)$block['id_vtfooter_block'].', '.(int)$id_shop.')');
			}
		}
		if (!($data = Db::getInstance()->ExecuteS("SHOW TABLES LIKE '"._DB_PREFIX_."vtfooter_block_item_shop'")))
		{
			$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'vtfooter_block_item_shop` (
				  `id_vtfooter_block_item` int(11) NOT NULL,
				  `id_shop` int(11) NOT NULL,
				  PRIMARY KEY (`id_vtfooter_block_item`,`id_shop`)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;';
			$result = Db::getInstance()->Execute($sql);
			if (!$result)
				return $result;
			$sql = 'SELECT id_vtfooter_block_item FROM `'._DB_PREFIX_.'vtfooter_block_item`';
			$id_items = Db::getInstance()->ExecuteS($sql);
			if ($id_items && $id_shops)
			{
				foreach ($id_shops as $id_shop)
					foreach ($id_items as $item)
						$return &= Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'vtfooter_block_item_shop` (`id_vtfooter_block_item`, `id_shop`)
										VALUES('.(int)$item['id_vtfooter_block_item'].', '.(int)$id_shop.')');
			}
		}
		return $return;
	}
	/**
	* DROP Table
	*/
	private function uninstallTradDone()
	{
		$query = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'vtfooter_block`,`'._DB_PREFIX_.'vtfooter_block_item`,`'._DB_PREFIX_.
					'vtfooter_block_item_lang`,`'._DB_PREFIX_.'vtfooter_block_lang`,`'._DB_PREFIX_.'vtfooter_block_item_shop`,`'._DB_PREFIX_.'vtfooter_block_shop`;';
		return Db::getInstance()->Execute($query);
	}
	public function hookActionShopDataDuplication($params)
	{
		Db::getInstance()->execute('
		INSERT IGNORE INTO '._DB_PREFIX_.'vtfooter_block_shop (id_vtfooter_block, id_shop)
		SELECT id_vtfooter_block, '.(int)$params['new_id_shop'].'
		FROM '._DB_PREFIX_.'vtfooter_block_shop
		WHERE id_shop = '.(int)$params['old_id_shop']);
		Db::getInstance()->execute('
		INSERT IGNORE INTO '._DB_PREFIX_.'vtfooter_block_item_shop (id_vtfooter_block_item, id_shop)
		SELECT id_vtfooter_block_item, '.(int)$params['new_id_shop'].'
		FROM '._DB_PREFIX_.'vtfooter_block_item_shop
		WHERE id_shop = '.(int)$params['old_id_shop']);
	}
	/**
	* Hook Left
	*/
	public function hookfooter($params)
	{
		return $this->processHook($params, 'footer');
	}
	/**
	* Proccess module by hook
	* $pparams: param of module
	* $pos: position call
	*/
	public function processHook($params = array(), $pos = 'footer')
	{
		$vtem_position = $this->vtem_position;
		$params = $this->params;
		$module_theme = $params->get('theme', 'default');
		$module_class = $params->get('class', 'customfooter');
		if (!$vtem_position)
			return '';
		$positions = array();
		$widths = array();
		foreach ($vtem_position as $key => $p)
		{
			$blocks = VtemBlock::getBlocks($p, $this->context->language->id);
			if ($blocks)
			{
				$nbw = 0;
				$tw = 0;
				$arrblocks = array();
				foreach ($blocks as &$b)
				{
					$items = VtemBlock::getItems($b['id_vtfooter_block'], $this->context->language->id);
					if ($items)
					{
						foreach ($items as &$i)
						{
							if ($i['type'] == 'link')
								$i['link_item'] = htmlentities($this->getLinkItem( $i['link_content'], $i['linktype'] ));
							elseif ($i['type'] == 'module')
								$i['module'] = $this->getModuleAssign($i['module_name'], $i['hook_name']);
						}
						if ($b['width'] > 0)
							$nbw += 1;
						$tw += $b['width'];
						$b['items'] = $items;
						$arrblocks[] = $b;
					}
				}
				if ($arrblocks)
				{
					$widths[$key]['nbw'] = $nbw;
					$widths[$key]['tw'] = $tw;
					$positions[$key] = $arrblocks;
				}
			}
		}
		$posis = $this->calWidth($positions, $widths);
		$this->smarty->assign(array(
			'vtempositions' => $posis,
			'module_theme' => $module_theme,
			'pos' => $pos,
			'module_class' => $module_class));
		return $this->display(__FILE__, 'vtemfooter.tpl');
	}
	public function calWidth($positions, $widths)
	{
		foreach ($positions as $key => &$bls)
		{
			$nbw = $widths[$key]['nbw'];
			$tw = $widths[$key]['tw'];
			$w = Tools::floorf((100 - $tw) / (count($bls) - $nbw <= 0 ? 1 : count($bls) - $nbw), 2);
			foreach ($bls as &$bl)
				$bl['width'] = ($bl['width'] > 0 ? $bl['width'] : $w);
		}
		return $positions;
	}
	public function getLinkItem($value, $type)
	{
		$result = '';
		switch ($type)
		{
			case 'product':
				if (Validate::isLoadedObject($obj_pro = new Product($value, true, $this->context->language->id)))
					$result = $this->context->link->getProductLink((int)$obj_pro->id, $obj_pro->link_rewrite, null, null, $this->context->language->id);
			break;
			case 'category':
				if (Validate::isLoadedObject($obj_cate = new Category($value, $this->context->language->id)))
					$result = $this->context->link->getCategoryLink((int)$obj_cate->id, $obj_cate->link_rewrite, $this->context->language->id);
			break;
			case 'cms':
				if (Validate::isLoadedObject($obj_cms = new CMS($value, $this->context->language->id)))
					$result = $this->context->link->getCMSLink((int)$obj_cms->id, $obj_cms->link_rewrite, $this->context->language->id);
			break;
			case 'link':
				$result = strrpos($value, 'http://') === false ? $this->context->link->getPageLink($value, false) : $value;
			break;
			case 'manufacturer':
				if (Validate::isLoadedObject($obj_manu = new Manufacturer($value, $this->context->language->id)))
					$result = $this->context->link->getManufacturerLink((int)$obj_manu->id, $obj_manu->link_rewrite, $this->context->language->id);
			break;
			case 'supplier':
				if (Validate::isLoadedObject($obj_supp = new Supplier($value, $this->context->language->id)))
					$result = $this->context->link->getSupplierLink((int)$obj_supp->id, $obj_supp->link_rewrite, $this->context->language->id);
			break;
		}
		return $result;
	}
	public function getModuleAssign($module_name = '', $hook_name = '')
	{
		$module = Module::getInstanceByName($module_name);
		$id_hook = Hook::getIdByName($hook_name);
		if (Validate::isLoadedObject($module) && $module->id)
		{
			$array = array();
			$array['id_hook'] = $id_hook;
			$array['module'] = $module_name;
			$array['id_module'] = $module->id;
			$hook_name = Tools::substr($hook_name, 7, Tools::strlen($hook_name));
			return self::vtemHookExec($hook_name, array(), $module->id, $array);
		}
		return '';
	}
	public static function vtemHookExec($hook_name, $hook_args = array(), $id_module = null, $array = array())
	{
		if ((!empty($id_module) && !Validate::isUnsignedId($id_module)) || !Validate::isHookName($hook_name))
			die(Tools::displayError());
		if (!isset($hook_args['cookie']) || !$hook_args['cookie'])
			$hook_args['cookie'] = Context::getContext()->cookie;
		if (!isset($hook_args['cart']) || !$hook_args['cart'])
			$hook_args['cart'] = Context::getContext()->cart;
		if ($id_module && $id_module != $array['id_module'])
			return;
		if (!($module_instance = Module::getInstanceByName($array['module'])))
			return;
		$retro_hook_name = Hook::getRetroHookName($hook_name);
		$hook_callable = is_callable(array($module_instance, 'hook'.$hook_name));
		$hook_retro_callable = is_callable(array($module_instance, 'hook'.$retro_hook_name));
		$output = '';
		if (($hook_callable || $hook_retro_callable) && Module::preCall($module_instance->name))
		{
			if ($hook_callable)
				$output = $module_instance->{'hook'.$hook_name}($hook_args);
			elseif ($hook_retro_callable)
				$output = $module_instance->{'hook'.$retro_hook_name}($hook_args);
		}
		return $output;
	}
	public function getFolderList($path)
	{
		$items = array();
		$handle = opendir($path);
		if (!$handle)
			return $items;
		while (false !== ($file = readdir($handle)))
		{
			if (is_dir($path.$file))
				$items[$file] = $file;
		}
		unset($items['.'], $items['..'], $items['.svn']);
		return $items;
	}
	public function getContent()
	{
		$html = '';
		if (Tools::isSubmit('submit'))
		{
			if (!count($this->post_errors))
			{
				$defined_configs = array('theme' => '', 'class' => '');
				foreach ($defined_configs as $config)
				{
					if (Tools::strlen($this->name.'_'.$config) >= 32)
						echo $this->name.'_'.$config;
					else
						Configuration::updateValue($this->name.'_'.$config, Tools::getValue($config), true);
				}
					$html .= '<div class="conf confirm">'.$this->l('Settings updated successful').'</div>';
			}
			else
			{
				foreach ($this->post_errors as $err)
					$html .= '<div class="alert error">'.$err.'</div>';
			}
		}
		elseif (Tools::getValue('submitDeleteBlock'))
		{
			if (Tools::getValue('id_vtfooter_block') && Validate::isLoadedObject($obj = new VtemBlock(Tools::getValue('id_vtfooter_block'))) && $obj->delete())
				Tools::redirectAdmin($this->base_config_url.'&vtemconf=1');
			else
				Tools::redirectAdmin($this->base_config_url.'&vtemconf=2');
		}
		elseif (Tools::isSubmit('submitDeleteData'))
		{
			$shops = Tools::getValue('shops');
			$items = Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'vtfooter_block_item`' );
			foreach ($items as $item)
			{
				$sql = 'DELETE FROM `'._DB_PREFIX_.'vtfooter_block_item_shop` WHERE `id_vtfooter_block_item` = '.
						(int)$item['id_vtfooter_block_item'].' AND `id_shop` IN ('.implode(',', $shops).')';
				Db::getInstance()->execute($sql);
				$sql = 'SELECT * FROM `'._DB_PREFIX_.'vtfooter_block_item_shop` WHERE `id_vtfooter_block_item` = '.(int)$item['id_vtfooter_block_item'];
				$result = Db::getInstance()->executes($sql);
				if (!$result)
				{
					$obj = new VtemItem((int)$item['id_vtfooter_block_item']);
					$obj->delete();
				}
			}
			$blocks = Db::getInstance()->ExecuteS('SELECT * FROM `'._DB_PREFIX_.'vtfooter_block`');
			if ($blocks)
			{
				foreach ($blocks as $block)
				{
					$sql = 'DELETE FROM `'._DB_PREFIX_.'vtfooter_block_shop` WHERE `id_vtfooter_block` = '.
							(int)$block['id_vtfooter_block'].' AND `id_shop` IN ('.implode(',', $shops).')';
					Db::getInstance()->execute($sql);
					$sql = 'SELECT * FROM `'._DB_PREFIX_.'vtfooter_block_shop` WHERE `id_vtfooter_block` = '.(int)$block['id_vtfooter_block'];
					if (!($result = Db::getInstance()->executes($sql)))
					{
						$obj = new VtemBlock((int)$block['id_vtfooter_block']);
						$obj->delete();
					}
				}
			}
		}
		$this->params = new VtemFooterCustomParams($this->name, $this->hook_assign);
		if (Tools::getValue('vtemconf') == 1)
			$html .= '<div class="conf confirm">'.$this->l('Delete Block successful').'</div>';
		elseif (Tools::getValue('vtemconf') == 2)
			$html .= '<div class="alert error">'.$this->l('Delete Block error').'</div>';
		if (count($this->post_errors))
		{
			foreach ($this->post_errors as $err)
				$html .= '<div class="alert error">'.$err.'</div>';
		}
		return $html.$this->getFormConfig();
	}
	private function getFormConfig()
	{
		$html = '';
		$context = Context::getContext();
		$context->controller->addCSS($this->_path.'/views/templates/admin/css/form.css', 'all');
		$context->controller->addJqueryPlugin(array('fancybox', 'tablednd'));
		$context->controller->addJS($this->_path.'views/templates/admin/js/form.js');
		$context->controller->addJS($this->_path.'views/templates/admin/js/dragdrop.js');
		$html .= '<script type="text/javascript">
			var id_language = '.$this->default_form_language.';
			vtemBlocksDnD("'.$this->secure_key.'");
			$(document).ready( function(){
				$("#vtemform").delegate(".vtemaddnew-block-item", "hover" , function(){
					$(this).fancybox({"width":980, "height":500, "type":"iframe", "scrolling":"yes", "titleShow": false});
				});
			});
		</script>';
		$html .= '<div class="vtemfooter-wrap panel">
			<h2 class="panel-heading">'.$this->l('Footer Configuration').'</h2>
			<div id="vtemform">';
				$vtem_position = $this->vtem_position;
				$div_lang_name = '';
				foreach ($vtem_position as $id_position)
				{
					$blocks = VtemBlock::getBlocks($id_position, $this->context->language->id);
					if ($blocks)
					{
						foreach ($blocks as $key => $b)
							$div_lang_name .= 'title'.$b['id_vtfooter_block'].'¤';
					}
				}
				$div_lang_name = rtrim($div_lang_name, '¤');
				foreach ($vtem_position as $id_position)
				{
					$blocks = VtemBlock::getBlocks($id_position, $this->context->language->id);
					$html .= '<fieldset class="vtemfieldset panel clearfix" id="vtemposition-'.$id_position.'">
									<h3 class="panel-heading"><img src="../img/admin/cog.gif" /> '.$this->l('Position').$id_position.'</h3>';
									if (count($blocks) < VTEM_FOOTER_LIMIT_BLOCK)
										$html .= '<a onclick="return false;" class="vtemaddnew-block-item button btn btn-default vtemaddnew-block-'.$id_position.'" 
														href="'._MODULE_DIR_.$this->name.'/popup.php?id_shop='.(int)$this->context->shop->id.'&id_position='.$id_position.
															'&addBlock=1&bo_theme='.$context->employee->bo_theme.'&id_divposition=vtemposition-'.$id_position.'&id_lang=
															'.$this->context->language->id.'&nb='.count($blocks).'&token='.Tools::getValue('token').'&secure_key=
															'.$this->secure_key.'" title="add">
											<img src="../img/admin/more.png"/> '.$this->l('Add new block').
										'</a><br class="clear clearfix"/>';
									if ($blocks)
									{
										foreach ($blocks as $key => $b)
										{
											$items = VtemBlock::getItems($b['id_vtfooter_block'], $this->context->language->id);
											$obj = new VtemBlock($b['id_vtfooter_block']);
											$html .= '<div class="vtem-block">
											<form id="form-'.$b['id_vtfooter_block'].'" action="'.$this->base_config_url.'&rand='.rand().
													'" method="post" enctype="multipart/form-data">
												<div class="vtem-load" style="display:none;"></div>
												<a class="vtem-delete-block" style="display:none;" href="'.$this->base_config_url.
												'&submitDeleteBlock=1&id_vtfooter_block='.$b['id_vtfooter_block'].'">&nbsp;</a>
												<input type="hidden" value="'.$b['id_vtfooter_block'].'" name="id_vtfooter_block"/>
												<div class="clearfix">
												<label>'.$this->l('Title:').'</label>
												<div class="margin-form">';
												foreach ($this->languages as $language)
													$html .= '<div class="input-group" id="title'.$b['id_vtfooter_block'].'_'.$language['id_lang'].'" style="display:'.
													($language['id_lang'] == $this->default_form_language ? 'block' : 'none').'; float: left;">
													<input size="20" class="fixed-width-md" type="text" name="title_'.$language['id_lang'].'" value="'.
													(isset($obj->title[(int)$language['id_lang']]) ? $obj->title[(int)$language['id_lang']] : '').'" />
														<span class="input-group-addon">*</span></div>';
													$html .= $this->displayFlags($this->languages, $this->default_form_language, $div_lang_name, 'title'.$b['id_vtfooter_block'], true);
												$html .= '</div></div>
												<label>'.$this->l('Show Title:').'</label>
												<div class="margin-form">
													<input type="radio" name="show_title" id="show_title_on" onclick="toggleDraftWarning(false);" value="1" '.
														($b['show_title'] ? 'checked="checked" ' : '').'/>
													<label class="t" for="show_title_on"><img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'"/></label>
													<input type="radio" name="show_title" id="show_title_off" onclick="toggleDraftWarning(true);" value="0" '.
														(!$b['show_title'] ? 'checked="checked" ' : '').'/>
													<label class="t" for="show_title_off"><img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'"/></label>
												</div>
												<label>'.$this->l('Width:').'</label>
												<div class="margin-form">
													<div class="input-group">
														<input type="text" value="'.$b['width'].'" name="width" size="10" class="vtem-width fixed-width-sm"/>
															<span class="input-group-addon">%</span>
													</div>
													<p class="clear text-muted">'.$this->l('enter a number < 100').'</p>
												</div>
												<div class="margin-form">
													<a href="javascript:void(0)" class="button btn btn-default" onclick="submitForm(\'form-'.
													$b['id_vtfooter_block'].'\')">'.$this->l('Update').'</a>
												</div>
											</form>';
											$html .= '<a onclick="return false;" class="vtemaddnew-block-item vtemaddnew-item" href="'._MODULE_DIR_.$this->name.'/popup.php?
											id_shop='.(int)$this->context->shop->id.'&id_vtfooter_block='.$b['id_vtfooter_block'].'&addItem=1
											&bo_theme='.$context->employee->bo_theme.'&id_vtemtable=vtemtable-'.$b['id_vtfooter_block'].
											'&id_lang='.$this->context->language->id.'&token='.Tools::getValue('token').'&secure_key='.$this->secure_key.'" title="add">
											<img src="../img/admin/add.gif" /> '.$this->l('Add new item').
											'</a><br/>
											<table class="table vtemtable tableDnD" id="vtemtable-'.$b['id_vtfooter_block'].'">
												<thead><tr class="nodrag nodrop">
													<th width="60%">'.$this->l('Title').'</th>
													<th width="30%">'.$this->l('Type').'</th>
													<th width="10%">'.$this->l('Action').'</th>
												</tr></thead>
												<tbody>';
												if ($items)
												{
													foreach ($items as $key => $item)
													{
														$html .= '<tr id="tr_'.$b['id_vtfooter_block'].'_'.$item['id_vtfooter_block_item'].'_'.$item['position'].'" class="tr_0_'.
															$item['id_vtfooter_block_item'].'_'.$b['id_vtfooter_block'].' '.($key % 2 ? '' : 'alt_row').'">
														<td class="dragHandle">'.$item['title'].'</td>
														<td class="dragHandle">'.$item['type'].'</td>
														<td>
															<a class="display vtemaddnew-block-item" href="'._MODULE_DIR_.$this->name.'/popup.php?id_shop='.(int)$this->context->shop->id.
															'&id_vtfooter_block='.$b['id_vtfooter_block'].'&addItem=1&bo_theme='.$context->employee->bo_theme.
															'&id_vtfooter_block_item='.$item['id_vtfooter_block_item'].'&id_vtemtr=tr_0_'.$item['id_vtfooter_block_item'].'_'.
															$b['id_vtfooter_block'].'&id_lang='.$this->context->language->id.'&token='.Tools::getValue('token').
															'&secure_key='.$this->secure_key.'" title="edit">
																<img title="edit" alt="edit" src="../img/admin/edit.gif"></a>
															<a class="display" onclick="VtemDelete(\'tr_0_'.$item['id_vtfooter_block_item'].'_'.$b['id_vtfooter_block'].'\', \'vtemtable-'.
															$b['id_vtfooter_block'].'\')" href="javascript:void(0)" title="delete">
																<img title="delete" alt="delete" src="../img/admin/delete.gif"></a>
														</td>
													</tr>';
													}
												}
											$html .= '</tbody>
											</table>
										</div>';
										}
									}
					$html .= '</fieldset>';
				}
			$html .= '</div>
		</div>';
		return $html;
	}
	public function getParamValue($name, $default = '')
	{
		return $this->params->get($name, $default);
	}
	public function getParamHooks($module_name, $hook_name)
	{
		return $this->params->getStrHook($module_name, $hook_name);
	}
	public function getModules()
	{
		$modules = $this->params->getModules();
		if (!$modules)
			return array();
		$result = array();
		foreach ($modules as $m)
		{
			if ($this->params->getHooksByModuleId($m['id_module']))
				$result[] = $m;
		}
		return $result;
	}
	public function getCategories()
	{
		return $this->params->vtemGetCategories();
	}
	public function displayVtemFlags($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="changeVtemLanguage(\''.$id.'\', \''.$ids.'\', '.$language['id_lang'].', \''.$language['iso_code'].'\');" /> ';
		$output .= '</div>';
			return $output;
	}
	public function getFolderAdmin()
	{
		$folders = array('cache', 'classes', 'config', 'controllers', 'css', 'docs', 'download', 'img', 'js', 'localization', 'log', 'mails',
		'modules', 'override', 'themes', 'tools', 'translations', 'upload', 'webservice', '.', '..');
		$handle = opendir(_PS_ROOT_DIR_);
		if (!$handle)
			return false;
		while (false !== ($folder = readdir($handle)))
		{
			if (is_dir(_PS_ROOT_DIR_.'/'.$folder))
			{
				if (!in_array($folder, $folders))
				{
					$folderadmin = opendir(_PS_ROOT_DIR_.'/'.$folder);
					if (!$folderadmin)
						return $folder;
					while (false !== ($file = readdir($folderadmin)))
					{
						if (is_file(_PS_ROOT_DIR_.'/'.$folder.'/'.$file) && ($file == 'header.inc.php'))
							return $folder;
					}
				}
			}
		}
	}
} 