PluginProbe
Customify / 1.2.4
Customify v1.2.4
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.2.4, at core/classes/forms/Form.php

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