<?php
namespace JExtstore\Component\JChat\Site\Controller;
/**
 * @package JCHAT::EXPORT::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\Factory;
use JExtstore\Component\JChat\Administrator\Framework\Controller as JChatController;
use JExtstore\Component\JChat\Administrator\Framework\Helpers\Users as JChatHelpersUsers;

/**
 * Controller to export conversations
 * Doesn't call view
 *
 * @package JCHAT::EXPORT::components::com_jchat
 * @subpackage controllers
 * @since 1.0
 */
class ExportController extends JChatController {
	/**
	 * Set model state always getting fresh vars from POST request
	 * 
	 * @access protected
	 * @param string $scope
	 * @param boolean $ordering
	 * @return object
	 */
	protected function setModelState($scope = 'default', $ordering = true, $explicitModel = null): object {
		// Set model state for basic stream
		$explicitModel->setState('userChatId', $this->app->input->getString('chatid', null));
		$explicitModel->setState('option', $this->option);
		
		return $explicitModel;
	}
	
	/**
	 * Display data for browser client as download attachment
	 * 
	 * @access public
	 * @return void
	 */
	public function display($cachable = false, $urlparams = false) {
		// Initialization
		$document = Factory::getApplication()->getDocument();
		$viewType = $document->getType ();
		$coreName = $this->getName ();
		
		// Instantiate model object with Dependency Injection
		$userSessionTable = JChatHelpersUsers::getSessiontable ();
		$model = $this->getModel($coreName, null, array('sessiontable'=>$userSessionTable));
		
		// Populate model state
		$this->setModelState('chatexport', false, $model);
		
		// No view required but only streammy output file
		$model->getData();
	}
	
	/**
	 * Display data for browser client as download attachment
	 * for the private messaging conversation user ID based
	 *
	 * @access public
	 * @return void
	 */
	public function displayMessaging() {
		// Initialization
		$document = Factory::getApplication()->getDocument();
		$viewType = $document->getType ();
		$coreName = $this->getName ();
	
		// Instantiate model object with Dependency Injection
		$userSessionTable = JChatHelpersUsers::getSessiontable ();
		$model = $this->getModel($coreName, null, array('sessiontable'=>$userSessionTable));
	
		$userID = $this->app->input->getInt('userid', null);
		$oldestMessageID = $this->app->input->getInt('oldestmessageid');
			
		// No view required but only streammy output file
		$model->getDataMessaging($userID, $oldestMessageID);
	}
}
