<?php

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

class pPlayer {
	var $id;
	var $name;
	var $location;
	var $template;
	var $params;
	var $paramlist;
	var $width;
	var $height;
	var $locked;

	var $db;
	var $db_prefix;
	var $smarty;

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

		$this->id = -1;
		$this->name = "";
		$this->location = "";
		$this->params = array();
		$this->paramlist = array();
		$this->locked = false;

		$this->template =  <<<EOF
<object type="application/x-shockwave-flash" data="{\$location}" width="{\$width}" height="{\$height}">
	<param name="movie" value="{\$location}" />
	<param name="FlashVars" value="flv={\$file}{foreach from=\$player_params key=n item=v}&amp;{\$n}={\$v}{/foreach}" />
	<param name="wmode" value="transparent" />
</object>
EOF;
	}

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

		$query = 'SELECT * FROM '.$this->db_prefix.'module_play_players 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->template = $row['template'];
		$this->width = $row['width'];
		$this->height = $row['height'];
		$this->locked = $row['locked'];
		if ($row['paramlist']!="")
			$this->paramlist = explode(';',$row['paramlist']);
		else
			$this->paramlist = array();

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

	function savedbnew($row)
	{
		$newid = $this->db->GenID($this->db_prefix."module_play_players_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_players (id,name,location,template,params,paramlist,width,height,locked) VALUES (?,?,?,?,?,?,?,?,?)';
		$dbresult = $this->db->Execute($query, array($newid,$this->name,$this->location,$this->template,implode(";",$params),implode(";",$this->paramlist),$this->width,$this->height,$this->locked));
		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_players SET name=?,location=?,template=?,params=?,paramlist=?,width=?,height=?,locked=? WHERE id = ?';
		$dbresult = $this->db->Execute($query, array($this->name,$this->location,$this->template,implode(";",$params),implode(";",$this->paramlist),$this->width, $this->height,$this->locked,$this->id));
		return ($dbresult !== false);
	}

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

	function isLocked()
	{
		return ($this->locked);
	}

	function checkFile()
	{
		global $gCms;
		if (!startswith($this->location, "modules/Play/"))
			$location = cms_join_path($gCms->config['uploads_path'],$this->location);
		else
			$location = cms_join_path($gCms->config['root_path'],$this->location);
		return (file_exists($location));
	}
}



?>