<?php

if (!defined('XOOPS_ROOT_PATH')) exit();

require_once XOOPS_ROOT_PATH."/kernel/XCube_Content.class.php";

/**
 * WARNING : THIS IS TEST CLASS
 *
 * @see XCube_ContentObjectManager
 */
class Legacy_ContentManager extends XCube_ContentObjectManager
{
	function fetchContent($modId,$className,$dataId)
	{
		$db=&$this->mRoot->mController->getDB();

		$table=$db->prefix("legacy_contents");
		
		$strFlag=false;

		if(is_int($dataId)) {
			$sql="SELECT * FROM ${table} WHERE mod_id=? AND class_name=? AND data_id=?";
			$db->prepare($sql);
			$db->bind_param("isi",$modId,$className,$dataId);
		}
		else {
			$sql="SELECT * FROM ${table} WHERE mod_id=? AND class_name=? AND data_str_id=?";
			$db->prepare($sql);
			$db->bind_param("iss",$modId,$className,$dataId);
			$strFlag=true;
		}

		$result=$db->execute();
		if(!$result) {
			die("You can not access to content manage data resource.");
		}

		if($row=$db->fetchArray($result)) {
			if($strFlag) {
				$ret=new XCube_ContentObject($row['id'],$row['mod_id'],$row['class_name'],$row['data_str_id']);
			}
			else {
				$ret=new XCube_ContentObject($row['id'],$row['mod_id'],$row['class_name'],$row['data_id']);
			}

			return $ret;
		}

		//
		// Because we can't get content id, we have to register this parameter
		// to object manager. 
		//
		// TODO the follow process depends on mysql database.
		//

		if($strFlag) {
			$db->prepare("INSERT INTO ${table} ( mod_id, class_name, data_str_id ) VALUES ( ?, ?, ? )");
			$db->bind_param("iss",$modId,$className,$dataId);
		}
		else {
			$db->prepare("INSERT INTO ${table} ( mod_id, class_name, data_id ) VALUES ( ?, ?, ? )");
			$db->bind_param("isi",$modId,$className,$dataId);
		}

		$result=$db->executeF();
		if(!$result) {
			die("OH MY GOD");
		}

		$ret = new XCube_ContentObject($db->getInsertId(),$modId,$className,$dataId);

		return $ret;
	}
}

?>