Skip to content

Commit

Permalink
add gallery on show page projects
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Oct 19, 2023
1 parent e5e84c4 commit 280d816
Show file tree
Hide file tree
Showing 12 changed files with 551 additions and 147 deletions.
6 changes: 4 additions & 2 deletions app/Http/Controllers/Dashboard/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Project\EditRequest;
use App\Http\Requests\Project\StoreRequest;
use App\Http\Resources\Project\EditProjectResource;
use App\Http\Resources\ProjectCardResource;
Expand Down Expand Up @@ -69,10 +70,11 @@ public function edit(Project $project)
]);
}

public function update(Request $request, Project $project)
public function update(EditRequest $request, Project $project)
{

$this->authorize('editAsNgo', $project);
ProjectService::update($project, $request->all());
ProjectService::update($project, $request->validated());

return redirect()->back()
->with('success', 'Project updated.');
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Http\Sorts\ProjectDonationsSumSort;
use App\Models\Project;
use App\Models\Volunteer;
use Embed\Embed;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
Expand Down Expand Up @@ -70,7 +71,7 @@ protected function projectList(Request $request, string $view): Response

public function show(Project $project)
{
// TODO: prevent display of unpublished projects
// dd($project->embedded_videos);
return Inertia::render('Public/Projects/Show', [
'project' => new ShowProjectResource($project),
]);
Expand Down
53 changes: 53 additions & 0 deletions app/Http/Requests/Project/EditRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests\Project;

use Illuminate\Foundation\Http\FormRequest;

class EditRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
//TODO: check if user is admin organization or organization member
return auth()->check();
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
return [
'name' => ['string', 'max:255', 'nullable'],
'target_budget' => ['numeric', 'nullable'],
'categories' => ['array'],
'categories.*' => ['nullable', 'exists:project_categories,id'],
'start' => ['date', 'nullable', 'after_or_equal:today'],
'end' => ['date', 'nullable', 'after:tomorrow'],
'counties' => ['array', 'nullable'],
'counties.*' => ['exists:counties,id', 'nullable'],
'description' => ['string', 'nullable', 'max:800'],
'scope' => ['string', 'nullable'],
'reason_to_donate' => ['string', 'nullable'],
'beneficiaries' => ['nullable', 'string'],
'accepting_volunteers' => ['boolean', 'nullable'],
'accepting_comments' => ['boolean', 'nullable'],
'videos' => ['nullable', 'array'],
'videos.*.url' => ['required', 'url'],
'external_links' => ['nullable', 'array'],
'external_links.*.title' => ['required', 'string'],
'external_links.*.url' => ['required', 'url'],
'is_national' => ['boolean', 'nullable'],
'gallery' => ['array', 'nullable'],
'gallery.*.file' => ['file', 'nullable'],
'preview' => ['file', 'nullable'],
];
}
}
9 changes: 9 additions & 0 deletions app/Http/Resources/Project/ShowProjectResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public function toArray(Request $request): array
'accepting_volunteers' => \boolval($this->accepting_volunteers),
'accepting_comments' => \boolval($this->accepting_comments),
'videos' => '',
'embedded_videos'=> $this->embedded_videos,
'swipe_gallery' => $this->getMedia('gallery')->map(function ($media) {
return [
'src' => $media->getFullUrl(),
'thumbnail' => $media->getFullUrl('preview'),
'w' => 1200,
'h' => 800,
];
})->toArray(),
'is_active' => $this->is_active,
'external_links' => collect($this->external_links)->map(function (array $link) {
$link['source'] = parse_url($link['url'], \PHP_URL_HOST);
Expand Down
13 changes: 13 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Concerns\LogsActivityForApproval;
use App\Enums\ProjectStatus;
use App\Traits\HasProjectStatus;
use Embed\Embed;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -241,4 +242,16 @@ public function markAsRejected(?string $reason = null): void
'user_id' => auth()->user()->id,
]);
}

public function getEmbeddedVideosAttribute(): array
{
$videos = $this->videos;
$embeddedVideos = [];
foreach ($videos as $video) {
$embeddedUrl = rescue(fn () => (new Embed())->get($video['url'])->code, '');
$embeddedVideos[] = $embeddedUrl;
}

return $embeddedVideos;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"awcodes/filament-tiptap-editor": "^2.6",
"camya/filament-title-with-slug": "^0.5.6",
"drewm/mailchimp-api": "^2.5",
"embed/embed": "^4.4",
"filament/filament": "^2.17",
"filament/spatie-laravel-media-library-plugin": "^2.17",
"filament/spatie-laravel-translatable-plugin": "^2.17",
Expand Down
Loading

0 comments on commit 280d816

Please sign in to comment.