<?php
namespace JExtstore\Component\JChat\Site\Controller;
/**
 * @package JCHAT::LAMESSAGES::administrator::components::com_jchat
 * @subpackage controllers
 * @author Joomla! Extensions Store
 * @Copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html  
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use JExtstore\Component\JChat\Administrator\Framework\Controller as JChatController;
use JExtstore\Component\JChat\Administrator\Framework\Helpers\Mailer as JChatMailer;

/**
 * Main controller leaved messages
 * @package JCHAT::LAMESSAGES::administrator::components::com_jchat
 * @subpackage controllers
 * @since 1.0
 */
class LamessagesController extends JChatController {
	/**
	 * Setta il model state a partire dallo userstate di sessione
	 * @access protected
	 * @return object
	 */
	protected function setModelState($scope = 'default', $ordering = true): object {
		// User state specific
		$option= $this->option;
		
		// Get default model
		$defaultModel = $this->getModel();
		
		$filter_order = $this->getUserStateFromRequest( "$option.$scope.filter_order", 'filter_order', 'a.sentdate', 'cmd' );
		$filter_order_Dir = $this->getUserStateFromRequest ( "$option.$scope.filter_order_Dir", 'filter_order_Dir', 'desc', 'word' );
		$fromPeriod = $this->getUserStateFromRequest( "$option.$scope.fromperiod", 'fromperiod');
		$toPeriod = $this->getUserStateFromRequest( "$option.$scope.toperiod", 'toperiod');
		$worked = $this->getUserStateFromRequest( "$option.$scope.workedfilter", 'workedfilter');
		$closed = $this->getUserStateFromRequest( "$option.$scope.closedfilter", 'closedfilter');
		
		// Security safe for pagination
		if(isset($_REQUEST['start']) && !isset($_REQUEST['limitstart'])) {
			$_REQUEST['limitstart'] = $_REQUEST['start'];
		}
		parent::setModelState($scope);
		
		// Set model state  
		$defaultModel->setState('order', $filter_order);
		$defaultModel->setState('order_dir', $filter_order_Dir );
		$defaultModel->setState('fromPeriod', $fromPeriod);
		$defaultModel->setState('toPeriod', $toPeriod);
		$defaultModel->setState('workedfilter', $worked);
		$defaultModel->setState('closedfilter', $closed);
		
		return $defaultModel;
	}
	
	/**
	 * Default listEntities
	 * 
	 * @access public
	 * @param $cachable string
	 *       	 the view output will be cached
	 * @return void
	 */
	public function display($cachable = false, $urlparams = false) {
		// Set model state
		$defaultModel = $this->setModelState('lamessages');
		
		// Check if the user is logged in
		if (! $this->user->id) {
			$this->app->enqueueMessage ( Text::_ ( 'COM_JCHAT_MUST_BE_LOGGEDIN_TOMANAGE_TICKETS' ) );
			return;
		}
		
		// Check if the user has access to the chat app based on access level parameter
		if (! $this->allowDisplay()) {
			$this->app->enqueueMessage ( Text::_ ( 'COM_JCHAT_NOACCESS' ) );
			return;
		}
		
		// Check if the user has access to the chat app based on access level parameter
		if (! $this->hasGroupsPermissions('ticketsmanager', $defaultModel->getComponentParams())) {
			$this->app->enqueueMessage ( Text::_ ( 'COM_JCHAT_NOACCESS' ) );
			return;
		}
		 
		// Parent construction and view display
		parent::display($cachable);
	}
	
