<?php

//global $Debug; $Debug=true;
class NewsView extends Slot {

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

	function __construct($pquery) {
		parent::__construct($pquery);
		$this->Cache = 0;
		$this->Passing = TRUE;
	}
	
	function init() {
		$request = array('conditions' => array(), 'pageRecordsQty' => '17', 'sortColumn'=>'date', 'sortType' =>'DESC'    );
		$request['curPageNumber'] = empty($this->Target[2]) || !validate(intval($this->Target[2]))? 1 : intval($this->Target[2]);
		$request['conditions'] = array('company' => $this->Properties['company']);
//$request['conditions'] = array('company' => 4);
		$this->Action->processEvent($this->SectionID,'getnews',$request,$this);
		$this->getPageControl($this->Home.'/'.$this->Target[1], '', '&nbsp');
		$this->setSchemeTemplate($this->Scheme);
	}

	function fillElements() {
		switch($this->Scheme) {
			default: $this->fillDefault(); break;
		}
	}

	function fillDefault() {		  //  printArray($this->PreparedTemplates);exit;
		foreach($this->PreparedTemplates['curPage'] as $pkey => $pval) {		
			if ($pval['type'] == 1) {  	// news
				if(is_array($pval['story_langs']))	{
					$date = new DateTime($pval['date']); 
					$HeaderDate = $date->format('F j, Y');
					
					$links = array();
					if (!empty($pval['links']) ) {
							foreach($pval['links'] as $keylink => $vallink) 
								$links[] = array('href' => $vallink['link'], 'title'=> $vallink['title']	);
					}

					$images = array();
					$path = $this->ImageWPath.'/news/'.$pval['id'].'/images';
					if (!empty($pval['files']) ) {
							foreach($pval['files'] as $keyfiles => $valfiles) 
								$images[] = array('link' => $path.'/'.date2pg($valfiles['number']).'.jpg'	);
					}

					$this->TempFillEls['news'][] = array(
								'title' => $pval['story_langs'][0]['title'],
								'date' => $HeaderDate,
								'short_text' => $pval['story_langs'][0]['short_text'],
								'links'  => $links,
								'images'  => $images,
							); 		
				}
			}  else {   				// event
				$dateto = new DateTime($pval['dateto']);
				if ($this->Properties['company'] == 4) {	//dimetrics
					$this->TempFillEls['events'][] = array(
								'title' => $pval['story_langs'][0]['title'],
								'date' => jb_verbose_date_range( $date->getTimestamp(), $dateto->getTimestamp() ),
								'short_text' => $pval['story_langs'][0]['short_text'],
								'place' => $pval['place'],
								'city'  => (trim($pval['place']) !='')? ', '.$pval['city']:$pval['city'],
							);

				} else {
					$date = new DateTime($pval['date']);
					$now = new DateTime();
					$this->TempFillEls[($now > $dateto?'pastevents':'events')][] = array(
								'title' => $pval['story_langs'][0]['title'],
								'date' => jb_verbose_date_range( $date->getTimestamp(), $dateto->getTimestamp() ),
								'short_text' => $pval['story_langs'][0]['short_text'],
								'place' => $pval['place'],
								'city'  => (trim($pval['place']) !='')? ', '.$pval['city']:$pval['city'],
							);
				}
			}
		}
		if (!empty( $this->TempFillEls['events'])) $this->TempFillEls['events_header'] = array(array());
		if (!empty( $this->TempFillEls['pastevents']))  $this->TempFillEls['pastevents_header'] = array(array());
	}
	
