<?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.
 */


/**
 * Description of BootstrapSelect
 *
 * @author Maggie Magpie
 * @link http://www.oldauntie.org
 * @author Maggie Marple <oldauntie@oldauntie.org>
 * @version 1.0
 */
class BootstrapSelect {
	
	public $name;
	public $id;
	public $rows;
	public $required = false;
	public $label = '';
	public $disabled = false;
	public $selected_value = null;
	public $attributes = '';
			
	protected $output;
	
	public function __construct( $name, $rows = array(), $label = '', $required = false ) {
		$this->name		= $name;
		$this->id		= $name;
		$this->label	= $label;
		$this->rows		= $rows;
		$this->required = $required;
	}
	
	public function setID( $id ){
		$this->id = $id;
	}
	
	public function setClass( $class ){
		$this->class = $class;
	}
	
	public function addClass( $class ){
		$this->class .= $class;
	}
	
	public function addRows( $rows ) {
		$this->rows = $rows;
	}
	
	public function output(){
		$required 	= '';
		$disabled	= '';
		
		if( $this->required == true ){
			$required = ' required'; 
		}
		
		if( $this->disabled == true ){
			$disabled = ' disabled';
		}
		
		$this->output = '<div class="form-group has-feedback">';
		if( empty( $this->label ) == false || is_null( $this->label ) == false ){
			$this->output .= '<label for="'.$this->id.'" class="control-label">'.$this->label.'</label>';
		}
		
		$this->output .= '<select name ="'.$this->name.'" class="form-control" id="' . $this->id . '"' . $required . $disabled . '>' ."\n";
		
		
		foreach($this->rows as $row){
			$values = array_values($row);
			
			$selected = '';
			if($this->selected_value == $values[0]){
				$selected = ' selected';
			}
			
			$this->output .= '<option value="' . $values[0] . '"' . $selected  .'>' . $values[1] . '</option>'."\n";
		}
					
		$this->output .= '</select>'."\n";
		$this->output .= '</div>'."\n";
		
		return $this->output;
	}		
}