	/**
	 * Edit entity
	 *
	 * @access public
	 * @return bool
	 */
	public function editEntity(): bool {
		$this->app->input->set ( 'hidemainmenu', 1 );
		$cid = $this->app->input->get ( 'cid', array (
				0
		), 'array' );
		$idEntity = ( int ) $cid [0];
		$model = $this->getModel ();
		$model->setState ( 'option', $this->option );
	
		// Try to load record from model
		if (! $record = $model->loadEntity ( $idEntity )) {
			// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
			$modelExceptions = $model->getErrors ();
			foreach ( $modelExceptions as $exception ) {
				$this->app->enqueueMessage ( $exception->getMessage (), $exception->getErrorLevel () );
			}
			$this->setRedirect ( "index.php?option=" . $this->option . "&task=" . $this->name . ".display", Text::_ ( 'COM_JCHAT_ERROR_EDITING' ) );
			return false;
		}
	
		// Additional model state setting
		$model->setState ( 'option', $this->option );
	
		// Check out control on record
		if ($record->checked_out && $record->checked_out != $this->user->id) {
			$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&view=" . $this->name), Text::_ ( 'COM_JCHAT_CHECKEDOUT_RECORD' ), 'notice' );
			return false;
		}
	
		// Access check
		if ($record->id && ! $this->allowEdit ( $this->option )) {
			$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&view=" . $this->name), Text::_ ( 'COM_JCHAT_ERROR_ALERT_NOACCESS' ), 'notice' );
			return false;
		}
	
		if (! $record->id && ! $this->allowAdd ( $this->option )) {
			$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&view=" . $this->name), Text::_ ( 'COM_JCHAT_ERROR_ALERT_NOACCESS' ), 'notice' );
			return false;
		}
	
		// Check out del record
		if ($record->id) {
			$record->checkout ( $this->user->id );
		}
	
		// Get view and pushing model
		$viewType = $this->document->getType();
		$viewName = $this->input->get('view', $this->default_view);
		$viewLayout = $this->input->get('layout', 'default', 'string');
		$view = $this->getView($viewName, $viewType, '', array('base_path' => $this->basePath, 'layout' => $viewLayout));
		$view->setModel ( $model, true );
	
		// Call edit view
		$view->editEntity ( $record );
		
		return true;
	}
	
	/**
	 * Manage entity apply/save after edit entity
	 *
	 * @access public
	 * @return bool
	 */
	public function saveEntity(): bool {
		$context = implode ( '.', array (
				$this->option,
				strtolower ( $this->getName () ),
				'errordataload'
		) );
	
		// Load della model e bind store
		$model = $this->getModel ();
	
		if (! $result = $model->storeEntity ()) {
			// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
			$modelException = $model->getError ( null, false );
			$this->app->enqueueMessage ( $modelException->getMessage (), $modelException->getErrorLevel () );
				
			// Store data for session recover
			$this->app->setUserState ( $context, $this->requestArray );
			$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&task=" . $this->name . ".editEntity&cid=" . $this->app->input->get ( 'id' )), Text::_ ( 'COM_JCHAT_ERROR_SAVING' ) );
			return false;
		}
	
		// Security safe if not model record id detected
		if (! $id = $result->id) {
			$id = $this->app->input->get ( 'id' );
		}
	
		// Redirects switcher
		switch($this->task) {
			case 'saveEntity':
				$redirects = array (
				'task' => 'display',
				'msgsufix' => '_SAVING'
						);
				break;
	
			case 'saveEntity2New':
				$redirects = array (
				'task' => 'editEntity',
				'msgsufix' => '_STORING'
						);
	
				break;
	
			default:
			case 'applyEntity':
				$redirects = array (
				'task' => 'editEntity&cid=' . $id,
				'msgsufix' => '_APPLY'
						);
				break;
		}
	
		$msg = 'COM_JCHAT_SUCCESS' . $redirects ['msgsufix'];
		$controllerTask = $redirects ['task'];
	
		$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&task=" . $this->name . "." . $controllerTask), Text::_ ( $msg ) );
	
		return true;
	}
	
