<?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 JlConcreteBricksGameBlockController extends BlockController {

	protected $btTable = "btJlConcreteBricksGame";
	protected $btInterfaceWidth = "350";
	protected $btInterfaceHeight = "400";

	public function getBlockTypeDescription() {
		return t('Concrete Bricks game for Concrete 5, move the falling bricks into place.');
	}

	public function getBlockTypeName() {
        return t('Concrete Bricks Game');
	}

	/*
	We need to laod this because it is not automatically made available in the view
	*/
	public function on_page_view() {
		$html = Loader::helper('html');
		$this->addHeaderItem($html->css('jquery.ui.css'));
		$this->addHeaderItem($html->css('ccm.dialog.css'));
		$this->addHeaderItem($html->javascript('jquery.ui.js'));
		$this->addHeaderItem($html->javascript('ccm.dialog.js'));
		$this->addHeaderItem($html->javascript('jquery.form.js'));
	}

	
	function save ($data) {
		if (isset($data['iconset'])){
			$data['options'][] = 'iconset:'.$data['iconset'];
		}

		if (isset($data['options'])){
			$data['options'] = implode (',',$data['options']);
		} else {
			$data['options'] = '';
		}
		
		parent::save($data);
		}
	
	function edit() {
		$this->set("options", $this->get_options ());
		if(!isset($this->game_rows)){
			$this->set("game_rows", 18);
			}
		if(!isset($this->game_cols)){
			$this->set("game_cols", 10);
			}
		}

	function add() {
		$default_options=array();

		$default_options=array();
		$default_options['show_heading']='show_heading';
		$default_options['show_next']='show_next';	
		$default_options['iconset']='C5';	

		$this->set("options", $default_options);
		$this->set("game_rows", 18);
		$this->set("game_cols", 10);
		}

	function view() {
		$this->set("options", $this->get_options ());	
		if(!isset($this->game_rows)){
			$this->set("game_rows", 18);
		}
		if(!isset($this->game_cols)){
			$this->set("game_cols", 10);
		}
		$this->set("for_user", new User());
		$this->set_block_tool('action_ajax_save_score', 'validate_ajax_save_score');
		}


	// here is where a score is saved
	function vaildated_ajax_save_score(){

		if (isset($_POST[user]) && $_POST[user]>0){
			$uID = (int)$_POST[user];
			$name = '';
		}elseif(isset($_POST[name]) && strlen($_POST[name])>3){
			$uID = 0;
			$name = $this->cleanup_text($_POST[name]);
		}
		if (empty($uID) && empty($name)){
			return "Failed to give uID [$uID] or name [$name]";
		}
		$comment = $this->cleanup_text($_POST['comment']);
		Loader::model('jl_concrete_bricks_game_results', 'jl_concrete_bricks');
		$rslt =	JlConcreteBricksGameResults::addScore(	
												$uID, $name, $comment, 
												(int)$_POST[score], (int)$_POST[time_taken], 
												(int)$_POST[completed_lines], (int)$_POST[level_achieved]);
		return ('Score Saved');
	}

	// unpacks the options from their string
	private function get_options (){
		$options = array();
		foreach (explode(',',$this->options) as $opt_val){
			if (preg_match("/^iconset\:(.+)/", $opt_val, $m)){
				$options['iconset'] = $m[1];
			} else {
				$options[$opt_val]=$opt_val;
			}
		}
		return $options;	
	}

	/*
	Set up ajax tool with all params needed inc
	validation token
	*/
	private function set_block_tool($tool_name, $tool_action){
		$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());
	}

	public function iconsets(){
		return array ('C5'=>'C5','Fish'=>'Fish', 'None'=>'None');
	
	}
	
	private function cleanup_text($text_in){	
		// just in case any &lt;&gt; simulate html tags and get turned back on completion of the loop
		$text_in = strip_tags($text_in);
		// clean out any non printing chars
		$text_in = preg_replace("/[^ \x20-\x7E]+/",' ',$text_in);
		// clean out funny quotes
		$text_in = preg_replace("/\`+/","'",$text_in);
		// reduce multiple white space chars
		$text_in = preg_replace("/[\s]+/",' ',$text_in);
		// handle situation of whitespace before period
		$text_in = preg_replace("/ \./",'.',$text_in);
		// reduce multiple punctuation
		$all_punctuation = "!-\/:-@\[-`{-~";
		$text_in = preg_replace("/([$all_punctuation]{1})([$all_punctuation]+)/","$1",$text_in);
		// a final clean of any multi white space
		$text_in = trim(preg_replace("/[\s]+/",' ',$text_in));
		// trim to 250 or less
		$text_in =  substr ($text_in,0,250);
		return trim($text_in);
	}
}
	
?>