<?php

if (!isset($gCms)) exit;

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

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


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

$errors = array();

if (!isset($params['cancel']) && (isset($params['submit']) || isset($params['apply'])))
{
	$recordid = $params['recordid'];
	$record = new pRecord();
	if ($recordid != 'new')
	{
		if (!$record->loaddb($recordid)) echo "erreur chargement record ".$recordid;
	}

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



	if (isset($params['width']) && ($params['width']=="" || is_numeric($params['width'])))
		$record->width = $params['width'];
	else $errors[] = $this->lang('error_wrongwidth');
	if (isset($params['height']) && ($params['height']=="" || is_numeric($params['height'])))
		$record->height = $params['height'];
	else $errors[] = $this->lang('error_wrongheight');
	if (isset($params['record_newparam_name']) && isset($params['record_newparam_value']) && $params['record_newparam_value']!="")
		$record->params[$params['record_newparam_name']] = $params['record_newparam_value'];
	foreach ($params as $name=>$value)
	{
		if (startswith($name,"record_param_"))
		{
			$name = substr($name,strlen("record_param_"));
			$record->params[$name] = $value;
		}
	}



	if (count($errors)==0)
	{
		if ($recordid == 'new')
		{
			if ($record->savedbnew())
				$recordid = $record->id;
			else
				$errors[] = $this->lang('error_savedbnew');
		}
		else
			if (!$record->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','',$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.editrecord.php';
	}
	else
		$this->redirect($id,'editrecord','',array_merge(array('recordid'=>$recordid),$module_error));
}


?>