	/**
	 * Delete a db table entity
	 *
	 * @access public
	 * @return bool
	 */
	public function deleteEntity(): bool {
		$cids = $this->app->input->get ( 'cid', array (), 'array' );
		// Access check
		if (! $this->allowDelete ( $this->option )) {
			$this->setRedirect ( "index.php?option=" . $this->option . "&task=" . $this->name . ".display", Text::_ ( 'COM_JCHAT_ERROR_ALERT_NOACCESS' ), 'notice' );
			return false;
		}
		// Load della model e checkin before exit
		$model = $this->getModel ();
	
		if (! $model->deleteEntity ( $cids )) {
			// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
			$modelException = $model->getError ( null, false );
			$this->app->enqueueMessage ( $modelException->getMessage (), $modelException->getErrorLevel () );
			$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&view=" . $this->name), Text::_ ( 'COM_JCHAT_ERROR_DELETE' ) );
			return false;
		}
	
		$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&view=" . $this->name), Text::_ ( 'COM_JCHAT_SUCCESS_DELETE' ) );
		
		return true;
	}
	
	/**
	 * Manage cancel edit for entity and unlock record checked out
	 *
	 * @access public
	 * @return void
	 */
	public function cancelEntity(): void {
		$id = $this->app->input->get ( 'id' );
		// Load della model e checkin before exit
		$model = $this->getModel ();
	
		if (! $model->cancelEntity ( $id )) {
			// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
			$modelException = $model->getError ( null, false );
			$this->app->enqueueMessage ( $modelException->getMessage (), $modelException->getErrorLevel () );
		}
	
		$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&view=" . $this->name), Text::_ ( 'COM_JCHAT_CANCELED_OPERATION' ) );
	}
	
	/**
	 * Checkin entities
	 *
	 * @access public
	 * @return bool
	 */
	public function checkin(): bool {
		// Access check
		if (! $this->user->authorise ( 'core.manage', 'com_checkin' )) {
			$this->setRedirect ( Route::_( "index.php?option=" . $this->option . "&view=" . $this->name ), Text::_ ( 'COM_JCHAT_ERROR_ALERT_NOACCESS' ), 'notice' );
			return false;
		}
		
		$cid = $this->app->input->get ( 'cid', array (
				0
		), 'array' );
		$id = ( int ) $cid [0];
		
		// Load della model e checkin before exit
		$model = $this->getModel ();
		
		if (! $model->cancelEntity ( $id )) {
			// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
			$modelException = $model->getError ( null, false );
			$this->app->enqueueMessage ( $modelException->getMessage (), $modelException->getErrorLevel () );
		}
		
		$this->setRedirect ( Route::_( "index.php?option=" . $this->option . "&view=" . $this->name ), Text::_ ( 'COM_JCHAT_CHECKEDIN_RECORD' ) );
		
		return true;
	}
	
	/**
	 * Avvia il processo di esportazione records
	 *
	 * @access public
	 * @return void
	 */
	public function exportMessages() {
		// Set model state
		$this->setModelState();
		// Mapping fields to load to column header
		$fieldsToLoadArray = array(	'a.name'=>Text::_('COM_JCHAT_LAMESSAGE_NAME'),
									'a.email'=>Text::_('COM_JCHAT_LAMESSAGE_EMAIL'),
									'a.phonenumber'=>Text::_('COM_JCHAT_LAMESSAGE_PHONENUMBER'),
									'a.message'=>Text::_('COM_JCHAT_MESSAGE'),  
									'a.sentdate'=>Text::_('COM_JCHAT_SENT'),
									'a.worked'=>Text::_('COM_JCHAT_WORKED_STATE'),
									'a.closed_ticket'=>Text::_('COM_JCHAT_CLOSED_TICKET'),
									'u.name AS username_logged'=>Text::_('COM_JCHAT_USERID'),
									'a.id AS msg_id'=>Text::_('ID'));
		$fieldsFunctionTransformation = array();
	
		$model = $this->getModel();
		
		// Additional phone number field
		if(!$model->getComponentParams()->get('tickets_form_include_phonenumber', 0)) {
			unset($fieldsToLoadArray['a.phonenumber']);
		}
		
		$data = $model->exportMessages($fieldsToLoadArray, $fieldsFunctionTransformation);
	
		if(!$data) {
			$this->setRedirect(Route::_("index.php?option=" . $this->option . "&view=" . $this->name), Text::_('NODATA_EXPORT'));
			return false;
		}
	
		// Get view
		// Get view and pushing model
		$viewType = $this->document->getType();
		$viewName = $this->input->get('view', $this->default_view);
		$viewLayout = $this->input->get('layout', 'default', 'string');
		$view = $this->getView($viewName, $viewType, '', array('base_path' => $this->basePath, 'layout' => $viewLayout));
		$view->sendCSVMessages($data, $fieldsFunctionTransformation);
	}
	
