| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The smallest unit of an option, the field it self. |
| 5 |
*/ |
| 6 |
abstract class VP_Control_Field implements iFactory |
| 7 |
{ |
| 8 |
|
| 9 |
/** |
| 10 |
* Unique name of the field |
| 11 |
* @var String |
| 12 |
*/ |
| 13 |
protected $_name; |
| 14 |
|
| 15 |
/** |
| 16 |
* Label for the field |
| 17 |
* @var String |
| 18 |
*/ |
| 19 |
protected $_label; |
| 20 |
|
| 21 |
/** |
| 22 |
* Description on what the field about |
| 23 |
* @var String |
| 24 |
*/ |
| 25 |
protected $_description; |
| 26 |
|
| 27 |
/** |
| 28 |
* Validation pattern string |
| 29 |
* @var String |
| 30 |
*/ |
| 31 |
protected $_validation; |
| 32 |
|
| 33 |
/** |
| 34 |
* dependency pattern string |
| 35 |
* @var String |
| 36 |
*/ |
| 37 |
protected $_dependency; |
| 38 |
|
| 39 |
/** |
| 40 |
* binding patter string |
| 41 |
* @var String |
| 42 |
*/ |
| 43 |
protected $_binding; |
| 44 |
|
| 45 |
/** |
| 46 |
* Default value for the field |
| 47 |
* @var String|Array |
| 48 |
*/ |
| 49 |
protected $_default; |
| 50 |
|
| 51 |
/** |
| 52 |
* Maximum height of the field |
| 53 |
* @var Integer |
| 54 |
*/ |
| 55 |
protected $_field_max_height; |
| 56 |
|
| 57 |
/** |
| 58 |
* Value for the field |
| 59 |
* @var String|Array |
| 60 |
*/ |
| 61 |
protected $_value; |
| 62 |
|
| 63 |
/** |
| 64 |
* Data to be rendered |
| 65 |
* @var Array |
| 66 |
*/ |
| 67 |
protected $_data; |
| 68 |
|
| 69 |
/** |
| 70 |
* Extra Classes for the container |
| 71 |
* @var Array |
| 72 |
*/ |
| 73 |
protected $_container_extra_classes; |
| 74 |
|
| 75 |
/** |
| 76 |
* Whether to hide this control in first rendering |
| 77 |
*/ |
| 78 |
protected $_is_hidden; |
| 79 |
|
| 80 |
/** |
| 81 |
* Class Constructor |
| 82 |
*/ |
| 83 |
public function __construct() |
| 84 |
{ |
| 85 |
$this->_data = array(); |
| 86 |
$this->_container_extra_classes = array(); |
| 87 |
} |
| 88 |
|
| 89 |
abstract public function render(); |
| 90 |
|
| 91 |
/** |
| 92 |
* Setup and return needed attribute as array |
| 93 |
* @return Array Data array |
| 94 |
*/ |
| 95 |
protected function _setup_data() |
| 96 |
{ |
| 97 |
// Set Basic Data |
| 98 |
$this->add_data('name', $this->get_name()); |
| 99 |
$this->add_data('default', $this->get_default()); |
| 100 |
$this->add_data('value', $this->get_value()); |
| 101 |
|
| 102 |
// Determine Type |
| 103 |
$type = 'vp-' . strtolower(substr(get_class($this), strrpos(get_class($this), '_') + 1)); |
| 104 |
|
| 105 |
// Is hidden |
| 106 |
if($this->is_hidden()) |
| 107 |
{ |
| 108 |
$this->add_container_extra_classes('vp-hide'); |
| 109 |
} |
| 110 |
|
| 111 |
// Set Control Head Data |
| 112 |
$this->add_data('head_info', array( |
| 113 |
'name' => $this->get_name(), |
| 114 |
'type' => $type, |
| 115 |
'container_extra_classes' => implode(' ', $this->get_container_extra_classes()), |
| 116 |
'is_hidden' => $this->is_hidden(), |
| 117 |
'validation' => $this->get_validation(), |
| 118 |
'dependency' => $this->get_dependency(), |
| 119 |
'binding' => $this->get_binding(), |
| 120 |
'label' => $this->get_label(), |
| 121 |
'description' => VP_Util_Text::parse_md($this->get_description()) |
| 122 |
)); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Basic self setup of the object |
| 127 |
* @param Array $arr Array representation of the field |
| 128 |
* @return VP_Control_Field Field object |
| 129 |
*/ |
| 130 |
protected function _basic_make($arr) |
| 131 |
{ |
| 132 |
$this->set_name(isset($arr['name']) ? $arr['name'] : '') |
| 133 |
->set_label(isset($arr['label']) ? $arr['label'] : '') |
| 134 |
->set_default(isset($arr['default']) ? $arr['default'] : null) |
| 135 |
->set_description(isset($arr['description']) ? $arr['description'] : '') |
| 136 |
->set_validation(isset($arr['validation']) ? $arr['validation'] : ''); |
| 137 |
|
| 138 |
if(isset($arr['dependency'])) |
| 139 |
{ |
| 140 |
$func = $arr['dependency']['function']; |
| 141 |
$field = $arr['dependency']['field']; |
| 142 |
$this->set_dependency($func . '|' . $field); |
| 143 |
} |
| 144 |
|
| 145 |
if(isset($arr['binding'])) |
| 146 |
{ |
| 147 |
$function = $arr['binding']['function']; |
| 148 |
$field = $arr['binding']['field']; |
| 149 |
$this->set_binding($function . '|' . $field); |
| 150 |
} |
| 151 |
|
| 152 |
return $this; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Add value to render data array |
| 157 |
* @param Mixed $item Value to be added to render data arary |
| 158 |
*/ |
| 159 |
public function add_data($key, $value) |
| 160 |
{ |
| 161 |
$this->_data[$key] = $value; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Get render data |
| 166 |
* |
| 167 |
* @return Array Render data array |
| 168 |
*/ |
| 169 |
public function get_data() { |
| 170 |
return $this->_data; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Set render data |
| 175 |
* |
| 176 |
* @param Array $_data Render data array |
| 177 |
*/ |
| 178 |
public function set_data($_data) { |
| 179 |
$this->_data = $_data; |
| 180 |
return $this; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Set single render data |
| 185 |
* |
| 186 |
* @param Array $_data Render data array |
| 187 |
*/ |
| 188 |
public function set_single_data($key, $_data) { |
| 189 |
$this->_data[$key] = $_data; |
| 190 |
return $this; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get single render data |
| 195 |
* |
| 196 |
* @param Array $_data Render data array |
| 197 |
*/ |
| 198 |
public function get_single_data($key) { |
| 199 |
return $this->_data[$key]; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Add value to render data array |
| 204 |
* @param Mixed $item Value to be added to render data arary |
| 205 |
*/ |
| 206 |
public function add_single_data($p_key, $key, $value) |
| 207 |
{ |
| 208 |
$this->_data[$p_key][$key] = $value; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Getter for $_name |
| 213 |
* |
| 214 |
* @return String unique name of the field |
| 215 |
*/ |
| 216 |
public function get_name() { |
| 217 |
return $this->_name; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Setter for $_name |
| 222 |
* |
| 223 |
* @param String $_name unique name of the field |
| 224 |
*/ |
| 225 |
public function set_name($_name) { |
| 226 |
$this->_name = $_name; |
| 227 |
return $this; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Getter for $_label |
| 232 |
* |
| 233 |
* @return String label of the field |
| 234 |
*/ |
| 235 |
public function get_label() { |
| 236 |
return $this->_label; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Setter for $_label |
| 241 |
* |
| 242 |
* @param String $_label label of the field |
| 243 |
*/ |
| 244 |
public function set_label($_label) { |
| 245 |
$this->_label = $_label; |
| 246 |
return $this; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Getter for $_description |
| 251 |
* |
| 252 |
* @return String description of the field |
| 253 |
*/ |
| 254 |
public function get_description() { |
| 255 |
return $this->_description; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Setter for $_description |
| 260 |
* |
| 261 |
* @param String $_description description of the field |
| 262 |
*/ |
| 263 |
public function set_description($_description) { |
| 264 |
$this->_description = $_description; |
| 265 |
return $this; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Getter for $_validation |
| 270 |
* |
| 271 |
* @return String validation pattern in string |
| 272 |
*/ |
| 273 |
public function get_validation() { |
| 274 |
return $this->_validation; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Setter for $_validation |
| 279 |
* |
| 280 |
* @param String $_validation validation pattern in string |
| 281 |
*/ |
| 282 |
public function set_validation($_validation) { |
| 283 |
$this->_validation = $_validation; |
| 284 |
return $this; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Getter for $_dependency |
| 289 |
* |
| 290 |
* @return String dependency pattern in string |
| 291 |
*/ |
| 292 |
public function get_dependency() { |
| 293 |
return $this->_dependency; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Setter for $_dependency |
| 298 |
* |
| 299 |
* @param String $_dependency dependency pattern in string |
| 300 |
*/ |
| 301 |
public function set_dependency($_dependency) { |
| 302 |
$this->_dependency = $_dependency; |
| 303 |
return $this; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Get $_binding |
| 308 |
* |
| 309 |
* @return String bind rule string |
| 310 |
*/ |
| 311 |
public function get_binding() { |
| 312 |
return $this->_binding; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Set $_binding |
| 317 |
* |
| 318 |
* @param String $_binding bind rule string |
| 319 |
*/ |
| 320 |
public function set_binding($_binding) { |
| 321 |
$this->_binding = $_binding; |
| 322 |
return $this; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Getter for $_default |
| 327 |
* |
| 328 |
* @return mixed default value of the field |
| 329 |
*/ |
| 330 |
public function get_default() { |
| 331 |
return $this->_default; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Setter for $_default |
| 336 |
* |
| 337 |
* @param mixed $_default default value of the field |
| 338 |
*/ |
| 339 |
public function set_default($_default) { |
| 340 |
$this->_default = $_default; |
| 341 |
return $this; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Get field value |
| 346 |
* |
| 347 |
* @return String|Array Value of field |
| 348 |
*/ |
| 349 |
public function get_value() { |
| 350 |
return $this->_value; |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Set field value |
| 355 |
* |
| 356 |
* @param String|Array $_value Value of field |
| 357 |
*/ |
| 358 |
public function set_value($_value) { |
| 359 |
$this->_value = $_value; |
| 360 |
return $this; |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Getter of $_field_max_height |
| 365 |
* |
| 366 |
* @return Integer Max height of the field |
| 367 |
*/ |
| 368 |
public function get_field_max_height() { |
| 369 |
return $this->_field_max_height; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Setter of $_field_max_height |
| 374 |
* |
| 375 |
* @param Integer $_field_max_height Max height of the field |
| 376 |
*/ |
| 377 |
public function set_field_max_height($_field_max_height) { |
| 378 |
$this->_field_max_height = $_field_max_height; |
| 379 |
return $this; |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Getter of $_container_extra_classes |
| 384 |
* |
| 385 |
* @return Array of Extra Classes for the container |
| 386 |
*/ |
| 387 |
public function get_container_extra_classes() { |
| 388 |
return $this->_container_extra_classes; |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Setter of $_container_extra_classes |
| 393 |
* |
| 394 |
* @param Array $_container_extra_classes Extra Classes for the container |
| 395 |
*/ |
| 396 |
public function set_container_extra_classes($_container_extra_classes) { |
| 397 |
$this->_container_extra_classes = $_container_extra_classes; |
| 398 |
return $this; |
| 399 |
} |
| 400 |
|
| 401 |
public function add_container_extra_classes($class) |
| 402 |
{ |
| 403 |
if(is_array($class)) |
| 404 |
{ |
| 405 |
$this->_container_extra_classes = array_merge($this->_container_extra_classes, $class); |
| 406 |
} |
| 407 |
else if(!in_array($class, $this->_container_extra_classes)) |
| 408 |
{ |
| 409 |
$this->_container_extra_classes[] = $class; |
| 410 |
} |
| 411 |
return $this->_container_extra_classes; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
/** |
| 416 |
* Get is_hidden status, will set the status if a boolean passed |
| 417 |
* |
| 418 |
* @return bool is_hidden status |
| 419 |
*/ |
| 420 |
public function is_hidden($_is_hidden = null) { |
| 421 |
if(!is_null($_is_hidden)) |
| 422 |
$this->_is_hidden = (bool) $_is_hidden; |
| 423 |
return $this->_is_hidden; |
| 424 |
} |
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Interface to force implementation of the 'factory' pattern method for each field class |
| 430 |
* to enable easier instantiation of each field class. |
| 431 |
*/ |
| 432 |
interface iFactory |
| 433 |
{ |
| 434 |
static function withArray($arr = array(), $class_name = null); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* EOF |
| 439 |
*/ |