| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.14 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Form\Fields; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Field |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Field options. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected $options = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* Validation message that appears when a field fails validation. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $validation_message = ''; |
| 34 |
|
| 35 |
public function __construct($options = []) |
| 36 |
{ |
| 37 |
// Merge default options with given options |
| 38 |
$this->options = array_merge($this->getDefaultOptions(), $options); |
| 39 |
|
| 40 |
// Then merge new options with each field options |
| 41 |
$this->options = array_merge($this->options, $this->getFieldOptions()); |
| 42 |
} |
| 43 |
|
| 44 |
protected function getFieldOptions() |
| 45 |
{ |
| 46 |
return [ |
| 47 |
'required' => isset($this->options['required']) ? $this->options['required'] : true |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
public function getType() |
| 52 |
{ |
| 53 |
return $this->type; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the default field options. |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
private function getDefaultOptions() |
| 62 |
{ |
| 63 |
return [ |
| 64 |
'type' => $this->type, |
| 65 |
'name' => '', |
| 66 |
'placeholder' => '' |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Validate the field. |
| 72 |
* |
| 73 |
* @param mixed $value |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function validate(&$value = '') |
| 78 |
{ |
| 79 |
if ($this->isRequired() && empty($value)) |
| 80 |
{ |
| 81 |
$this->validation_message = firebox()->_('FB_THIS_IS_A_REQUIRED_FIELD'); |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns whether the field is required. |
| 90 |
* |
| 91 |
* @return bool |
| 92 |
*/ |
| 93 |
public function isRequired() |
| 94 |
{ |
| 95 |
return $this->options['required']; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Prepare value to be displayed to the user as plain text |
| 100 |
* |
| 101 |
* @param mixed $value |
| 102 |
* |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
public function prepareValue($value) |
| 106 |
{ |
| 107 |
if (is_bool($value)) |
| 108 |
{ |
| 109 |
return $value ? '1' : '0'; |
| 110 |
} |
| 111 |
|
| 112 |
if (is_array($value)) |
| 113 |
{ |
| 114 |
return implode(', ', $value); |
| 115 |
} |
| 116 |
|
| 117 |
// Strings and numbers |
| 118 |
return \FireBox\Core\Helpers\Form\Field::escape($value); |
| 119 |
} |
| 120 |
|
| 121 |
public function getValue() |
| 122 |
{ |
| 123 |
return isset($this->options['value']) ? $this->options['value'] : null; |
| 124 |
} |
| 125 |
|
| 126 |
public function prepareValueHTML($value) |
| 127 |
{ |
| 128 |
return $this->prepareValue($value); |
| 129 |
} |
| 130 |
|
| 131 |
public function getValueRaw() |
| 132 |
{ |
| 133 |
return $this->getValue(); |
| 134 |
} |
| 135 |
|
| 136 |
public function getValueHTML() |
| 137 |
{ |
| 138 |
return $this->prepareValueHTML($this->getValueRaw()); |
| 139 |
} |
| 140 |
|
| 141 |
public function getValidationMessage() |
| 142 |
{ |
| 143 |
return $this->validation_message; |
| 144 |
} |
| 145 |
|
| 146 |
public function setValue($value = '') |
| 147 |
{ |
| 148 |
if (!$value) |
| 149 |
{ |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
$this->options['value'] = $value; |
| 154 |
} |
| 155 |
|
| 156 |
public function setOptionValue($key = '', $value = '') |
| 157 |
{ |
| 158 |
if (!$key) |
| 159 |
{ |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
$this->options[$key] = $value; |
| 164 |
} |
| 165 |
|
| 166 |
public function getOptionValue($key = '') |
| 167 |
{ |
| 168 |
if (!$key) |
| 169 |
{ |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
return isset($this->options[$key]) ? $this->options[$key] : ''; |
| 174 |
} |
| 175 |
|
| 176 |
public function addInputCSSClass($css = '') |
| 177 |
{ |
| 178 |
$this->options['input_css_class'][] = $css; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Return the field label. |
| 183 |
* |
| 184 |
* @return string |
| 185 |
*/ |
| 186 |
public function getLabel() |
| 187 |
{ |
| 188 |
return $this->getOptionValue('label'); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Returns the field input. |
| 193 |
* |
| 194 |
* @return void |
| 195 |
*/ |
| 196 |
protected function getInput() {} |
| 197 |
|
| 198 |
public function render() |
| 199 |
{ |
| 200 |
ob_start(); |
| 201 |
$this->getInput(); |
| 202 |
$input = ob_get_contents(); |
| 203 |
ob_end_clean(); |
| 204 |
|
| 205 |
$payload = array_merge( |
| 206 |
$this->options, |
| 207 |
[ |
| 208 |
'input' => $input |
| 209 |
] |
| 210 |
); |
| 211 |
|
| 212 |
return fpframework()->renderer->render('form/fields/tmpl', $payload, true); |
| 213 |
} |
| 214 |
} |