<?php
namespace JExtstore\Component\JChat\Site\Controller;
/**
 * @package JCHAT::FORM::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\Factory;
use Joomla\CMS\Uri\Uri;
use JExtstore\Component\JChat\Administrator\Framework\Controller as JChatController;

/**
 * Group public chat controller class
 * The entity in this MVC core is the contact user managed for group public conversation
 *
 * @package JCHAT::FORM::components::com_jchat
 * @subpackage controllers
 * @since 1.0
 */
class FormController extends JChatController {
	/**
	 * Display the chat form
	 * @access public
	 * @return void
	 */
	public function display($cachable = false, $urlparams = false) {
		// 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;
		}
		
		$document = Factory::getApplication()->getDocument();
		$viewType = $document->getType();
		$coreName = $this->getName();
		$viewLayout = $this->app->input->get ( 'layout', 'default' );
	
		$view = $this->getView($coreName, $viewType, '', array('base_path' => $this->basePath));
	
		// Get/Create the model
		if ($model = $this->getModel($coreName)) {
			// Push the model into the view (as default)
			$view->setModel($model, true);
		}

		// Check if we have a meeting join form request
		if($meetingHash = $this->app->input->get('meeting_hash')) {
			$model->setState('meeting_hash', $meetingHash);
		}

		if(	$this->user->id && $model->getComponentParams()->get('guestenabled', 0) == 2 &&
			$model->getState('meeting_hash', null) &&
			$model->getComponentParams()->get('meetings_enabled', 0) &&
			$model->getComponentParams()->get('meeting_auto_logout', 0)) {
			// Prepare the logout options.
			$options = array(
				'clientid' => $this->app->get('shared_session', '0') ? null : 0,
			);
			
			// Perform the log out.
			if($this->app->logout(null, $options)) {
				$this->setRedirect(Uri::current());
				return;
			}
		}
		
		// Set the layout
		$view->setLayout($viewLayout);
	
		$view->display();
	}
	
	/**
	 * Save new contact user for public conversation
	 * 
	 * @access public
	 * @return bool
	 */
	public function saveEntity(): bool {
		// Initialization
		$document = Factory::getApplication()->getDocument();
		$coreName = $this->getName ();
		
		// Manage async posted data
		$postData = $this->app->input->post->getString('data');
		if(ini_get('magic_quotes_gpc') == 1) {
			$postData = stripslashes($postData);
		}
		
		if($postData) {
			$data = json_decode($postData);
			if(!empty($data->formFields)) {
				foreach ($data->formFields as $postVar) {
					// Exclude not needed ajax posted vars
					if(in_array($postVar->name, array('option', 'task', 'format'))) {
						continue;
					}
					// Set vars both in raw and J Input object 'post' instance
					$this->requestArray[$postVar->name] = $postVar->value;
					$this->app->input->post->set($postVar->name, $postVar->value);
				}
			}
			// Force primary key as current session id
			$this->requestArray['sessionid'] = session_id();
		}
		
		// Load della model e bind store
		$model = $this->getModel ('Form', null, array('name'=>'Sessionstatus'));
		
		if (!$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 );
			$response['storing'] = array('status'=>false, 'details'=>$modelException->getMessage());
		} else {
			$response['storing'] = array('status'=>true);
		}
		
		// Get view and pushing model
		$view = $this->getView ( $coreName, 'json', '', array ('base_path' => $this->basePath ) );
		
		// Format response for JS client as requested
		$view->display($response);
		
		return true;
	}
}