<?php

/**
 * @version        1.9.0
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Dang Thuc Dam
 * @copyright	   Copyright (C) 2011-2025 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;

class EDocmanViewPlayHtml extends OSViewHtml
{

	function display()
	{
		$db = Factory::getContainer()->get('db');
		$user = Factory::getApplication()->getIdentity();
		$config = EDocmanHelper::getConfig();
		
		$file = [];
		$file['user']   = $user;
		$document		= Factory::getApplication()->getDocument();
		$fileId			= Factory::getApplication()->getInput()->get('id', 0, 'int');
		if($fileId == 0 || !EdocmanHelper::canAccessDocument($fileId))
		{
			throw new Exception(Text::_('JERROR_ALERTNOAUTHOR'), 404);
		}

        $model			= $this->getModel();
        $file           = $model->getDocumentDetails($fileId);
        $file_url       = $file->filename;
        $fileExt        = EDocmanHelper::getExt($file);

        $model->copyMediaToTmpDirectory($file->id,$file->filename,$file->original_filename);

		if ($file_url != "") 
		{
			$canPlay	= 1;
			if ($canPlay == 1) 
			{
				$original_filename = explode(".",$file->original_filename);
				$original_filename_ext = $original_filename[count($original_filename) - 1];
				$t['playfilewithpath']	    = Uri::root()."edocmanviewer/".md5($file->id)."/".$file->id.".".$original_filename_ext;
				$t['playerpath']			= Uri::base().'components/com_edocman/assets/flowplayer/';
				$t['playerwidth']			= '100%';
				$t['playerheight']		    = '100%';
				$t['html5_play']			= 1;
			} 
			else 
			{
				echo Text::_('EDOCMAN_ERROR_NO_CORRECT_FILE_TO_PLAY_FOUND');exit;
			}
		} 
		else 
		{
			echo Text::_('EDOCMAN_ERROR_NO_FILE_TO_PLAY_FOUND');exit;
		}

		$t['filetype']	= $fileExt;
		if ($fileExt == 'mp3') 
		{
			$t['filetype'] 		= 'mp3';
			$t['playerheight']	= 30;
		} 
		else if ($fileExt == 'ogg') 
		{
			$t['filetype'] 		= 'ogg';
			$t['playerheight']	= 30;
		}

		$this->userId				= $user->id;
        $this->file                 = $file;
        $this->t                    = $t;
		$user_agent					= $_SERVER['HTTP_USER_AGENT'];
		if (strpos( $user_agent, 'Safari') !== false)
		{
		   $this->isSafari			= true;
		}
		else
		{
		   $this->isSafari			= false;
		}
		$this->setLayout('default');
		parent::display();
	}

	function str2bool( $str )
	{
		return in_array( strtolower(trim($str)) , array(1,'yes','on', 'true', 'oui') );
	}
}
