Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for subfields #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ add_action( 'gk-gravityexport-combined-entries-mapping', function ( array $mappi

## Changelog

### Unreleased
* Bugfix: Fields with subfields weren't mapped properly.
* Bugfix: Sorting could have an ambiguous field name, and therefor fail.

### 0.1.0 on December 8, 2022

* Initial release
32 changes: 26 additions & 6 deletions class-combined-entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ private function get_all_form_ids(): array {
* Add the entries from the combined forms into the entries stream.
*/
private function add_combined_entries(
int $form_id,
?int $form_id,
?int $feed_id,
array $search_criteria,
array $sorting,
array $paging
): array {
$this->should_sort = true; // make sure to sort by form first on the query.
// make sure to sort by form first on the query.
$this->should_sort = apply_filters( 'gk/gravityexport/combined/should-sort', true );

return array_map(
\Closure::fromCallable( [ $this, 'remap_entry' ] ),
Expand All @@ -103,7 +104,7 @@ private function sort_by_form_id_first( array $sql_parts ): array {
if ( $this->should_sort ) {
$sql_parts['order'] = str_replace(
'ORDER BY',
sprintf( 'ORDER BY FIND_IN_SET(form_id, \'%s\'),', implode( ',', $this->get_all_form_ids() ) ),
sprintf( 'ORDER BY FIND_IN_SET(`t1`.`form_id`, \'%s\'),', implode( ',', $this->get_all_form_ids() ) ),
$sql_parts['order']
);

Expand All @@ -123,13 +124,12 @@ private function remap_entry( array $entry ): array {

$values = [];
foreach ( $entry as $key => $value ) {
if ( ! is_int( $key ) ) {
if ( ! is_numeric( $key ) ) {
// We only care about integers because those are the fields.
continue;
}

if ( null !== $new_key = $this->mapping[ $this->form_id ][ $entry['form_id'] ][ $key ] ?? null ) {
// Keep track of the values for the mapped fields on their mapped id.
if ( null !== $new_key = $this->get_mapping( $entry['form_id'], $key ) ) {
$values[ $new_key ] = $value;
}

Expand All @@ -144,4 +144,24 @@ private function remap_entry( array $entry ): array {

return $entry;
}

/**
* Whether the field id is mapped.
*
* @param int|string $form_id
* @param int|string $field_id
*
* @return string|null The mapped (sub) field id.
*/
private function get_mapping( $form_id = 0, $field_id = 0 ): ?string {
$mapped_id = $this->mapping[ $this->form_id ][ $form_id ][ (int) $field_id ] ?? null;
if ( ! $mapped_id ) {
return null;
}
// In case the field id is a subfield.
$parts = explode( '.', $field_id );
$parts[0] = $mapped_id;

return implode( '.', $parts );
}
}
2 changes: 1 addition & 1 deletion gravityexport-combined-entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Plugin Name: GravityExport Combined Entries
* Description: Adds ability to merge entries of multiple forms into one result set with GravityExport.
* Version: 0.1.0
* Version: 0.1.1
* Author: GravityKit
* Author URI: https://www.gravitykit.com
* License: GPLv2 or later
Expand Down