| @@ -154,9 +154,9 @@ | ||
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | unset( $filename ); |
| 157 | 157 | |
| 158 | - $comment_count = FrmDb::get_count( | |
| 158 | + self::$comment_count = FrmDb::get_count( | |
| 159 | 159 | 'frm_item_metas', |
| 160 | 160 | array( |
| 161 | 161 | 'item_id' => $atts['entry_ids'], |
| 162 | 162 | 'field_id' => 0, |
| @@ -167,9 +167,8 @@ | ||
| 167 | 167 | 'order_by' => 'count(*) DESC', |
| 168 | 168 | 'limit' => 1, |
| 169 | 169 | ) |
| 170 | 170 | ); |
| 171 | - self::$comment_count = $comment_count; | |
| 172 | 171 | |
| 173 | 172 | self::prepare_csv_headings(); |
| 174 | 173 | |
| 175 | 174 | /** |
| @@ -418,23 +417,24 @@ | ||
| 418 | 417 | |
| 419 | 418 | $flat = array(); |
| 420 | 419 | |
| 421 | 420 | foreach ( $headings as $key => $heading ) { |
| 422 | - if ( is_array( $heading ) ) { | |
| 423 | - $repeater_id = str_replace( 'repeater', '', $key ); | |
| 424 | - $repeater_headings = array(); | |
| 421 | + if ( ! is_array( $heading ) ) { | |
| 422 | + $flat[ $key ] = $heading; | |
| 423 | + continue; | |
| 424 | + } | |
| 425 | 425 | |
| 426 | - foreach ( $fields_by_repeater_id[ $repeater_id ] as $col ) { | |
| 427 | - $repeater_headings += self::field_headings( $col ); | |
| 428 | - } | |
| 426 | + $repeater_id = str_replace( 'repeater', '', $key ); | |
| 427 | + $repeater_headings = array(); | |
| 429 | 428 | |
| 430 | - for ( $i = 0; $i < $max[ $repeater_id ]; $i++ ) { | |
| 431 | - foreach ( $repeater_headings as $repeater_key => $repeater_name ) { | |
| 432 | - $flat[ $repeater_key . '[' . $i . ']' ] = $repeater_name; | |
| 433 | - } | |
| 429 | + foreach ( $fields_by_repeater_id[ $repeater_id ] as $col ) { | |
| 430 | + $repeater_headings += self::field_headings( $col ); | |
| 431 | + } | |
| 432 | + | |
| 433 | + for ( $i = 0; $i < $max[ $repeater_id ]; $i++ ) { | |
| 434 | + foreach ( $repeater_headings as $repeater_key => $repeater_name ) { | |
| 435 | + $flat[ $repeater_key . '[' . $i . ']' ] = $repeater_name; | |
| 434 | 436 | } |
| 435 | - } else { | |
| 436 | - $flat[ $key ] = $heading; | |
| 437 | 437 | } |
| 438 | 438 | } |
| 439 | 439 | |
| 440 | 440 | self::$fields_by_repeater_id = $fields_by_repeater_id; |
| @@ -508,9 +508,9 @@ | ||
| 508 | 508 | // Order by parent_item_id so children will be first. |
| 509 | 509 | $order_by = ' ORDER BY parent_item_id DESC'; |
| 510 | 510 | } else { |
| 511 | 511 | // When Pro is not installed, only query for direct ID matches only as we do not expect parent item id |
| 512 | - // matches and the more simplified query is faster. | |
| 512 | + // Matches and the more simplified query is faster. | |
| 513 | 513 | $where = array( |
| 514 | 514 | 'id' => $next_set, |
| 515 | 515 | ); |
| 516 | 516 | $order_by = ''; |
| @@ -521,12 +521,12 @@ | ||
| 521 | 521 | foreach ( $entries as $entry ) { |
| 522 | 522 | self::$entry = $entry; |
| 523 | 523 | unset( $entry ); |
| 524 | 524 | |
| 525 | - if ( self::$entry->form_id !== self::$form_id ) { | |
| 525 | + if ( self::$entry->form_id === self::$form_id ) { | |
| 526 | + self::prepare_csv_row(); | |
| 527 | + } else { | |
| 526 | 528 | self::add_repeat_field_values_to_csv( $entries ); |
| 527 | - } else { | |
| 528 | - self::prepare_csv_row(); | |
| 529 | 529 | } |
| 530 | 530 | } |
| 531 | 531 | } |
| 532 | 532 | |
| @@ -556,13 +556,13 @@ | ||
| 556 | 556 | * @return void |
| 557 | 557 | */ |
| 558 | 558 | private static function add_repeat_field_values_to_csv( &$entries ) { |
| 559 | 559 | if ( isset( self::$entry->metas ) ) { |
| 560 | - // add child entries to the parent | |
| 560 | + // Add child entries to the parent | |
| 561 | 561 | foreach ( self::$entry->metas as $meta_id => $meta_value ) { |
| 562 | 562 | if ( ! is_numeric( $meta_id ) || '' === $meta_value ) { |
| 563 | - // if the hook is being used to include field keys in the metas array, | |
| 564 | - // we need to skip the keys and only process field ids | |
| 563 | + // If the hook is being used to include field keys in the metas array, | |
| 564 | + // We need to skip the keys and only process field ids | |
| 565 | 565 | continue; |
| 566 | 566 | } |
| 567 | 567 | |
| 568 | 568 | if ( ! isset( $entries[ self::$entry->parent_item_id ] ) ) { |
| @@ -572,10 +572,10 @@ | ||
| 572 | 572 | |
| 573 | 573 | if ( ! isset( $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] ) ) { |
| 574 | 574 | $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] = array(); |
| 575 | 575 | } elseif ( ! is_array( $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] ) ) { |
| 576 | - // if the data is here, it should be an array but if this field has collected data | |
| 577 | - // both while inside and outside of the repeating section, it's possible this is a string. | |
| 576 | + // If the data is here, it should be an array but if this field has collected data | |
| 577 | + // Both while inside and outside of the repeating section, it's possible this is a string. | |
| 578 | 578 | $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] = (array) $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ]; |
| 579 | 579 | } |
| 580 | 580 | |
| 581 | 581 | // Add the repeated values. |
| @@ -585,9 +585,9 @@ | ||
| 585 | 585 | self::$entry->metas = self::fill_missing_repeater_metas( self::$entry->metas, $entries ); |
| 586 | 586 | $entries[ self::$entry->parent_item_id ]->metas += self::$entry->metas; |
| 587 | 587 | }//end if |
| 588 | 588 | |
| 589 | - // add the embedded form id | |
| 589 | + // Add the embedded form id | |
| 590 | 590 | if ( ! isset( $entries[ self::$entry->parent_item_id ]->embedded_fields ) ) { |
| 591 | 591 | $entries[ self::$entry->parent_item_id ]->embedded_fields = array(); |
| 592 | 592 | } |
| 593 | 593 | |
| @@ -603,11 +603,10 @@ | ||
| 603 | 603 | * |
| 604 | 604 | * @return array |
| 605 | 605 | */ |
| 606 | 606 | private static function fill_missing_repeater_metas( $metas, &$entries ) { |
| 607 | - $field_ids = array_keys( $metas ); | |
| 608 | - $field_id = end( $field_ids ); | |
| 609 | - $field = self::get_field( $field_id ); | |
| 607 | + $field_id = array_key_last( $metas ); | |
| 608 | + $field = self::get_field( $field_id ); | |
| 610 | 609 | |
| 611 | 610 | if ( ! $field || empty( $field->field_options['in_section'] ) ) { |
| 612 | 611 | return $metas; |
| 613 | 612 | } |
| @@ -618,17 +617,19 @@ | ||
| 618 | 617 | return $metas; |
| 619 | 618 | } |
| 620 | 619 | |
| 621 | 620 | foreach ( self::$fields_by_repeater_id[ $repeater_id ] as $repeater_child ) { |
| 622 | - if ( ! isset( $metas[ $repeater_child->id ] ) ) { | |
| 623 | - $metas[ $repeater_child->id ] = ''; | |
| 621 | + if ( isset( $metas[ $repeater_child->id ] ) ) { | |
| 622 | + continue; | |
| 623 | + } | |
| 624 | 624 | |
| 625 | - if ( ! isset( $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] ) || ! is_array( $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] ) ) { // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 626 | - $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] = array(); | |
| 627 | - } | |
| 625 | + $metas[ $repeater_child->id ] = ''; | |
| 628 | 626 | |
| 629 | - $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ][] = ''; | |
| 627 | + if ( ! isset( $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] ) || ! is_array( $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] ) ) { // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 628 | + $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] = array(); | |
| 630 | 629 | } |
| 630 | + | |
| 631 | + $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ][] = ''; | |
| 631 | 632 | } |
| 632 | 633 | |
| 633 | 634 | return $metas; |
| 634 | 635 | } |
| @@ -727,30 +728,32 @@ | ||
| 727 | 728 | * |
| 728 | 729 | * @return void |
| 729 | 730 | */ |
| 730 | 731 | private static function add_array_values_to_columns( &$row, $atts ) { |
| 731 | - if ( is_array( $atts['field_value'] ) ) { | |
| 732 | - foreach ( $atts['field_value'] as $key => $sub_value ) { | |
| 733 | - if ( is_array( $sub_value ) ) { | |
| 734 | - // This is combo field inside repeater. The heading key has this format: [86_first[0]]. | |
| 735 | - foreach ( $sub_value as $sub_key => $sub_sub_value ) { | |
| 736 | - $column_key = $atts['col']->id . '_' . $sub_key . '[' . $key . ']'; | |
| 732 | + if ( ! is_array( $atts['field_value'] ) ) { | |
| 733 | + return; | |
| 734 | + } | |
| 737 | 735 | |
| 738 | - if ( ! is_numeric( $sub_key ) && isset( self::$headings[ $column_key ] ) ) { | |
| 739 | - $row[ $column_key ] = $sub_sub_value; | |
| 740 | - } | |
| 736 | + foreach ( $atts['field_value'] as $key => $sub_value ) { | |
| 737 | + if ( is_array( $sub_value ) ) { | |
| 738 | + // This is combo field inside repeater. The heading key has this format: [86_first[0]]. | |
| 739 | + foreach ( $sub_value as $sub_key => $sub_sub_value ) { | |
| 740 | + $column_key = $atts['col']->id . '_' . $sub_key . '[' . $key . ']'; | |
| 741 | + | |
| 742 | + if ( ! is_numeric( $sub_key ) && isset( self::$headings[ $column_key ] ) ) { | |
| 743 | + $row[ $column_key ] = $sub_sub_value; | |
| 741 | 744 | } |
| 745 | + } | |
| 742 | 746 | |
| 743 | - continue; | |
| 744 | - } | |
| 747 | + continue; | |
| 748 | + } | |
| 745 | 749 | |
| 746 | - $column_key = $atts['col']->id . '_' . $key; | |
| 750 | + $column_key = $atts['col']->id . '_' . $key; | |
| 747 | 751 | |
| 748 | - if ( ! is_numeric( $key ) && isset( self::$headings[ $column_key ] ) ) { | |
| 749 | - $row[ $column_key ] = $sub_value; | |
| 750 | - } | |
| 752 | + if ( ! is_numeric( $key ) && isset( self::$headings[ $column_key ] ) ) { | |
| 753 | + $row[ $column_key ] = $sub_value; | |
| 751 | 754 | } |
| 752 | - }//end if | |
| 755 | + } | |
| 753 | 756 | } |
| 754 | 757 | |
| 755 | 758 | /** |
| 756 | 759 | * @param array $row |
| @@ -801,9 +804,9 @@ | ||
| 801 | 804 | } |
| 802 | 805 | } |
| 803 | 806 | |
| 804 | 807 | if ( is_array( $row ) ) { |
| 805 | - // implode the repeated field values | |
| 808 | + // Implode the repeated field values | |
| 806 | 809 | $row = implode( self::$separator, FrmAppHelper::array_flatten( $row, 'reset' ) ); |
| 807 | 810 | } |
| 808 | 811 | |
| 809 | 812 | $val = self::encode_value( $row ); |
| @@ -843,9 +846,9 @@ | ||
| 843 | 846 | $convmap = false; |
| 844 | 847 | |
| 845 | 848 | switch ( self::$to_encoding ) { |
| 846 | 849 | case 'macintosh': |
| 847 | - // this map was derived from the differences between the MacRoman and UTF-8 Charsets | |
| 850 | + // This map was derived from the differences between the MacRoman and UTF-8 Charsets | |
| 848 | 851 | // Reference: |
| 849 | 852 | // http://www.alanwood.net/demos/macroman.html. |
| 850 | 853 | $convmap = array( 256, 304, 0, 0xffff, 306, 337, 0, 0xffff, 340, 375, 0, 0xffff, 377, 401, 0, 0xffff, 403, 709, 0, 0xffff, 712, 727, 0, 0xffff, 734, 936, 0, 0xffff, 938, 959, 0, 0xffff, 961, 8210, 0, 0xffff, 8213, 8215, 0, 0xffff, 8219, 8219, 0, 0xffff, 8227, 8229, 0, 0xffff, 8231, 8239, 0, 0xffff, 8241, 8248, 0, 0xffff, 8251, 8259, 0, 0xffff, 8261, 8363, 0, 0xffff, 8365, 8481, 0, 0xffff, 8483, 8705, 0, 0xffff, 8707, 8709, 0, 0xffff, 8711, 8718, 0, 0xffff, 8720, 8720, 0, 0xffff, 8722, 8729, 0, 0xffff, 8731, 8733, 0, 0xffff, 8735, 8746, 0, 0xffff, 8748, 8775, 0, 0xffff, 8777, 8799, 0, 0xffff, 8801, 8803, 0, 0xffff, 8806, 9673, 0, 0xffff, 9675, 63742, 0, 0xffff, 63744, 64256, 0, 0xffff ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong |
| 851 | 854 | break; |