<?php  
defined('C5_EXECUTE') or die("Access Denied.");
/*
Concrete Bricks game by John Liddiard (aka JohntheFish)
www.jlunderwater.co.uk
This software is licensed under the terms described in the concrete5.org marketplace. 
Please find the add-on there for the latest license copy.
*/
class JlConcreteBricksScoresBlockController extends BlockController {

	protected $btTable = "btJlConcreteBricksScores";
	protected $btInterfaceWidth = "350";
	protected $btInterfaceHeight = "400";

	public function getBlockTypeDescription() {
		return t('Concrete Bricks game for Concrete 5. Show the top scores');
	}

	public function getBlockTypeName() {
        return t('Concrete Bricks Top Scores');
	}

	function save ($data) {
		if (isset($data['dt_format_option'])){
			$data['options'][] = 'dt:'.$data['dt_format_option'];
		}

		if (isset($data['options'])){
			$data['options'] = implode (',',$data['options']);
		} else {
			$data['options'] = '';
		}
		
		
		parent::save($data);
		}
	
	function edit() {
		$this->set("options", $this->get_options ());
		if(!$this->show_best){
			$this->set("show_best", 10);
			}
		if (!isset($options['dt_format_option'])){
			$options['dt_format_option']='Relative 2';
		}

		$this->set("all_tf_options",$this->allDateTimeFormatOptions());
		}

	function add() {
		$default_options=array();
		$default_options['comment']='comment';
		$default_options['GameDate']='GameDate';
		$default_options['time_taken']='time_taken';
		$default_options['completed_lines']='completed_lines';
		$default_options['dt_format_option']='Relative 2';
		$default_options['level_achieved']='level_achieved';
		$default_options['show_borders']='show_borders';
		$default_options['show_heading']='show_heading';
		$default_options['show_col_headings']='show_col_headings';	
		$this->set("options", $default_options);
		$this->set("show_best",10);
		$this->set("all_tf_options",$this->allDateTimeFormatOptions());
		}

	function view() {
		$options = $this->get_options ();
		if (!isset($options['dt_format_option'])){
			$options['dt_format_option']='Relative 2';
		}
		$this->set("options", $options);
		
		$for_user=0;
		if ($options['current_user_only']){
			$for_user = new User();
			if (isset($for_user) && gettype($for_user)=='object' && $for_user->getUserID()>0){
				$uID = $for_user->getUserID();
				$this->set("current_user_name", $for_user->getUserName());
			}
		}
		$this->set("show_best",$this->show_best);
		
		Loader::model('jl_concrete_bricks_game_results', 'jl_concrete_bricks');
		$best_scores = JlConcreteBricksGameResults::getBestScores($this->show_best,$uID);
		$this->set("best_scores", $best_scores);

		$this->set_block_tool('action_ajax_update_table', 'validate_ajax_update_table');

		}

	// unpacks the options from their string
	private function get_options (){
		$options = array();
		foreach (explode(',',$this->options) as $opt_val){
			if (preg_match("/^dt:(.+)/", $opt_val, $m)){
				$options['dt_format_option'] = $m[1];
			} else {
				$options[$opt_val]=$opt_val;
			}
		}
		return $options;	
	}

	// Time formatting, same as in last updated, 
	public function selectedTime($ref_time_string, $current_selection=null){
		try {
			Loader::library('jl/time_presentation_cbgame','jl_concrete_bricks');
			$tp = new timePresentationCbgame();
			return $tp->selectedTime($ref_time_string, $current_selection);
		} catch (Exception $e) {
			return 'Exception: '.$e->getMessage();
		}
	}

	public function allDateTimeFormatOptions(){
		try {
			Loader::library('jl/time_presentation_cbgame','jl_concrete_bricks');
			$tp = new timePresentationCbgame();
			return $tp->hashTimeFormatsAvailable();
		} catch (Exception $e) {
			return 'Exception: '.$e->getMessage();
		}
	}

	/*
	Set up ajax tool with all params needed inc
	validation token, but skip if ajax
	*/
	private function set_block_tool($tool_name, $tool_action){
		// collectionID may not exist if within ajax
		if (Page::getCurrentPage()){
			$tool_helper = Loader::helper('concrete/urls');
			$bt = BlockType::getByHandle($this->btHandle);
			$this->set ($tool_name, $tool_helper->getBlockTypeToolsURL($bt).'/'.$tool_name);
			$vt = Loader::helper('validation/token');
			$this->set ($tool_action, $vt->generate($tool_action));
			$this->set ('blockID', $this->bID);
			$this->set ('collectionID', Page::getCurrentPage() -> getCollectionId());
		} elseif (isset($_POST['collectionid']) && isset($_POST['blockid'])){
			$this->set ('ajax_mode', 'ajax_mode');
		}
	}

}
	
