Skip to content

Commit

Permalink
[TM-1528] send cover image for project in dashboard (#594)
Browse files Browse the repository at this point in the history
* [TM-1528] send cover image for project in dashboard

* [TM-1528] send cover image for project in dashboard
  • Loading branch information
cesarLima1 authored Nov 29, 2024
1 parent 9fa19bf commit 1b98f92
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
20 changes: 13 additions & 7 deletions app/Http/Resources/V2/Dashboard/ProjectProfileDetailsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

namespace App\Http\Resources\V2\Dashboard;

use App\Models\Traits\HasProjectCoverImage;
use App\Models\V2\Forms\FormOptionListOption;
use Illuminate\Http\Resources\Json\JsonResource;

class ProjectProfileDetailsResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
use HasProjectCoverImage;

public function toArray($request)
{
$coverImage = $this->getProjectCoverImage($this->resource);

$data = [
'name' => $this->name,
'descriptionObjetive' => $this->objectives,
Expand All @@ -26,9 +25,16 @@ public function toArray($request)
'targetLandUse' => $this->land_use_types,
'landTenure' => $this->land_tenure_project_area,
'framework' => $this->framework_key,
'cover_image' => $coverImage ? [
'id' => $coverImage->id,
'url' => $coverImage->getUrl(),
'thumbnail' => $coverImage->getUrl('thumbnail'),
'is_cover' => $coverImage->is_cover,
'mime_type' => $coverImage->mime_type,
] : null,
];

return $this->appendFilesToResource($data);
return $data;
}

public function getCountryLabel($slug)
Expand Down
57 changes: 57 additions & 0 deletions app/Models/Traits/HasProjectCoverImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Models\Traits;

use App\Models\V2\Nurseries\Nursery;
use App\Models\V2\Nurseries\NurseryReport;
use App\Models\V2\Projects\Project;
use App\Models\V2\Projects\ProjectReport;
use App\Models\V2\Sites\Site;
use App\Models\V2\Sites\SiteReport;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

trait HasProjectCoverImage
{
public function getProjectCoverImage(Project $project): ?Media
{
$models = [
['type' => get_class($project), 'ids' => [$project->id]],
['type' => Site::class, 'ids' => $project->sites->pluck('id')->toArray()],
['type' => Nursery::class, 'ids' => $project->nurseries->pluck('id')->toArray()],
['type' => ProjectReport::class, 'ids' => $project->reports->pluck('id')->toArray()],
['type' => SiteReport::class, 'ids' => $project->siteReports->pluck('id')->toArray()],
['type' => NurseryReport::class, 'ids' => $project->nurseryReports->pluck('id')->toArray()],
];

$coverMedia = Media::where(function ($query) use ($models) {
foreach ($models as $model) {
$query->orWhere(function ($query) use ($model) {
$query->where('model_type', $model['type'])
->whereIn('model_id', $model['ids']);
});
}
})
->where('is_cover', true)
->first();

if ($coverMedia) {
return $coverMedia;
}

// If no cover image found, the latest image is sent
return Media::where(function ($query) use ($models) {
foreach ($models as $model) {
$query->orWhere(function ($query) use ($model) {
$query->where('model_type', $model['type'])
->whereIn('model_id', $model['ids']);
});
}
})
->where(function ($query) {
$query->where('mime_type', 'like', 'image/jpeg')
->orWhere('mime_type', 'like', 'image/png');
})
->latest()
->first();
}
}

0 comments on commit 1b98f92

Please sign in to comment.