<?php

class ProductRSSView extends Slot {

	var $SectionID = 23;
	var $ModuleID = 2;

	function __construct($pquery) {
		parent::__construct($pquery);
	}

	function init() {
		$id = !empty($this->ID)? $this->ID : (isset($this->LocalTarget[0])? intval($this->LocalTarget[0]) : 0);
		if(empty($id)) header('Location: '.$this->Home.'/Units'.$this->SID);
		else {
			$this->Action->processEvent($this->SectionID,'open',array('id' => $id,'ship' => 0,'catid' => $this->_ModuleID,'children' => 0),$this);
			if(!empty($this->PreparedTemplates['_error'])) header('Location: '.$this->Home.'/Units'.$this->SID);
		}
	}

	function fillElements() {
		global $REGISTRY;
		header("Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, post-check=0, pre-check=0, max-age=0",true);
		header("Pragma: no-cache",true);
		header("Content-type: text/xml");
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
		foreach($REGISTRY['VirtualSlotTarget']['main'] as $path => $handler) {
			if(!empty($this->PreparedTemplates['id']) && $handler == 'ProductView:object?id='.$this->PreparedTemplates['id']) {
				$link = $path;
				$cat = explode('/',$path);
				$pop = array_pop($cat);
				$cat = implode('/',$cat);
				$selected = $this->Db->SelectAny('SELECT c.name FROM categorylineitem cl, category c WHERE cl.item='.$this->PreparedTemplates['id'].' AND c.id=cl.ownerid order by cl.ownerid asc limit 1');
				if(!empty($selected)) $category = $selected[0]['name'];
				else $category = 'Night Vision Optics';
			}
		}
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'open');
		$this->TempFillEls['pubdate'] = gmdate("D, d M Y H:i:s").' GMT';
		$this->TempFillEls['channel'] = $this->PreparedTemplates['name'];
		$this->TempFillEls['item'][0] = array(
			'title' => !empty($this->PreparedTemplates['nav_name'])? $this->PreparedTemplates['nav_name'] : $this->PreparedTemplates['name'],
			'desc' => htmlspecialchars(strip_tags($this->PreparedTemplates['full_desc'])),
			'link' => $link,
			'pubdate' => gmdate("D, d M Y H:i:s").' GMT',
			'category' => $category,
			'images' => '<br>',
		);
		if(!empty($this->PreparedTemplates['files'])) {
			$path = '/products/'.$this->PreparedTemplates['id'];
			$count = count($this->PreparedTemplates['files']);
			$j = 1;
			$middle_images = array();
			$big_images = array();
			for($i = 0; $i < $count; $i++) {
				if($this->PreparedTemplates['files'][$i]['filetype']==8) {
					$middle_images[0] = !empty($this->PreparedTemplates['files'][$i]['image_link'])? '/products/'.$this->PreparedTemplates['files'][$i]['image_link'].'/middle.png' : $path.'/middle.png';
				}
				else if($this->PreparedTemplates['files'][$i]['filetype']==2) {
					$this->TempFillEls['id'] = !empty($this->PreparedTemplates['files'][$i]['image_link'])? $this->PreparedTemplates['files'][$i]['image_link'] : $this->PreparedTemplates['id'];
					$big_images[0] = !empty($this->PreparedTemplates['files'][$i]['image_link'])? '/products/'.$this->PreparedTemplates['files'][$i]['image_link'].'/big.jpg' : $path.'/big.jpg';
				}
				else if($this->PreparedTemplates['files'][$i]['filetype']==3 && validate(trim($this->PreparedTemplates['files'][$i]['imagetype']))) {
					$middle_images[$j] = $path.'/images/middle/'.date2pg($this->PreparedTemplates['files'][$i]['number']).'.png';
					$big_images[$j] = $path.'/images/big/'.date2pg($this->PreparedTemplates['files'][$i]['number']).'.jpg';
					$j++;
				}
			}
			ksort($middle_images);
			foreach($middle_images as $key => $value)
				$this->TempFillEls['item'][0]['images'] .= ($key%8==0 && $key!=0? '<br>' : '').makeLink($this->Home.$this->ImageWPath.$big_images[$key],makeImage($this->ImagePath.$value,$this->Home.$this->ImageWPath.$value,(!empty($this->PreparedTemplates['nav_name'])? $this->PreparedTemplates['nav_name'] : $this->PreparedTemplates['name']))).' ';
		}
		if(!empty($this->PreparedTemplates['parameters'])) {
			$count = count($this->PreparedTemplates['parameters']);
			if($count > 0) $this->TempFillEls['item'][0]['desc'] .= '<p>Specification:</p><ul>';
			for($i = 0; $i < $count; $i++) {
				$this->TempFillEls['item'][0]['desc'] .= '<li>'.$this->PreparedTemplates['parameters'][$i]['name'].' '.$this->PreparedTemplates['parameters'][$i]['value'].'</li>';
			}
			if($count > 0) $this->TempFillEls['item'][0]['desc'] .= '</ul>';
		}
		if(!empty($this->PreparedTemplates['features'])) {
			$count = count($this->PreparedTemplates['features']);
			if($count > 0) $this->TempFillEls['item'][0]['desc'] .= '<p>Features:</p><ul>';
			for($i = 0; $i < $count; $i++) {
				$this->TempFillEls['item'][0]['desc'] .= '<li>'.$this->PreparedTemplates['features'][$i]['value'].'</li>';
			}
			if($count > 0) $this->TempFillEls['item'][0]['desc'] .= '</ul>';
		}
	}
}
?>