<?php

if (!isset($gCms)) exit;


if (! $this->CheckPermission('Use Play'))
	return $this->DisplayErrorPage($id, $params, $returnid,$this->Lang('accessdenied'));


include_once dirname(__FILE__).'/class.record.php';
include_once dirname(__FILE__).'/class.player.php';

$recordid = isset($params['recordid']) ? $params['recordid'] : -1;
$active_tab = isset($params['active_tab']) ? $params['active_tab'] : false;

if (isset($record))
{
}
elseif ($recordid=='new')
	$record = new pRecord();
else {
	$record = new pRecord();
	if (!$record->loaddb($recordid))
		return $this->DisplayErrorPage($id, $params, $returnid,$this->Lang('error_unknownrecord'));
}


$checkfile = $record->checkfile();

echo $this->StartTabHeaders();
echo $this->SetTabHeader('informations',$this->Lang('informations'), ('informations' == $active_tab));
echo $this->SetTabHeader('params',$this->Lang('params'), ('params' == $active_tab));
if ($checkfile && $record->player!=-1)
	echo $this->SetTabHeader('preview',$this->Lang('preview'), ('preview' == $active_tab));
echo $this->EndTabHeaders();

echo $this->StartTabContent();
echo $this->CreateFormStart($id,"saverecord",$returnid);
echo $this->CreateInputHidden($id, "recordid", $recordid);

echo $this->StartTab('informations', $params);

$players = getPlayPlayers();
$playerlist = array();
foreach ($players as $playertmp)
	$playerlist[$playertmp->name] = $playertmp->id;

$this->smarty->assign('nametext', $this->lang('name'));
$this->smarty->assign('nameinput', $this->createinputtext($id,'name',$record->name,30));
$this->smarty->assign('locationtext', $this->lang('location'));
$this->smarty->assign('locationinput', $this->createinputtext($id,'location',$record->location,30));
$this->smarty->assign('browse', $this->createlink($id,'browse',$returnid, $this->lang('browse'),array('curdir'=>dirname($record->location),'browse'=>'flvmp3'),'',false,false,'onclick="window.open(this.href,\'browser\',\'width=500,height=500\');return false;"'));
$this->smarty->assign('playertext', $this->lang('player'));
$this->smarty->assign('playerinput', $this->CreateInputDropdown($id, 'player', $playerlist, -1, $record->player));

$this->smarty->assign('submit', $this->CreateInputSubmit($id, 'submit', $this->lang('submit')));
$this->smarty->assign('apply', $this->CreateInputSubmit($id, 'apply', $this->lang('apply')));
$this->smarty->assign('cancel', $this->CreateInputSubmit($id, 'cancel', $this->lang('cancel')));

$infos = array();



if ($checkfile=="local")
{
	if (endswith($record->location,".flv") && $this->checkffmpeg())
		$infos[$this->lang('thumbnail')] = '<img alt="'.$this->lang('error_thumbnotavailable').'"src="'.$this->getthumb($record,$id,$returnid).'" alt="" />';

	include_once(dirname(__FILE__).'/class.ffmpeg.php');
	$MMfile = new pMMfile(cms_join_path($config['uploads_path'],$record->location),$this->getpreference('ffmpeg','ffmpeg'));
	$infos[$this->lang('playtime')] = $MMfile->playtime();
	$infos[$this->lang('filesize')] = $MMfile->filesize();
	if ($MMfile->hasvideo())
	{
		$infos[$this->lang('hasvideo')] = $this->lang('yes');
		$infos[$this->lang('width')] = $MMfile->width()." pixels";
		$infos[$this->lang('height')] = $MMfile->height()." pixels";
	}
	else $infos[$this->lang('hasvideo')] = $this->lang('no');
	$infos[$this->lang('hasaudio')] = ($MMfile->hasaudio() ? $this->lang('yes') :$this->lang('no'));
}
elseif ($checkfile=="remote")
	$infos[$this->lang('remotefile')] = $this->lang('noinformationavailable');
else
	$infos[$this->lang('unknownfile')] = $this->lang('noinformationavailable');

$this->smarty->assign_by_ref('infos',$infos);
if (count($infos)>0)
{
	$this->smarty->assign('infosstart',$this->CreateFieldsetStart( $id, 'infosstart', $this->lang('infos')));
	$this->smarty->assign('infosend',$this->CreateFieldsetEnd());
}



echo $this->processtemplate('record.tpl');


echo $this->EndTab();


echo $this->StartTab('params', $params);


$this->smarty->assign('widthtext', $this->lang('width'));
$this->smarty->assign('widthinput', $this->createinputtext($id,'width',$record->width,20));
$this->smarty->assign('widthclear','<a href="" onclick="document.getElementById(\''.$id.'width\').value=\'0\';return false;">'.$this->lang('clear').'</a>');
$this->smarty->assign('heighttext', $this->lang('height'));
$this->smarty->assign('heightinput', $this->createinputtext($id,'height',$record->height,20));
$this->smarty->assign('heightclear','<a href="" onclick="document.getElementById(\''.$id.'height\').value=\'0\';return false;">'.$this->lang('clear').'</a>');



$param_inputs = array();
if ($record->player!=-1)
{
	$player = new pPlayer();
	if (!$player->loaddb($record->player))
		echo $this->showerrors($this->lang('unknownplayer'));

	$this->smarty->assign('paramsstart',$this->CreateFieldsetStart( $id, 'paramsstart', $this->lang('flashvars')));
	$this->smarty->assign('paramsend',$this->CreateFieldsetEnd());

	$paramlist = array();
	foreach ($player->paramlist as $param)
	{
		if (!isset($record->params[$param]))
			$paramlist[$param] = $param;
	}

	foreach ($record->params as $name=>$value)
	{
		$entry = new stdclass();
		$entry->label = $name;
		$entry->input = $this->createinputtext($id,'record_param_'.$name,$value,20);
		$entry->clear = '<a href="" onclick="document.getElementById(\''.$id.'record_param_'.$name.'\').value=\'\';return false;">'.$this->lang('clear').'</a>';
		$param_inputs[] = $entry;
	}
	$entry = new stdclass();
	$entry->label = $this->CreateInputDropdown($id, 'record_newparam_name', $paramlist);
	$entry->input = $this->createinputtext($id,'record_newparam_value','',20);
	$param_inputs[] = $entry;
}
$this->smarty->assign_by_ref('recordparams', $param_inputs);


echo '<p>'.$this->lang('record_params_message').'</p>';
echo $this->processtemplate('recordparams.tpl');





echo $this->EndTab();
if ($checkfile && $record->player!=-1 && $player->checkfile())
{
	echo $this->StartTab('preview', $params);
	$width = ($record->width > 0 ? $record->width : $player->width);
	$height = ($record->height > 0 ? $record->height : $player->height);
	echo $this->ShowPlayerTemplate($id, $returnid, $player->template, $player->location, $record->id, $width, $height, array_merge($player->params, $record->params));
	echo $this->EndTab();

}

echo $this->CreateFormEnd();

echo $this->EndTabContent();



?>