<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

include_once('classes/core/CoreObject.class.php');

/**
 * Version of Content
 *
 * @author     vinadelmar <vinadelmar@users.sourceforge.net>
 * @package    Core
 * @subpackage Versions
 */
class ContentVersion extends CoreObject {
	/**
	 * @var integer
	 */
	var $id;
	
	/**
	 * @var integer timestamp
	 */
	var $date;
	
	/**
	 * @var array
	 */
	var $valuesTables;
	
	/**
	 * Constructor
	 *
	 * @access public
	 * @param  integer $versionId
	 * @param  integer $versionDate timestamp
	 */
	function ContentVersion($versionId, $versionDate) {
		$this->id = $versionId;
		$this->date = $versionDate;
		$this->valuesTables = array();
	}
	
	/**
	 * @return integer
	 */
	function getVersionId() {
		return $this->id;
	}
	
	/**
	 * Gets Version Date
	 *
	 * @param  string $format
	 * @return mixed string or integer
	 */
	function getVersionDate($format='') {
		if ($format) {
			return date($format, $this->date);
		}
		
		return $this->date;
	}
	
	/**
	 * Gets All Content Translations
	 *
	 * @return array
	 */
	function getValuesTables() {
		return $this->valuesTables;
	}
	
	/**
	 * Gets Content Translation
	 *
	 * @param  integer $langId
	 * @return mixed array or null
	 */
	function getValuesTable($langId) {
		if (!$this->valuesTableExists($langId)) {
			return null;
		}
		
		return $this->valuesTables[$langId];
	}
	
	/**
	 * @return boolean
	 */
	function valuesTableExists($langId) {
		return isset($this->valuesTables[$langId]);
	}
	
	/**
	 * Adds Content Translation
	 *
	 * @param  integer $langId
	 * @param  array   $valuesTable
	 * @return boolean
	 */
	function addValuesTable($langId, $valuesTable) {
		if ($this->valuesTableExists($langId)) {
			$this->debug('ContentVersion::addValuesTable, warning : values table for lang #'.$langId.' already set (add skiped)', 8);
			return false;
		}
		
		$this->valuesTables[$langId] = $valuesTable;
		$this->debug('ContentVersion::addValuesTable, success : values table for lang #'.$langId.' added', 9);
		return true;
	}
	
	/**
	 * Exports Object Into XML String
	 *
	 * @return string XML
	 */
	function toXml() {
		$xml  = '';
		$xml .= '<version id="'.$this->getVersionId().'" date="'.$this->getVersionDate('Y-m-d H:i:s').'">'."\n";
		
		foreach ($this->valuesTables as $langId => $valuesTable) {
			$xml .= '  <lang id="'.$langId.'">'."\n";

			foreach ($valuesTable as $varName => $value) {
				$xml .= '    <'.$varName.'>'."\n";
				$xml .= '    '.$value->toXML();
				$xml .= '    </'.$varName.'>'."\n";
			}
			
			$xml .= '  </lang>'."\n";
		}
		
		$xml .= '</version>';
		
		return $xml;
	}
}

?>