Your IP : 216.73.216.240


Current Path : /home/j/u/v/juvelize/www/components/com_icagenda/controllers/
Upload File :
Current File : /home/j/u/v/juvelize/www/components/com_icagenda/controllers/event.php

<?php
/**
 *----------------------------------------------------------------------------
 * iCagenda     Events Management Extension for Joomla!
 *----------------------------------------------------------------------------
 * @version     3.9.0 2024-03-01
 *
 * @package     iCagenda.Site
 * @link        https://www.icagenda.com
 *
 * @author      Cyril Rezé
 * @copyright   (c) 2012-2024 Cyril Rezé / iCagenda. All rights reserved.
 * @license     GNU General Public License version 3 or later; see LICENSE.txt
 *
 * @since       1.0
 *----------------------------------------------------------------------------
*/

defined('_JEXEC') or die;

JLoader::register('iCagendaController', JPATH_COMPONENT . '/controller.php');

/**
 * Event controller class for iCagenda.
 *
 * @since  3.8
 */
class iCagendaControllerEvent extends iCagendaController
{
	/**
	 * Method to check out an event for editing and redirect to the edit form.
	 *
	 * @return  boolean
	 *
	 * @since   3.8
	 */
	public function edit()
	{
		$app  = JFactory::getApplication();
		$user = JFactory::getUser();

		// Get the previous event id (if any) and the current user id.
		$previousId = (int) $app->getUserState('com_icagenda.edit.event.id');
		$eventId    = $this->input->getInt('event_id');

		$cookieLogin = $user->get('cookieLogin');

		// Check if the user logged in with a cookie
		if (!empty($cookieLogin))
		{
			// If so, the user must login to edit the event.
			$app->enqueueMessage(JText::_('JGLOBAL_REMEMBER_MUST_LOGIN'), 'message');
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=login', false));

			return false;
		}

		// Set the event id for the event to edit in the session.
		$app->setUserState('com_icagenda.edit.event.id', $eventId);

		// Get the model.
		$model = $this->getModel('Event', 'iCagendaModel');

		// Redirect to the edit screen.
		$return  = $this->getReturnPage();
//		$this->setRedirect(JRoute::_('index.php?option=com_icagenda&view=manager&layout=event_edit&event_id=' . $eventId . '&return=' . base64_encode($return), false));
		$this->setRedirect('index.php?option=com_icagenda&view=manager&layout=event_edit&event_id=' . $eventId . '&return=' . base64_encode($return));

		return true;
	}
	/**
	 * Method to save a event data.
	 *
	 * @return  void
	 *
	 * @since   3.8
	 */
	public function save()
	{
		// Check for request forgeries.
		$this->checkToken();

		$app     = JFactory::getApplication();
		$model   = $this->getModel('Manager', 'iCagendaModel');
		$user    = JFactory::getUser();
		$eventId = (int) $app->input->get('event_id');

		// Get the user data.
		$requestData = $app->input->post->get('jform', array(), 'array');

		// Validate the posted data.
		$form = $model->getForm();

		if (!$form)
		{
			JError::raiseError(500, $model->getError());

			return false;
		}

		// Send an object which can be modified through the plugin event
//		$objData = (object) $requestData;
//		$app->triggerEvent(
//			'onIcagendaNormaliseRequestData',
//			array('com_icagenda.event', $objData, $form)
//		);
//		$requestData = (array) $objData;

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

		// Check for errors.
		if ($data === 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_icagenda.edit.event.data', $requestData);

			// Redirect back to the edit screen.
			$userId = (int) $app->getUserState('com_icagenda.edit.event.id');
			$this->setRedirect(JRoute::_('index.php?option=com_icagenda&view=manager&layout=event_edit&event_id=' . $eventId, false));

			return false;
		}

		// Attempt to save the data.
		$return = $model->save($data);

		// Check for errors.
		if ($return === false)
		{
			// Save the data in the session.
			$app->setUserState('com_icagenda.edit.event.data', $data);

			// Redirect back to the edit screen.
			$this->setMessage(JText::sprintf('COM_ICAGENDA_MANAGER_EVENT_SAVE_FAILED', $model->getError()), 'warning');
			$this->setRedirect(JRoute::_('index.php?option=com_icagenda&view=manager&layout=event_edit&event_id=' . $eventId, false));

			return false;
		}

		$this->setMessage(JText::_('COM_ICAGENDA_MANAGER_EVENT_SAVE_SUCCESS'));
		$this->setRedirect(JRoute::_($this->getReturnPage(), false));
//		$this->setRedirect(JRoute::_('index.php?option=com_icagenda&view=event&id=' . $eventId, false));

		// Flush the data from the session.
		$app->setUserState('com_icagenda.edit.event.data', null);

		return $result;
	}

	/**
	 * Get the return URL.
	 *
	 * If a "return" variable has been passed in the request
	 *
	 * @return  string	The return URL.
	 *
	 * @since   3.8
	 */
	protected function getReturnPage()
	{
		$return = $this->input->get('return', null, 'base64');

		if (empty($return) || !JUri::isInternal(base64_decode($return)))
		{
			return JUri::base();
		}
		else
		{
			return base64_decode($return);
		}
	}
}