diff --git a/app/Filament/Pages/CustomMedia.php b/app/Filament/Pages/CustomMedia.php new file mode 100644 index 0000000..54327c5 --- /dev/null +++ b/app/Filament/Pages/CustomMedia.php @@ -0,0 +1,235 @@ +company = auth()->user()->company; + + $media = $this->company?->partnerMedia; + + if (! $media) { + return; + } + + $this->media = $media; + + $this->form->fill($media->toArray()); + + $documents = [ + 'journalism_organization', + 'press_council_certificate', + ]; + + $mediaItems = $media->getMedia('partnerMedia'); + + foreach ($documents as $docType) { + $media = $mediaItems + ->where('custom_properties.doc_type', str($docType)->replace('_docs', '')->slug('-')) + ->sortByDesc('created_at') + ->first(); + + if ($media) { + $this->data["{$docType}_docs"] = [$media->getPathRelativeToRoot()]; + } + } + } + + public static function form(Schema $schema): Schema + { + $documents = [ + [ + 'title' => 'Organisasi Kewartawanan', + 'text_name' => 'journalism_organization', + 'placeholder' => '*****', + 'max' => 100, + 'max_size' => 1024 * 10, + 'file_name' => 'journalism_organization_docs', + 'accept' => ['application/pdf'], + 'folder' => 'media/journalism-organization/', + ], + [ + 'title' => 'Sertifikat Dewan Pers', + 'text_name' => 'press_council_certificate', + 'placeholder' => '*****', + 'max' => 100, + 'max_size' => 1024 * 10, + 'file_name' => 'press_council_certificate_docs', + 'accept' => ['application/pdf'], + 'folder' => 'media/press-council-certificate/', + ], + ]; + + return $schema + ->components([ + Section::make('Informasi') + ->schema([ + TextInput::make('name') + ->label('Nama') + ->placeholder('PWK News') + ->autocomplete(false) + ->autofocus() + ->required() + ->maxLength(100), + + TextInput::make('link') + ->label('Tautan') + ->placeholder('https://www.pwknews.com') + ->autocomplete(false) + ->nullable() + ->maxLength(50) + ->url(), + + Textarea::make('address') + ->label('Alamat') + ->placeholder('Jl. Kebontoro, Jakarta Pusat') + ->autocomplete(false) + ->required() + ->columnSpanFull(), + + Radio::make('type') + ->label('Jenis') + ->options(MediaType::options()) + ->inline() + ->required() + ->in(implode(',', array_column(MediaType::cases(), 'value'))), + + Radio::make('classification') + ->label('Klasifikasi') + ->options(MediaClassification::options()) + ->inline() + ->required() + ->in(implode(',', array_column(MediaClassification::cases(), 'value'))), + ]) + ->columns(2) + ->columnSpan(2), + + Group::make() + ->schema( + + collect($documents) + ->map( + fn ($doc) => Section::make($doc['title']) + ->schema([ + TextInput::make($doc['text_name']) + ->hiddenLabel() + ->placeholder($doc['placeholder']) + ->autocomplete(false) + ->required() + ->maxLength($doc['max']), + + AdvancedFileUpload::make($doc['file_name']) + ->hiddenLabel() + ->disk(config('filesystems.default')) + ->acceptedFileTypes($doc['accept']) + ->maxSize($doc['max_size']) + ->directory($doc['folder'].now()->toDateString()) + ->required(), + ]) + )->toArray() + ) + ->columns(2), + ]) + ->statePath('data'); + } + + public function save() + { + $data = $this->form->getState(); + + $media = PartnerMedia::updateOrCreate([ + 'id' => $this->media?->id, + ], [ + 'company_id' => $this->company->id, + 'name' => $data['name'], + 'link' => $data['link'], + 'address' => $data['address'], + 'type' => $data['type'], + 'classification' => $data['classification'], + 'journalism_organization' => $data['journalism_organization'], + 'press_council_certificate' => $data['press_council_certificate'], + ]); + + $this->media = $media; + + $documents = [ + 'journalism_organization_docs', + 'press_council_certificate_docs', + ]; + + foreach ($documents as $field) { + if (empty($this->data[$field])) { + continue; + } + + foreach ((array) $this->data[$field] as $filePath) { + $fullPath = Storage::disk(config('filesystems.default'))->path($filePath); + + if (! file_exists($fullPath)) { + continue; + } + + $media + ->addMediaFromDisk($filePath, config('filesystems.default')) + ->preservingOriginal() + ->withCustomProperties([ + 'feature' => 'media', + 'date' => now()->toDateString(), + 'doc_type' => str($field) + ->replace('_docs', '') + ->slug('-'), + ]) + ->toMediaCollection('partnerMedia'); + } + } + + Notification::make() + ->title('Informasi media berhasil diperbarui.') + ->success() + ->send(); + } +} diff --git a/app/Models/Company.php b/app/Models/Company.php index 1bd0b85..aa89b27 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -21,7 +21,7 @@ public function user(): BelongsTo return $this->belongsTo(User::class); } - public function parternMedia(): HasOne + public function partnerMedia(): HasOne { return $this->hasOne(PartnerMedia::class); } diff --git a/app/Models/PartnerMedia.php b/app/Models/PartnerMedia.php new file mode 100644 index 0000000..2747cf8 --- /dev/null +++ b/app/Models/PartnerMedia.php @@ -0,0 +1,27 @@ +belongsTo(Company::class); + } + + public function journalists(): HasMany + { + return $this->hasMany(Journalist::class); + } +} diff --git a/database/migrations/2025_11_20_041125_create_partner_media_table.php b/database/migrations/2025_11_20_041125_create_partner_media_table.php new file mode 100644 index 0000000..8b35d19 --- /dev/null +++ b/database/migrations/2025_11_20_041125_create_partner_media_table.php @@ -0,0 +1,40 @@ +id(); + $table->foreignIdFor(Company::class); + $table->string('name', 50); + $table->text('address'); + $table->string('link', 50)->nullable(); + $table->enum('type', [MediaType::values()])->default(MediaType::ONLINE)->comment(MediaType::comment()); + $table->enum('classification', [MediaClassification::values()])->comment(MediaClassification::comment()); + $table->string('journalism_organization')->nullable(); + $table->string('press_council_certificate')->nullable(); + $table->timestamp('created_at')->useCurrent(); + $table->timestamp('updated_at')->nullable()->useCurrentOnUpdate(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('partner_media'); + } +}; diff --git a/resources/views/filament/pages/custom-media.blade.php b/resources/views/filament/pages/custom-media.blade.php new file mode 100644 index 0000000..e5fc471 --- /dev/null +++ b/resources/views/filament/pages/custom-media.blade.php @@ -0,0 +1,9 @@ + +
+ {{ $this->form }} + +
+ Simpan +
+
+