<?php

class LeftCRView extends Slot {

	var $ModuleID = 1;
	var $SectionID = 2;
	var $type = array(
		'product' => 3,
		'category' => 2,
		'module' => 1,
	);
	var $cri = array();
	var $area = 1;

	function __construct($pquery) {
		if(!empty($pquery['VName']) && $pquery['VName']=='left_cr') $this->area = 1;
		else $this->area = 2;
		parent::__construct($pquery);
	}

	function init() {
		global $REGISTRY;
		if(!empty($REGISTRY['Page']['id'])) {
			if($this->area==1) {
				$this->cri = $this->Db->SelectAny('SELECT cr.* FROM customerreviewitem cri, customerreview cr WHERE cri.moduletype='.$this->type[$REGISTRY['Page']['entity']].' AND cri.object=cr.id AND cri.ownerid='.$REGISTRY['Page']['id'].' order by cri.sort_order');
				$this->Scheme = 'default';
			}
			else {
				$this->cri = $this->Db->SelectAny('SELECT * FROM customerreview');
				shuffle($this->cri);
				$this->Scheme = 'list';
			}
		}
		$this->setSchemeTemplate($this->Scheme);
	}

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

	function fillList() {
		foreach($this->cri as $key => $value) {
			$this->TempFillEls['items'][] = array(
				'name' => $value['name'],
				'desc' => $value['full_description'],
				'anchor' => $value['id'],
				'ext_link' => $value['ext_link'],
				'ext_link_name' => $value['ext_link_name'],
			);
		}
	}

	function fillDefault() {
		global $REGISTRY;
		$rand_keys = array();
		!empty($this->cri) and $rand_keys = array_rand($this->cri, (count($this->cri) > 3? 3 : count($this->cri)));
		!is_array($rand_keys) and $rand_keys = array($rand_keys);
		foreach($this->cri as $key => $value) {
			if(in_array($key, $rand_keys)) {
				$this->TempFillEls['CR'][0]['items'][] = array(
					'name' => $value['name'],
					'desc' => $value['short_description'],
					'id' => $value['id'],
				);
			}
		}
		if($REGISTRY['Page']['entity']=='module' && !empty($REGISTRY['Page']['id']) && $REGISTRY['Page']['id']==$REGISTRY['HomeTarget']) {
			$this->TempFillEls['CR'][0]['cr_top'] = array(array());
		}
	}
}
?>