From b380870b34af358e68cdce55c1b21e29a00a120c Mon Sep 17 00:00:00 2001 From: Lars Jansen Date: Mon, 9 Dec 2024 13:56:57 +0100 Subject: [PATCH] fix: delete button --- app/Helpers/ConversationActionButtons.php | 192 +++++++++++++++++++ app/Helpers/ConversationActions.php | 173 ----------------- resources/views/conversations/view.blade.php | 125 ++++++------ 3 files changed, 263 insertions(+), 227 deletions(-) create mode 100644 app/Helpers/ConversationActionButtons.php delete mode 100644 app/Helpers/ConversationActions.php diff --git a/app/Helpers/ConversationActionButtons.php b/app/Helpers/ConversationActionButtons.php new file mode 100644 index 000000000..9c07511db --- /dev/null +++ b/app/Helpers/ConversationActionButtons.php @@ -0,0 +1,192 @@ + [ + 'icon' => 'glyphicon-share-alt', + 'location' => self::LOCATION_TOOLBAR, + 'label' => __('Reply'), + 'permission' => function ($conversation) { + return ( ! $conversation->isPhone() || ( $conversation->customer && $conversation->customer->getMainEmail() ) ) + && \Eventy::filter('conversation.reply_button.enabled', true, $conversation); + }, + 'class' => 'conv-reply', + 'fixed_location' => true, + ], + 'note' => [ + 'icon' => 'glyphicon-edit', + 'location' => self::LOCATION_TOOLBAR, + 'label' => __('Note'), + 'permission' => function ($conversation) { + return \Eventy::filter('conversation.note_button.enabled', true, $conversation); + }, + 'class' => 'conv-add-note', + 'fixed_location' => true, + ], + 'delete' => [ + 'icon' => 'glyphicon-trash', + 'location' => self::LOCATION_TOOLBAR, + 'label' => $conversation->state != \App\Conversation::STATE_DELETED ? __('Delete') : __('Delete Forever'), + 'permission' => function ($conversation, $user) { + return $user->can('delete', $conversation); + }, + 'class' => $conversation->state != \App\Conversation::STATE_DELETED ? 'conv-delete' : 'conv-delete-forever', + 'fixed_location' => true, + 'show_in_both' => true, + ], + 'delete_mobile' => [ + 'icon' => 'glyphicon-trash', + 'location' => self::LOCATION_DROPDOWN, + 'label' => $conversation->state != \App\Conversation::STATE_DELETED ? __('Delete') : __('Delete Forever'), + 'permission' => function ($conversation, $user) { + return $user->can('delete', $conversation); + }, + 'class' => $conversation->state != \App\Conversation::STATE_DELETED ? 'conv-delete' : 'conv-delete-forever', + 'fixed_location' => true, + 'mobile_only' => true, + ], + + // Default dropdown actions + 'follow' => [ + 'icon' => 'glyphicon-bell', + 'location' => self::LOCATION_DROPDOWN, + 'label' => __('Follow'), + 'permission' => function () { + return true; + }, + 'class' => 'conv-follow', + 'has_opposite' => true, + 'opposite' => [ + 'label' => __('Unfollow'), + 'class' => 'conv-follow', + ], + 'fixed_location' => true, + ], + 'forward' => [ + 'icon' => 'glyphicon-arrow-right', + 'location' => self::LOCATION_DROPDOWN, + 'label' => __('Forward'), + 'permission' => function () { + return true; + }, + 'class' => 'conv-forward', + 'fixed_location' => true, + ], + 'merge' => [ + 'icon' => 'glyphicon-indent-left', + 'location' => self::LOCATION_DROPDOWN, + 'label' => __('Merge'), + 'permission' => function ($conversation) { + return ! $conversation->isChat(); + }, + 'class' => '', + 'url' => function ($conversation) { + return route('conversations.ajax_html', array_merge([ 'action' => 'merge_conv' ], \Request::all(), [ 'conversation_id' => $conversation->id ])); + }, + 'attrs' => [ + 'data-trigger' => 'modal', + 'data-modal-title' => __('Merge Conversations'), + 'data-modal-no-footer' => 'true', + 'data-modal-on-show' => 'initMergeConv', + ], + 'fixed_location' => true, + ], + 'move' => [ + 'icon' => 'glyphicon-log-out', + 'location' => self::LOCATION_DROPDOWN, + 'label' => __('Move'), + 'permission' => function ($conversation, $user) { + return $user->can('move', \App\Conversation::class); + }, + 'class' => '', + 'url' => function ($conversation) { + return route('conversations.ajax_html', array_merge([ 'action' => 'move_conv' ], \Request::all(), [ 'conversation_id' => $conversation->id ])); + }, + 'attrs' => [ + 'data-trigger' => 'modal', + 'data-modal-title' => __('Move Conversation'), + 'data-modal-no-footer' => 'true', + 'data-modal-on-show' => 'initMoveConv', + ], + 'fixed_location' => true, + ], + 'print' => [ + 'icon' => 'glyphicon-print', + 'location' => self::LOCATION_DROPDOWN, + 'label' => __('Print'), + 'class' => '', + 'permission' => function () { + return true; + }, + 'url' => function () { + return \Request::getRequestUri() . '&print=1'; + }, + 'attrs' => [ + 'target' => '_blank', + ], + 'fixed_location' => true, + ], + ]; + + // Allow overriding default actions while preserving backwards compatibility + $actions = Eventy::filter('conversation.action_buttons', $actions, $conversation, $user, $mailbox); + + // Filter actions based on permissions + foreach ($actions as $key => $action) { + if (! $action['permission']($conversation, $user)) { + unset($actions[ $key ]); + } + } + + return $actions; + } + + /** + * Get actions for a specific location + */ + public static function getActionsByLocation($actions, $location) + { + return array_filter($actions, function ($action) use ($location) { + if (! empty($action['fixed_location'])) { + // Modified logic for responsive display + if ($location === self::LOCATION_TOOLBAR) { + // Show in toolbar if it's a toolbar action + return $action['location'] === self::LOCATION_TOOLBAR; + } elseif ($location === self::LOCATION_DROPDOWN) { + // Show in dropdown if explicitly flagged or if it's a dropdown action + return $action['location'] === self::LOCATION_DROPDOWN || + ( ! empty($action['show_in_dropdown']) && $action['location'] === self::LOCATION_TOOLBAR ); + } + } + + return $action['location'] === $location; + }); + } + + /** + * Override an action's location + * This should only be used for custom actions, not default ones + */ + public static function setActionLocation($action_key, $location) + { + // This method can be used to change location of custom actions + // Default actions will maintain their original location for compatibility + return Eventy::filter('conversation.action.location', $location, $action_key); + } +} diff --git a/app/Helpers/ConversationActions.php b/app/Helpers/ConversationActions.php deleted file mode 100644 index b52eaca50..000000000 --- a/app/Helpers/ConversationActions.php +++ /dev/null @@ -1,173 +0,0 @@ - [ - 'icon' => 'glyphicon-share-alt', - 'location' => self::LOCATION_TOOLBAR, // Forcing toolbar location for compatibility - 'label' => __( 'Reply' ), - 'permission' => function ( $conversation ) { - return ( ! $conversation->isPhone() || ( $conversation->customer && $conversation->customer->getMainEmail() ) ) - && Eventy::filter( 'conversation.reply_button.enabled', true, $conversation ); - }, - 'class' => 'conv-reply', - 'fixed_location' => true, - ], - 'note' => [ - 'icon' => 'glyphicon-edit', - 'location' => self::LOCATION_TOOLBAR, // Forcing toolbar location for compatibility - 'label' => __( 'Note' ), - 'permission' => function ( $conversation ) { - return Eventy::filter( 'conversation.note_button.enabled', true, $conversation ); - }, - 'class' => 'conv-add-note', - 'fixed_location' => true, - ], - 'delete' => [ - 'icon' => 'glyphicon-trash', - 'location' => self::LOCATION_BOTH, // Keeping both locations for compatibility - 'label' => $conversation->state != Conversation::STATE_DELETED ? __( 'Delete' ) : __( 'Delete Forever' ), - 'permission' => function ( $conversation, $user ) { - return $user->can( 'delete', $conversation ); - }, - 'class' => $conversation->state != Conversation::STATE_DELETED ? 'conv-delete' : 'conv-delete-forever', - 'mobile_only' => true, - 'fixed_location' => true, - ], - - // Default dropdown actions - keeping them in dropdown for backwards compatibility - 'follow' => [ - 'icon' => 'glyphicon-bell', - 'location' => self::LOCATION_DROPDOWN, - 'label' => __( 'Follow' ), - 'permission' => function () { - return true; - }, - 'class' => 'conv-follow', - 'has_opposite' => true, - 'opposite' => [ - 'label' => __( 'Unfollow' ), - 'class' => 'conv-follow', - ], - 'fixed_location' => true, - ], - 'forward' => [ - 'icon' => 'glyphicon-arrow-right', - 'location' => self::LOCATION_DROPDOWN, - 'label' => __( 'Forward' ), - 'permission' => function () { - return true; - }, - 'class' => 'conv-forward', - 'fixed_location' => true, - ], - 'merge' => [ - 'icon' => 'glyphicon-indent-left', - 'location' => self::LOCATION_DROPDOWN, - 'label' => __( 'Merge' ), - 'permission' => function ( $conversation ) { - return ! $conversation->isChat(); - }, - 'class' => '', - 'url' => function ( $conversation ) { - return route( 'conversations.ajax_html', array_merge( [ 'action' => 'merge_conv' ], Request::all(), [ 'conversation_id' => $conversation->id ] ) ); - }, - 'attrs' => [ - 'data-trigger' => 'modal', - 'data-modal-title' => __( 'Merge Conversations' ), - 'data-modal-no-footer' => 'true', - 'data-modal-on-show' => 'initMergeConv', - ], - 'fixed_location' => true, - ], - 'move' => [ - 'icon' => 'glyphicon-log-out', - 'location' => self::LOCATION_DROPDOWN, - 'label' => __( 'Move' ), - 'permission' => function ( $conversation, $user ) { - return $user->can( 'move', Conversation::class ); - }, - 'class' => '', - 'url' => function ( $conversation ) { - return route( 'conversations.ajax_html', array_merge( [ 'action' => 'move_conv' ], Request::all(), [ 'conversation_id' => $conversation->id ] ) ); - }, - 'attrs' => [ - 'data-trigger' => 'modal', - 'data-modal-title' => __( 'Move Conversation' ), - 'data-modal-no-footer' => 'true', - 'data-modal-on-show' => 'initMoveConv', - ], - 'fixed_location' => true, - ], - 'print' => [ - 'icon' => 'glyphicon-print', - 'location' => self::LOCATION_DROPDOWN, - 'label' => __( 'Print' ), - 'permission' => function () { - return true; - }, - 'url' => function () { - return Request::getRequestUri() . '&print=1'; - }, - 'attrs' => [ - 'target' => '_blank', - ], - 'fixed_location' => true, - 'class' => '', - ], - ]; - - // Allow overriding default actions while preserving backwards compatibility - $actions = Eventy::filter( 'conversation.actions', $actions, $conversation, $user, $mailbox ); - - // Filter actions based on permissions - foreach ( $actions as $key => $action ) { - if ( ! $action['permission']( $conversation, $user ) ) { - unset( $actions[ $key ] ); - } - } - - return $actions; - } - - /** - * Get actions for a specific location - */ - public static function getActionsByLocation( $actions, $location ) { - return array_filter( $actions, function ( $action ) use ( $location ) { - // If action has fixed_location, respect its original location - if ( ! empty( $action['fixed_location'] ) ) { - return $action['location'] === $location || $action['location'] === self::LOCATION_BOTH; - } - - // For new/custom actions, use the specified location - return $action['location'] === $location || $action['location'] === self::LOCATION_BOTH; - } ); - } - - /** - * Override an action's location - * This should only be used for custom actions, not default ones - */ - public static function setActionLocation( $action_key, $location ) { - // This method can be used to change location of custom actions - // Default actions will maintain their original location for compatibility - return Eventy::filter( 'conversation.action.location', $location, $action_key ); - } -} diff --git a/resources/views/conversations/view.blade.php b/resources/views/conversations/view.blade.php index 613772cb0..9fd53df42 100644 --- a/resources/views/conversations/view.blade.php +++ b/resources/views/conversations/view.blade.php @@ -35,75 +35,92 @@ {{-- There should be no spaces between buttons --}} @foreach ($toolbar_actions as $action_key => $action) - @if (!empty($action['url'])) + @if ($action_key === 'delete') + {{-- Special handling for delete button --}} + + @elseif (!empty($action['url'])) {{-- Action with URL (like move, merge) --}} - $attr_value) - {{ $attr_key }}="{{ $attr_value }}" - @endforeach - @endif - data-toggle="tooltip" - data-placement="bottom" - title="{{ $action['label'] }}" - aria-label="{{ $action['label'] }}"> - - + $attr_value) + {{ $attr_key }}="{{ $attr_value }}" + @endforeach + @endif + data-toggle="tooltip" + data-placement="bottom" + title="{{ $action['label'] }}" + aria-label="{{ $action['label'] }}"> + + @else {{-- Simple button action --}} - $attr_value) - {{ $attr_key }}="{{ $attr_value }}" - @endforeach - @endif - > + $attr_value) + {{ $attr_key }}="{{ $attr_value }}" + @endforeach + @endif + > @endif @endforeach @action('conversation.action_buttons', $conversation, $mailbox) - + {{-- More Actions Dropdown --}}