PluginProbe
Repeater Fields for Gravity Forms / 2.4.5
Repeater Fields for Gravity Forms v2.4.5
3.2.0 3.1.1 3.1.0 3.0.4 3.0.3 3.0.2 3.0.1 3.0.0 2.5.1 2.5.0 2.4.6 trunk 2.0.5 2.0.9 2.1.0 2.3.2 2.3.7 2.4.1 2.4.3 2.4.4 2.4.5
repeater-for-gravity-forms / fields / repeater_field.php

repeater_field.php in Repeater Fields for Gravity Forms 2.4.5, at fields/repeater_field.php

684 lines 21.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // If Gravity Forms isn't loaded, bail.
3 if (! class_exists('GFForms')) {
4 die();
5 }
6 /**
7 * Class GF_Field_Phone
8 *
9 * Handles the behavior of Phone fields.
10 *
11 * @since Unknown
12 */
13 class Superaddons_GFRepeater_Field extends GF_Field
14 {
15 private $lead_id = null;
16 /**
17 * Defines the field type.
18 *
19 * @since Unknown
20 * @access public
21 *
22 * @var string The field type.
23 */
24 public $type = 'repeater_end';
25 public $list_fields_validate = array();
26 /**
27 * Defines the field title to be used in the form editor.
28 *
29 * @since Unknown
30 * @access public
31 *
32 * @used-by GFCommon::get_field_type_title()
33 *
34 * @return string The field title. Translatable and escaped.
35 */
36 public function get_form_editor_field_title()
37 {
38 return esc_attr__('Repeater end', 'repeater-for-gravity-forms');
39 }
40 public function get_form_editor_button()
41 {
42 return array(
43 'group' => 'advanced_fields',
44 'text' => $this->get_form_editor_field_title()
45 );
46 }
47 /**
48 * Defines the field settings available within the field editor.
49 *
50 * @since Unknown
51 * @access public
52 *
53 * @return array The field settings available for the field.
54 */
55 function get_form_editor_field_settings()
56 {
57 return array(
58 'conditional_logic_field_setting',
59 'prepopulate_field_setting',
60 'error_message_setting',
61 'label_setting',
62 'field_field_repeater_end_text_setting',
63 'field_field_repeater_initial_rows_setting',
64 'field_field_repeater_max_setting',
65 'label_placement_setting',
66 'admin_label_setting',
67 'size_setting',
68 'rules_setting',
69 'visibility_setting',
70 'duplicate_setting',
71 'placeholder_setting',
72 'description_setting',
73 'css_class_setting',
74 );
75 }
76 /**
77 * Defines if conditional logic is supported in this field type.
78 *
79 * @since Unknown
80 * @access public
81 *
82 * @used-by GFFormDetail::inline_scripts()
83 * @used-by GFFormSettings::output_field_scripts()
84 *
85 * @return bool true
86 */
87 public function is_conditional_logic_supported()
88 {
89 return false;
90 }
91 /**
92 * Returns the field input.
93 *
94 * @since Unknown
95 * @access public
96 *
97 * @used-by GFCommon::get_field_input()
98 * @uses GF_Field::is_entry_detail()
99 * @uses GF_Field::is_form_editor()
100 * @uses GF_Field_Phone::$failed_validation
101 * @uses GF_Field_Phone::get_phone_format()
102 * @uses GFFormsModel::is_html5_enabled()
103 * @uses GF_Field::get_field_placeholder_attribute()
104 * @uses GF_Field_Phone::$isRequired
105 * @uses GF_Field::get_tabindex()
106 *
107 * @param array $form The Form Object.
108 * @param string $value The value of the input. Defaults to empty string.
109 * @param null|array $entry The Entry Object. Defaults to null.
110 *
111 * @return string The HTML markup for the field.
112 */
113 public function get_field_input($form, $value = '', $entry = null)
114 {
115 if (is_array($value)) {
116 $value = '';
117 }
118 $is_entry_detail = $this->is_entry_detail();
119 $is_form_editor = $this->is_form_editor();
120 $form_id = $form['id'];
121 $id = intval($this->id);
122 $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id";
123 $size = $this->size;
124 $disabled_text = $is_form_editor ? "disabled='disabled'" : '';
125 $class_suffix = $is_entry_detail ? '_admin' : '';
126 $class = $size . $class_suffix . " gf-field-repeater-data hidden";
127 $class = esc_attr($class);
128 $instruction_div = '';
129 $html_input_type = 'hidden';
130 $placeholder_attribute = $this->get_field_placeholder_attribute();
131 $required_attribute = $this->isRequired ? 'aria-required="true"' : '';
132 $invalid_attribute = $this->failed_validation ? 'aria-invalid="true"' : 'aria-invalid="false"';
133 $aria_describedby = $this->get_aria_describedby();
134 $tabindex = $this->get_tabindex();
135 $repeater_add_button = $this->field_repeater_end_text;
136 if ($repeater_add_button == "") {
137 $repeater_add_button = esc_attr__("Add more", "gravityforms-repeater");
138 }
139 $initial_rows = $this->repeater_initial_rows;
140 if ($initial_rows == "") {
141 $initial_rows = 1;
142 }
143 $limit = $this->repeater_max;
144 if ($limit == "" or $limit < 1) {
145 $limit = 9999;
146 }
147 $limit = apply_filters("yeeaddons_gf_repeater_limit", 5, $limit);
148 $initial_rows = apply_filters("yeeaddons_gf_repeater_initial_rows", 1, $initial_rows);
149 $input = "<input data-initial_rows_map_check='" . $this->repeater_initial_rows_map . "' data-initial_rows_map='input_{$form_id}_" . $this->repeater_initial_rows_map . "' data-map_id='field_" . $form_id . "_" . $id . "' name='input_{$id}' id='{$field_id}' type='{$html_input_type}' value='{$value}' class='{$class}' {$tabindex} {$placeholder_attribute} {$required_attribute} {$invalid_attribute} {$disabled_text}/>";
150 $html = '<div data-initial_rows_map_check="' . $this->repeater_initial_rows_map . '" class="repeater-field-warp-item-data" data-initial_rows="' . $initial_rows . '" data-limit="' . $limit . '" data-initial_rows_map="input_' . $form_id . '_' . $this->repeater_initial_rows_map . '" data-map_id="field_' . $form_id . '_' . $id . '">
151 <div class="repeater-field-warp-item">
152 </div>
153 <div class="repeater-field-footer"><a href="#"" class="gf-repeater-field-button-add" >' . $repeater_add_button . '</a></div>
154 ' . $input . '
155 <input type="hidden" class="gf-field-repeater-data-html hidden" />
156 </div>';
157 if (is_admin()) {
158 $html = '<hr>End Repeater<hr>';
159 return sprintf("<div class='ginput_container1'>%s</div>", $html);
160 } else {
161 return sprintf("<div class='ginput_container'>%s</div>", $html);
162 }
163 }
164 public static function remove_validation($form)
165 {
166 $zo = false;
167 $zo_datas = array();
168 foreach ($form["fields"] as $field) {
169 $type = $field->type;
170 if ($type == "repeater_start") {
171 $zo = true;
172 continue;
173 }
174 if ($type == "repeater_end") {
175 $zo = false;
176 continue;
177 }
178 if ($zo) {
179 if (!isset($field->repeater_validate)) {
180 $field->repeater_validate = array("isRequired" => $field->isRequired);
181 }
182 $field->isRequired = false;
183 }
184 }
185 return $form;
186 }
187 function validate($value, $form)
188 {
189 $datas = json_decode($value, true);
190 $list_fields_validate = array();
191 $failedValidation = false;
192 foreach ($form["fields"] as $field) {
193 if (is_array($field->repeater_validate)) {
194 foreach ($field->repeater_validate as $k => $v) {
195 if ($k == "isRequired" && $v == true) {
196 $list_fields_validate["input_" . $field->id] = $field->type;
197 }
198 }
199 }
200 }
201 if (isset($datas["id"]) && is_array($datas["id"])) {
202 foreach ($datas["id"] as $id) {
203 foreach ($datas["fields"] as $field) {
204 if ($field == "") {
205 continue;
206 }
207 $field = str_replace('[]', '', $field);
208 if (isset($list_fields_validate[$field])) {
209 $getInputData = rgpost($field . "__" . $id);
210 switch ($list_fields_validate[$field]) {
211 case "name":
212 $first_name = rgpost($field . "_3__" . $id);
213 $last_name = rgpost($field . "_6__" . $id);
214 if (empty($first_name) || empty($last_name)) {
215 $failedValidation = true;
216 }
217 break;
218 case 'address':
219 $address = rgpost($field . "_1__" . $id);
220 if (empty($address)) {
221 $failedValidation = true;
222 }
223 break;
224 case 'fileupload':
225 if (isset($_FILES[$field . "__" . $id]) && $_FILES[$field . "__" . $id]['name'][0]) {
226 //file uploaded
227 } else {
228 $failedValidation = true;
229 }
230 break;
231 default:
232 if (is_array($getInputData)) {
233 foreach ($getInputData as $vl) {
234 if (empty($vl)) {
235 $failedValidation = true;
236 }
237 }
238 } else {
239 if (empty($getInputData)) {
240 $failedValidation = true;
241 }
242 }
243 break;
244 }
245 }
246 }
247 }
248 if ($failedValidation) {
249 $this->failed_validation = true;
250 if ($this->errorMessage) {
251 $this->validation_message = $this->errorMessage;
252 } else {
253 $this->validation_message = esc_html__('This field is required.', 'gravityforms');
254 }
255 return;
256 } else {
257 $this->failed_validation = false;
258 }
259 }
260 }
261 function custom_validation($form)
262 {
263 $dataRepeater = array();
264 $zo = false;
265 $zo_datas = array();
266 foreach ($form["fields"] as $field) {
267 var_dump($field);
268 die();
269 $type = $field->type;
270 if ($type == "repeater_start") {
271 $zo = true;
272 continue;
273 }
274 if ($zo) {
275 var_dump($field);
276 $zo_datas[$field->id] = array("isRequired" => $field->isRequired);
277 $field->isRequired = false;
278 }
279 if ($type == "repeater_end") {
280 $zo = false;
281 $zo_datas = array();
282 continue;
283 }
284 }
285 return $form;
286 }
287 /**
288 * Gets the value of the submitted field.
289 *
290 * @since Unknown
291 * @access public
292 *
293 * @used-by GFFormsModel::get_field_value()
294 * @uses GF_Field::get_value_submission()
295 * @uses GF_Field_Phone::sanitize_entry_value()
296 *
297 * @param array $field_values The dynamic population parameter names with their corresponding values to be populated.
298 * @param bool $get_from_post_global_var Whether to get the value from the $_POST array as opposed to $field_values. Defaults to true.
299 *
300 * @return array|string
301 */
302 public function get_value_submission($field_values, $get_from_post_global_var = true)
303 {
304 $value = parent::get_value_submission($field_values, $get_from_post_global_var);
305 $value = $this->sanitize_entry_value($value, $this->formId);
306 return $value;
307 }
308 /**
309 * Sanitizes the entry value.
310 *
311 * @since Unknown
312 * @access public
313 *
314 * @used-by GF_Field_Phone::get_value_save_entry()
315 * @used-by GF_Field_Phone::get_value_submission()
316 *
317 * @param string $value The value to be sanitized.
318 * @param int $form_id The form ID of the submitted item.
319 *
320 * @return string The sanitized value.
321 */
322 public function sanitize_entry_value($value, $form_id)
323 {
324 $value = is_array($value) ? array_map('sanitize_text_field', $value) : sanitize_text_field($value);
325 return $value;
326 }
327 /**
328 * Gets the field value when an entry is being saved.
329 *
330 * @since Unknown
331 * @access public
332 *
333 * @used-by GFFormsModel::prepare_value()
334 * @uses GF_Field_Phone::sanitize_entry_value()
335 * @uses GF_Field_Phone::$phoneFormat
336 *
337 * @param string $value The input value.
338 * @param array $form The Form Object.
339 * @param string $input_name The input name.
340 * @param int $lead_id The Entry ID.
341 * @param array $lead The Entry Object.
342 *
343 * @return string The field value.
344 */
345 public function get_value_save_entry($value, $form, $input_name, $lead_id, $lead)
346 {
347 $this->lead_id = $lead_id;
348 $datas = json_decode($value, true);
349 //var_dump($value);
350 $values = array();
351 $form_id = $this->formId;
352 $get_form = GFFormsModel::get_form_meta_by_id($form_id);
353 $form = $get_form[0];
354 $fields = array();
355 foreach ($datas["fields"] as $f) {
356 $datas_f = explode(".", $f);
357 $fields[] = $datas_f[0];
358 }
359 $fields = array_unique($fields);
360 foreach ($datas["id"] as $id_rand) {
361 $datas_step = array();
362 foreach ($fields as $field) {
363 $field = str_replace("[]", "", $field);
364 $inputs = $this->get_inputs($form, $field);
365 if (is_array($inputs)) {
366 $getInputData = array();
367 foreach ($inputs as $child_input) {
368 $getInputName = "input_" . $child_input["id"] . "__" . $id_rand;
369 $vl = rgpost(str_replace('.', '_', strval($getInputName)));
370 if ($vl != "") {
371 $getInputData[$child_input["id"]] = $vl;
372 }
373 }
374 $datas_step[$getInputName] = $getInputData;
375 } else {
376 $getInputName = $field . "__" . $id_rand;
377 if (isset($_FILES[$getInputName])) {
378 $datas_step[$getInputName] = apply_filters("yeeaddons_gf_repeater_file", "Upgrade to pro version", $form_id, $_FILES[$getInputName]);
379 } else if (isset($_POST[$getInputName])) {
380 $datas_step[$getInputName] = rgpost(str_replace('.', '_', strval($getInputName)));
381 } else {
382 $getInputData = rgpost(str_replace('.', '_', strval($getInputName)));
383 $datas_step[$getInputName] = $getInputData;
384 }
385 }
386 }
387 $values[$id_rand] = $datas_step;
388 }
389 return maybe_serialize($values);
390 }
391 public function upload_file($form_id, $file)
392 {
393 $target = GFFormsModel::get_file_upload_path($form_id, $file['name']);
394 if (! $target) {
395 return 'FAILED (Upload folder could not be created.)';
396 }
397 if (move_uploaded_file($file['tmp_name'], $target['path'])) {
398 $this->set_permissions($target['path']);
399 return $target['url'];
400 } else {
401 return 'FAILED (Temporary file could not be copied.)';
402 }
403 }
404 function set_permissions($path)
405 {
406 GFFormsModel::set_permissions($path);
407 }
408 public function get_value_entry_list($value, $entry, $field_id, $columns, $form)
409 {
410 if (empty($value)) {
411 return '';
412 } else {
413 $dataArray = GFFormsModel::unserialize($value);
414 $arrayCount = count($dataArray);
415 if ($arrayCount > 1) {
416 $returnText = $arrayCount . ' entries';
417 } else {
418 $returnText = $arrayCount . ' entry';
419 }
420 return $returnText;
421 }
422 }
423 function get_datas_field($format = "html", $value = '', $form_id = "")
424 {
425 global $wpdb;
426 $dataArray = GFFormsModel::unserialize($value);
427 $get_form = GFFormsModel::get_form_meta_by_id($form_id);
428 $form = $get_form[0];
429 if (isset($_GET['lid'])) {
430 $result = GFAPI::get_entry($_GET['lid']);
431 } else {
432 $table = $wpdb->prefix . "gf_entry";
433 $mylink = $wpdb->get_row("SELECT id FROM $table ORDER BY id DESC");
434 $result = GFAPI::get_entry($mylink->id);
435 }
436 if ($format === 'html') {
437 $html = '<ol>';
438 foreach ($dataArray as $step_datas) {
439 $html .= '<li><ul>';
440 foreach ($step_datas as $name => $vl) {
441 $type = $this->get_type($form, $name, $form_id);
442 $lb = $this->get_field_label($form, $name, $type, false, $form_id);
443 if (is_array($vl)) {
444 switch ($type) {
445 case "address":
446 $vl_data = "";
447 foreach ($vl as $k => $v) {
448 $child_lb = $this->get_field_label($form, "input_" . $k, $type, true);
449 $vl_data .= $child_lb . ": " . $v . "<br>";
450 }
451 $html .= '<li>' . $lb . ": <br>" . $vl_data . "</li>";
452 break;
453 default:
454 $vl_data = implode(", ", $vl);
455 $html .= '<li>' . $lb . ": " . $vl_data . "</li>";
456 break;
457 }
458 } else {
459 $vl_data = $vl;
460 switch ($type) {
461 case "fileupload":
462 if ($vl_data == "Upgrade to pro version") {
463 $html .= '<li>' . $lb . ": " . $vl_data . "</li>";
464 } else {
465 if (filter_var($vl_data, FILTER_VALIDATE_URL) === FALSE) {
466 $content = array();
467 $main_name = explode("__", $name);
468 $main_name = explode("_", $main_name[0]);
469 if (isset($main_name[4])) {
470 $main_name_id = $main_name[4];
471 } else {
472 $main_name_id = $main_name[1];
473 }
474 $vl_data = explode(",", $vl_data);
475 if (isset($result[$main_name_id])) {
476 $data_uploads = json_decode($result[$main_name_id], true);
477 foreach ($vl_data as $n) {
478 $n = sanitize_file_name($n);
479 foreach ($data_uploads as $name) {
480 $name_s = explode(".", $n);
481 $name_s = $name_s[0];
482 $re = "/" . $name_s . "\.|" . $name_s . "[\d]\./";
483 if (preg_match($re, $name)) {
484 $content[] = '<a href="' . $name . '" download>' . $n . "</a> ";
485 break;
486 }
487 }
488 }
489 }
490 $html .= '<li>' . $lb . ': ' . implode(" | ", $content) . "</li>";
491 } else {
492 $html .= '<li>' . $lb . ': <a href="' . $vl_data . '" download>' . $vl_data . "</a></li>";
493 }
494 }
495 break;
496 default:
497 $html .= '<li>' . $lb . ": " . $vl_data . "</li>";
498 break;
499 }
500 }
501 }
502 $html .= '</ul></li>';
503 }
504 $html .= '<ol>';
505 $html = apply_filters("yeeadons_gravity_forms_repeater_html", $html, $dataArray, $form);
506 return $html;
507 } else {
508 $html = '';
509 foreach ($dataArray as $step_datas) {
510 foreach ($step_datas as $name => $vl) {
511 $type = $this->get_type($form, $name, $form_id);
512 $lb = $this->get_field_label($form, $name, $type, false, $form_id);
513 if (is_array($vl)) {
514 switch ($type) {
515 case "address":
516 $vl_data = "";
517 foreach ($vl as $k => $v) {
518 $child_lb = $this->get_field_label($form, "input_" . $k, $type, true);
519 $vl_data .= $child_lb . ": " . $v . "\n";
520 }
521 $html .= $lb . " : " . $vl_data . "\n";
522 break;
523 default:
524 $vl_data = implode(", ", $vl);
525 $html .= $lb . ": " . $vl_data . "\n";
526 break;
527 }
528 } else {
529 $vl_data = $vl;
530 switch ($type) {
531 case "fileupload":
532 if (filter_var($vl_data, FILTER_VALIDATE_URL) === FALSE) {
533 $content = array();
534 $main_name = explode("__", $name);
535 $main_name = explode("_", $main_name[0]);
536 $main_name_id = $main_name[4];
537 $vl_data = explode(",", $vl_data);
538 if (isset($result[$main_name_id])) {
539 $data_uploads = json_decode($result[$main_name_id], true);
540 foreach ($vl_data as $n) {
541 $n = sanitize_file_name($n);
542 foreach ($data_uploads as $name) {
543 $name_s = explode(".", $n);
544 $name_s = $name_s[0];
545 $re = "/" . $name_s . "\.|" . $name_s . "[\d]\./";
546 if (preg_match($re, $name)) {
547 $content[] = '<a href="' . $name . '" download>' . $n . "</a> ";
548 break;
549 }
550 }
551 }
552 }
553 $html .= $lb . ': ' . implode(" | ", $content) . "\n";
554 } else {
555 $html .= $lb . ':' . $vl_data . "\n";
556 }
557 break;
558 default:
559 $html .= $lb . ": " . $vl_data . "\n";
560 break;
561 }
562 }
563 }
564 $html .= "\n";
565 }
566 $html = apply_filters("yeeadons_gravity_forms_repeater_text", $html, $dataArray, $form);
567 return $html;
568 }
569 }
570 public function get_value_export($entry, $input_id = '', $use_text = false, $is_csv = false)
571 {
572 //Export doesn’t require encoding, but field data may require some manipulation or formatting before it is exported
573 $form_id = $entry["form_id"];
574 $return = $this->get_datas_field("text", rgar($entry, $input_id), $form_id);
575 return $return;
576 }
577 public function get_value_merge_tag($value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br)
578 {
579 if (empty($value)) {
580 return '';
581 }
582 $form_id = isset($form['id']) ? absint($form['id']) : null;
583 $return = $this->get_datas_field($format, $value, $form_id);
584 return $return;
585 }
586 public function get_value_entry_detail($value, $currency = '', $use_text = false, $format = 'html', $media = 'screen')
587 {
588 global $wpdb;
589 if (empty($value)) {
590 return '';
591 } else {
592 $form_id = $this->formId;
593 $return = $this->get_datas_field($format, $value, $form_id);
594 return $return;
595 }
596 }
597 function get_field_label($form, $name = '', $type = '', $child = false, $form_id = '')
598 {
599 if (is_array($form)) {
600 if (!array_key_exists('fields', $form)) {
601 return false;
602 }
603 } else {
604 return false;
605 }
606 $name = str_replace("gform_multifile_upload_" . $form_id, "input", $name);
607 $names = explode("__", $name);
608 $names = explode("_", $names[0]);
609 $name = $names[1];
610 foreach ($form['fields'] as $field_key => $field_value) {
611 if (is_array($field_value->inputs)) {
612 foreach ($field_value->inputs as $children_id) {
613 if ($children_id["id"] == $name) {
614 if ($child) {
615 return $children_id["label"];
616 } else {
617 return $field_value->label;
618 }
619 }
620 }
621 } else {
622 if ($field_value->id == $name) {
623 return $field_value->label;
624 }
625 }
626 }
627 return "";
628 }
629 function get_inputs($form, $name = '')
630 {
631 if (is_array($form)) {
632 if (!array_key_exists('fields', $form)) {
633 return false;
634 }
635 } else {
636 return false;
637 }
638 $names = explode("__", $name);
639 $names = explode("_", $names[0]);
640 $name = $names[1];
641 foreach ($form['fields'] as $field_key => $field_value) {
642 if ($field_value->id == $name) {
643 if ($field_value->type == "date") {
644 return $name;
645 }
646 if (is_array($field_value->inputs)) {
647 return $field_value->inputs;
648 }
649 }
650 }
651 return false;
652 }
653 function get_type($form, $name = '', $form_id = "")
654 {
655 if (is_array($form)) {
656 if (!array_key_exists('fields', $form)) {
657 return false;
658 }
659 } else {
660 return false;
661 }
662 $name = str_replace("gform_multifile_upload_" . $form_id, "input", $name);
663 $names = explode("__", $name);
664 $names = explode("_", $names[0]);
665 $name = $names[1];
666 foreach ($form['fields'] as $field_key => $field_value) {
667 if (is_array($field_value->inputs)) {
668 foreach ($field_value->inputs as $children_id) {
669 if ($children_id["id"] == $name) {
670 return $field_value->type;
671 }
672 }
673 } else {
674 if ($field_value->id == $name) {
675 return $field_value->type;
676 }
677 }
678 }
679 return "";
680 }
681 }
682 // Register the phone field with the field framework.
683 GF_Fields::register(new Superaddons_GFRepeater_Field());
684