diff --git a/example/google-sheets/westeros-houses/register.php b/example/google-sheets/westeros-houses/register.php index 63cd4a08..122759e6 100644 --- a/example/google-sheets/westeros-houses/register.php +++ b/example/google-sheets/westeros-houses/register.php @@ -76,23 +76,8 @@ function register_westeros_houses_block(): void { ], ], ], - 'preprocess_response' => function ( mixed $response_data ) use ( $columns ): array { - if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) { - $values = $response_data['values']; - array_shift( $values ); // Drop the first row - - $response_data['values'] = array_map( - function ( $row, $index ) use ( $columns ) { - $combined = array_combine( $columns, $row ); - $combined['RowId'] = $index + 1; // Add row_id field, starting from 1 - return $combined; - }, - $values, - array_keys( $values ) - ); - } - - return $response_data; + 'preprocess_response' => function ( mixed $response_data ): array { + return GoogleSheetsDataSource::preprocess_list_response( $response_data ); }, ] ); @@ -139,20 +124,8 @@ function ( $row, $index ) use ( $columns ) { ], ], ], - 'preprocess_response' => function ( mixed $response_data, array $input_variables ) use ( $columns ): array { - $selected_row = null; - $row_id = $input_variables['row_id']; - - if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) { - $raw_selected_row = $response_data['values'][ $row_id ]; - if ( is_array( $raw_selected_row ) ) { - $selected_row = array_combine( $columns, $raw_selected_row ); - $selected_row = array_combine( $columns, $selected_row ); - $selected_row['RowId'] = $row_id; - } - } - - return $selected_row; + 'preprocess_response' => function ( mixed $response_data, array $input_variables ): array { + return GoogleSheetsDataSource::preprocess_get_response( $response_data, $input_variables ); }, ] ); diff --git a/inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php b/inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php index 43229040..191b3d0b 100644 --- a/inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php +++ b/inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php @@ -3,6 +3,7 @@ namespace RemoteDataBlocks\Integrations\Google\Sheets; use RemoteDataBlocks\Config\DataSource\HttpDataSource; +use RemoteDataBlocks\Config\Query\HttpQuery; use RemoteDataBlocks\Integrations\Google\Auth\GoogleAuth; use RemoteDataBlocks\Validation\Types; @@ -65,4 +66,122 @@ protected static function map_service_config( array $service_config ): array { }, ]; } + + public function ___temp_get_query(): HttpQuery { + $service_config = $this->config['service_config']; + + $input_schema = [ + 'row_id' => [ + 'name' => 'Row ID', + 'type' => 'id', + ], + ]; + + $output_schema = [ + 'is_collection' => false, + 'type' => [ + 'row_id' => [ + 'name' => 'Row ID', + 'path' => '$.RowId', + 'type' => 'id', + ], + ], + ]; + + + foreach ( $service_config['sheets'][0]['output_query_mappings'] as $mapping ) { + $mapping_key = $mapping['key']; + $output_schema['type'][ $mapping_key ] = [ + 'name' => $mapping['name'] ?? $mapping_key, + 'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]', + 'type' => $mapping['type'] ?? 'string', + ]; + } + + return HttpQuery::from_array( [ + 'data_source' => $this, + 'endpoint' => function (): string { + return $this->get_endpoint() . '/values/' . $this->config['service_config']['sheets'][0]['name']; + }, + 'input_schema' => $input_schema, + 'output_schema' => $output_schema, + 'preprocess_response' => function ( mixed $response_data, array $input_variables ): array { + return GoogleSheetsDataSource::preprocess_get_response( $response_data, $input_variables ); + }, + ] ); + } + + public function ___temp_get_list_query(): HttpQuery { + $service_config = $this->config['service_config']; + + $output_schema = [ + 'is_collection' => true, + 'path' => '$.values[*]', + 'type' => [ + 'row_id' => [ + 'name' => 'Row ID', + 'path' => '$.RowId', + 'type' => 'id', + ], + ], + ]; + + + foreach ( $service_config['sheets'][0]['output_query_mappings'] as $mapping ) { + $mapping_key = $mapping['key']; + $output_schema['type'][ $mapping_key ] = [ + 'name' => $mapping['name'] ?? $mapping_key, + 'path' => $mapping['path'] ?? '$.fields["' . $mapping_key . '"]', + 'type' => $mapping['type'] ?? 'string', + ]; + } + + return HttpQuery::from_array( [ + 'data_source' => $this, + 'endpoint' => function (): string { + return $this->get_endpoint() . '/values/' . $this->config['service_config']['sheets'][0]['name']; + }, + 'input_schema' => [], + 'output_schema' => $output_schema, + 'preprocess_response' => function ( mixed $response_data, array $input_variables ): array { + return GoogleSheetsDataSource::preprocess_list_response( $response_data ); + }, + ] ); + } + + public static function preprocess_list_response( array $response_data ): array { + if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) { + $values = $response_data['values']; + $columns = array_shift( $values ); // Get column names from first row + + $response_data['values'] = array_map( + function ( $row, $index ) use ( $columns ) { + $combined = array_combine( $columns, $row ); + $combined['RowId'] = $index + 1; // Add row_id field, starting from 1 + return $combined; + }, + $values, + array_keys( $values ) + ); + } + + return $response_data; + } + + public static function preprocess_get_response( array $response_data, array $input_variables ): array { + $selected_row = null; + $row_id = $input_variables['row_id']; + + if ( isset( $response_data['values'] ) && is_array( $response_data['values'] ) ) { + $values = $response_data['values']; + $columns = array_shift( $values ); // Get column names from first row + $raw_selected_row = $values[ $row_id - 1 ]; + if ( is_array( $raw_selected_row ) ) { + $selected_row = array_combine( $columns, $raw_selected_row ); + $selected_row['RowId'] = $row_id; + } + } + + return $selected_row; + } } diff --git a/inc/Integrations/Google/Sheets/GoogleSheetsIntegration.php b/inc/Integrations/Google/Sheets/GoogleSheetsIntegration.php new file mode 100644 index 00000000..c070974e --- /dev/null +++ b/inc/Integrations/Google/Sheets/GoogleSheetsIntegration.php @@ -0,0 +1,54 @@ + $data_source->get_display_name(), + 'render_query' => [ + 'query' => $data_source->___temp_get_query(), + ], + 'selection_queries' => [ + [ + 'query' => $data_source->___temp_get_list_query(), + 'type' => 'list', + ], + ], + ], + $block_overrides + ) + ); + } + + public static function register_loop_block_for_google_sheets_data_source( GoogleSheetsDataSource $data_source, array $block_overrides = [] ): void { + register_remote_data_block( + array_merge( + [ + 'title' => sprintf( '%s Loop', $data_source->get_display_name() ), + 'render_query' => [ + 'loop' => true, + 'query' => $data_source->___temp_get_list_query(), + ], + ], + $block_overrides + ) + ); + } +} diff --git a/remote-data-blocks.php b/remote-data-blocks.php index 05f23e33..6bdd7a91 100644 --- a/remote-data-blocks.php +++ b/remote-data-blocks.php @@ -45,6 +45,7 @@ // Integrations Integrations\Airtable\AirtableIntegration::init(); +Integrations\Google\Sheets\GoogleSheetsIntegration::init(); Integrations\Shopify\ShopifyIntegration::init(); Integrations\SalesforceB2C\SalesforceB2CIntegration::init(); Integrations\VipBlockDataApi\VipBlockDataApi::init();