<?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
 *
 */

/**
 * @package    Core
 * @subpackage Parser
 */

/**
 * Parent Class
 */
include_once('classes/core/parser/XMLObjectParser.class.php');

/**
 * Types Manager
 */
include_once('classes/core/types/TypesManager.class.php');

/**
 * @author vinadelmar <vinadelmar@users.sourceforge.net>
 * @package    Core
 * @subpackage Parser
 */
class XMLContentParser extends XMLObjectParser {
	var $typesManager;
	var $typesArray;
	var $versionsArray;
	
	function XMLContentParser(&$typesManager, $typesArray, $xmlFile) {
		parent::XMLObjectParser($xmlFile);
		$this->typesManager = $typesManager;
		$this->typesArray = $typesArray;
		$this->versionsArray = array();
	}
	
	function XMLParseFile() {
		if (!parent::XMLParseFile()) {
			return false;
		}
		
		$contentId = $this->values[0]['attributes']['id'];
		$contentType = $this->values[0]['attributes']['type'];
		
		$num_versions = count($this->tags['version']);
		
		$k = 0;
		
		for ($i=0; $i<$num_versions; $i=$i+2) {
			$v0 = $this->tags['version'][$i];
			$v1 = $this->tags['version'][$i+1];
			$versionId = $this->values[$v0]['attributes']['id'];
			
			for ($j=$k; $this->tags['lang'][$j]>$v0 && $this->tags['lang'][$j]<$v1; $j=$j+2) {
				$l0 = $this->tags['lang'][$j];
				$l1 = $this->tags['lang'][$j+1];
				$langId = $this->values[$l0]['attributes']['id'];
				
				$valuesTable = array();
				
				foreach ($this->typesArray as $name => $type) {
					$value = $this->XMLGetValue($name, $type, $l0, $l1);
					$valuesTable[$name] = $this->typesManager->createTypeObject($type, $value);
				}
				
				$this->versionsArray[$versionId][$langId] = new Content(null, 0, $contentId, $contentType, $this->typesArray, $valuesTable, 0);
			}
			
			$k = $j;
		}
		
		return true;
	}
	
	/**
	 * @access private
	 */
	function XMLGetValue($name, $type, $from, $until) {
// TODO
		$num_tags = count($this->tags[$name]);
		$i = 0;
		
		while (!$found && $i<$num_tags) {
			$found = ($this->tags[$name][$i]>$from) && ($this->tags[$name][$i]<$until);
			$i++;
		}
		
		$pos = $this->tags[$name][$i-1];
		
		switch ($type) {
  		case INTEGER:
  			break;
  		case FLOAT:
  			break;
  		case BOOL:
  			break;
  		case STRING:
  		case SHORTTEXT:
  		case TEXT:
  			$value = $this->values[$pos]['value'];
  			break;
  		case COLOR:
  			break;
  		case LINK:
  			break;
  		case DATE:
  			$value  = '';
  			$value .= $this->values[$pos+1]['value'];     // year
  			$value .= '-'.$this->values[$pos+2]['value']; // month
  			$value .= '-'.$this->values[$pos+3]['value']; // day
  			break;
  		case TIME:
  			break;
  		case DOCUMENT:
  			break;
  		case IMAGE:
  			$value = 'foo';
  			break;
  		case REFERENCE:
  			break;
  		default:
  			$value = null;
		}
		
		return $value;
	}
	
	function getContent($versionId, $langId) {
		if (!isset($this->versionsArray[$versionId]) || !isset($this->versionsArray[$versionId][$langId])) {
			return null;
		}
		
		return $this->versionsArray[$versionId][$langId];
	}
}
?>