<?php

class MetaInfoView extends Slot {

	var $ModuleID = 1;
	var $SectionID = 14;
	var $module = 1;
	var $city = 0;
	var $path = '';
	var $modules = array(
		'module' => 1,
		'category' => 2,
		'product' => 3
	);
	var $type = 0;

	function __construct($pquery) {
		//$this->Slot($pquery);
		parent::__construct($pquery);
	}

	function init() { 
		global $REGISTRY, $RequestBroker;
		//printArray($REGISTRY);
		if(empty($REGISTRY['Request']['Compare'])) {
			$this->module = $this->modules[$REGISTRY['Page']['entity']];
			if(empty($REGISTRY['Page']['id'])) {
				$RequestBroker->getPageId(str_replace('/printpage','',$REGISTRY['Request']['RawTarget']));
			}
			//printArray($REGISTRY['Page']);
			if(!empty($REGISTRY['Page']['id'])) {
				$this->Action->processEvent($this->SectionID,'open',array('module' => $this->module,'item' => $REGISTRY['Page']['id']),$this);
				foreach($REGISTRY['VirtualSlotTarget']['main'] as $path => $handler)
					if($handler == ($REGISTRY['Page']['entity']=='category'? 'Store' : 'Product').'RSSView:object?id='.$REGISTRY['Page']['id']) $this->path = $path;
			}
		}
	}

	function fillElements() {
		global $REGISTRY;
		if(!empty($this->LocalTarget[0]) && $this->LocalTarget[0]=='printpage') $this->type = 1;
		if($this->city) {
			$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'city');
			$this->TempFillEls['title'] = ($this->type? 'Print Page - ' : '').eregi_replace('"','',$this->PreparedTemplates[0]['meta_title']);
			/*$this->TempFillEls['metas'][] = array(
				'type' => 'keywords',
				'value' => eregi_replace('"','',$this->PreparedTemplates[0]['meta_keywords']),
			);*/
			$this->TempFillEls['metas'][] = array(
				'type' => 'description',
				'value' => eregi_replace('"','',$this->PreparedTemplates[0]['meta_description']),
			);
			$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'city');
		}
		else if(!empty($REGISTRY['Request']['Compare'])) {
			$this->TempFillEls['title'] = ($this->type? 'Print Page - ' : '').str_replace('"','',implode(' VS ',$REGISTRY['Request']['Compare']['names']));
			$this->TempFillEls['title'].= ' - '.md5(($this->type? 'Print Page - ' : '').$REGISTRY['Request']['Compare']['color']['label'].'_'.implode('_compare_',$REGISTRY['Request']['Compare']['items']));
			$this->TempFillEls['metas'][] = array(
				'type' => 'description',
				'value' => 'Compare '.str_replace('"','',implode(' VS ',$REGISTRY['Request']['Compare']['names'])),
			);
		}
		else {
			$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
			foreach($this->PreparedTemplates as $key => $value) {
				if($value['metatype']['id']==1) {
//					$this->TempFillEls['title'] = eregi_replace('"','',$value['value']);
					$this->TempFillEls['title'] = str_replace('"','',$value['value']);
					if($value['module']['type']==6) {
						parse_str(getenv('REDIRECT_QUERY_STRING'));
						if(!empty($query)) $this->TempFillEls['title'] = ($this->type? 'Print Page - ' : '').'Search query: '.htmlspecialchars(urldecode($query)).' - '.(isset($page)? 'Page '.($page+1).' - ' : '').(isset($sort)? 'Sort: '.ucfirst($sort).(isset($direct)? '('.ucfirst($direct).')' : '').' - ' : '').$this->TempFillEls['title'];
					}
					else $this->TempFillEls['title'] = ($this->type? 'Print Page - ' : '').$this->TempFillEls['title'];
				}
				else if($value['metatype']['id']==2) {
					$desc = '';
					if($value['module']['type']==6) {
						parse_str(getenv('REDIRECT_QUERY_STRING'));
						if(!empty($query)) $desc = 'Search query: '.htmlspecialchars(urldecode($query)).' - '.(isset($page)? 'Page '.($page+1).' - ' : '').(isset($sort)? 'Sort: '.ucfirst($sort).(isset($direct)? '('.ucfirst($direct).')' : '').' - ' : '');
					}
					$this->TempFillEls['metas'][] = array(
						'type' => $value['metatype']['name'],
					//	'value' => $desc.eregi_replace('"','',$value['value']),
						'value' => $desc.str_replace('"','',$value['value']),
					);
				}
				else {
					$desc = '';
					if($value['module']['type']==6) {
						parse_str(getenv('REDIRECT_QUERY_STRING'));
						if(!empty($query)) $desc = 'Search query: '.htmlspecialchars(urldecode($query)).' - ';
					}
					if ($value['metatype']['name'] != "keywords") {
						$this->TempFillEls['metas'][] = array(
							'type' => $value['metatype']['name'],
							//'value' => $desc.eregi_replace('"','',$value['value']),
							  'value' => $desc.str_replace('"','',$value['value']),
						);
					}
				}
			}
			//if($this->module!=1) $this->TempFillEls['rss'] = '<link rel="alternate" type="application/rss+xml" title="'.$this->TempFillEls['title'].'" href="'.$this->path.'" />';
		}
	}
}
?>