SHEET_UPDATERECORD::update_row_google_sheet( $user_id,  $action_data,  $recipe_id,  $args )

Validation function when the action is hit


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)

$args

(Required)


Source Source

File: src/integrations/google-sheet/actions/sheet-updaterecord.php

	public function update_row_google_sheet( $user_id, $action_data, $recipe_id, $args ) {

		$gs_spreadsheet     = $action_data['meta']['GSSPREADSHEET'];
		$gs_worksheet       = $action_data['meta']['GSWORKSHEET'];
		$lookup_field       = $action_data['meta']['GSWORKSHEETCOLUMN'];
		$lookup_field_value = Automator()->parse->text( $action_data['meta']['GSWORKSHEET_SOURCE_VALUE'], $recipe_id, $user_id, $args );
		$worksheet_field    = $action_data['meta']['WORKSHEET_FIELDS'];
		$fields             = json_decode( $worksheet_field, true );

		$key_values      = array();
		$check_all_empty = true;
		$hashed          = sha1( Google_Sheet_Helpers::$hash_string );
		$sheet_id        = substr( $hashed, 0, 9 );

		if ( (string) $gs_worksheet === (string) $sheet_id || intval( '-1' ) === intval( $gs_worksheet ) ) {
			$gs_worksheet = 0;
		}

		$fields_count = count( $fields );

		for ( $i = 0; $i < $fields_count; $i ++ ) {
			$key                = $fields[ $i ]['GS_COLUMN_NAME'];
			$value              = Automator()->parse->text( $fields[ $i ]['GS_COLUMN_VALUE'], $recipe_id, $user_id, $args );
			$key_values[ $key ] = $value;
			if ( ! empty( $value ) ) {
				$check_all_empty = false;
			}
		}

		// Process the update.
		$sheet = $action_data['meta']['GSWORKSHEET_readable'];

		$lookup_field_parts = explode( '-', $lookup_field );

		$selected_column_range = $sheet . '!' . $lookup_field_parts[1];

		$helper = Automator()->helpers->recipe->google_sheet->options;

		try {

			$range_values = $helper->api_get_range_values( $gs_spreadsheet, $selected_column_range );

			$existing_rows = array();

			if ( isset( $range_values['data']['values'] ) && isset( $range_values['data']['range'] ) ) {
				$existing_rows[ $range_values['data']['range'] ] = $range_values['data']['values'];
			}

			$matched_range = $this->match_range( $existing_rows, $sheet, $selected_column_range, $lookup_field_value );

			$row_values = array();

			foreach ( $fields as $field ) {

				$cell_value = null; // Pass null to avoid overwriting the cell value.

				if ( true === $field['COLUMN_UPDATE'] ) {
					$cell_value = Automator()->parse->text( $field['GS_COLUMN_VALUE'], $recipe_id, $user_id, $args );
				}

				// Add the value to our request body.
				$row_values[] = $cell_value;

			}

			$response = $helper->api_update_row( $gs_spreadsheet, $matched_range, $row_values, $action_data );

			// Complete the action if there are no issues.
			Automator()->complete_action( $user_id, $action_data, $recipe_id );

			return;

		} catch ( \Exception $e ) {
			return $this->complete_with_errors( $user_id, $action_data, $recipe_id, $e->getMessage() );
		}

	}