<?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 AdminLayerSliderController 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 actionAddNewSlider()
	{
		$title = Tools::getValue('title', 'Untitled');
		$id = LSSliders::add($title);
		$this->module->insertSlider(array('id_slider' => $id));
		Tools::redirectAdmin($this->context->link->getAdminLink('AdminLayerSliderEdit').'&id='.$id);
	}

	public function actionModulePosition()
	{
		$id = (int)Tools::getValue('id', 0);
		$hook = Tools::getValue('hook', '');
		$this->module->removeHook($id, $hook);
		$this->module->addHook($id, $hook);
		LSSliders::saveHook($id, $hook);

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

	public function actionDuplicateSlider()
	{
		$id = Tools::getValue('id', 0);
		$sliders = LSSliders::find($id);
		$slider = $sliders['data'];

		if (empty($slider['properties']['title']))
			$slider['properties']['title'] = 'Untitled';

		$slider['properties']['title'] .= ' copy';
		$slider['properties']['hook'] = '';
		$slider['properties']['shop'] = 0;
		$slider['properties']['lang'] = 0;

		$name = $slider['properties']['title'];
		$id = LSSliders::add($name, $slider);
		$this->module->insertSlider(array('id_slider' => $id));

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

	public function actionManageSliders()
	{
		$task = Tools::getValue('task', '');
		$ids = Tools::getValue('id', array());
		if (!is_array($ids))
			$ids = array($ids);

		switch ($task)
		{
			case 'remove':
				foreach ($ids as $id)
				{
					LSSliders::remove((int)$id);
					$this->module->deleteSlider((int)$id);
				}
				break;
			case 'delete':
				foreach ($ids as $id)
				{
					LSSliders::delete((int)$id);
					$this->module->deleteSlider((int)$id);
				}
				break;
			case 'restore':
				foreach ($ids as $id)
				{
					LSSliders::restore((int)$id);
					$this->module->insertSlider(array('id_slider' => (int)$id));
				}
				break;
			case 'merge':
				if ($sliders = LSSliders::find($ids))
				{
					$ids = array();
					foreach ($sliders as $key => $item)
					{
						$ids[] = '#'.$item['id'];

						if ($key === 0)
							$data = $item['data'];
						else
							$data['layers'] = array_merge($data['layers'], $item['data']['layers']);
					}

					$name = 'Merged sliders of '.implode(', ', $ids);
					$data['properties']['title'] = $name;
					$data['properties']['hook'] = '';
					$data['properties']['shop'] = 0;
					$data['properties']['lang'] = 0;
					$id = LSSliders::add($name, $data);
					$this->module->insertSlider(array('id_slider' => $id));
				}
				break;
		}

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

	public function actionImportSample()
	{
		include LS_ROOT_PATH.'/classes/class.ls.importutil.php';

		if (Tools::getValue('slider') == 'all')
		{
			foreach (glob(LS_ROOT_PATH.'/demos/*.zip') as $item)
			{
				$item = basename($item);
				if (Tools::substr($item, strrpos($item, '.') + 1) == 'zip')
				{
					$import = new LSImportUtil(LS_ROOT_PATH.'/demos/'.basename($item));
					if (is_array($import->id))
					{
						foreach ($import->id as $id)
							$this->module->insertSlider(array('id_slider' => $id));
					}
				}
			}
		}
		elseif (is_string(Tools::getValue('slider')))
		{
			if (file_exists(LS_ROOT_PATH.'/demos/'.basename(Tools::getValue('slider'))))
			{
				$import = new LSImportUtil(LS_ROOT_PATH.'/demos/'.basename(Tools::getValue('slider')));
				$this->module->insertSlider(array('id_slider' => $import->id[0]));
			}
		}

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

	public function actionImportSlider()
	{
		if (!is_uploaded_file($_FILES['import_file']['tmp_name']))
		{
			$this->context->cookie->ls_error = 'No data received!';
			Tools::redirectAdmin($this->context->link->getAdminLink('AdminLayerSlider'));
		}

		include LS_ROOT_PATH.'/classes/class.ls.importutil.php';
		$import = new LSImportUtil($_FILES['import_file']['tmp_name'], $_FILES['import_file']['name']);
		if (is_array($import->id))
		{
			foreach ($import->id as $id)
				$this->module->insertSlider(array('id_slider' => $id));
		}

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

	public function actionExportSlider()
	{
		$post_sliders = Tools::getValue('sliders');
		if (isset($post_sliders[0]) && $post_sliders[0] == -1)
			$sliders = LSSliders::find(array('limit' => 200));
		elseif (!empty($post_sliders))
			$sliders = LSSliders::find($post_sliders);
		else
		{
			$this->context->cookie->ls_error = 'Invalid data received!';
			Tools::redirectAdmin($this->context->link->getAdminLink('AdminLayerSlider'));
		}

		if (class_exists('ZipArchive'))
		{
			include LS_ROOT_PATH.'/classes/class.ls.exportutil.php';
			$zip = new LSExportUtil;
		}

		$data = array();	// ???
		foreach ($sliders as $item)
		{
			$item['data']['properties']['hook'] = '';
			$item['data']['properties']['shop'] = 0;
			$item['data']['properties']['lang'] = 0;

			$data[] = $item['data'];

			if (class_exists('ZipArchive'))
			{
				$name = sanitize_file_name($item['name']);
				$zip->addSettings(Tools::jsonEncode($item['data']), $name);

				if (Tools::getIsset('exportWithImages'))
				{
					$images = $zip->getImagesForSlider($item['data']);
					$images = $zip->getFSPaths($images);
					$zip->addImage($images, $name);
				}
			}
		}

		if (class_exists('ZipArchive'))
			$zip->download();
		else
		{
			$name = 'LayerSlider Export '.date('Y-m-d').' at '.date('H.i.s').'.json';
			header('Content-type: application/force-download');
			header('Content-Disposition: attachment; filename="'.str_replace(' ', '_', $name).'"');
			die(call_user_func('base64_encode', Tools::jsonEncode($data)));
		}
	}

	public function actionSaveAdvancedSettings()
	{
		update_option('load_jquery', Tools::getValue('load_jquery', false));

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

	public function actionSaveGoogleFonts()
	{
		$url_params = Tools::getValue('url_params', array());

		$fonts = array();
		if (isset($url_params))
		{
			foreach ($url_params as $key => $val)
			{
				if (!empty($val))
				{
					$only_on_admin = Tools::getValue('only_on_admin');
					$fonts[] = array('param' => $val, 'admin' => isset($only_on_admin[$key]) ? true : false);
				}
			}
		}
		update_option('ls-google-fonts', $fonts);

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

	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');
	}

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

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

		parent::display();
	}

}
