Your IP : 216.73.216.68


Current Path : /home/jeromecohp/www/administrator/components/com_emailbeautifier/controllers/
Upload File :
Current File : /home/jeromecohp/www/administrator/components/com_emailbeautifier/controllers/template.php

<?php
/**
 * @package     EmailBeautifier
 * @subpackage  com_emailbeautifier
 *
 * @author      Techjoomla <extensions@techjoomla.com>
 * @copyright   Copyright (C) 2009 - 2022 Techjoomla. All rights reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

// No direct access.
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Session\Session;
use Joomla\CMS\MVC\Controller\FormController;
use Joomla\CMS\Response\JsonResponse;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;

/**
 * EmailBeautifier Controller for template view
 *
 * @package     EmailBeautifier
 * @subpackage  com_emailbeautifier
 * @since       1.5
 */
class EmailbeautifierControllerTemplate extends FormController
{
	/**
	 * Method to load new template HTML and CSS on ajax call for change template on template form.
	 *
	 * @return  json | boolean on failure
	 *
	 * @since   1.5
	 */
	public function loadTemplate()
	{
		$app    = Factory::getApplication();
		$model  = $this->getModel('template');

		try
		{
			$templateData = $model->loadTemplate();
			$data         = json_encode($templateData);
			echo new JsonResponse($data);
		}
		catch (Exception $e)
		{
			http_response_code(500);
			echo new JsonResponse($e);
		}

		$app->close();
	}

	/**
	 * Save template data
	 *
	 * @param   integer  $key     key.
	 *
	 * @param   integer  $urlVar  url
	 *
	 * @return  boolean | string  The arguments to append to the redirect URL.
	 *
	 * @since   2.3.0
	 */
	public function save($key = null, $urlVar = '')
	{
		// Check for request forgeries.
		Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));

		// Initialise variables.
		$app        = Factory::getApplication();
		$input      = $app->input;
		$model      = $this->getModel('Template', 'EmailbeautifierModel');
		$table      = $model->getTable();

		$data       = $input->post->get('jform', array(), 'array');
		$task       = $this->getTask();

		$checkin    = property_exists($table, $table->getColumnAlias('checked_out'));

		// Determine the name of the primary key for the data.
		if (empty($key))
		{
			$key = $table->getKeyName();
		}

		// To avoid data collisions the urlVar may be different from the primary key.
		if (empty($urlVar))
		{
			$urlVar = $key;
		}

		$recordId   = $this->input->getInt($urlVar);

		// Populate the row id from the session.
		$data[$key] = $recordId;

		// Access check.
		if (!$this->allowSave($data, $key))
		{
			$this->setError(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'));
			$this->setMessage($this->getError(), 'error');

			$this->setRedirect('index.php?option=com_emailbeautifier&view=template' . $this->getRedirectToListAppend());

			return false;
		}

		// Get form
		// Sometimes the form needs some posted data, such as for plugins and modules.
		$form = $model->getForm($data, false);

		if (!$form)
		{
			$app->enqueueMessage($model->getError(), 'error');

			return false;
		}

		// Validate the posted data.
		$validData = $model->validate($form, $data);

		// Check for errors.
		if ($validData === false)
		{
			// Get the validation messages.
			$errors = $model->getErrors();

			// Push up to three validation messages out to the user.
			for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
			{
				if ($errors[$i] instanceof Exception)
				{
					$app->enqueueMessage($errors[$i]->getMessage(), 'warning');
				}
				else
				{
					$app->enqueueMessage($errors[$i], 'warning');
				}
			}

			// Save the data in the session.
			$app->setUserState('com_emailbeautifier.edit.template.data', $data);

			// Redirect back to the edit screen
			$this->setRedirect('index.php?option=com_emailbeautifier&view=template' . $this->getRedirectToItemAppend($recordId, $urlVar));

			$this->redirect();
		}

		// Attempt to save the data.
		if (!$model->save($validData))
		{
			// Save the data in the session.
			$app->setUserState('com_emailbeautifier.edit.template.data', $data);

			// Redirect back to the edit screen.
			$this->setError(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
			$this->setMessage($this->getError(), 'error');

			$this->setRedirect('index.php?option=com_emailbeautifier&view=template' . $this->getRedirectToItemAppend($recordId, $urlVar));

			return false;
		}

		// Save succeeded, so check-in the record.
		if ($checkin && $model->checkin($validData['id']) === false)
		{
			// Save the data in the session.
			$app->setUserState('com_emailbeautifier.edit.template.data', $validData);

			// Check-in failed, so go back to the record and display a notice.
			$this->setError(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()));
			$this->setMessage($this->getError(), 'error');

			$this->setRedirect('index.php?option=com_emailbeautifier&view=template' . $this->getRedirectToItemAppend($recordId, $urlVar));

			return false;
		}

		$this->setMessage(Text::_('COM_EMAILBEAUTIFIER_TEMPLATE_SAVED'));

		// Redirect the user and adjust session state based on the chosen task.
		switch ($task)
		{
			case 'apply':
				// Set the record data in the session.
				$recordId = $model->getState('com_emailbeautifier.edit.template.id');
				$this->holdEditId('com_emailbeautifier.edit.template', $recordId);
				$app->setUserState('com_emailbeautifier.edit.template.data', null);
				$model->checkout($recordId);

				// Redirect back to the edit screen.
				$this->setRedirect('index.php?option=com_emailbeautifier&view=template' . $this->getRedirectToItemAppend($recordId, $urlVar));
			break;

			case 'save2new':
				// Clear the record id and data from the session.
				$this->releaseEditId('com_emailbeautifier.edit.template', $recordId);
				$app->setUserState('com_emailbeautifier.edit.template.data', null);

				// Redirect back to the edit screen.
				$this->setRedirect('index.php?option=com_emailbeautifier&view=template' . $this->getRedirectToItemAppend(null, $urlVar));
			break;

			default:
				// Clear the record id and data from the session.
				$this->releaseEditId('com_emailbeautifier.edit.template', $recordId);
				$app->setUserState('com_emailbeautifier.edit.template.data', null);

				$url = 'index.php?option=com_emailbeautifier&view=templates' . $this->getRedirectToListAppend();

				// Check if there is a return value
				$return = $this->input->get('return', null, 'base64');

				if (!is_null($return) && Uri::isInternal(base64_decode($return)))
				{
					$url = base64_decode($return);
				}

				// Redirect to the list screen.
				$this->setRedirect($url);

			break;
		}
	}
}