<?php
namespace Bitweaver\Plugins;

use Smarty\BlockHandler\BlockHandlerInterface;
use Smarty\Template;

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty {forminput} block plugin
 *
 * Type:     block
 * Name:     forminput
 */
class BlockFormInput implements BlockHandlerInterface {

	public function handle( $params, $content, Template $template, &$repeat): string {
		global $gSmartyFormHorizontal;

		// defaults
		$attr = "";
		$class = '';
		if( $gSmartyFormHorizontal ) {
			$class = 'col-sm-8';
		}

		if( !empty( $params['class'] ) ){
			$class .= ' '.trim( $params['class'] );
			if( $gSmartyFormHorizontal && (strpos( $params['class'], 'submit' ) !== FALSE || strpos( $params['class'], 'offset' ) !== FALSE) ) {
				$class .= ' col-sm-offset-4';
			}
		}

		$labelStart = '';
		$labelEnd = '';

		if( !empty( $params['label'] ) ){
			if( $gSmartyFormHorizontal ) {
				$class .= ' col-sm-offset-4';
			}
			$class .= ' '.trim( $params['label'] );
			$labelStart = '<label>';
			$labelEnd = '</label>';
		}

		if( !empty( $params['id'] ) ){
			$attr .= 'id="'.trim( $params['id'] ).'" ';
		}

		if( !empty( $params['style'] ) ){
			$attr .= 'style="'.trim( $params['style'] ).'" ';
		}

		if( $content ) {
			return '<div class="'.$class.'" '.$attr.' >'.$labelStart.$content.$labelEnd.'</div>';
		}
			return '';

	}

	public function isCacheable(): bool {
		return true;
	}
}