PluginProbe
Hash Form – Drag & Drop Form Builder / trunk
Hash Form – Drag & Drop Form Builder vtrunk
1.4.4 1.4.3 1.4.2 1.4.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.6.1 1.2.7 1.2.8 1.2.9 1.3.0 All 47 releases
hash-form / includes / fields / HashFormFieldAddress.php

HashFormFieldAddress.php in Hash Form – Drag & Drop Form Builder trunk, at includes/fields/HashFormFieldAddress.php

196 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('ABSPATH') || die();
3
4 class HashFormFieldAddress extends HashFormFieldType {
5
6 protected $type = 'address';
7
8 protected function field_settings_for_type() {
9 return array(
10 'default' => false
11 );
12 }
13
14 protected function sub_fields() {
15 return array(
16 'line1' => array(
17 'type' => 'text',
18 'label' => esc_html__('Line 1', 'hash-form')
19 ),
20 'line2' => array(
21 'type' => 'text',
22 'label' => esc_html__('Line 2', 'hash-form')
23 ),
24 'city' => array(
25 'type' => 'text',
26 'label' => esc_html__('City', 'hash-form')
27 ),
28 'state' => array(
29 'type' => 'text',
30 'label' => esc_html__('State/Province', 'hash-form')
31 ),
32 'postal' => array(
33 'type' => 'number',
34 'label' => esc_html__('Zip/Postal', 'hash-form')
35 ),
36 'country' => array(
37 'type' => 'select',
38 'label' => esc_html__('Country', 'hash-form')
39 ));
40 }
41
42 protected function show_after_default() {
43 $sub_fields = $this->sub_fields();
44 foreach ($sub_fields as $name => $sub_field) {
45 $this->single_field($name, $sub_field);
46 }
47 }
48
49 protected function single_field($name, $sub_field) {
50 $field = $this->get_field();
51 $field_id = $field['id'];
52 $field_key = $field['field_key'];
53 $label = $sub_field['label'];
54 $type = $sub_field['type'];
55 $desc = isset($field['desc'][$name]) ? $field['desc'][$name] : '';
56 $placeholder = isset($field['placeholder'][$name]) ? $field['placeholder'][$name] : '';
57 $value = isset($field['default_value'][$name]) ? $field['default_value'][$name] : '';
58 $disable = isset($field['disable'][$name]) ? $field['disable'][$name] : 'on';
59 $country_grid_class = $name !== 'country' ? ' hf-grid-2' : '';
60 ?>
61 <div class="hf-form-row hf-sub-field-<?php echo esc_attr($name); ?>" data-sub-field-name="<?php echo esc_attr($name); ?>" data-field-id="<?php echo esc_attr($field_id); ?>">
62 <div class="hf-sub-field-label">
63 <?php echo esc_html($label); ?>
64 <label class="hf-field-show-hide">
65 <input type="checkbox" name="field_options[disable_<?php echo esc_attr($field_id); ?>][<?php echo esc_attr($name); ?>]" id="hf-disable-<?php echo esc_attr($name); ?>-<?php echo esc_attr($field_id); ?>" value="off" data-disablefield="hf-subfield-container-<?php echo esc_attr($name); ?>-<?php echo esc_attr($field_id); ?>" <?php checked(($disable == 'off'), true) ?>>
66 <label for="hf-disable-<?php echo esc_attr($name); ?>-<?php echo esc_attr($field_id); ?>"></label>
67 </label>
68 </div>
69
70 <div class="hf-grid-container">
71 <?php if ($name !== 'country') { ?>
72 <div class="hf-form-row hf-grid-2">
73 <input type="<?php echo esc_attr($type); ?>" name="default_value_<?php echo esc_attr($field_id); ?>[<?php echo esc_attr($name); ?>]" value="<?php echo esc_attr($value); ?>" data-changeme="hf-field-<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($name); ?>" data-changeatt="value">
74 <label class="hf-field-desc"><?php esc_html_e('Default Value', 'hash-form'); ?></label>
75 </div>
76 <div class="hf-form-row hf-grid-2">
77 <input type="text" name="field_options[placeholder_<?php echo esc_attr($field_id); ?>][<?php echo esc_attr($name); ?>]" value="<?php echo esc_attr($placeholder); ?>" data-changeme="hf-field-<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($name); ?>" data-changeatt="placeholder">
78 <label class="hf-field-desc"><?php esc_html_e('Placeholder', 'hash-form'); ?></label>
79 </div>
80 <?php } ?>
81 <div class="hf-form-row<?php echo esc_attr($country_grid_class); ?>">
82 <input type="text" name="field_options[desc_<?php echo esc_attr($field_id); ?>][<?php echo esc_attr($name); ?>]" value="<?php echo esc_html($desc); ?>" data-changeme="hf-subfield-desc-<?php echo esc_attr($name); ?>-<?php echo esc_attr($field_id); ?>">
83 <label class="hf-field-desc"><?php esc_html_e('Description', 'hash-form'); ?></label>
84 </div>
85 </div>
86 </div>
87 <?php
88 }
89
90 protected function extra_field_default_opts() {
91 $sub_fields = $this->sub_fields();
92 $field_options = array();
93 foreach ($sub_fields as $name => $fields) {
94 $field_options['desc'][$name] = $fields['label'];
95 }
96 return $field_options;
97 }
98
99 public function validate($args) {
100 $errors = isset($args['errors']) ? $args['errors'] : array();
101 $field = $this->get_field();
102
103 if ($field->required == '1') {
104 $sub_fields = $this->sub_fields();
105 unset($sub_fields['line2']);
106
107 foreach ($sub_fields as $name => $sub_field) {
108 if (isset($args['value'][$name]) && empty($args['value'][$name])) {
109 $errors['field' . $args['id']] = HashFormFields::get_error_msg($this->field, 'blank');
110 }
111 }
112 }
113 return $errors;
114 }
115
116 protected function input_html() {
117 $field = $this->get_field();
118 $field_id = $field['id'];
119 $field_key = $field['field_key'];
120 ?>
121 <div class="hf-grouped-field" id="hf-grouped-field-<?php echo esc_attr($field_id); ?>">
122 <?php
123 $sub_fields = $this->sub_fields();
124 foreach ($sub_fields as $name => $sub_field) {
125 $value = isset($field['default_value'][$name]) ? $field['default_value'][$name] : '';
126 $placeholder = isset($field['placeholder'][$name]) ? $field['placeholder'][$name] : '';
127 $disable = isset($field['disable'][$name]) ? $field['disable'][$name] : 'on';
128 $class = $disable == 'off' ? ' hf-hidden' : ' ';
129
130 $label = isset($field['desc'][$name]) ? $field['desc'][$name] : '';
131 $type = $sub_field['type'];
132
133 if (is_admin() || !($disable == 'off')) {
134 ?>
135 <div id="hf-subfield-container-<?php echo esc_attr($name) . '-' . esc_attr($field_id); ?>" class="hf-subfield-element hf-subfield-element-<?php echo esc_attr($name); ?> hf-grid-6 <?php echo esc_attr($class); ?>" data-sub-field-name="<?php echo esc_attr($name); ?>">
136 <?php
137 /*
138 * Every line of the address is announced by name.
139 * Without this each box read as the field's own label,
140 * so city, state and postcode were indistinguishable
141 * to anyone not looking at the caption beneath them.
142 */
143 $sub_label = ('' !== trim((string) $label)) ? $label : $sub_field['label'];
144 $sub_label = trim((string) $sub_label);
145 $aria_label = $sub_label ? $field['name'] . ' ' . $sub_label : $field['name'];
146
147 if ($type !== 'select') {
148 ?>
149 <input type="<?php echo esc_attr($type); ?>" id="hf-field-<?php echo esc_attr($field_key); ?>-<?php echo esc_attr($name); ?>" aria-label="<?php echo esc_attr($aria_label); ?>"<?php echo !empty($field['required']) ? ' aria-required="true"' : ''; ?> value="<?php echo esc_attr(apply_filters('hashform_translate_string', $value, 'Hash Form', $field['id'] . ' - ' . ucwords($name) . ' Value')); ?>" name="<?php echo esc_attr($this->html_name()) . '[' . esc_attr($name) . ']'; ?>" placeholder="<?php echo esc_attr(apply_filters('hashform_translate_string', $placeholder, 'Hash Form', $field['id'] . ' - ' . ucwords($name) . ' Placeholder')); ?>">
150 <?php
151 } else {
152 $this->get_country_select(HashFormHelper::get_countries(), $aria_label, $value);
153 }
154 ?>
155 <div class="hf-field-desc" id="hf-subfield-desc-<?php echo esc_attr($name); ?>-<?php echo esc_attr($field_id); ?>">
156 <?php
157 echo esc_html(apply_filters('hashform_translate_string', $label, 'Hash Form', HashFormBuilder::get_form_title($field['form_id']) . ' - ' . $field['id'] . ' - ' . ucwords($name) . ' Label'));
158 ?>
159 </div>
160 </div>
161 <?php
162 }
163 }
164 ?>
165 </div>
166 <?php
167 }
168
169 /**
170 * @param array $args Country names.
171 * @param string $aria_label Accessible name for the control.
172 * @param string $selected Country to preselect.
173 */
174 protected function get_country_select($args, $aria_label = '', $selected = '') {
175 $field = $this->get_field();
176 $field_key = $field['field_key'];
177 ?>
178 <select id="<?php echo 'hf-field-' . esc_attr($field_key) . '-country'; ?>" name="<?php echo esc_attr($this->html_name()) . '[country]'; ?>"<?php echo $aria_label ? ' aria-label="' . esc_attr($aria_label) . '"' : ''; ?><?php echo !empty($field['required']) ? ' aria-required="true"' : ''; ?>>
179 <?php
180 foreach ($args as $arg) {
181 /*
182 * The stray "';" that used to sit after this tag was left
183 * over from string-built markup and printed as literal text
184 * after every country in the list.
185 */
186 ?>
187 <option value="<?php echo esc_attr($arg); ?>"<?php selected($selected, $arg); ?>><?php echo esc_html($arg); ?></option>
188 <?php
189 }
190 ?>
191 </select>
192 <?php
193 }
194
195 }
196