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
144 changes: 24 additions & 120 deletions app/Nova/Actions/BulkUploadMediaFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
namespace App\Nova\Actions;

use App\MediaUpload;
use DigitalCreative\Filepond\Filepond;
use DigitalCreative\Filepond\Data\Data;
use Illuminate\Bus\Queueable;
use Illuminate\Http\UploadedFile;
use Illuminate\Http\Request;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\File;
use Laravel\Nova\Http\Requests\NovaRequest;

class BulkUploadMediaFiles extends Action
Expand All @@ -37,7 +34,7 @@ class BulkUploadMediaFiles extends Action
*/
public function handle(ActionFields $fields, Collection $models)
{
$uploadedFiles = $this->collectUploadedFiles($fields);
$uploadedFiles = $this->collectUploadedFiles($fields, request());
if (empty($uploadedFiles)) {
return Action::danger('Please select one or more files.');
}
Expand All @@ -56,11 +53,7 @@ public function handle(ActionFields $fields, Collection $models)
continue;
}

$originalName = $this->extractOriginalName($file);
if ($originalName === null) {
$skipped++;
continue;
}
$originalName = $file->getClientOriginalName();
$extension = strtolower((string) pathinfo($originalName, PATHINFO_EXTENSION));
if (!in_array($extension, $allowedExtensions, true)) {
$skipped++;
Expand All @@ -71,7 +64,7 @@ public function handle(ActionFields $fields, Collection $models)
$safeBaseName = Str::slug($baseName) ?: 'upload-file';
$destination = 'nova/uploads/' . now()->format('Y/m');
$finalFileName = $safeBaseName . '-' . Str::random(8) . '.' . $extension;
$storedPath = $this->storeFile($file, $destination, $finalFileName);
$storedPath = $file->storeAs($destination, $finalFileName, 'resources');

if (!$storedPath) {
$skipped++;
Expand All @@ -98,123 +91,34 @@ public function handle(ActionFields $fields, Collection $models)
*/
public function fields(NovaRequest $request): array
{
return [
Filepond::make('Files', 'files')
->multiple()
->limit(50)
->acceptedTypes('.jpg,.jpeg,.png,.gif,.webp,.svg,.pdf,.doc,.docx,.ppt,.pptx,.xls,.xlsx,.txt')
->rules('required')
->help('Drag and drop multiple files or click to select. Supported: images, PDF, Office docs, and TXT.'),
];
}

/**
* Collect uploaded files from Nova action fields + raw request payload.
*
* @return UploadedFile[]
*/
protected function collectUploadedFiles(ActionFields $fields): array
{
$candidates = [];

if (isset($fields->files)) {
$candidates[] = $fields->files;
}
$fields = [];

$requestItems = request()->collect('files')->all();
if (!empty($requestItems)) {
$candidates[] = $requestItems;
for ($i = 1; $i <= 20; $i++) {
$fields[] = File::make("File {$i}", "file_{$i}")
->rules('nullable', 'max:51200', 'mimes:jpg,jpeg,png,gif,webp,svg,pdf,doc,docx,ppt,pptx,xls,xlsx,txt');
}

$requestFiles = request()->allFiles();
if (!empty($requestFiles)) {
$candidates[] = $requestFiles;
}

$flatten = function ($value) use (&$flatten): array {
if ($value instanceof UploadedFile) {
return [$value];
}

if (is_string($value)) {
return [$value];
}

if (!is_array($value)) {
return [];
}

$result = [];
foreach ($value as $item) {
$result = array_merge($result, $flatten($item));
}
$fields[0]->help('Upload up to 20 files in one run (mix of images/docs).');

return $result;
};

$files = [];
foreach ($candidates as $candidate) {
$files = array_merge($files, $flatten($candidate));
}

return $files;
}

protected function extractOriginalName(UploadedFile|string $file): ?string
{
if ($file instanceof UploadedFile) {
return $file->getClientOriginalName();
}

try {
$data = Data::fromEncrypted($file);
return $data->filename;
} catch (\Throwable $e) {
return null;
}
return $fields;
}

protected function storeFile(UploadedFile|string $file, string $destination, string $finalFileName): ?string
/**
* Collect uploaded files from explicit action slots.
*
* @return array
*/
protected function collectUploadedFiles(ActionFields $fields, Request $request): array
{
if ($file instanceof UploadedFile) {
return $file->storeAs($destination, $finalFileName, 'resources');
}

try {
$data = Data::fromEncrypted($file);
} catch (\Throwable $e) {
return null;
}

$targetPath = $destination . '/' . $finalFileName;
$stream = Storage::disk($data->disk)->readStream($data->path);

if (is_resource($stream)) {
try {
$stored = Storage::disk('resources')->writeStream($targetPath, $stream, ['visibility' => 'public']);
} finally {
fclose($stream);
}
} else {
// Some environments return null instead of a stream for temp files.
// Fallback to reading file contents directly.
$contents = Storage::disk($data->disk)->get($data->path);
if (!is_string($contents) || $contents === '') {
Log::warning('[BulkUploadMediaFiles] Unable to read temp file contents', [
'disk' => $data->disk,
'path' => $data->path,
]);
return null;
$files = [];
for ($i = 1; $i <= 20; $i++) {
$key = "file_{$i}";
$file = $fields->{$key} ?? $request->file($key);
if ($file) {
$files[] = $file;
}

$stored = Storage::disk('resources')->put($targetPath, $contents, 'public');
}

// Clean only this temp file. Deleting the whole directory can remove
// sibling files from the same multi-upload batch.
$data->deleteFile();

return $stored ? $targetPath : null;
return $files;
}

}
6 changes: 6 additions & 0 deletions app/Nova/TrainingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public function fields(Request $request): array
->nullable()
->rules('nullable', 'url'),

Text::make('Third button text', 'third_button_text')->nullable(),

Text::make('Third button URL', 'third_button_url')
->nullable()
->rules('nullable', 'url'),

Text::make('Meta title', 'meta_title')
->nullable()
->help('Optional HTML title override'),
Expand Down
2 changes: 2 additions & 0 deletions app/TrainingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class TrainingResource extends Model
'button_url',
'secondary_button_text',
'secondary_button_url',
'third_button_text',
'third_button_url',
'meta_title',
'meta_description',
'position',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('training_resources', function (Blueprint $table) {
$table->string('third_button_text')->nullable()->after('secondary_button_url');
$table->string('third_button_url')->nullable()->after('third_button_text');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('training_resources', function (Blueprint $table) {
$table->dropColumn([
'third_button_text',
'third_button_url',
]);
});
}
};
4 changes: 2 additions & 2 deletions resources/views/static/training/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class="absolute top-0 right-0 h-full max-w-[calc(70vw)] object-cover hidden md:b
@foreach($dynamicResults as $result)
<div class="flex overflow-hidden flex-col bg-white rounded-lg cursor-pointer" onclick="window.location.href='{{ $result['link'] }}'">
<div class="relative">
<img src="{{ $result['image'] }}" class="w-full" />
<img src="{{ $result['image'] }}" class="w-full md:h-[244px] md:object-cover" />
</div>
<div class="block flex-grow px-6 py-8">
<p class="text-dark-blue text-lg p-0 font-semibold mb-2 font-['Montserrat']">
Expand All @@ -306,7 +306,7 @@ class="absolute top-0 right-0 h-full max-w-[calc(70vw)] object-cover hidden md:b
@foreach($results as $result)
<div class="flex overflow-hidden flex-col bg-white rounded-lg cursor-pointer" onclick="window.location.href='{{ $result['link'] }}'">
<div class="relative">
<img src="{{ $result['image'] }}" class="w-full" />
<img src="{{ $result['image'] }}" class="w-full md:h-[244px] md:object-cover" />
</div>
<div class="block flex-grow px-6 py-8">
<p class="text-dark-blue text-lg p-0 font-semibold mb-2 font-['Montserrat']">
Expand Down
31 changes: 23 additions & 8 deletions resources/views/training/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,29 @@ class="mb-12 w-full h-full max-h-[630px] object-contain"
@endif

@if(!empty($trainingResource->button_text) && !empty($trainingResource->button_url))
<a
class="max-xl:!hidden inline-block bg-[#F95C22] rounded-full py-2.5 px-6 font-['Blinker'] hover:bg-hover-orange duration-300 text-base font-semibold leading-7 text-black normal-case"
href="{{ $trainingResource->button_url }}"
target="_blank"
rel="noopener noreferrer"
>
{{ $trainingResource->button_text }}
</a>
<div>
<a
class="max-xl:!hidden inline-block bg-[#F95C22] rounded-full py-2.5 px-6 font-['Blinker'] hover:bg-hover-orange duration-300 text-base font-semibold leading-7 text-black normal-case"
href="{{ $trainingResource->button_url }}"
target="_blank"
rel="noopener noreferrer"
>
{{ $trainingResource->button_text }}
</a>

@if(!empty($trainingResource->third_button_text) && !empty($trainingResource->third_button_url))
<div class="mt-4">
<a
class="max-xl:!hidden inline-block bg-[#F95C22] rounded-full py-2.5 px-6 font-['Blinker'] hover:bg-hover-orange duration-300 text-base font-semibold leading-7 text-black normal-case"
href="{{ $trainingResource->third_button_url }}"
target="_blank"
rel="noopener noreferrer"
>
{{ $trainingResource->third_button_text }}
</a>
</div>
@endif
</div>
@endif

@if(!empty($trainingResource->contacts_section))
Expand Down
Loading