PluginProbe
Customify / 1.5.0
Customify v1.5.0
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / core / classes / forms / FormField.php

FormField.php in Customify 1.5.0, at core/classes/forms/FormField.php

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