Skip to content
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ Forms Bridge has the following add-ons:

**🗓️ Productivity**

- [Airtable](https://formsbridge.codeccoop.org/documentation/airtable)
- [Airtable](https://formsbridge.codeccoop.org/documentation/airtable/)
- [Google Calendar](https://formsbridge.codeccoop.org/documentation/google-calendar/)
- [Google Sheets](https://formsbridge.codeccoop.org/documentation/google-sheets/)
- [Grist](https://formsbridge.codeccoop.org/documentation/grist/)
- [Nextcloud](https://formsbridge.codeccoop.org/documentation/nextcloud/)

**📨 Messaging & Collaboration**
Expand Down
72 changes: 22 additions & 50 deletions forms-bridge/addons/airtable/class-airtable-addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,16 @@ class Airtable_Addon extends Addon {
* @return boolean
*/
public function ping( $backend ) {
$bridge = new Airtable_Form_Bridge(
array(
'name' => '__airtable-' . time(),
'backend' => $backend,
'endpoint' => '/v0/meta/bases',
'method' => 'GET',
)
);
$backend = FBAPI::get_backend( $backend );

$response = $bridge->submit();
if ( ! $backend ) {
Logger::log( 'Airtable backend ping error: Backend is unkown or invalid', Logger::ERROR );
return false;
}

$response = $backend->get( '/v0/meta/bases' );
if ( is_wp_error( $response ) ) {
Logger::log( 'Airtable backend ping error: Unable to recover the credential access token', Logger::ERROR );
Logger::log( 'Airtable backend ping error: Unable to list airtable bases', Logger::ERROR );
return false;
}

Expand All @@ -78,25 +76,24 @@ public function ping( $backend ) {
* @return array|WP_Error
*/
public function fetch( $endpoint, $backend ) {
$bridge = new Airtable_Form_Bridge(
array(
'name' => '__airtable-meta-bases',
'backend' => $backend,
'endpoint' => '/v0/meta/bases',
'method' => 'GET',
),
);
$backend = FBAPI::get_backend( $backend );
if ( ! $backend ) {
return new WP_Error( 'invalid_backend', 'Backend is unkown or invalid', array( 'backend' => $backend ) );
}

$response = $bridge->submit();
if ( $endpoint && '/v0/meta/tables' !== $endpoint ) {
return $backend->get( $endpoint );
}

$response = $backend->get( '/v0/meta/bases' );

if ( is_wp_error( $response ) ) {
return $response;
}

$tables = array();
foreach ( $response['data']['bases'] as $base ) {
$schema_response = $bridge->patch( array( 'endpoint' => "/v0/meta/bases/{$base['id']}/tables" ) )
->submit();
$schema_response = $backend->get( "/v0/meta/bases/{$base['id']}/tables" );

if ( is_wp_error( $schema_response ) ) {
return $schema_response;
Expand Down Expand Up @@ -172,45 +169,20 @@ public function get_endpoint_schema( $endpoint, $backend, $method = null ) {

$schema = array();
foreach ( $fields as $field ) {
if (
in_array(
$field['type'],
array(
'aiText',
'formula',
'autoNumber',
'button',
'count',
'createdBy',
'createdTime',
'lastModifiedBy',
'lastModifiedTime',
'rollup',
'externalSyncSource',
'multipleCollaborators',
'multipleLookupValues',
'multipleRecordLinks',
),
true,
)
) {
continue;
}

switch ( $field['type'] ) {
case 'rating':
case 'number':
$type = 'number';
break;
case 'checkbox':
$type = 'boolean';
break;
case 'multipleSelects':
$type = 'array';
case 'select':
$type = $field['is_multi'] ? 'array' : 'string';
break;
case 'multipleAttachments':
case 'file':
$type = 'file';
break;
case 'textarea':
default:
$type = 'string';
break;
Expand Down
105 changes: 92 additions & 13 deletions forms-bridge/addons/airtable/class-airtable-form-bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ public function get_fields() {
return new WP_Error( 'invalid_bridge', 'The bridge is invalid', $this->data );
}

$backend = $this->backend;
if ( ! $backend ) {
return new WP_Error( 'invalid_backend', 'The bridge backend is unkown or invalid', $this->data );
}

$base_id = $this->base_id();
$table_id = $this->table_id();

if ( ! $base_id || ! $table_id ) {
return new WP_Error( 'invalid_endpoint', 'The bridge has an invalid endpoint', $this->data );
return new WP_Error( 'invalid_endpoint', 'The bridge has an invalid endpoint', $this->data );
}

$response = $this->patch(
array(
'method' => 'GET',
'endpoint' => "/v0/meta/bases/{$base_id}/tables",
)
)->submit();
$endpoint = "/v0/meta/bases/{$base_id}/tables";
$response = $backend->get( $endpoint );

if ( is_wp_error( $response ) ) {
return $response;
Expand All @@ -97,7 +98,81 @@ public function get_fields() {
return new WP_Error( 'not_found', 'Table not found', $this->data );
}

return $table['fields'];
$fields = array();
foreach ( $table['fields'] as $air_field ) {
if (
in_array(
$air_field['type'],
array(
'aiText',
'formula',
'autoNumber',
'button',
'count',
'createdBy',
'createdTime',
'lastModifiedBy',
'lastModifiedTime',
'rollup',
'externalSyncSource',
'multipleCollaborators',
'multipleLookupValues',
'multipleRecordLinks',
),
true,
)
) {
continue;
}

$field = array(
'id' => $air_field['id'],
'name' => $air_field['name'],
'label' => $air_field['name'],
);

switch ( $air_field['type'] ) {
case 'multipleAttachments':
$field['type'] = 'file';
$field['is_multi'] = true;
break;
case 'rating':
case 'number':
$field['type'] = 'number';
break;
case 'checkbox':
$field['type'] = 'checkbox';
break;
case 'multipleSelects':
case 'singleSelect':
$field['type'] = 'select';
$field['options'] = array_map(
function ( $choice ) {
return array(
'value' => $choice['name'],
'label' => $choice['name'],
);
},
$air_field['options']['choices'],
);

$field['is_multi'] = 'multipleSelects' === $air_field['type'];
break;
case 'date':
$field['type'] = 'date';
break;
case 'multilineText':
$field['type'] = 'textarea';
break;
default:
$field['type'] = 'text';
break;
}

$fields[] = $field;
}

return $fields;
}

/**
Expand Down Expand Up @@ -136,7 +211,7 @@ public function submit( $payload = array(), $attachments = array() ) {

$l = count( $fields );
for ( $i = 0; $i < $l; ++$i ) {
if ( 'multipleAttachments' === $fields[ $i ]['type'] ) {
if ( 'file' === $fields[ $i ]['type'] ) {
$attachment_field = $fields[ $i ];
$attachment_name = $attachment_field['name'];

Expand Down Expand Up @@ -170,7 +245,11 @@ function ( $name ) use ( $attachment_name ) {
$field_name = $data_field['name'];

if ( isset( $payload[ $field_name ] ) ) {
if ( 'multipleSelects' === $data_field['type'] && ! is_array( $payload[ $field_name ] ) ) {
if (
'select' === $data_field['type']
&& $data_field['is_multi']
&& ! is_array( $payload[ $field_name ] )
) {
$payload[ $field_name ] = array( $payload[ $field_name ] );
}

Expand Down Expand Up @@ -204,7 +283,7 @@ function ( $name ) use ( $attachment_name ) {
$filename = basename( $path );
$filetype = wp_check_filetype( $path );
if ( empty( $filetype['type'] ) ) {
$filetype['type'] = mime_content_type( $path );
$filetype['type'] = mime_content_type( $path ) ?: 'octet/stream';
}
}
}
Expand All @@ -217,10 +296,10 @@ function ( $name ) use ( $attachment_name ) {
)->post(
"/v0/{$base_id}/{$record_id}/{$attachment['id']}/uploadAttachment",
array(
'contentType' => $filetype['type'],
'contentType' => $filetype['type'] ?? 'octet/stream',
'file' => $attachment['file'],
'filename' => $filename,
)
),
);

if ( is_wp_error( $upload_response ) ) {
Expand Down
Loading