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/Airtable/AirtableIntegration.php b/inc/Integrations/Airtable/AirtableIntegration.php index e6a58830..04e64ee3 100644 --- a/inc/Integrations/Airtable/AirtableIntegration.php +++ b/inc/Integrations/Airtable/AirtableIntegration.php @@ -10,7 +10,9 @@ public static function init(): void { } public static function register_blocks(): void { - $data_source_configs = DataSourceCrud::get_configs_by_service( REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE ); + $data_source_configs = DataSourceCrud::get_configs_by_service( + REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE + ); foreach ( $data_source_configs as $config ) { $data_source = AirtableDataSource::from_array( $config ); @@ -18,7 +20,10 @@ public static function register_blocks(): void { } } - public static function register_block_for_airtable_data_source( AirtableDataSource $data_source, array $block_overrides = [] ): void { + public static function register_block_for_airtable_data_source( + AirtableDataSource $data_source, + array $block_overrides = [] + ): void { register_remote_data_block( array_merge( [ @@ -38,7 +43,10 @@ public static function register_block_for_airtable_data_source( AirtableDataSour ); } - public static function register_loop_block_for_airtable_data_source( AirtableDataSource $data_source, array $block_overrides = [] ): void { + public static function register_loop_block_for_airtable_data_source( + AirtableDataSource $data_source, + array $block_overrides = [] + ): void { register_remote_data_block( array_merge( [ diff --git a/inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php b/inc/Integrations/Google/Sheets/GoogleSheetsDataSource.php index 43229040..fbe48e4e 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; @@ -51,7 +52,10 @@ protected static function get_service_config_schema(): array { protected static function map_service_config( array $service_config ): array { return [ 'display_name' => $service_config['display_name'], - 'endpoint' => sprintf( 'https://sheets.googleapis.com/v4/spreadsheets/%s', $service_config['spreadsheet']['id'] ), + 'endpoint' => sprintf( + 'https://sheets.googleapis.com/v4/spreadsheets/%s', + $service_config['spreadsheet']['id'] + ), 'request_headers' => function () use ( $service_config ): array { $access_token = GoogleAuth::generate_token_from_service_account_key( $service_config['credentials'], @@ -65,4 +69,120 @@ 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 { + 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..dbd39ee5 --- /dev/null +++ b/inc/Integrations/Google/Sheets/GoogleSheetsIntegration.php @@ -0,0 +1,64 @@ + $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 a2447cc2..73a2cc3a 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();