<?php
/**
* LayerSlider v5.1.2 - Responsive Slideshow Module http://layerslider.webshopworks.com
*
*  @author    WebshopWorks <info@webshopworks.com>
*  @copyright 2015 WebshopWorks
*  @license   One Domain Licence
*/

if (!defined('_PS_VERSION_'))
	exit;

class PSOpts
{
	private static $tab = '&nbsp;&nbsp;&nbsp;&nbsp;';
	private static $products = array();

	private static function getProducts($lang, $id_category)
	{
		if (empty(self::$products[$lang]))
		{
			self::$products[$lang] = array();
			$products = &self::$products[$lang];
			$res = Db::getInstance()->executeS('SELECT cp.id_product, pl.name, MAX(cp.id_category) AS id_category '.'FROM '._DB_PREFIX_.'category_product AS cp '.
				'LEFT JOIN '._DB_PREFIX_.'product_lang AS pl '.'ON cp.id_product = pl.id_product '.'WHERE pl.id_lang = '.$lang.' GROUP BY cp.id_product '.
				'ORDER BY id_category, pl.name');
			foreach ($res as &$product)
			{
				$cat = $product['id_category'];
				if (!isset($products[$cat]))
					$products[$cat] = array();
				$products[$cat][] = &$product;
			}
		}
		return isset(self::$products[$lang][$id_category]) ? self::$products[$lang][$id_category] : array();
	}

	public static function getNestedCategories($root_category = null, $id_lang = false, $active = true, $groups = null, $use_shop_restriction = true, $sql_filter = '', $sql_sort = '', $sql_limit = '')
	{
		$id_shop = Context::getContext()->shop->id;

		if (isset($root_category) && !Validate::isInt($root_category))
			die(Tools::displayError());

		if (!Validate::isBool($active))
			die(Tools::displayError());

		if (isset($groups) && Group::isFeatureActive() && !is_array($groups))
			$groups = (array)$groups;

		$result = Db::getInstance()->executeS('
			SELECT c.*, cl.*
			FROM `'._DB_PREFIX_.'category` c
			'.($use_shop_restriction ? 'INNER JOIN '._DB_PREFIX_.
			'category_shop category_shop ON (category_shop.id_category = c.id_category AND category_shop.id_shop = 1)' : '').'
			LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category` AND cl.id_shop = '.$id_shop.'
			'.(isset($groups) && Group::isFeatureActive() ? 'LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON c.`id_category` = cg.`id_category`' : '').'
			'.(isset($root_category) ? 'RIGHT JOIN `'._DB_PREFIX_.'category` c2 ON c2.`id_category` = '.(int)$root_category.
			' AND c.`nleft` >= c2.`nleft` AND c.`nright` <= c2.`nright`' : '').'
			WHERE 1 '.$sql_filter.' '.($id_lang ? 'AND `id_lang` = '.(int)$id_lang : '').'
			'.($active ? ' AND c.`active` = 1' : '').'
			'.(isset($groups) && Group::isFeatureActive() ? ' AND cg.`id_group` IN ('.implode(',', $groups).')' : '').'
			'.(!$id_lang || (isset($groups) && Group::isFeatureActive()) ? ' GROUP BY c.`id_category`' : '').'
			'.($sql_sort != '' ? $sql_sort : ' ORDER BY c.`level_depth` ASC').'
			'.($sql_sort == '' && $use_shop_restriction ? ', category_shop.`position` ASC' : '').'
			'.($sql_limit != '' ? $sql_limit : ''));

		$categories = array();
		$buff = array();

		if (!isset($root_category))
			$root_category = Category::getRootCategory()->id;

		foreach ($result as $row)
		{
			$current = &$buff[$row['id_category']];
			$current = $row;

			if ($row['id_category'] == $root_category)
				$categories[$row['id_category']] = &$current;
			else
				$buff[$row['id_parent']]['children'][$row['id_category']] = &$current;
		}

		return $categories;
	}

	private static function generateCategories(&$cats = null, &$a, $tabs = '')
	{
		foreach ($cats as &$cat)
		{
			if ($cat['id_parent'] == 1)
			{
				$a[] = array('value' => 'all', 'option' => '- All -');
				$a[] = array('value' => '', 'option' => '- None -');
				$a[] = array('value' => 'index', 'option' => $tabs.$cat['name']);
			}
			else
				$a[] = array('value' => 'c-'.$cat['id_category'], 'option' => $tabs.$cat['name']);

			if (isset($cat['children']))
				self::generateCategories($cat['children'], $a, $tabs.self::$tab);

			$lang = (int)Context::getContext()->language->id;
			$prods = self::getProducts($lang, $cat['id_category']);
			foreach ($prods as &$prod)
				$a[] = array('value' => 'p-'.$prod['id_product'], 'option' => $tabs.self::$tab.$prod['name']);

		}
	}

	private static function generateCMSCategories(&$e, &$a, $tabs = '')
	{
		if ($e['id_cms_category'] == 1)
		{
			$a[] = array('value' => 'all', 'option' => '- All -');
			$a[] = array('value' => '', 'option' => '- None -');
			$a[] = array('value' => 'new-products', 'option' => $tabs.'New products');
			$a[] = array('value' => 'best-sales', 'option' => $tabs.'Best sellers');
			$a[] = array('value' => 'prices-drop', 'option' => $tabs.'Price drop');
			$a[] = array('value' => 'manufacturers', 'option' => $tabs.'Manufacturers');
			$a[] = array('value' => 'supplier', 'option' => $tabs.'Suppliers');
			$a[] = array('value' => 'sitemap', 'option' => $tabs.'Sitemap');
			$a[] = array('value' => 'c-'.$e['id_cms_category'], 'option' => $tabs.'Pages');
		}
		else
			$a[] = array('value' => 'c-'.$e['id_cms_category'], 'option' => $tabs.$e['name']);

		if (isset($e['children']))
			foreach ($e['children'] as &$child)
				self::generateCMSCategories($child, $a, $tabs.self::$tab);

		foreach ($e['cms'] as &$c)
			$a[] = array('value' => 'p-'.$c['id_cms'], 'option' => $tabs.self::$tab.$c['meta_title']);

	}

	public static function getCategories(&$opts)
	{
		if (!is_array($opts))
			$opts = array();
		$cats = self::getNestedCategories();
		self::generateCategories($cats, $opts);
	}

	public static function getCMSCategories(&$opts)
	{
		if (!is_array($opts))
			$opts = array();
		$cats = CMSCategory::getRecurseCategory();
		self::generateCMSCategories($cats, $opts);
	}

}

?>