| 1 |
<?php defined('ABSPATH') or die; |
| 2 |
|
| 3 |
/* This file is property of Pixel Grade Media. You may NOT copy, or redistribute |
| 4 |
* it. Please see the license that came with your copy for more information. |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* @package pixcustomify |
| 9 |
* @category core |
| 10 |
* @author Pixel Grade Team |
| 11 |
* @copyright (c) 2013, Pixel Grade Media |
| 12 |
*/ |
| 13 |
class PixCustomifyFormFieldImpl extends PixCustomifyHTMLElementImpl implements PixCustomifyFormField { |
| 14 |
|
| 15 |
/** |
| 16 |
* @param array config |
| 17 |
*/ |
| 18 |
static function instance($config = null) { |
| 19 |
$i = new self; |
| 20 |
$i->configure($config); |
| 21 |
return $i; |
| 22 |
} |
| 23 |
|
| 24 |
// Error Handling Helpers |
| 25 |
// ------------------------------------------------------------------------ |
| 26 |
|
| 27 |
/** |
| 28 |
* @return boolean true if field has errors |
| 29 |
*/ |
| 30 |
function has_errors() { |
| 31 |
$form = $this->getmeta('form', null); |
| 32 |
$errors = $form->errors_for($this->getmeta('name')); |
| 33 |
return ! empty($errors); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return string first error message |
| 38 |
*/ |
| 39 |
function one_error() { |
| 40 |
$form = $this->getmeta('form', null); |
| 41 |
$errors = $form->errors_for($this->getmeta('name')); |
| 42 |
return array_shift($errors); |
| 43 |
} |
| 44 |
|
| 45 |
// Rendering |
| 46 |
// ------------------------------------------------------------------------ |
| 47 |
|
| 48 |
/** |
| 49 |
* Render field emulates wordpress template behaviour. First searches for |
| 50 |
* name, then searches field type and so on. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
function render() { |
| 55 |
$form = $this->getmeta('form'); |
| 56 |
|
| 57 |
// we reverse the order so that last added is first checked |
| 58 |
$template_paths = array_reverse($form->getmeta('template-paths', array())); |
| 59 |
|
| 60 |
if (empty($template_paths)) { |
| 61 |
throw new Exception('Missing template paths.'); |
| 62 |
} |
| 63 |
|
| 64 |
// the following are the file patterns we look for |
| 65 |
$patterns = array |
| 66 |
( |
| 67 |
'fields/'.$this->getmeta('name'), |
| 68 |
'fields/'.$this->getmeta('type'), |
| 69 |
$this->getmeta('name'), |
| 70 |
$this->getmeta('type') |
| 71 |
); |
| 72 |
|
| 73 |
foreach ($patterns as $pattern) { |
| 74 |
foreach ($template_paths as $path) { |
| 75 |
$dirpath = rtrim($path, '\\/').DIRECTORY_SEPARATOR; |
| 76 |
if (file_exists($dirpath.$pattern.EXT)) { |
| 77 |
return $this->render_template_file($dirpath.$pattern.EXT); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
throw new Exception('Failed to match any pattern for field ['.$this->getmeta('name').'] of type '.$this->getmeta('type', '[unknown]')); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @param string template path |
| 87 |
* @return string rendered field |
| 88 |
*/ |
| 89 |
protected function render_template_file($_template_filepath) { |
| 90 |
// variables which we wish to expose to template |
| 91 |
$field = $this; # $this will also work |
| 92 |
$form = $this->getmeta('form'); |
| 93 |
$name = $this->getmeta('name', null); |
| 94 |
$label = $this->getmeta('label', null); |
| 95 |
$default = $this->getmeta('default', null); |
| 96 |
$desc = $this->getmeta('desc', ''); |
| 97 |
$rendering = $this->getmeta('rendering', 'standard'); |
| 98 |
|
| 99 |
// cleaned name (names may be "something[]") |
| 100 |
$idname = preg_replace('/[^a-zA-Z0-9_-]/', '', $name); |
| 101 |
|
| 102 |
ob_start(); |
| 103 |
include $_template_filepath; |
| 104 |
return ob_get_clean(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
function __toString() { |
| 111 |
return $this->render(); |
| 112 |
} |
| 113 |
|
| 114 |
} # class |
| 115 |
|