<?php

class UndoHandler extends Handler{

	var $module;
	var $modules = array();
	var $max = 0;
	var $arj_obj = array();

	function __construct(){
		parent::__construct('UndoHandler');
	}

	function createProduct($ship = FALSE) {
		$product = new Product();
		$product->item = new ParentItem();
		$product->item->itemtype = new ItemType();
		$product->files = new ObjectsList();
		$product->files->type = new ProductFile();
		$product->generation = new Generation();
		$product->warranty = new Warranty();
		$product->parameters = new ObjectsList();
		$product->parameters->type = new Parameter();
		$product->features = new ObjectsList();
		$product->features->type = new Feature();
		$product->reviews = new ObjectsList();
		$product->reviews->type = new Review();
		$product->customerreviewitems = new ObjectsList();
		$product->customerreviewitems->type = new CustomerReviewItem();
		$product->prices = new ObjectsList();
		$product->prices->type = new Price();
		$product->productlineitems = new ObjectsList();
		$product->productlineitems->type = new ProductLineItem();
		$product->productlineitems->type->lineitem = new Product();
		$product->links = new ObjectsList();
		$product->links->type = new Link();
		$product->technologylineitems = new ObjectsList();
		$product->technologylineitems->type = new TechnologyLineItem();
		$product->technologylineitems->type->lineitem = new Technology();
		$product->recomenduselineitems = new ObjectsList();
		$product->recomenduselineitems->type = new RecomendUseLineItem();
		$product->recomenduselineitems->type->lineitem = new RecomendUse();
		$product->articlelineitems = new ObjectsList();
		$product->articlelineitems->type = new ArticleLineItem();
		$product->articlelineitems->type->lineitem = new Article();
		$product->shipcosts = new ObjectsList();
		$product->shipcosts->type = new ShipCost();
		$product->shipcosts->type->type = new ShipCostType();
		$product->metas = new ObjectsList();
		$product->metas->type = new Meta();
		$product->productjszones = new ObjectsList();
		$product->productjszones->type = new ProductJSZone();
		return $product;
	}

	function createLayout() {
		$layout = new Layout();
		$layout->contenttypes = new ObjectsList();
		$layout->contenttypes->type = new LayoutContentType();
		return $layout;
	}

	function createModule() {
		$module = new Module();
		$module->parent = new Module();
		$module->bc_parent = new Module();
		$module->layout = new Layout();
		//$module->layout->contenttypes = new ObjectsList();
		//$module->layout->contenttypes->type = new LayoutContentType();
		$module->type = new ModuleType();
		$module->contents = new ObjectsList();
		$module->contents->type = new Content();
		$module->modulejszones = new ObjectsList();
		$module->modulejszones->type = new ModuleJSZone();
		//$module->modulecomments = new ObjectsList();
		//$module->modulecomments->type = new ModuleComment();
		$module->moduletemplates = new ObjectsList();
		$module->moduletemplates->type = new ModuleTemplate();
		$module->menu = new ObjectsList();
		$module->menu->type = new ModuleMenuLineItem();
		$module->menu->type->menu = new Menu();
		$module->metas = new ObjectsList();
		$module->metas->type = new Meta();
		$module->metas->type->moduletype = new ModuleType();
		$module->metas->type->metatype = new MetaType();
		$module->customerreviewitems = new ObjectsList();
		$module->customerreviewitems->type = new CustomerReviewItem();
		//$module->menu->type->menu->menulineitems = new ObjectsList();
		//$module->menu->type->menu->menulineitems->type = new MenuLineItem();
		return $module;
	}

	function createMenu() {
		$menu = new Menu();
		$menu->menulineitems = new ObjectsList();
		$menu->menulineitems->type = new MenuLineItem();
		return $menu;
	}
	
	function createTemplate(){
		$TemplateHandler = new TemplateHandler();
		$template = $TemplateHandler->createTemplate();
		return $template;
	}
	
	function createSubdomain () {
		return new Subdomain();
	}

	function createCR() {
		return new CustomerReview();
	}

	function createCity() {
		$CitiesHandler = new CitiesHandler();
		$city = $CitiesHandler->createCity();
		return $city;
	}
	
	function createImportedFeed($with_data = false) {
		$FeedHandler = new FeedHandler();
		$importedfeed = $FeedHandler->createImportedFeed($with_data);
		return $importedfeed;
	}
	
	function createFeed() {
		$FeedHandler = new FeedHandler();
		$feed = $FeedHandler->createFeed();
		return $feed;
	}

