194 lines
7.1 KiB
PHP
194 lines
7.1 KiB
PHP
<?php
|
|
/**
|
|
* core/template/forms.php
|
|
*
|
|
* This class is for managing template forms.
|
|
*
|
|
* @version 3.0
|
|
* @author Joey Kimsey <Joey@thetempusproject.com>
|
|
* @link https://TheTempusProject.com/Core
|
|
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
|
*/
|
|
namespace TheTempusProject\Houdini\Classes;
|
|
|
|
use DateTimeZone;
|
|
|
|
class Forms {
|
|
public static $options = [];
|
|
|
|
/**
|
|
* Sets the specified radio button with $x value to checked.
|
|
*
|
|
* @param {string} [$fieldName] - The name of the radio field.
|
|
* @param {string} [$value] - The field value to be selected.
|
|
*/
|
|
public static function selectRadio( $fieldName, $value ) {
|
|
$selected = 'CHECKED:' . $fieldName . '=' . $value;
|
|
Components::set( $selected, 'checked="checked"' );
|
|
}
|
|
|
|
/**
|
|
* This will add an option to our selected options menu that will
|
|
* automatically be selected when the template is rendered.
|
|
*
|
|
* @param {string} [$value] - The value of the option you want selected.
|
|
*/
|
|
public static function selectOption( $value ) {
|
|
$find = "#\<option (.*?)value=\'" . $value . "\'#s";
|
|
$replace = "<option $1value='" . $value . "' selected";
|
|
self::$options[$find] = $replace;
|
|
}
|
|
|
|
public static function getFormFieldHtml( $fieldname, $fieldTitle, $type, $defaultValue = '', $options = null ) {
|
|
$out = '';
|
|
switch ( $type ) {
|
|
case 'radio':
|
|
case 'bool':
|
|
case 'boolean':
|
|
$fieldHtml = self::getRadioHtml( $fieldname, [ 'true', 'false' ], $defaultValue );
|
|
break;
|
|
case 'select':
|
|
$fieldHtml = self::getSelectHtml( $fieldname, $options, $defaultValue );
|
|
break;
|
|
case 'customSelect':
|
|
if ( empty( $options ) ) {
|
|
$options = '{' . $fieldname . '-options}';
|
|
}
|
|
$fieldHtml = self::getSelectHtml( $fieldname, $options, $defaultValue );
|
|
break;
|
|
case 'block':
|
|
$fieldHtml = self::getTextBlockHtml( $fieldname, $defaultValue );
|
|
break;
|
|
case 'text':
|
|
case 'url':
|
|
$fieldHtml = self::getTextHtml( $fieldname, $defaultValue );
|
|
break;
|
|
case 'checkbox':
|
|
$fieldHtml = self::getCheckboxHtml( $fieldname, $defaultValue );
|
|
break;
|
|
case 'timezone':
|
|
$fieldHtml = self::getTimezoneHtml( $defaultValue );
|
|
break;
|
|
case 'file':
|
|
$fieldHtml = self::getFileHtml( $fieldname );
|
|
break;
|
|
default:
|
|
Debug::error( "unknown field type: $type" );
|
|
break;
|
|
}
|
|
$out .= '<div class="form-group">';
|
|
$out .= '<label for="' . $fieldname . '" class="col-lg-3 control-label">' . $fieldTitle . '</label>';
|
|
$out .= '<div class="col-lg-3">';
|
|
$out .= $fieldHtml;
|
|
$out .= '</div>';
|
|
// @todo need to remove this or make it more generic (can't depend on bedrock anymore)
|
|
if ( 'file' === $type ) {
|
|
$out .= '<div class="col-lg-3 avatar-125" align="center">';
|
|
$out .= '<img alt="User Avatar" src="' . $defaultValue . '" class="img-circle img-responsive">';
|
|
$out .= '</div>';
|
|
}
|
|
$out .= '</div>';
|
|
return $out;
|
|
}
|
|
|
|
public static function getTimezoneHtml( $default ) {
|
|
$tzlist = DateTimeZone::listIdentifiers( DateTimeZone::ALL );
|
|
$out = '<select name="timezone" id="timezone" class="form-control">';
|
|
foreach ( $tzlist as $value ) {
|
|
if ( $default == $value ) {
|
|
$selected = ' selected';
|
|
} else {
|
|
$selected = '';
|
|
}
|
|
$out .= '<option value="' . $value . '"' . $selected . '>' . $value . '</option>';
|
|
}
|
|
$out .= '</select>';
|
|
return $out;
|
|
}
|
|
|
|
public static function getFileHtml( $name ) {
|
|
$out = '<input type="file" class="form-control" name="' . $name . '" id="' . $name . '">';
|
|
return $out;
|
|
}
|
|
|
|
public static function getCheckboxHtml( $name, $checked = false ) {
|
|
if ( false !== $checked ) {
|
|
$checked = ' checked';
|
|
} else {
|
|
$checked = '';
|
|
}
|
|
return '<input type="checkbox" class="form-control" name="' . $name . '" id="' . $name . '" value="true"' . $checked . '>';
|
|
}
|
|
|
|
public static function getTextHtml( $name, $default = '' ) {
|
|
$out = '<input type="text" class="form-control" name="' . $name . '" id="' . $name . '" value="' . $default . '">';
|
|
return $out;
|
|
}
|
|
|
|
public static function getTextBlockHtml( $name, $default = '' ) {
|
|
// @todo add some sort of mechanism for changing the size, maybe options?
|
|
$out = '<textarea class="form-control" name="' . $name . '" maxlength="2000" rows="10" cols="50" id="entry">' . $default . '</textarea>';
|
|
return $out;
|
|
}
|
|
|
|
public static function getSelectHtml( $name, $options, $default = null ) {
|
|
$out = '<select name="' . $name . '" id="' . $name . '" class="form-control">';
|
|
if ( !is_string( $options ) ) {
|
|
$out .= self::getOptionsHtml( $options, $default );
|
|
} else {
|
|
$out .= $options;
|
|
}
|
|
$out .= '</select>';
|
|
return $out;
|
|
}
|
|
|
|
public static function getOptionsHtml( $options = [], $default = null ) {
|
|
$out = '';
|
|
if ( isset( $options[0] ) ) {
|
|
$assocOptions = [];
|
|
foreach ( $options as $key => $value ) {
|
|
$assocOptions[$value] = $value;
|
|
}
|
|
$options = $assocOptions;
|
|
}
|
|
foreach ( $options as $name => $value ) {
|
|
if ( $default == $value ) {
|
|
$selected = ' selected';
|
|
} else {
|
|
$selected = '';
|
|
}
|
|
$out .= '<option value="' . $value . '"' . $selected . '>' . $name . '</option>';
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
public static function getRadioHtml( $name, $options, $default = null ) {
|
|
if ( null === $default ) {
|
|
$option_one = '';
|
|
$option_two = '';
|
|
} elseif ( $options[0] == $default ) {
|
|
$option_one = ' checked="checked"';
|
|
$option_two = '';
|
|
} elseif ( $options[1] == $default ) {
|
|
$option_one = '';
|
|
$option_two = ' checked="checked"';
|
|
} else {
|
|
$option_one = '';
|
|
$option_two = '';
|
|
}
|
|
$out = '<fieldset class="form-group">
|
|
<div class="form-check">
|
|
<label class="form-check-label">
|
|
<input type="radio" class="" name="' . $name . '" id="' . $name . '" value="' . $options[0] . '"' . $option_one . '>
|
|
Yes
|
|
</label>
|
|
<label class="form-check-label">
|
|
<input type="radio" class="" name="' . $name . '" id="' . $name . '" value="' . $options[1] . '"' . $option_two . '>
|
|
No
|
|
</label>
|
|
</div>
|
|
</fieldset>';
|
|
return $out;
|
|
}
|
|
}
|