<?php   

class RandomizerBlockController extends BlockController { 
	
	var $pobj;
	
	protected $btDescription = "A way to randomly display other blocks.";
	protected $btName = "Randomizer";
	protected $btTable = 'btRandomizer';
	protected $btInterfaceWidth = "350";
	protected $btInterfaceHeight = "300";
	protected $btWrapperClass = 'ccm-ui';
	
	public $blockPool_cID=0;
	public $blockPool_arHandle='';
	public $blockSource = 'page';
	
	public function getJavaScriptStrings() {
		return array(
			'target-page-required' => t('Please choose a target page where the blocks will be pulled from.')		
		);
	}	
	
	
	//returns handles for available areas
	public function getCollectionAreaData($cID){
		$db = Loader::db();
		return $db->getAll('SELECT arID, arHandle FROM Areas WHERE cID='.intval($cID));
	}
	
	public function getCollectionData(){
		$data=array();
		$data['blockSource'] = $this->blockSource;
		$data['blockPool_cID'] = $this->blockPool_cID;
		$data['blockPool_arHandle']=$this->blockPool_arHandle;
		$data['blockPool_arHandle']=$this->scrapbook_arHandle;
		$data['displayCount']=$this->displayCount;
		$data['displayOrder']=$this->displayOrder;
		$data['animate']=$this->animate;
		$data['animationType']=$this->animationType;
		$data['animationTrans']=$this->animationTrans;
		$data['animationDuration']=$this->animationDuration;
		$data['animateDisplayLimit']=$this->animateDisplayLimit;
		return $data;
	}
	
	
	public function validate($args) {
		$error = Loader::helper('validation/error');
		return $error;
	}
	
	
	public function save($data){
		$args['blockSource'] 		= $data['blockSource'];
		$args['blockPool_cID'] 		= intval($data['blockPool_cID']);
		$args['blockPool_arHandle'] = 'Main';
		
		// set the page and area appropriately depending on what kind of place we're getting the data from
		switch($args['blockSource']) {
			case 'stack':
				$args['blockPool_arHandle'] = STACKS_AREA_NAME;
				$args['blockPool_cID'] = $data['stack_cID'];
			break;
			
			case 'scrapbook':
				$args['blockPool_arHandle'] = $data['scrapbook_arHandle'];
				$args['blockPool_cID'] = Loader::helper('concrete/scrapbook')->getGlobalScrapbookPage()->getCollectionID(); 
			break;
			
			case 'page':
			default:
				$args['blockPool_arHandle'] = $data['blockPool_arHandle'];
			break;
		}
		
		$args['displayCount'] = intval($data['displayCount']);
		$args['displayOrder'] = ($data['displayOrder'])?$data['displayOrder']:'cycle';
		$args['animate'] = intval($data['animate']);
		$args['animationType'] = ($data['animationType'])?$data['animationType']:'fade';		
		$args['animationTrans'] = intval($data['animationTrans']);
		$args['animationDuration'] = intval($data['animationDuration']);
		$args['animateDisplayLimit'] = intval($data['animateDisplayLimit']);
		
		parent::save($args);
	}	
	
	public function view() {
		$blockPoolBlocks=array();
		$blockPoolCollection=Page::getById( $this->blockPool_cID );
		if($blockPoolCollection){
			$area = Area::get( $blockPoolCollection, $this->blockPool_arHandle);
			if($area) $blockPoolBlocks = $area->getAreaBlocksArray( $blockPoolCollection ); 
			if($this->displayOrder=='cycle') shuffle($blockPoolBlocks); 					
		}
		$this->set('blockPoolBlocks', $blockPoolBlocks); 
		$this->set('displayCount', $this->displayCount);
	}
	
	public function on_page_view(){
		$this->set('blockPoolBlocks', $blockPoolBlocks); 
		if($this->animate){
			$htmlhelper = Loader::helper('html'); 
			$animationTransMilli=intval($this->animationTrans)*1000;
			$animationDurationMilli=intval($this->animationDuration)*1000; 
			$jsString = '$(function() {$("#ccm-randomizerBlock'.$this->bID.'").cycle({ fx: "'.$this->animationType.'", timeout: '.$animationDurationMilli.', delay:  '.'1'.',speedout:  '.$animationTransMilli.', next: "#randomizerRightArrow'.$this->bID.'", prev: "#randomizerLeftArrow'.$this->bID.', cleartypeNoBg: true" });});';
			$this->addHeaderItem('<script type="text/javascript">'.$jsString.'</script>','CONTROLLER');			
		}		
	}
	
	public function outputAutoHeaderItems(){ 
        $blockPoolBlocks=array();
        $blockPoolCollection=Page::getById( $this->blockPool_cID );
        if($blockPoolCollection) {
            $area = Area::get( $blockPoolCollection, $this->blockPool_arHandle);
            if($area) $blockPoolBlocks = $area->getAreaBlocksArray( $blockPoolCollection );
        } 
        $b = $this->getBlockObject();
		
        $bvt = new BlockViewTemplate($b);
        $headers = $bvt->getTemplateHeaderItems();
		
        //            $headers = array();
        foreach($blockPoolBlocks as $block){
            $controller = $block->getInstance();
            if($controller instanceof BlockController && method_exists($controller, 'on_page_view')) {
            	$controller->on_page_view();
            }
            $bvt = new BlockViewTemplate($block);
            $headers = array_merge($headers,$bvt->getTemplateHeaderItems());
        }
        if (count($headers) > 0){ 
            foreach($headers as $h){ 
                $this->addHeaderItem($h); 
            }
        } 
    }
 	
}
	
?>
