<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

// namespace Bootstrap;

/**
 * Description of BootstrapInput
 *
 * @author Maggie Magpie
 */
class BootstrapInput {
	const TYPE_EMAIL			= 'email';
	const TYPE_HIDDEN		= 'hidden';
	const TYPE_PASSWORD		= 'password';
	const TYPE_TEXT			= 'text';
	
	private $type = self::TYPE_TEXT;
	
	public $name;
	public $id;
	public $value;
	public $class = 'form-control';
	public $required = false;
	public $readonly = false;
	public $label = '';
	public $placeholder = '';
	public $helptext = '';
	public $disabled = false;
	public $attributes = '';
	public $prepend_control = '';
	public $append_control = '';
			
	protected $output;
	
	public function __construct( $name, $value='', $label = '', $required = false ) {
		$this->name		= $name;
		$this->id		= $name;
		$this->label	= $label;
		$this->value	= $value;
		$this->required = $required;
	}
	
	public function setID( $id ){
		$this->id = $id;
	}
	
	public function setType( $type ){
		$this->type = $type;
	}
	
	public function setClass( $class ){
		$this->class = $class;
	}
	
	public function addClass( $class ){
		$this->class .= $class;
	}
	
	public function setValue( $value ) {
		$this->value = $value;
	}
	
	function setAttribute( $attribute ){
		$this->attributes = ' ' . $attribute;
	}

	function addAttribute( $attribute ){
		$this->attributes .= ' ' . $attribute;
	}

	
	public function output(){
		$required 	= '';
		$disabled	= '';
		$readonly	= '';
		$this->output = '';
		
		if( $this->required == true ){
			$required = ' required'; 
		}
		
		if( $this->readonly == true ){
			$readonly = ' readonly'; 
		}
		
		if( $this->disabled == true ){
			$disabled = ' disabled';
		}
		
		// $this->output .= '<div class="form-group has-feedback">';
		
		if( empty( $this->prepend_control ) == false ){
			$this->output .= '<span class="input-group-btn">' . $this->prepend_control . '</span>';
		}
	
		if( empty( $this->label ) == false ){
			$this->output .='<label for="'.$this->id.'" class="control-label">'.$this->label.'</label>';
		}
			
		$this->output .='<input type="'.$this->type.'"
							name ="'.$this->name.'"
							title ="'.$this->helptext.'"
							class="'.$this->class.'" 
							id="'.$this->id.'"
							placeholder="'.$this->placeholder.'"
							value="'.$this->value.'"'
							.$required
							.$disabled
							.$readonly
							. ' ' . $this->attributes
							.'>';
		if( empty( $this->append_control ) == false ){
			$this->output .='<span class="input-group-btn">' . $this->append_control . '</span>';
		}
		
		// $this->output .= '</div>' . "\n";
		
		return $this->output;
	}
	
	
	public static function create( $name, $value='', $label = '', $required = false, $placeholder = '' ){
		$bso = new BootstrapInput( $name, $value, $label, $required );
		$bso->placeholder = $placeholder;
		return $bso->output();
	}
}