	/**
	 * Manage answered worked state for the ticket
	 * 
	 * @access public
	 */
	public function stateFlags() {
		// Access check
		if (! $this->allowEditState ( $this->option )) {
			$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&view=" . $this->name), Text::_ ( 'COM_JCHAT_ERROR_ALERT_NOACCESS' ), 'notice' );
			return false;
		}
		
		$cid = $this->app->input->get ( 'cid', array (
				0
		), 'array' );
		$idEntity = ( int ) $cid [0];
		
		$model = $this->getModel ();
		
		if (! $model->changeTicketState($idEntity, $this->task)) {
			// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
			$modelException = $model->getError ( null, false );
			$this->app->enqueueMessage ( $modelException->getMessage (), $modelException->getErrorLevel () );
			$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&view=" . $this->name), Text::_ ( 'COM_JCHAT_ERROR_STATE_CHANGE' ) );
			return false;
		}
		
		$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&view=" . $this->name), Text::_ ( 'COM_JCHAT_LAMESSAGE_STATE_CHANGED' ) );
	}
	
	/**
	 * Risponde all'email del richiedente con il messaggio inserito dall'agente
	 * richiedendo la serializzazione delle risposte nell'apposito db field
	 *
	 * @access public
	 * @return void
	 */
	public function responseMessage() {
		$task = $this->app->input->get('task', 'responseMessage');
		$responseSubject = $this->app->input->getString('email_subject');
		$responseText = $this->app->input->getRaw('response', '');
		$idEntity = $this->app->input->getInt('id');
		
		// Response text vuota validazione lato server con return false
		if(!trim($responseSubject)) {
			$controllerTask = 'editEntity&cid=' . $idEntity; 
			$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&task=" . $this->name  . "." . $controllerTask), Text::_('COM_JCHAT_VALIDATION_ERROR'));
			return false;
		}
		 
		$model = $this->getModel();
		
		// Root controller -> dependency injection
		$mailer = JChatMailer::getInstance('Joomla');
		if (! $model->sendResponseStore($mailer, $idEntity, $responseSubject, $responseText)) {
			// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
			$modelException = $model->getError ( null, false );
			$this->app->enqueueMessage ( $modelException->getMessage (), $modelException->getErrorLevel () );
				
			$this->setRedirect ( "index.php?option=" . $this->option . "&task=" . $this->name . ".editEntity&cid=" . $this->app->input->get ( 'id' ), Text::_ ( 'COM_JCHAT_ERROR_SEND_MESSAGE' ) );
			return false;
		}
		
		$controllerTask = 'editEntity&cid=' . $idEntity; 
		$this->setRedirect ( Route::_("index.php?option=" . $this->option . "&task=" . $this->name  . "." . $controllerTask), Text::_('COM_JCHAT_SUCCESS_SEND_MESSAGE'));
	}
	
	/**
	 * Class constructor
	 * @return Object&
	 */
	public function __construct($config = array(), MVCFactoryInterface $factory = null, $app = null, $input = null) {
		parent::__construct($config, $factory, $app, $input);
	
		// Registering alias task
		$this->registerTask('applyEntity', 'saveEntity');
		$this->registerTask('workedFlagOff', 'stateFlags');
		$this->registerTask('workedFlagOn', 'stateFlags');
		$this->registerTask('closedFlagOff', 'stateFlags');
		$this->registerTask('closedFlagOn', 'stateFlags');
	}
}