    function getPageControl($self, $sort = '') {
        $open_tag = "";
        $close_tag = "";
        $pages = '';
        $per_page = isset($this->PreparedTemplates['pageRecordsQty'])? $this->PreparedTemplates['pageRecordsQty'] : 10;
        $num_rows = isset($this->PreparedTemplates['recordsTotal'])? $this->PreparedTemplates['recordsTotal'] : 0;
        $cur_page = isset($this->PreparedTemplates['curPageNumber'])? $this->PreparedTemplates['curPageNumber'] - 1 : 0;
        if($this->PreparedTemplates['curPageNumber'] > $this->PreparedTemplates['pagesTotal'] && count($this->PreparedTemplates['curPage'])) header("Location: ".$self);
        $this->Navigator($self, $per_page, $num_rows, $cur_page, $sort, $this);
        $this->TempFillEls['navbar'] = implode('<br>',$this->navbar);
        foreach ($this->pager as $key => $value)
            $pages .= $open_tag.$value.$close_tag;                
        $this->TempFillEls['pagecontrol'][] = array(
            'prev' => $this->prev,
            'first' => $this->first,
            'pager' => $pages,
            'last' => $this->last,
            'next' => $this->next,
        );
        if(count($this->PreparedTemplates['curPage'])== 0) {
            if(!empty($this->PreparedTemplates['pagesTotal'])) header('Location: '.$self.$this->SID);
            else {
                $this->TempFillEls['navbar'] = 'This Section is empty!';
                $this->TempFillEls['pagecontrol'] = array();
            }
        }
        else {
            if(empty($this->pager)) $this->TempFillEls['pagecontrol'] = array();
            $this->TempFillEls['pagecontrol2'] = $this->TempFillEls['pagecontrol'];
        }
    }

    function Navigator($self, $per_page = '10', $num_rows = '0', $cur_page = '0', $sort = '') {
		$this->navbar = array();
		$this->pager = array();
		$this->next = '';
		$this->prev = '';
		$this->first = '';
		$this->last = '';
		switch($num_rows > 0) {
			case 'true':
				$page_num = $cur_page + 1;
				$total = ceil($num_rows/$per_page);
				$buffer = 5;
				if(validate($total)) {
					$prevpage = $cur_page - 1;
					$nextpage = $cur_page + 1;
					if($page_num != 1)
						$this->prev = makeLink($self.(!empty($query)? '&'.$query.'=' : '/').($prevpage+1).(validate($sort)? '/'.$sort : ''), '&lt;&lt;', array(), '', ' class="internal3 Left" ','Previous');
					if($total > 1) {
						$begin = $cur_page <= $buffer || $total <= ($buffer * 2) ? 0 : $cur_page - $buffer;
						if($begin == 0) $end = $total <= ($buffer * 2) ? ($total - 1) : ($buffer * 2);
						else $end = ($cur_page + $buffer) > ($total - 1) ? ($total - 1) : ($cur_page + $buffer);
						if($end == ($total - 1) && ($total - ($buffer * 2 + 1)) > -1) $begin = $total - ($buffer * 2 + 1);
						if($begin > 0) $this->pager[] = '<span>...</span>';
						//for($i = $begin; $i <= $end; $i++) $this->pager[] = $cur_page == $i ? '<span class="active">'.($i + 1).'</span>' : makeLink($self.(!empty($query)? '&'.$query.'=' : '/').($i+1).(validate($sort) ? '/'.$sort : ''), ($i + 1) ,         array(), '', ' class=internal3 '  );
						for($i = $begin; $i <= $end; $i++) $this->pager[] = $cur_page == $i ? ' <span class="internal3 Left"> '.($i + 1).'</span> ' : makeLink($self.(!empty($query)? '&'.$query.'=' : '/').($i+1).(validate($sort) ? '/'.$sort : ''), ($i + 1) ,         array(), '', ' class="internal3 Left" '  );
						//if($end < ($total - 1)) $this->pager[] = '<span>...</span>';
						if($end < ($total - 1)) $this->pager[] = '<span class="internal3 Left">...</span>';
						$this->first = $cur_page != 0 ? makeLink($self.(!empty($query)? '&'.$query.'=0' : '/1').(validate($sort)? '/'.$sort : ''), 'First',         array(), '', ' class="internal3 Left" '          ) : '';
						$this->last = $cur_page != ($total - 1) ? makeLink($self.(!empty($query)? '&'.$query.'=' : '/').$total.(validate($sort)? '/'.$sort : ''), 'Last',      array(), '', ' class="internal3 Left" '    ) : '';
					}
					if($page_num != $total) $this->next = makeLink($self.(!empty($query)? '&'.$query.'=' : '/').($nextpage+1).(validate($sort)? '/'.$sort : ''), '&gt;&gt;', array(), '', ' class="internal3 Left" ', 'Next');
				}
			break;
			case 'false': break;
		}
	}
	


    
	
}

?>