<?php

class FeaturesView extends Slot {

	var $ModuleID = 3;
	var $SectionID = 51;
	var $describe = array();
	var $curPageNumber = 0;	

	function __construct($pquery) {
		parent::__construct($pquery);
		$this->Cache = 0;
		$this->Passing = TRUE;
	}
	
	function init() {
		//$this->Action->processEvent($this->SectionID,'open_describe',array('id' => $this->ModuleID),$this);
		//if(empty($this->PreparedTemplates['_error'])) $this->describe = $this->PreparedTemplates;
		$this->PreparedTemplates = array();
		
		if(!empty($this->LocalTarget[1]) && validate(intval($this->LocalTarget[1]))) {
					$this->Action->processEvent($this->SectionID,'open_story',array('id' => $this->LocalTarget[1]),$this);
		} 
		switch(@$this->LocalTarget[0]) {
			case 'Reviews':
				if(!empty($this->LocalTarget[1]) && is_AlphaNumDefis(stripslashes($this->LocalTarget[1]),1,1024)) {
					$this->Action->processEvent($this->SectionID,'open_url',array('url_name' => $this->LocalTarget[1]),$this);
					if(!empty($this->PreparedTemplates['_error'])) header('Location: '.$this->Home.'/news'.$this->SID);
					else $this->Scheme = 'story';
				}
				else header('Location: '.$this->Home.'/features'.$this->SID);
			break;
			case 'news':
				$request = array('conditions' => array(),'pageRecordsQty' => '10'    , 'sortColumn'=>'date' , 'sortType' =>'DESC'    );
				$request['curPageNumber'] = empty($this->LocalTarget[0]) || !validate(intval($this->LocalTarget[0]))? 1 : intval($this->LocalTarget[0]+1);
				$this->curPageNumber = ($request['curPageNumber'] == 0)? $request['curPageNumber']: $request['curPageNumber']-1;
				$this->Action->processEvent($this->SectionID,'open',$request,$this);
				$this->getPageControl($this->Home.'/news', '', '&nbsp');
			break;
			
			default:
				$this->Action->processEvent($this->SectionID,'getlistfeatures',array(),$this); 
			break;
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function fillElements() {
		if(!empty($this->describe)) {
			$this->TempFillEls['describe'][0]['top'][0]['sys_name'] = strtoupper($this->describe['sys_name']);
			foreach($this->describe['contents'] as $key => $value) {
				$this->TempFillEls['describe'][0]['descr'][$key] = array(
					'top_name' => !empty($value['name'])? array(array('header' => strtoupper($value['name']))) : array(),
					'text' => $value['content_langs'][0]['value'],
				);
			}
		}
		switch($this->Scheme) {
			case 'story': $this->fillStory(); break;
			case 'features': $this->fillFeatures(); break;
			default: $this->fillDefault(); break;
		}
	}

	function fillFeatures() {
		foreach($this->PreparedTemplates['features'] as $key => $val) {
			foreach($val["story_langs"] as $key2 => $val2) {
				if($val2['lang'] == $this->LM->Langs[$this->language]) {
					$this->TempFillEls['features'][] = array(
						'text' => $val2['short_text'],
						'big_image' => file_exists($this->ImagePath.'/features/'.$val['id'].'/big.jpg')? makeImage($this->ImagePath.'/features/'.$val['id'].'/big.jpg', $this->Home.'/night_vision_images/features/'.$val['id'].'/big.jpg')  : "",
					);
					break;
				}
			}
		}
	}
	
	function fillStory() {
		foreach($this->PreparedTemplates['story_langs'] as $key => $val) {
			if($val['lang'] == $this->LM->Langs[$this->language]) {
				$this->TempFillEls['story'][] = array(
					'title' => $val['title'],
					'text' => $val['text'],
				);
			}
		}
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'open_url');
		if(!count($this->TempFillEls['story'])) header('Location: '.$this->Home.'/NewsMeinung'.$this->SID);
	}

	function fillDefault() {
		$onlyOneNews = 1;
		$this->story = $this->Action->getTemplate($this->SectionID,'open_story');
		foreach($this->PreparedTemplates['curPage'] as $pkey => $pval) {
			if(is_array($pval['story_langs']))		{ 
						$date = new DateTime($pval['date']); 
						$HeaderDate = $date->format('F j, Y');
					    if ($onlyOneNews == 1 )					    {
							if (empty ($this->story)) {
											$this->TempFillEls['story'][] = array(
												'image' => makeImage($this->ImagePath.'/news/'.$pval['id'].'.jpg', $this->ImageWPath.'/news/'.$pval['id'].'.jpg', '', 'left', array(8,4), 0,	 '', 'style="clear:both;"'	),
												'title' => $pval['story_langs'][0]['title'],
												'date' => $HeaderDate,
												'text' => $pval['story_langs'][0]['text'],
											);
										    $onlyOneNews++;
							} else {
								foreach($this->PreparedTemplates['curPage'] as $key => $val) 
								    if (isset($this->LocalTarget[1]) && $val['id'] == $this->LocalTarget[1]) { 
										$date = new DateTime($val['date']); 
										$HeaderDate = $date->format('F j, Y');
									    }
											$this->TempFillEls['story'][] = array(
												'image' => makeImage($this->ImagePath.'/news/'.$this->story['id'].'.jpg', $this->ImageWPath.'/news/'.$this->story['id'].'.jpg', '', 'left', array(8,4), 0,	 '', 'style="clear:both;"'	),
												'title' => $this->story['story_langs'][0]['title'],
												'date' => $HeaderDate,
												'text' => $this->story['story_langs'][0]['text'],
											);
										    $onlyOneNews++;
							}
					    }
						$this->TempFillEls['list'][] = array(
							'title' => $pval['story_langs'][0]['title'],
							'date' => $HeaderDate,
							'url' => $this->Target[1].'/'.$this->curPageNumber.'/'.$pval['id'],
						);
			}
		}
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'open');
		if(!count($this->TempFillEls['story'])) header('Location: '.$this->Home.$this->SID);
	}
	
}

?>