	function createCategory() {
		$category = new Category();
		$category->links = new ObjectsList();
		$category->links->type = new Link();
		$category->color = new Color();
		$category->metas = new ObjectsList();
		$category->metas->type = new Meta();
		$category->categoryjszones = new ObjectsList();
		$category->categoryjszones->type = new CategoryJSZone();
		$category->reviews = new ObjectsList();
		$category->reviews->type = new Review();
		$category->customerreviewitems = new ObjectsList();
		$category->customerreviewitems->type = new CustomerReviewItem();
		return $category;
	}

	function createParentItem() {
		return new ParentItem();
	}

	function _parseObject($object) {
		if(strtolower(get_class($object)) == 'objectslist') return $object->data;
		else return get_object_vars($object);
	}

	function _parseContent($object) {
		isset($TemplatedElements) or $TemplatedElements = array();
		foreach(is_object($object)? $this->_parseObject($object) : $object as $key => $value)
			$TemplatedElements[$key] = is_object($value) || is_array($value)? $this->_parseContent($value) : $value;
		return $TemplatedElements;
	}

	function GetArrayInFile($Array,$name_array,$file_name){
		if(!$file_arr = fopen($file_name, "w+")) {print "Function GetArrayInFile cannot make file.<br>";}
		$arr_str = "<?php \r\n$".$name_array." = ";
		$arr_str .= var_export($Array, true);
		$arr_str .= ";\r\n?>";
		fputs($file_arr, $arr_str);
		fclose($file_arr);
		@chmod($file_name, 0664);
		return $arr_str;
	}

	function GetUndoArray($path, $type='module') {
		if(file_exists($path)) include($path);
		switch($type) {
			case 'module':
				if(isset($Undo_object)) {
					$my_object=$this->createModule();
					$this->parseRequest($Undo_object,$my_object);
					return $this->_parseContent($my_object);
				}
				else return array('_error' => 'Object not found');
			break;
			case 'layout':
				if(isset($Undo_object)) {
					foreach($Undo_object as $key => $value) if(is_array($value)) unset($Undo_object['key']);
					return $Undo_object;
				}
				else return array('_error' => 'Object not found');
			break;
			case 'template':
				if(isset($Undo_object)) {
					$my_object = $this->createTemplate();
					$this->parseRequest($Undo_object, $my_object);
					return $this->_parseContent($my_object);
				}
				else return array('_error' => 'Object not found');
			break;
			case 'menu':
				if(isset($Undo_object)) {
					$my_object=$this->createMenu();
					$this->parseRequest($Undo_object,$my_object);
					return $my_object;
				}
				else return array('_error' => 'Object not found');
			break;
			case 'subdomain':
				if(isset($Undo_object)) {
					$my_object=$this->createSubdomain();
					$this->parseRequest($Undo_object, $my_object);
					return $my_object;
				}
				else return array('_error' => 'Object not found');
			break;
			case 'customerreview':
				if(isset($Undo_object)) {
					$my_object=$this->createCR();
					$this->parseRequest($Undo_object, $my_object);
					return $my_object;
				}
				else return array('_error' => 'Object not found');
			break;
			case 'importedfeed':
				if(isset($Undo_object)) {
					$my_object=$this->createImportedFeed();
					$this->parseRequest($Undo_object, $my_object);
					return $my_object;
				}
				else return array('_error' => 'Object not found');
			break;
			case 'city':
				if(isset($Undo_object)) {
					$my_object=$this->createCity();
					$this->parseRequest($Undo_object, $my_object);
					return $my_object;
				}
				else return array('_error' => 'Object not found');
			break;
			case 'feed':
				if(isset($Undo_object)) {
					$my_object=$this->createFeed();
					$this->parseRequest($Undo_object, $my_object);
					return $my_object;
				}
				else return array('_error' => 'Object not found');
			break;
			case 'category':
				if(isset($Undo_object)) {
					$my_object=$this->createCategory();
					$this->parseRequest($Undo_object,$my_object);
					return $this->_parseContent($my_object);
				}
				else return array('_error' => 'Object not found');
			break;
			case 'parentitem':
				if(isset($Undo_object)) {
					$my_object=$this->createParentItem();
					$this->parseRequest($Undo_object,$my_object);
					return $this->_parseContent($my_object);
				}
				else return array('_error' => 'Object not found');
			break;
			case 'product':
				if(isset($Undo_object)) {
					$my_object=$this->createProduct();
					$this->parseRequest($Undo_object,$my_object);
					return $this->_parseContent($my_object);
				}
				else return array('_error' => 'Object not found');
			break;
		}
	}
}
?>