PluginProbe
Repeater Fields for Gravity Forms / 2.1.0
Repeater Fields for Gravity Forms v2.1.0
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.1.0, at fields/repeater_field.php

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