PluginProbe
Customify / 1.7.2
Customify v1.7.2
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 / Form.php

Form.php in Customify 1.7.2, at core/classes/forms/Form.php

191 lines 4.4 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 PixCustomifyFormImpl extends PixCustomifyHTMLElementImpl implements PixCustomifyForm {
10
11 /** @var array templates */
12 protected $fields = null;
13
14 /**
15 * @param array config
16 */
17 static function instance($config = null) {
18 $i = new self;
19 $i->configure($config);
20 return $i;
21 }
22
23 /**
24 * Apply configuration.
25 */
26 protected function configure($config = null) {
27 if ($config === null) {
28 $config = array('template-paths' => array(), 'fields' => array());
29 }
30
31 // setup errors
32 $this->errors = array();
33
34 // setup default autocomplete
35 $this->autocomplete = pixcustomify::instance('PixCustomifyMeta', array());
36
37 // setup fields
38 $this->fields = pixcustomify::instance('PixCustomifyMeta', $config['fields']);
39 unset($config['fields']);
40
41 // invoke htmltag instance configuration
42 parent::configure($config);
43
44 // setup paths
45 $this->setmeta('template-paths', $config['template-paths']);
46
47 // @todo CLEANUP the empty action should redirect to the same page but
48 // it's probably wiser to explicitly provide the right page url
49 $this->set('action', '');
50 $this->set('method', 'POST');
51 }
52
53 /**
54 * Shorthand.
55 *
56 * @return static $this
57 */
58 function addtemplatepath($path) {
59 return $this->addmeta('template-paths', $path);
60 }
61
62 /**
63 * Note: the field configuration parameter is indented for use when
64 * invoking fields as part of creating other fields (ie. embeded field
65 * configuration inside custom fields). It is not meant for overwriting
66 * configuration and will not accept partial configuration; albeit the
67 * minimal field configuration is fairly minimal.
68 *
69 * @param string field name
70 * @param array complete field configuration
71 * @return string
72 */
73 function field($fieldname, $fieldconfig = null) {
74 if ($fieldconfig === null) {
75 $fieldconfig = $this->fields->get($fieldname);
76 }
77
78 return pixcustomify::instance('PixCustomifyFormField', $fieldconfig)
79 ->setmeta('form', $this)
80 ->setmeta('name', $fieldname);
81 }
82
83 // Errors
84 // ------------------------------------------------------------------------
85
86 /** @var array field errors */
87 protected $errors = null;
88
89 /**
90 * @return static $this
91 */
92 function errors($errors) {
93 $this->errors = $errors;
94 return $this;
95 }
96
97 /**
98 * @param string field name
99 * @return array error keys with messages
100 */
101 function errors_for($fieldname) {
102 if (isset($this->errors[$fieldname])) {
103 return $this->errors[$fieldname];
104 }
105 else { // no errors set
106 return array();
107 }
108 }
109
110
111 // Autocomplete
112 // ------------------------------------------------------------------------
113
114 /** @var PixCustomifyMeta autocomplete */
115 protected $autocomplete = null;
116
117 /**
118 * Autocomplete meta object passed on by the processor.
119 *
120 * @param PixCustomifyMeta autocomplete values
121 * @return static $this
122 */
123 function autocomplete(PixCustomifyMeta $autocomplete) {
124 $this->autocomplete = $autocomplete;
125 return $this;
126 }
127
128 /**
129 * Retrieves the value registered for auto-complete. This will not fallback
130 * to the default value set in the configuration since fields are
131 * responsible for managing their internal complexity.
132 *
133 * Typically the autocomplete values are what the processor passes on to
134 * the form.
135 *
136 * @return mixed
137 */
138 function autovalue($key, $default = null) {
139 return $this->autocomplete->get($key, $default);
140 }
141
142 // Rendering
143 // ------------------------------------------------------------------------
144
145 /**
146 * @return string
147 */
148 function __toString() {
149 return $this->startform();;
150 }
151
152 /**
153 * @return string
154 */
155 function startform() {
156 return "<form {$this->htmlattributes()}>";
157 }
158
159 /**
160 * @return string
161 */
162 function endform() {
163 return '</form>';
164 }
165
166 /**
167 * @param string template path
168 * @param array configuration
169 * @return string
170 */
171 function fieldtemplate($templatepath, $conf = array()) {
172 $config = pixcustomify::instance('PixCustomifyMeta', $conf);
173 return $this->fieldtemplate_render($templatepath, $config);
174 }
175
176 /**
177 * @param string template path
178 * @param PixCustomifyMeta configuration
179 * @return string
180 */
181 protected function fieldtemplate_render($_template_path, PixCustomifyMeta $conf) {
182 // variables which we wish to expose to template
183 $form = $this; # $this will also work
184
185 ob_start();
186 include $_template_path;
187 return ob_get_clean();
188 }
189
190 } # class
191