<?php

if (!isset($gCms)) exit;

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

if (!isset($params['playerid']))
	return $this->DisplayErrorPage($id, $params, $returnid,$this->Lang('error_noplayerid'));

$errors = array();

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




if (!isset($params['cancel']) && (isset($params['submit']) || isset($params['apply'])))
{
	$playerid = $params['playerid'];
	$player = new pPlayer();
	if ($playerid != 'new')
	{
		if (!$player->loaddb($playerid))
			$errors[] = $this->lang('error_unknownplayer');
	}

	if (isset($params['name']) && $params['name']!="")
		$player->name = $params['name'];
	else $errors[] = $this->lang('error_emptyname');
	if (isset($params['location']) && $params['location']!="")
		$player->location = $params['location'];
	if (!$player->checkfile())
		 $errors[] = $this->lang('error_wronglocation');


	if (isset($params['width']) && $params['width']!="" && $params['width']!=0 && is_numeric($params['width']))
		$player->width = $params['width'];
	else $errors[] = $this->lang('error_wrongwidth');
	if (isset($params['height']) && $params['height']!="" && $params['height']!=0 && is_numeric($params['height']))
		$player->height = $params['height'];
	else $errors[] = $this->lang('error_wrongheight');

	if (isset($params['paramlist']))
		$player->paramlist = explode(";",$params['paramlist']);

	if (isset($params['template']) && $params['template']!="")
		$player->template = $params['template'];
	else $errors[] = $this->lang('error_emptytemplate');

	if (isset($params['player_newparam_name']) && isset($params['player_newparam_value']) && $params['player_newparam_value']!="")
		$player->params[$params['player_newparam_name']] = $params['player_newparam_value'];

	foreach ($params as $name=>$value)
	{
		if (startswith($name,"player_param_"))
		{
			$name = substr($name,strlen("player_param_"));
			$player->params[$name] = $value;
		}
	}

	if (count($errors)==0)
	{
		if ($playerid == 'new')
		{
			if ($player->savedbnew())
				$playerid = $player->id;
			else
				$errors[] = $this->lang('error_savedbnew');
		}
		else
			if (!$player->updatedb())
				$errors[] = $this->lang('error_updatedb');
	}
}
if (count($errors)>0)
{
	$module_error = '';
	foreach ($errors as $error)
		$module_error .= $error."<br />";
	$module_error = array("module_error"=>$module_error);
}
else
	$module_error = array();

if ((isset($params['submit']) || isset($params['cancel'])) && count($module_error)==0)
	$this->redirect($id,'defaultadmin','',array_merge(array('active_tab'=>'players'),$module_error));
elseif (isset($params['apply']) || count($module_error)>0)
{
	if (count($module_error)>0)
	{
		echo $this->showerrors($module_error);
		require_once dirname(__FILE__).'/action.editplayer.php';
	}
	else
		$this->redirect($id,'editplayer','',array_merge(array('playerid'=>$playerid),$module_error));
}

?>