<?php namespace ProcessWire;

/**
 * ProcessWire Radios Inputfield
 * 
 * ProcessWire 3.x, Copyright 2016 by Ryan Cramer
 * https://processwire.com
 * 
 * @property int $optionColumns Number of columns to display radios in, or 0 for vertical stacked, or 1 for inline (default=0)
 * 
 */

class InputfieldRadios extends InputfieldSelect {

	public static function getModuleInfo() {
		return array(
			'title' => __('Radio Buttons', __FILE__), // Module Title
			'summary' => __('Radio buttons for selection of a single item', __FILE__), // Module Summary
			'version' => 105,
			'permanent' => true, 
			);
	}

	public function init() {
		$this->set('optionColumns', 0); 
		parent::init();
	}

	public function ___render() {	

		$defaults = array(
			'wbr' => true, 
			'noSelectLabels' => true,
		);
		$settings = $this->wire('config')->get('InputfieldRadios');
		$settings = is_array($settings) ? array_merge($defaults, $settings) : $defaults;
		
		$this->checkDefaultValue();
		$inline = false;
		$columns = (int) $this->optionColumns;
		if($columns === 1) $inline = true; 
		$options = $this->getOptions();
		$liAttr = '';

		if($columns) {
			if(count($options) >= $columns && !$inline) {
				$liWidth = round(100 / $columns)-1;  // 1% padding-right added from stylesheet
				$liAttr = " style='width: {$liWidth}%;'";
				$ulClass = 'InputfieldRadiosColumns';
			} else {
				// don't bother setting a width, we will let them float where they want instead
				$ulClass = 'InputfieldRadiosFloated';
			}
			$out = "<ul class='$ulClass pw-clearfix ui-helper-clearfix'>";

		} else {
			$out = "<ul class='InputfieldRadiosStacked'>"; 
		}

		foreach($options as $key => $value) {
			$checked = '';

			$id = $this->id . "_" . $this->wire('sanitizer')->name($key); 

			$attrs = $this->getOptionAttributes($key);
			if($this->isOptionSelected($key)) $checked = " checked='checked'";
			$disabled = empty($attrs['disabled']) ? "" : " disabled='disabled'";
			unset($attrs['selected'], $attrs['checked'], $attrs['disabled']); 
			$textClass = $settings['noSelectLabels'] ? 'pw-no-select' : '';
			if($disabled) $textClass .= ' ui-state-disabled';
			$attrs = $this->getOptionAttributesString($attrs);
			if($attrs) $attrs = ' ' . $attrs;
			$label = $settings['wbr'] ? str_replace(' ', ' !wbr!', $value) : $value;
			$label = $this->entityEncode($label, Inputfield::textFormatBasic);
			if($settings['wbr']) $label = str_replace('!wbr!', '<wbr>', $label);
			$class = trim($this->wire('sanitizer')->entities($this->attr('class')));

			$out .= 
				"<li$liAttr><label$attrs>" . 
				"<input$checked$disabled " . 
				"type='radio' " . 
				"name='{$this->name}' " . 
				"id='$id' " .
				"class='$class' " . 
				"value='" . htmlspecialchars($key, ENT_QUOTES) . "' />" . 
				"<span class='$textClass'>$label</span>" . 
				"</label></li>";
		}

		$out .=	"</ul>";

		return $out; 
	}

	public function set($key, $value) {
		if($key == 'optionColumns') {
			$value = (int) $value;
			if($value < 0) $value = 0;
			if($value > 10) $value = 10;
		}
		return parent::set($key, $value); 
	}

	public function ___getConfigInputfields() {
		$inputfields = parent::___getConfigInputfields(); 
		/** @var InputfieldInteger $f */
		$f = $this->wire('modules')->get('InputfieldInteger');
		$f->label = $this->_('Columns of Radio Buttons');
		$f->description = $this->_('If you want the radio buttons to display in columns (rather than stacked), enter the number of columns you want to use (up to 10). To display buttons side-by-side (inline) enter 1.'); 
		$f->notes = $this->_('If no number is specified here, then each radio button will display on its own line.'); 
		$f->attr('name', 'optionColumns'); 
		$f->attr('value', (int) $this->optionColumns); 
		$inputfields->add($f);	
		return $inputfields; 
	}

}
