From 5e459a6571c46aa4522b39aeb8f628e834001456 Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Tue, 12 Nov 2024 12:52:21 +0100 Subject: [PATCH] fix: some elements crashed since the inscrease of files in tickets and messages --- app/Http/Controllers/Bo/TicketController.php | 53 +- .../Bo/Credit/CreateEstimateRequest.php | 14 +- app/Lib/Helpers/ToolboxHelper.php | 21 +- app/Models/Ticket.php | 16 + .../views/bo/pages/tickets/index.blade.php | 166 +++--- .../views/bo/pages/tickets/show.blade.php | 537 +++++++++--------- 6 files changed, 432 insertions(+), 375 deletions(-) diff --git a/app/Http/Controllers/Bo/TicketController.php b/app/Http/Controllers/Bo/TicketController.php index a7be411..4295d11 100644 --- a/app/Http/Controllers/Bo/TicketController.php +++ b/app/Http/Controllers/Bo/TicketController.php @@ -178,8 +178,8 @@ class TicketController extends Controller $ticket = new Ticket($request->validated()); - /** @var null|array<\Illuminate\Http\UploadedFile> */ - $fileTickets = $request->file_tickets; + /** @var array<\Illuminate\Http\UploadedFile> */ + $fileTickets = $request->file_tickets ?? []; if (count($fileTickets) > 0) { foreach ($fileTickets as $key => $file) { if ($key === 0) { @@ -544,8 +544,13 @@ class TicketController extends Controller Session::has('tickets.sort_col') and Session::has('tickets.sort_way') and in_array(Session::get('tickets.sort_col'), [ - 'id', 'site_id', 'created_at', 'created_by', - 'type', 'status', 'assigned_to' + 'id', + 'site_id', + 'created_at', + 'created_by', + 'type', + 'status', + 'assigned_to' ]) ) { $tickets->orderBy( @@ -653,30 +658,32 @@ class TicketController extends Controller }; try { - if (!\is_null($ticket->file)) { - $uploadedFile = GitLab::projects()->uploadFile($gitlabRepoId, $ticket->file); - $description .= $this->formatGitlabFiles($ticket, $uploadedFile, 1); + foreach ($ticket->getFiles() as $key => $file) { + if (!\is_null($file)) { + $uploadedFile = GitLab::projects()->uploadFile($gitlabRepoId, $file); + $description .= $this->formatGitlabFiles($ticket, $uploadedFile, $key + 1); + } } - if (!\is_null($ticket->file2)) { - $uploadedFile2 = GitLab::projects()->uploadFile($gitlabRepoId, $ticket->file2); - $description .= $this->formatGitlabFiles($ticket, $uploadedFile2, 2); - } + // if (!\is_null($ticket->file2)) { + // $uploadedFile2 = GitLab::projects()->uploadFile($gitlabRepoId, $ticket->file2); + // $description .= $this->formatGitlabFiles($ticket, $uploadedFile2, 2); + // } - if (!\is_null($ticket->file3)) { - $uploadedFile3 = GitLab::projects()->uploadFile($gitlabRepoId, $ticket->file3); - $description .= $this->formatGitlabFiles($ticket, $uploadedFile3, 3); - } + // if (!\is_null($ticket->file3)) { + // $uploadedFile3 = GitLab::projects()->uploadFile($gitlabRepoId, $ticket->file3); + // $description .= $this->formatGitlabFiles($ticket, $uploadedFile3, 3); + // } - if (!\is_null($ticket->file4)) { - $uploadedFile4 = GitLab::projects()->uploadFile($gitlabRepoId, $ticket->file4); - $description .= $this->formatGitlabFiles($ticket, $uploadedFile4, 4); - } + // if (!\is_null($ticket->file4)) { + // $uploadedFile4 = GitLab::projects()->uploadFile($gitlabRepoId, $ticket->file4); + // $description .= $this->formatGitlabFiles($ticket, $uploadedFile4, 4); + // } - if (!\is_null($ticket->file5)) { - $uploadedFile5 = GitLab::projects()->uploadFile($gitlabRepoId, $ticket->file5); - $description .= $this->formatGitlabFiles($ticket, $uploadedFile5, 5); - } + // if (!\is_null($ticket->file5)) { + // $uploadedFile5 = GitLab::projects()->uploadFile($gitlabRepoId, $ticket->file5); + // $description .= $this->formatGitlabFiles($ticket, $uploadedFile5, 5); + // } $issue = GitLab::issues()->create($gitlabRepoId, [ 'title' => 'ts-' . $ticket->id, diff --git a/app/Http/Requests/Bo/Credit/CreateEstimateRequest.php b/app/Http/Requests/Bo/Credit/CreateEstimateRequest.php index 0c30c4e..da90770 100644 --- a/app/Http/Requests/Bo/Credit/CreateEstimateRequest.php +++ b/app/Http/Requests/Bo/Credit/CreateEstimateRequest.php @@ -33,14 +33,18 @@ class CreateEstimateRequest extends FormRequest $ticket = $this->ticket; return [ 'amount' => [ - 'required', 'numeric', 'gt:0', function ($attribute, $value, $fail) { - if ((\floatval($value) * 1000) % 62.5 !== 0) { + 'required', + 'numeric', + 'gt:0', + function ($attribute, $value, $fail) { + if (fmod((\floatval($value) * 1000), 62.5) !== 0.0) { $fail("$attribute doit être un multiple de 62.5 (soit 0.0625)"); } } ], 'credit_account_id' => [ - 'required', 'exists:credit_accounts,id', + 'required', + 'exists:credit_accounts,id', function ($attribute, $value, $fail) use ($ticket) { /** @var \App\Models\Credit\Account|null */ $account = Account::whereId($value)->first(); @@ -57,11 +61,11 @@ class CreateEstimateRequest extends FormRequest * * @return array */ - public function messages(): array + public function attributes(): array { return [ 'amount' => 'Montant', - 'credit_account_id' => 'Compte' + 'credit_account_id' => 'Compte à débiter' ]; } } diff --git a/app/Lib/Helpers/ToolboxHelper.php b/app/Lib/Helpers/ToolboxHelper.php index 68bd885..507eadc 100644 --- a/app/Lib/Helpers/ToolboxHelper.php +++ b/app/Lib/Helpers/ToolboxHelper.php @@ -68,6 +68,18 @@ class ToolboxHelper return \preg_match('/(\.png|\.jpg|\.jpeg|\.gif)$/', $url, $m) === 1; } + /** + * Check uri points to pdf file + * + * @param string $url + * @return boolean + */ + public static function isPdfFile(string $url): bool + { + $m = []; + return \preg_match('/(\.pdf)$/', $url, $m) === 1; + } + /** * Strip scheme (https) on oembed urls * Only embed allowed are for MP4 self hosted videos right now @@ -339,7 +351,7 @@ class ToolboxHelper ): Builder { $currentLocale = app()->currentLocale(); $locales = collect(config('app.locales')) - ->mapWithKeys(fn (string $locale) => [$locale => $locale]); + ->mapWithKeys(fn(string $locale) => [$locale => $locale]); if (!$locales->has($defaultLocale)) { // * Remove default locale. $locales->pull($defaultLocale); @@ -357,7 +369,7 @@ class ToolboxHelper $q->whereRaw("JSON_EXTRACT({$column}, '$.{$currentLocale}') = '{$value}'"); } // * Query any other locale - $locales->each(fn ($locale) => $q->orWhereRaw( + $locales->each(fn($locale) => $q->orWhereRaw( "JSON_EXTRACT({$column}, '$.{$locale}') = '{$value}'" )); }); @@ -449,7 +461,10 @@ class ToolboxHelper $fileFullPath = self::getUploadedFileFullPath($value, $useStorage); if ( !\in_array(self::getUploadedFileMimeType($value, $useStorage), [ - 'image/jpeg', 'image/jpg', 'image/png', 'image/gif' + 'image/jpeg', + 'image/jpg', + 'image/png', + 'image/gif' ]) ) { $fail(trans(':attribute doit être une image de type \'image/jpeg,image/jpg,image/png,image/gif\'', [ diff --git a/app/Models/Ticket.php b/app/Models/Ticket.php index 4fe561e..460ef6d 100644 --- a/app/Models/Ticket.php +++ b/app/Models/Ticket.php @@ -275,4 +275,20 @@ class Ticket extends Model Str::limit(\preg_replace('/[[:cntrl:]]+/', '', $ticket->text), 35), ); } + + /** + * Get an array of all files columns of the ticket. + * + * @return array + */ + public function getFiles(): array + { + return [ + $this->file, + $this->file2, + $this->file3, + $this->file4, + $this->file5, + ]; + } } diff --git a/resources/views/bo/pages/tickets/index.blade.php b/resources/views/bo/pages/tickets/index.blade.php index 1d3c8ac..6642a15 100644 --- a/resources/views/bo/pages/tickets/index.blade.php +++ b/resources/views/bo/pages/tickets/index.blade.php @@ -226,101 +226,101 @@
@php - $ticket_files = [$ticket->file, $ticket->file2, $ticket->file3, $ticket->file4, $ticket->file5] + $ticket_files = $ticket->getFiles(); @endphp - @foreach($ticket_files as $key => $file) - @if ($file !== null) + @foreach ($ticket_files as $key => $file) + @if ($file !== null && (\App\Lib\Helpers\ToolboxHelper::isImageFile($file) || \App\Lib\Helpers\ToolboxHelper::isPdfFile($file))) - @break - @endif - @endforeach - {{-- Modal du ticket --}} - - - - @if (auth()->user()->isAdmin()) - + @break @endif - @can('changeStatus', $ticket) - @if (!$ticket->status->equals(\App\Enums\Tickets\TicketStatusEnum::archived)) -
- @method('PUT') - @csrf - -
- @endif + @endforeach + {{-- Modal du ticket --}} + + + + @if (auth()->user()->isAdmin()) + + @endif + @can('changeStatus', $ticket) + @if (!$ticket->status->equals(\App\Enums\Tickets\TicketStatusEnum::archived)) +
+ @method('PUT') + @csrf + +
+ @endif + @endcan +
+
+ + + + + + + @endforeach + + + {{ $tickets->withQueryString()->links() }} + @endsection diff --git a/resources/views/bo/pages/tickets/show.blade.php b/resources/views/bo/pages/tickets/show.blade.php index cfbe157..4d590bb 100644 --- a/resources/views/bo/pages/tickets/show.blade.php +++ b/resources/views/bo/pages/tickets/show.blade.php @@ -5,273 +5,288 @@ @section('bodyClass', 'content ticketsShow') @section('content') -
-

- - {{ $ticket->full_name }} - @if ($ticket->is_internal)@endif - {{ $ticket->type->label() }} -

-
- @can('suscribeToTicket', $ticket) -
- @csrf -
- - -
- @push('scripts') - - @endpush -
- @endcan - @can('assignTo', $ticket) -
- @csrf -
- - - @push('scripts') - - @endpush -
-
- @endcan - @can('changeStatus', $ticket) -
- @csrf -
- - - @push('scripts') - - @endpush -
-
- @endcan - @if($ticket->status->equals(\App\Enums\Tickets\TicketStatusEnum::archived)) - @can('reopen', $ticket) -
- @csrf - -
- @endcan - @endif -
-
-@can('assignLabels', App\Models\Ticket::class) -
-
- @foreach ($ticket->labels as $label) - {{ $label->name }} - @endforeach -
-
-@endcan -
-@if($ticket->estimation) -@include('estimations.pendingCard', ['ticket' => $ticket, 'estimation' => $ticket->estimation]) -
-@endif -
-

- {{ __('Suivi') }} - @can('createMessage', $ticket) - - - @endcan - @can('estimate', $ticket) - {{ __('Estimation') }} - @endcan -

- @foreach ($messages as $message) - @if ( - $message->type->equals(\App\Enums\MessageTypeEnum::public) || - (Auth::user()->can('createPrivateMessage', $ticket)) - ) - @if ($message->user_id === null) -
- {!! nl2br(clean($message->message)) !!} - Le {{ \Carbon\Carbon::parse($message->created_at)->format('d/m/Y à H:i')}} - ({{ __('Message système') }}) -
- @else -
- @if ($message->type->equals(\App\Enums\MessageTypeEnum::private)) -    - @endif - {!! \App\Lib\Helpers\ToolboxHelper::plainToHtml($message->message) !!} - @foreach ([$message->file, $message->file2, $message->file3] as $file) - @if ($file !== null) -
Ouvrir la pièce jointe + @endif @endif @endforeach -

- - {!! __('Créé le :date par :userName', [ - 'date' => \Carbon\Carbon::parse($message->created_at)->format('d/m/Y à H:i'), - 'userName' => $message->user ? $message->user->name : __('Utilisateur supprimé') - ]) !!} - - @if($message->user_id === Auth::user()->id) - @can('updateMessage', $ticket, $message) - - - @endcan - @endif
+
+
+

{{ __('Description') }}

+

{!! clean(\App\Lib\Helpers\ToolboxHelper::plainToHtml($ticket->text)) !!}

+ {!! __('Créé le :date par :userName', [ + 'date' => \Carbon\Carbon::parse($ticket->created_at)->format('d/m/Y à H:i'), + 'userName' => $ticket->user ? $ticket->user->name : __('Utilisateur supprimé'), + ]) !!} +
+ @php + $ticket_files = $ticket->getFiles(); + @endphp + @if (!empty(Arr::whereNotNull($ticket_files))) +
+

{{ __('Pièces jointes') }}

+ @foreach ($ticket_files as $key => $file) + @if (!is_null($file)) + + @endif + @endforeach +
@endif - @endif - @endforeach -
-
-
-

{{__('Description') }}

-

{!! clean(\App\Lib\Helpers\ToolboxHelper::plainToHtml($ticket->text)) !!}

- {!! __('Créé le :date par :userName', [ - 'date' => \Carbon\Carbon::parse($ticket->created_at)->format('d/m/Y à H:i'), - 'userName' => $ticket->user ? $ticket->user->name : __('Utilisateur supprimé') - ]) !!} -
-@php - $ticket_files = [$ticket->file, $ticket->file2, $ticket->file3, $ticket->file4, $ticket->file5] -@endphp -
-

{{ __('Pièces jointes') }}

-@foreach($ticket_files as $key => $file) -@if (!is_null($file)) - -
-@endif -@endforeach -
-@can('createGitlabIssue', $ticket) -
- @csrf - -
-@else - @can('isAdmin') - @if (!\is_null($ticket->gitlab_issue)) -
-

- {{ __("Lien vers l'issue Gitlab : ")}} - {{ __("Ouvrir") }} -

- @endif - @endcan -@endcan -@endsection +
+ @can('createGitlabIssue', $ticket) +
+ @csrf + +
+ @else + @can('isAdmin') + @if (!\is_null($ticket->gitlab_issue)) +
+

+ {{ __("Lien vers l'issue Gitlab : ") }} + {{ __('Ouvrir') }} +

+ @endif + @endcan + @endcan + @endsection