<?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 AdminLayerSliderTransitionController extends ModuleAdminController
{
	public function __construct()
	{
		parent::__construct();
		require_once _PS_MODULE_DIR_.$this->module->name.'/helper.php';
	}

	public function postProcess()
	{
		parent::postProcess();
		if (isset($this->context->cookie->ls_error))
		{
			$this->errors[] = $this->context->cookie->ls_error;
			unset($this->context->cookie->ls_error);
		}
		$action = Tools::getValue('action', '');
		if ($action)
			$this->{'action'.$action}();
	}

	public function actionSaveTransitions()
	{
		$transitions = array();

		$transitions['t2d'] = (array)Tools::getValue('t2d', array());
		$transitions['t3d'] = (array)Tools::getValue('t3d', array());

		array_walk_recursive($transitions['t2d'], 'layerslider_builder_convert_numbers');
		array_walk_recursive($transitions['t3d'], 'layerslider_builder_convert_numbers');

		foreach ($transitions['t3d'] as $key => $val)
		{
			if (strstr($val['rows'], ','))
			{
				$tmp = explode(',', $val['rows']);
				$tmp[0] = (int)trim($tmp[0]);
				$tmp[1] = (int)trim($tmp[1]);
				$transitions['t3d'][$key]['rows'] = $tmp;
			}
			else
				$transitions['t3d'][$key]['rows'] = (int)$val['rows'];

			if (strstr($val['cols'], ','))
			{
				$tmp = explode(',', $val['cols']);
				$tmp[0] = (int)trim($tmp[0]);
				$tmp[1] = (int)trim($tmp[1]);
				$transitions['t3d'][$key]['cols'] = $tmp;
			}
			else
				$transitions['t3d'][$key]['cols'] = (int)$val['cols'];

			if (isset($val['tile']['depth']))
				$transitions['t3d'][$key]['tile']['depth'] = 'large';

			if (!isset($val['before']['enabled']))
				unset($transitions['t3d'][$key]['before']['transition']);

			if (!isset($val['after']['enabled']))
				unset($transitions['t3d'][$key]['after']['transition']);
		}

		foreach ($transitions['t2d'] as $key => $val)
		{
			if (strstr($val['rows'], ','))
			{
				$tmp = explode(',', $val['rows']);
				$tmp[0] = (int)trim($tmp[0]);
				$tmp[1] = (int)trim($tmp[1]);
				$transitions['t2d'][$key]['rows'] = $tmp;
			}
			else
				$transitions['t2d'][$key]['rows'] = (int)$val['rows'];

			if (strstr($val['cols'], ','))
			{
				$tmp = explode(',', $val['cols']);
				$tmp[0] = (int)trim($tmp[0]);
				$tmp[1] = (int)trim($tmp[1]);
				$transitions['t2d'][$key]['cols'] = $tmp;
				}
			else
				$transitions['t2d'][$key]['cols'] = (int)$val['cols'];

			if (empty($val['transition']['rotateX']))
				unset($transitions['t2d'][$key]['transition']['rotateX']);

			if (empty($val['transition']['rotateY']))
				unset($transitions['t2d'][$key]['transition']['rotateY']);

			if (empty($val['transition']['rotate']))
				unset($transitions['t2d'][$key]['transition']['rotate']);

			if (empty($val['transition']['scale']) || $val['transition']['scale'] == '1.0' || $val['transition']['scale'] == '1')
				unset($transitions['t2d'][$key]['transition']['scale']);

		}

		$upload_dir = wp_upload_dir();
		$custom_trs = $upload_dir['basedir'].'/layerslider.custom.transitions.js';
		$data = 'var layerSliderCustomTransitions = '.Tools::jsonEncode($transitions).';';
		file_put_contents($custom_trs, $data);
		die('SUCCESS');
	}

	public function setMedia()
	{
		parent::setMedia();

		$path = __PS_BASE_URI__."modules/{$this->module->name}/";
		$this->addCSS($path.'views/css/wp_specs.css');
		$this->addCSS($path.'views/css/base/dashicons.css');
		$this->addCSS($path.'views/css/base/global.css');
		$this->addCSS($path.'views/css/base/admin.css');
		$this->addCSS($path.'views/css/base/admin_new.css');
		$this->addCSS($path.'views/css/base/layerslider.css');
		$this->addCSS($path.'views/css/base/layerslider.transitiongallery.css');
		$this->addCSS($path.'views/css/base/minicolors/jquery.minicolors.css');

		if (version_compare(_PS_JQUERY_VERSION_, '1.8.3', '<'))
		{
			$this->addJS($path.'views/js/jquery-1.8.3.min.js');
			$this->addJS($path.'views/js/jquery.noConflict.js');
		}
		$this->addJS($path.'views/js/jquery-ui-1.10.4.custom.min.js');
		$this->addJS($path.'views/js/wp_specs.js');
		$this->addJS($path.'views/js/base/admin.js');
		$this->addJS($path.'views/js/base/builder.js');
		$this->addJS($path.'views/js/base/layerslider.kreaturamedia.js');
		$this->addJS($path.'views/js/base/layerslider.transitions.js');
		$this->addJS($path.'views/js/base/layerslider.transition.gallery.js');
		$this->addJS($path.'views/js/base/minicolors/jquery.minicolors.js');
	}

	public function display()
	{
		$this->content = '<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.11.2/TweenMax.min.js"></script>';
		ob_start();
		LSScreen::addHelp();
		require_once LS_ROOT_PATH.'views/transition_builder.php';
		$this->content .= ob_get_contents();
		ob_end_clean();

		$this->context->smarty->assign(array('content' => $this->content));
		$this->display_footer = false;

		parent::display();
	}

}
