<?php
	/**
	 * Bootstrap framework helper class.
	 * Helper for boostrap framework. Creates various HTML/Javascript elements ready for page output
	 * 
	 * @link http://www.oldauntie.org
	 * @author Maggie Marple <oldauntie@oldauntie.org>
	 * @version 1.0
	 */
 
class Bootstrap {
	protected $output;
	protected $js;
	
	const ALERT_DANGER		= 'danger';
	const ALERT_INFO		= 'info';
	const ALERT_SUCCESS		= 'success';
	const ALERT_WARNING		= 'warning';
	
	const BUTTON_CLASS_DEFAULT		= 'btn btn-default';
	const BUTTON_CLASS_PRIMARY		= 'btn btn-primary';
	
	
	function __construct() {

	}
	
	
	function message($message, $type = 'danger', $timeout = 0){
		$output = '<div class="alert alert-'.$type.' alert-dismissible fade in">
		<button type="button" class="close" data-dismiss="alert" aria-label="Close">
			<span aria-hidden="true">&times;</span>
		</button>
		<strong>'.$message.'</strong>
		</div>';
		
		if( $timeout > 0 ){
			$output .= '<script>';
			$output .= 'window.setTimeout(function() {
					$(".alert").fadeTo(500, 0).slideUp(500, function(){
						$(this).remove(); 
					});
					}, ' . $timeout . ' );';
			$output .= '</script>';
		}
		return $output;
		
	}
	
	function redirect($target, $delaytime=0){
		$output = 	"<script>";
		$output .=  "window.setTimeout(function() {
						window.location.href = '" . $target . "';
					}, " . $delaytime . ");";
		$output .=  "</script>";
					
		return $output;
	}
	
	function dialog($message){
		$output = 	"<script>";
		$output .= "waitingDialog.show('" . $message . "', {dialogSize: 'sm', progressType: 'warning'});";
		$output .=  "</script>";
					
		return $output;
	}
	
	function confirm($linkToId, $message, $goto){
		$output = 	"<script>";
		$output .=	"$( '#" . $linkToId . "' ).click(function() {
								bootbox.confirm('" . $message . "', function(result) {
									if(result){
										window.location.href = '" . $goto . "';
									}
								});
							});";
		$output .=  "</script>";
					
		return $output;
	}
	
	function modal ( $title, $body, $buttonArray = array(), $modalId = "myModal" ) {
		$output = '<!-- Modal -->
					<div class="modal fade" id="' . $modalId . '" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
					  <div class="modal-dialog" role="document">
						<div class="modal-content">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
								<h4 class="modal-title" id="' . $modalId . '-title">' . $title . '</h4>
							</div>
							<div class="modal-body" id="' . $modalId . '-body" >
							' . $body . '
							</div>
							<div class="modal-footer">';
		
		if ( is_array ( $buttonArray ) && empty ( $buttonArray ) == false ){
			foreach ( $buttonArray as $button ) {
					$output .= $button . "\n";
			}
		}
						  /*
							<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
							<button type="button" class="btn btn-primary">Save changes</button>
						  */
						  
		$output .= '		</div>
						</div>
					  </div>
					</div>';
		return $output;
	}
	
	

	
	public static function openForm( $name, $action, $method = 'POST', $id = '', $attributes = '' ){
		if(empty($id) == true){
			$id = $name;
		}
		
		if( empty( $attributes ) == false ){
			$attributes = ' '.$attributes;
		}
		
		$output = '<form method="' . $method . '" name="'.$name.'" id="'.$id.'" action="'.$action.'"' . $attributes . '>'."\n";
		
		return $output;
	}
	
	
	public static function closeForm(){
		$output = "\n" . '</form>' . "\n";
		return $output;
	}
	
	/**
	 * return a select html element (combobox) compliant with Boostrap framework
	 * 
	 * @param string $name
	 * @param array $rows
	 * @param mixed $selected_value
	 * @param boolean $required
	 * @param string $label
	 * @param string $id
	 * @param boolean $disabled
	 * @return string
	 */
	static function addSelect( $name, $rows, $selected_value = '', $required = false, $label = '', $id = '', $disabled = false ){
		$bso = new BootstrapSelect( $name, $rows, $label, $required );
		$bso->selected_value = $selected_value;
		$bso->disabled = $disabled;
		
		if( empty( $id ) == false ){
			$bso->setID( $id );
		}
		
		$bso->addRows( $rows );
		
		return $bso->output();
		unset( $bso );
	}
	
	
	function addTextArea( $name, $value, $isRequired = false, $label='', $placeholder = '', $helptext='', $id = '', $rows = 3 ){
		
		$required 	= '';
		
		if($isRequired == true){
			$required = ' required'; 
		}else{
			$required = '';
		}
		
		
		if(empty($id) == true){
			$id = 'textarea-'.$name;
		}
		
		$output = '<div class="form-group has-feedback">' . 
						'<label for="'.$id.'" class="control-label">'.$label.'</label>
						<textarea name ="'.$name.'"
								title ="'.$helptext.'"
								class="form-control" 
								id="'.$id.'"
								rows="'.$rows.'"
								placeholder="'.$placeholder.'"'
								.$required
								.'>' . $value . '</textarea>
					</div>' . "\n";
					
		return $output;
	}
	
	
	/**
	 * 
	 * @param string $name
	 * @param string $value
	 * @param boolean $required
	 * @param string $label
	 * @param string $placeholder
	 * @param string $helptext
	 * @param string $id Element ID
	 * @param boolean $disabled
	 * @return string
	 */
	function addDatePicker( $name, 
						$value, 
						$required = false, 
						$label='', 
						$placeholder='', 
						$helptext='', 
						$id = '', 
						$disabled = false
						){
						
		$bso = new DatePicker( $name, $value, $label );
		$bso->required = $required;
		$bso->placeholder = $placeholder;
		$bso->helptext = $helptext;
		$bso->disabled = $disabled;
		
		// set element Id for input
		if( empty( $id ) == false || is_null( $id ) == false ){
			$bso->setID( $id );
		}
		
		return $bso->output();
	}
	
	
	
	/**
	 * 
	 * @param string $type
	 * @param string $name
	 * @param mixed $value
	 * @param boolean $required
	 * @param string $label
	 * @param string $placeholder
	 * @param string $helptext
	 * @param string $id element ID
	 * @param boolean $disabled
	 * @return string
	 */
	function addInput($type = BootstrapInput::TYPE_TEXT, 
						$name, 
						$value, 
						$required = false, 
						$label='', 
						$placeholder='', 
						$helptext='', 
						$id = '', 
						$disabled = false
					){
		$bso = new BootstrapInput( $name, $value, $label, $required );
		$bso->setType( $type );
		$bso->placeholder	= $placeholder;
		$bso->helptext		= $helptext;
		$bso->disabled		= $disabled;
		// set element Id for input
		if( empty( $id ) == false ){
			$bso->setID( $id );
		}
		
		return $bso->output();
	}


	/**
	 * Quick add a Bootstrap control submit to a page
	 * 
	 * @param string $name
	 * @param string $label
	 * @param mixed $value
	 * @param string $id
	 * @param string $class
	 * @return type
	 */
	function addSubmit( $name, $label, $value='', $id='', $class = self::BUTTON_CLASS_PRIMARY ){
		return self::addButton( $name, $label, $value, '', $id, array(), $class, 'submit' );
	}
	
	/**
	 * Quick add a Bootstrap control button to a page
	 * @param string $name
	 * @param string $label
	 * @param mixed $value
	 * @param string $onclick
	 * @param string $id
	 * @param string $attributes further control attributes, properties
	 * @param string $class
	 * @param string $type control type
	 * @return string representing bootstrap element
	 */
	function addButton( $name, 
					$label, 
					$value='', 
					$onclick='', 
					$id = '', 
					$attributes = array(), 
					$class = self::BUTTON_CLASS_PRIMARY, 
					$type = BootstrapButton::TYPE_BUTTON
					){
		
		$bsb = new BootstrapButton( $name,  $label, $value );
		
		
		if( empty( $onclick ) == false ){
			$bsb->onclick = $onclick; 
		}

		if( empty( $id ) == false ){
			$bsb->setID( $id ); 
		}
		
		$bsb->setType( $type );
		$bsb->attributes = $attributes;
		$bsb->setClass( $class );
		
		return $bsb->output();
	}
	
	
	/*
	 * @todo: delete me
	 * 
	 * @deprecates
	 */
	/*
	function addHelpButton ( $helptext, $title="Instructions" ) {
		$output = '<button type="button" class="btn btn-default" data-toggle="popover" trigger="focus" title="' . $title . '" data-content="'. $helptext . '">
  						<span class="glyphicon glyphicon-education" aria-hidden="true"></span>
					</button>';
		
		return $output;
		
	}
	 */
	
	
	function addPopOver( $id, $toggleText, $content, $popover_header = "" ){
		$output = '<a href="#" id="' . $id . '" data-trigger="hover" data-html="true" data-toggle="popover" title="' . $popover_header . '" data-content="' . $content . '">' . $toggleText . '</a>';
		return $output;
	}
	

}
