Your IP : 216.73.216.182


Current Path : /var/www/html/components/com_jchat/Controller/
Upload File :
Current File : /var/www/html/components/com_jchat/Controller/LivestreamingController.php

<?php
namespace JExtstore\Component\JChat\Site\Controller;
/**
 * @package JCHAT::LIVESTREAMING::components::com_jchat 
 * @subpackage controllers
 * @author Joomla! Extensions Store
 * @copyright (C) 2024 - 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 JExtstore\Component\JChat\Administrator\Framework\Controller as JChatController;
use JExtstore\Component\JChat\Administrator\Framework\Helpers\Users as JChatHelpersUsers;

/**
 * Livestreaming controller class
 *
 * @package JCHAT::LIVESTREAMING::components::com_jchat
 * @subpackage controllers
 * @since 2.48
 */
class LivestreamingController extends JChatController {
	/**
	 * Set model state always getting fresh vars from POST/FILES request
	 *
	 * @access protected
	 * @param string $scope
	 * @param boolean $ordering
	 * @return object
	 */
	protected function setModelState($scope = 'default', $defaultModel = null): object {
		// Set model state for basic stream
		$defaultModel->setState('blobfile', $this->app->getInput()->files->get('blob'));
		$defaultModel->setState('blobfilename', $this->app->getInput()->getString('filename'));
		$defaultModel->setState('propagatedItemid', $this->app->getInput()->getInt('itemid'));
		
		return $defaultModel;
	}
	
	/**
	 * Display the video livestreaming view
	 *
	 * @access public
	 * @return void
	 */
	public function display($cachable = false, $urlparams = false) {
		// Check if the user has access to the chat app based on various access parameters
		if (! $this->allowDisplay()) {
			$this->app->enqueueMessage ( Text::_ ( 'COM_JCHAT_NOACCESS' ) );
			return;
		}
	
		$viewType = $this->document->getType ();
		$coreName = $this->getName ();
		$viewLayout = $this->app->getInput()->get ( 'layout', 'default' );
	
		$view = $this->getView ( $coreName, $viewType, '', array (
				'base_path' => $this->basePath
		) );
	
		// Instantiate models object with Dependency Injection chain
		$userSessionTable = JChatHelpersUsers::getSessiontable ();
		$streamModel = $this->getModel ( 'Stream', null, array (
				'sessiontable' => $userSessionTable
		) );
	
		// Get/Create the model
		$tpl = null;
		if ($model = $this->getModel ( $coreName, null, array (
				'streamModel' => $streamModel,
				'sessiontable' => $userSessionTable
		) )) {
			$model->setState('livestreaming_iswatcher', $this->app->getInput()->getString('livestreamingid', ''));
			if($liveStreamingId = $this->app->getInput()->getString('livestreamingid', '')) {
				$model->setState('livestreaming_broadcaster_sessionid', $liveStreamingId);
				$tpl = 'watcher';
			}
			
			// Push the model into the view (as default)
			$view->setModel ( $model, true );
			
			// Update the session status of the current user being it a broadcaster or a watcher
			if($liveStreamingId)  {
				// This is a watcher
				$model->updateEntity($liveStreamingId);
			} else {
				// This is a broadcaster
				$model->updateEntity();
			}
		}
	
		// Check if the user is logged in
		if (!$model->getComponentParams()->get('livestreaming_access_guest', 0) && !$this->user->id) {
			$this->app->enqueueMessage ( Text::_ ( 'COM_JCHAT_MUST_BE_LOGGEDIN_FOR_LIVESTREAMING' ) );
			return;
		}
		
		// Set the layout
		$view->setLayout ( $viewLayout );
		$view->display ($tpl);
	}

	/**
	 * Update the entity record #__sessionstatus, set the livestreaming_Watch field for a watcher that enters/exits a live
	 *
	 * @access public
	 * @return void
	 */
	public function updateEntity() {
		// Initialization
		$viewType = $this->document->getType ();
		$coreName = $this->getName ();
	
		// Instantiate model object with Dependency Injection
		$userSessionTable = JChatHelpersUsers::getSessiontable ();
		$model = $this->getModel($coreName, null, array('sessiontable'=>$userSessionTable));
	
		// Retrieve POST data
		$broadcasterSessionId = $this->app->getInput()->getString('broadcastersessionid');
		$watcherIsWatching = $this->app->getInput()->getInt('watcheriswatching', 0);
	
		// Try to store record using model
		$response = $model->updateEntity($broadcasterSessionId, $watcherIsWatching);
	
		// Get view
		$view = $this->getView ( $coreName, $viewType, '', array ('base_path' => $this->basePath ) );
	
		// Format response for JS client as requested
		$view->display($response);
	}
	
	/**
	 * Save uploaded media files to the media folder for jchat
	 *
	 * @access public
	 * @return bool
	 */
	public function saveEntity(): bool {
		
		// Initialization
		$viewType = $this->document->getType ();
		$coreName = $this->getName ();
		
		// Upload file
		// Instantiate model object with Dependency Injection
		$userSessionTable = JChatHelpersUsers::getSessiontable ();
		$model = $this->getModel($coreName, null, array('sessiontable'=>$userSessionTable));

		// Populate model state and get model
		$this->setModelState('livestreaming', $model);
		
		$result = $model->storeEntity();
		
		// Get view and pushing model
		$view = $this->getView ($coreName, 'json', '');
		$view->display($result);
		
		return true;
	}
	
	/**
	 * Delete/Archive uploaded media files to the media folder for jchat
	 *
	 * @access public
	 * @return bool
	 */
	public function deleteEntity(): bool {
		
		// Initialization
		$viewType = $this->document->getType ();
		$coreName = $this->getName ();
		
		// Upload file
		// Instantiate model object with Dependency Injection
		$userSessionTable = JChatHelpersUsers::getSessiontable ();
		$model = $this->getModel($coreName, null, array('sessiontable'=>$userSessionTable));
		
		// Populate model state and get model
		$this->setModelState('livestreaming', $model);
		
		$result = $model->deleteEntity(null);
		
		// Get view and pushing model
		$view = $this->getView ($coreName, 'json', '');
		$view->display($result);
		
		return true;
	}
}