<?php

function getPlayRecords () {
	$results = array();
	global $gCms;
	$db = & $gCms->getDb();
	$query = 'SELECT * FROM '.$gCms->config["db_prefix"].'module_play_records';
	$dbresult = $db->Execute($query);
	while ($dbresult && $row = $dbresult->FetchRow())
	{
		$record = new pRecord;
		$record->loadrow($row);
		$results[] = $record;
	}
	return $results;
}

class pRecord {
	var $id;
	var $name;
	var $location;
	var $description;
	var $params;
	var $player;
	var $width;
	var $height;

	var $db;
	var $db_prefix;

	function pRecord() {
		global $gCms;
		$this->db = & $gCms->getDb();
		$this->db_prefix = & $gCms->config["db_prefix"];

		$this->id = -1;
		$this->name = "";
		$this->location = "";
		$this->description = "";
		$this->player = -1;
		$this->width = 0;
		$this->height = 0;
		$this->params = array();
	}
	function loaddb($id = -1)
	{
		if ($id!=-1)
			$this->id = $id;
		if ($this->id == -1)
			return false;

		$query = 'SELECT * FROM '.$this->db_prefix.'module_play_records WHERE id = ?';
		$row = $this->db->GetRow($query, array($this->id));
		if ($row)
			$this->loadrow($row);

		return ($row !== false);
	}

	function loadrow($row)
	{
		$this->id = $row['id'];
		$this->name = $row['name'];
		$this->location = $row['location'];
		$this->description = $row['description'];
		$this->player = $row['player'];
		$this->width = $row['width'];
		$this->height = $row['height'];

		$this->params = array();
		$params_tmp = explode(";",$row['params']);
		foreach ($params_tmp as $param)
		{
			$param = explode("=",$param);
			if (count($param)>1)
				$this->params[$param[0]] = $param[1];
		}
	}

	function savedbnew()
	{
		$newid = $this->db->GenID($this->db_prefix."module_play_records_seq");
		$this->id = $newid;

		$params = array();
		foreach ($this->params as $name=>$value)
		{ if ($value!="") $params[] = $name."=".$value;}

		$query = 'INSERT INTO '.$this->db_prefix.'module_play_records (id,name,location,description,player,params,width,height) VALUES (?,?,?,?,?,?,?,?)';
		$dbresult = $this->db->Execute($query, array($newid,$this->name,$this->location,$this->description,$this->player,implode(";",$params),$this->width,$this->height));
		if ($dbresult )
			return $this->id;
		else
			return false;
	}

	function updatedb()
	{
		if ($this->id == -1) return false;

		$params = array();
		foreach ($this->params as $name=>$value)
		{ if ($value!="") $params[] = $name."=".$value;}

		$query = 'UPDATE '.$this->db_prefix.'module_play_records SET name=?,location=?,description=?,player=?,params=?,width=?,height=? WHERE id = ?';
		$dbresult = $this->db->Execute($query, array($this->name,$this->location,$this->description,$this->player,implode(";",$params),$this->width,$this->height,$this->id));
		return ($dbresult !== false);
	}

	function deletedb()
	{
		if ($this->id == -1) return false;
		$query = "DELETE FROM ".$this->db_prefix."module_play_records WHERE id = ?";
		$dbresult = $this->db->Execute($query, array($this->id));
		return ($dbresult !== false);
	}

	function checkFile()
	{
		global $gCms;
		// Check on server
		$location = cms_join_path($gCms->config['uploads_path'],$this->location);
		if (strpos($location,"http://")===false && is_file($location))
			return "local";
		// Check on the web
		if (strpos($location,"http://")!==false) {
			/*$result = @fopen($this->location, "r");                  Do not check remote files anymore : too long response time...
			if (!$result) return false;
			fclose($result);*/
			return "remote";
		}
		return false;
	}
}

?>