<?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 AdminLayerSliderStyleController 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 actionSaveStyle()
	{
		$upload_dir = wp_upload_dir();
		$file = $upload_dir['basedir'].'/layerslider.custom.css';
		$content = Tools::getValue('contents', '');

		if (is_writable($upload_dir['basedir']))
			file_put_contents($file, $content);

		else

			$this->context->cookie->ls_error = 'It looks like your files isn\'t writable, so PHP couldn\'t make any changes (CHMOD).';

		Tools::redirectAdmin($this->context->link->getAdminLink('AdminLayerSliderStyle'));
	}

	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/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/minicolors/jquery.minicolors.js');

		$this->addCSS($path.'views/css/base/codemirror/lib/codemirror.css');
		$this->addCSS($path.'views/css/base/codemirror/theme/solarized.mod.css');

		$this->addJS($path.'views/js/base/codemirror/lib/codemirror.js');
		$this->addJS($path.'views/js/base/codemirror/mode/css/css.js');
		$this->addJS($path.'views/js/base/codemirror/mode/javascript/javascript.js');
		$this->addJS($path.'views/js/base/codemirror/addon/fold/foldcode.js');
		$this->addJS($path.'views/js/base/codemirror/addon/fold/foldgutter.js');
		$this->addJS($path.'views/js/base/codemirror/addon/fold/brace-fold.js');
		$this->addJS($path.'views/js/base/codemirror/addon/selection/active-line.js');

		$this->addCSS($path.'views/css/base/skin.editor.css');
	}

	public function display()
	{
		ob_start();
		LSScreen::addHelp();
		require_once LS_ROOT_PATH.'views/style_editor.php';
		$this->content = ob_get_contents();
		ob_end_clean();

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

		parent::display();
	}

}
