Skip to content

Commit

Permalink
feat: added custom field mapping to CalDAV
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars- committed Nov 19, 2024
1 parent 64377df commit f1b882b
Showing 1 changed file with 118 additions and 9 deletions.
127 changes: 118 additions & 9 deletions Http/Controllers/LJPcCalendarModuleAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ public function getEvents( Request $request ) {

$calendarEvents = $calendar->events( $start, $end );
foreach ( $calendarEvents as $event ) {
// Generate mapping for used custom fields
if ( isset( $event['custom_fields'] ) && is_array( $event['custom_fields'] ) ) {
$event['custom_fields_mapping'] = $this->generateCustomFieldMapping(
$calendar->custom_fields,
$event['custom_fields']
);
}
$events[] = $event;
}
}
Expand All @@ -303,6 +310,14 @@ public function getEvents( Request $request ) {
return response()->json( $events );
}

/**
* Update an event
*
* @param Request $request
*
* @return JsonResponse
* @throws Exception
*/
/**
* Update an event
*
Expand Down Expand Up @@ -377,6 +392,7 @@ public function updateEvent( Request $request ) {
if ( isset( $customFieldsData['conversation_id'] ) ) {
$processedCustomFields['conversation_id'] = $customFieldsData['conversation_id'];
}

if ( count( $processedCustomFields ) > 0 ) {
if ( ! is_array( $validatedData['body'] ) ) {
$validatedData['body'] = [ 'body' => $validatedData['body'] ];
Expand All @@ -386,15 +402,34 @@ public function updateEvent( Request $request ) {
$validatedData['body']['custom_fields'] = [];
}

$validatedData['body']['custom_fields'] = array_merge( $validatedData['body']['custom_fields'], $processedCustomFields );
$validatedData['body']['custom_fields'] = array_merge(
$validatedData['body']['custom_fields'],
$processedCustomFields
);

// Add mapping for used custom fields
$validatedData['body']['custom_fields_mapping'] = $this->generateCustomFieldMapping(
$calendar->custom_fields,
$processedCustomFields
);
}

$response = $caldavClient->updateEvent( $remainingUrl, $validatedData['uid'], $validatedData['title'], $validatedData['body'], $start, $end, $validatedData['location'] );
$response = $caldavClient->updateEvent(
$remainingUrl,
$validatedData['uid'],
$validatedData['title'],
$validatedData['body'],
$start,
$end,
$validatedData['location']
);

if ( $response['statusCode'] < 200 || $response['statusCode'] > 300 ) {
Log::error( 'Error updating event', $response );

return response()->json( [ 'error' => 'Error updating event' ], 500 );
}

$calendar->getExternalContent( true );
}

Expand Down Expand Up @@ -454,6 +489,29 @@ public function deleteEvent( Request $request ) {
return response()->json( [ 'ok' => true, 'message' => 'Event deleted successfully' ] );
}

/**
* Helper function to generate custom field mapping
*
* @param array $customFields Array of all possible custom fields
* @param array $usedCustomFields Array of used custom field values
*
* @return array Mapping of used custom field IDs to their names
*/
private function generateCustomFieldMapping( array $customFields, array $usedCustomFields ): array {
$mapping = [];
$fields = $customFields['fields'] ?? [];

foreach ( $fields as $field ) {
$fieldId = 'custom_field_' . $field['id'];
// Only include fields that are actually used in the item
if ( isset( $usedCustomFields[ $fieldId ] ) ) {
$mapping[ $fieldId ] = $field['name'];
}
}

return $mapping;
}

/**
* Create an event
*
Expand Down Expand Up @@ -515,6 +573,7 @@ public function createEvent( Request $request ) {
$calendarItem->location = $validatedData['location'] ?? '';
$calendarItem->body = $validatedData['body'] ?? '';
$calendarItem->custom_fields = $processedCustomFields;

$calendarItem->save();
} else if ( $calendar->type === 'caldav' ) {
$fullUrl = $calendar->custom_fields['url'];
Expand All @@ -527,14 +586,32 @@ public function createEvent( Request $request ) {
if ( ! is_array( $validatedData['body'] ) ) {
$validatedData['body'] = [ 'body' => empty( $validatedData['body'] ) ? '-' : $validatedData['body'] ];
}

$validatedData['body']['custom_fields'] = $processedCustomFields;

$response = $caldavClient->createEvent( $remainingUrl, $uid, $validatedData['title'], $validatedData['body'], $start, $end, $isAllDay, $validatedData['location'] );
// Add mapping for used custom fields
$validatedData['body']['custom_fields_mapping'] = $this->generateCustomFieldMapping(
$calendar->custom_fields,
$processedCustomFields
);

$response = $caldavClient->createEvent(
$remainingUrl,
$uid,
$validatedData['title'],
$validatedData['body'],
$start,
$end,
$isAllDay,
$validatedData['location']
);

if ( $response['statusCode'] < 200 || $response['statusCode'] > 300 ) {
Log::error( 'Error creating event', $response );

return response()->json( [ 'error' => 'Error creating event' ], 500 );
}

$calendar->getExternalContent( true );
}

Expand Down Expand Up @@ -600,13 +677,24 @@ public function createEventFromConversation( int $conversation, Request $request
$calendarItem->is_all_day = $isAllDay;
$calendarItem->location = $validatedData['location'] ?? '';
$calendarItem->body = $validatedData['body'] ?? '';
$customFields = $calendarItem->custom_fields;

// Merge all custom fields
$customFields = $calendarItem->custom_fields;
if ( ! is_array( $customFields ) ) {
$customFields = [];
}
$customFields['conversation_id'] = $conversation;
$customFields['author_id'] = auth()->user()->id;
$calendarItem->custom_fields = array_merge( $customFields, $processedCustomFields );
$mergedCustomFields = array_merge( $customFields, [
'conversation_id' => $conversation,
'author_id' => auth()->user()->id,
], $processedCustomFields );

$calendarItem->custom_fields = $mergedCustomFields;

// Add mapping for used custom fields
$calendarItem->custom_fields_mapping = $this->generateCustomFieldMapping(
$calendar->custom_fields,
$mergedCustomFields
);

$calendarItem->save();

Expand All @@ -622,17 +710,38 @@ public function createEventFromConversation( int $conversation, Request $request
if ( ! is_array( $validatedData['body'] ) ) {
$validatedData['body'] = [ 'body' => empty( $validatedData['body'] ) ? '-' : $validatedData['body'] ];
}
$validatedData['body']['custom_fields'] = array_merge( [

// Merge all custom fields
$mergedCustomFields = array_merge( [
'conversation_id' => $conversation,
'author_id' => auth()->user()->id,
], $processedCustomFields );

$response = $caldavClient->createEvent( $remainingUrl, $uid, $validatedData['title'], $validatedData['body'], $start, $end, $isAllDay, $validatedData['location'] );
$validatedData['body']['custom_fields'] = $mergedCustomFields;

// Add mapping for used custom fields
$validatedData['body']['custom_fields_mapping'] = $this->generateCustomFieldMapping(
$calendar->custom_fields,
$mergedCustomFields
);

$response = $caldavClient->createEvent(
$remainingUrl,
$uid,
$validatedData['title'],
$validatedData['body'],
$start,
$end,
$isAllDay,
$validatedData['location']
);

if ( $response['statusCode'] < 200 || $response['statusCode'] > 300 ) {
Log::error( 'Error creating event', $response );

return response()->json( [ 'error' => 'Error creating event' ], 500 );
}

$calendar->getExternalContent( true );
}

Expand Down

0 comments on commit f1b882b

Please sign in to comment.