<commit_before><?php namespace NpmWeb\FormBuilder;

use Log;

class FormBuilder
	extends \NpmWeb\ClientValidationGenerator\Laravel\FormBuilder
{
	
	private $default_col_width = 'large-6';

	public function setModel( $model ) {
		Log::debug(__METHOD__.'()');
		$this->model = $model;
	}

	/**
	 * Display a field read-only
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $options
	 * @return string
	 */
	public function readonly($fieldname, $default = null, $options = [])
	{
		$label = ( isset($options['label']) ? $options['label'] : $this->label($fieldname) );
		$columns_class = ( isset($options['columns_class'] ) ? $options['columns_class'] : $this->default_col_width );

		// get model
		if( $this->model ) {
			$model = $this->model;
		} else {
			throw new \Exception('no model specified');
		}

		// format data
		$value = $model->$fieldname;
		$format = array_key_exists('format',$options)
			? $options['format']
			: null;
		switch($format) {
			case 'url':
				$value = '<a href="'.esc_attr($value).'">'.esc_body($value).'</a>';
				break;
			case 'email': 
				$value = '<a href="mailto:'.esc_attr($value).'">'.esc_body($value).'</a>';
				break;
			// default: do nothing
		}

		$config = $this->_processOptions($name, $options);
		return $this->_outputHelper( $fieldname, $config, $value );
	}

	/**
	 * Create a text input field.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $options
	 * @return string
	 */
	public function text($name, $default = null, $options = [])
	{
		//Log::debug(__METHOD__.'()');

		$config = $this->_processOptions($name, $options);
		return $this->_outputHelper( $name, $config, parent::text($name, $default, $config->extras) );
	}

	/**
	 * Create a password input field.
	 *
	 * @param  string  $name
	 * @param  array   $options
	 * @return string
	 */
	public function password($name, $options = array())
	{
		//Log::debug(__METHOD__.'()');
		$config = $this->_processOptions($name, $options);
		return $this->_outputHelper( $name, $config, 
			parent::password($name, $config->extras) );
	}

	/**
	 * Create an e-mail input field.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $options
	 * @return string
	 */
	public function email($name, $value = null, $options = array())
	{
		//Log::debug(__METHOD__.'()');
		$config = $this->_processOptions($name, $options);
		return $this->_outputHelper( $name, $config, 
			parent::email($name, $value, $config->extras) );
	}

	/**
	 * Create a telephone input field.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $options
	 * @return string
	 */
	public function tel($name, $value = null, $options = array())
	{
		//Log::debug(__METHOD__.'()');
		$config = $this->_processOptions($name, $options);
		return $this->_outputHelper( $name, $config, 
			parent::input('tel', $name, $value, $config->extras) );
	}


	/**
	 * Create a select box field.
	 *
	 * @param  string  $name
	 * @param  array   $list
	 * @param  string  $selected
	 * @param  array   $options
	 * @return string
	 */
	public function select($name, $list = array(), $selected = null, $options = array())
	{
		$config = $this->_processOptions($name,$options);

		return $this->_outputHelper( $name, $config, parent::select($name, $list, $selected, $config->extras) );
	}

	/**
	 * Create a radio button input field.
	 *
	 * @param  string  $name
	 * @param  mixed   $value
	 * @param  bool    $checked
	 * @param  array   $options
	 * @return string
	 */
	public function radio($name, $value = null, $checked = null, $options = array())
	{
		//Log::debug(__METHOD__.'()');

		$config = $this->_processOptions($name,$options);
		//$radioHtml = parent::radio($name, $value, $checked, $config->extras);
		$id = (array_key_exists('id', $config->extras) ? $config->extras['id'] : esc_attr($name.'_'.$value) );
		ob_start(); 
		?>
		<div class="<?php echo $config->columns_class ?> columns">
			<label class="radio">
				<input type="radio" name="<?php echo $name ?>" value="<?php echo $value ?>" id="<?php echo $config->extras['id'] ?>"<?php if ($checked) echo ' checked="checked"'; ?>><span class="radio"><?php echo $config->label; ?></span>
			</label>
		</div>				

		<?php
		return ob_get_clean();

	}



	// go through the options and pull out the pieces we're interested in
	protected function _processOptions( $name, $options )
	{
		$config = new \stdClass();
		$config->label = ( isset($options['label']) ? 
							esc_body($options['label']) : 
							( isset($options['labelHtml']) ? $options['labelHtml'] : $this->label($name) )
						);
		$config->columns_class = ( isset($options['columns_class'] ) ? $options['columns_class'] : $this->default_col_width );
		$config->extras = array_key_exists('extras',$options)
			? $options['extras']
			: [];

		if (!array_key_exists('id', $config->extras) ) $config->extras['id'] = esc_attr( $name );
		if (!array_key_exists('placeholder',$config->extras) ) $config->extras['placeholder'] = $config->label; // configurable?<commit_msg>fixes a bug where, when label not set, html code is shown as the placeholder<commit_after><?php namespace NpmWeb\FormBuilder;

use Log;

class FormBuilder
	extends \NpmWeb\ClientValidationGenerator\Laravel\FormBuilder
{
	
	private $default_col_width = 'large-6';

	public function setModel( $model ) {
		Log::debug(__METHOD__.'()');
		$this->model = $model;
	}

	/**
	 * Display a field read-only
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $options
	 * @return string
	 */
	public function readonly($fieldname, $default = null, $options = [])
	{
		$label = ( isset($options['label']) ? $options['label'] : $this->label($fieldname) );
		$columns_class = ( isset($options['columns_class'] ) ? $options['columns_class'] : $this->default_col_width );

		// get model
		if( $this->model ) {
			$model = $this->model;
		} else {
			throw new \Exception('no model specified');
		}

		// format data
		$value = $model->$fieldname;
		$format = array_key_exists('format',$options)
			? $options['format']
			: null;
		switch($format) {
			case 'url':
				$value = '<a href="'.esc_attr($value).'">'.esc_body($value).'</a>';
				break;
			case 'email': 
				$value = '<a href="mailto:'.esc_attr($value).'">'.esc_body($value).'</a>';
				break;
			// default: do nothing
		}

		$config = $this->_processOptions($fieldname, $options);
		return $this->_outputHelper( $fieldname, $config, $value );
	}

	/**
	 * Create a text input field.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $options
	 * @return string
	 */
	public function text($name, $default = null, $options = [])
	{
		//Log::debug(__METHOD__.'()');

		$config = $this->_processOptions($name, $options);
		return $this->_outputHelper( $name, $config, parent::text($name, $default, $config->extras) );
	}

	/**
	 * Create a password input field.
	 *
	 * @param  string  $name
	 * @param  array   $options
	 * @return string
	 */
	public function password($name, $options = array())
	{
		//Log::debug(__METHOD__.'()');
		$config = $this->_processOptions($name, $options);
		return $this->_outputHelper( $name, $config, 
			parent::password($name, $config->extras) );
	}

	/**
	 * Create an e-mail input field.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $options
	 * @return string
	 */
	public function email($name, $value = null, $options = array())
	{
		//Log::debug(__METHOD__.'()');
		$config = $this->_processOptions($name, $options);
		return $this->_outputHelper( $name, $config, 
			parent::email($name, $value, $config->extras) );
	}

	/**
	 * Create a telephone input field.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $options
	 * @return string
	 */
	public function tel($name, $value = null, $options = array())
	{
		//Log::debug(__METHOD__.'()');
		$config = $this->_processOptions($name, $options);
		return $this->_outputHelper( $name, $config, 
			parent::input('tel', $name, $value, $config->extras) );
	}


	/**
	 * Create a select box field.
	 *
	 * @param  string  $name
	 * @param  array   $list
	 * @param  string  $selected
	 * @param  array   $options
	 * @return string
	 */
	public function select($name, $list = array(), $selected = null, $options = array())
	{
		$config = $this->_processOptions($name,$options);

		return $this->_outputHelper( $name, $config, parent::select($name, $list, $selected, $config->extras) );
	}

	/**
	 * Create a radio button input field.
	 *
	 * @param  string  $name
	 * @param  mixed   $value
	 * @param  bool    $checked
	 * @param  array   $options
	 * @return string
	 */
	public function radio($name, $value = null, $checked = null, $options = array())
	{
		//Log::debug(__METHOD__.'()');

		$config = $this->_processOptions($name,$options);
		//$radioHtml = parent::radio($name, $value, $checked, $config->extras);
		$id = (array_key_exists('id', $config->extras) ? $config->extras['id'] : esc_attr($name.'_'.$value) );
		ob_start(); 
		?>
		<div class="<?php echo $config->columns_class ?> columns">
			<label class="radio">
				<input type="radio" name="<?php echo $name ?>" value="<?php echo $value ?>" id="<?php echo $config->extras['id'] ?>"<?php if ($checked) echo ' checked="checked"'; ?>><span class="radio"><?php echo $config->label; ?></span>
			</label>
		</div>				

		<?php
		return ob_get_clean();

	}



	// go through the options and pull out the pieces we're interested in
	protected function _processOptions( $name, $options )
	{
		$config = new \stdClass();
		$config->label = ( isset($options['label']) ? 
							esc_body($options['label']) : 
							( isset($options['labelHtml']) ? $options['labelHtml'] : $this->label($name) )
						);
		$config->columns_class = ( isset($options['columns_class'] ) ? $options['columns_class'] : $this->default_col_width );
		$config->extras = array_key_exists('extras',$options)
			? $options['extras']
			: [];

		if (!array_key_exists('id', $config->extras) ) $config->extras['id'] = esc_attr( $name );
		if (!array_key_exists('placeholder',$config->extras) ) $config->extras['placeholder'] = self::formatLabel($name,null); // configurable?
{'fraction_non_alphanumeric': 0.1950622122073087, 'fraction_numerical': 0.00019594396002743216, 'mean_word_length': 3.765639589169001, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 118, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2218736', 'n_tokens_mistral': 3701, 'n_tokens_neox': 3352, 'n_words': 773}