ContainerDetailResource.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Http\Resources\API;
  3. use App\Models\Asset;
  4. use App\Models\Library;
  5. use App\Models\Project;
  6. use App\Models\User;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Resources\Json\JsonResource;
  9. class ContainerDetailResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * @return array<string, mixed>
  15. */
  16. public function toArray(Request $request): array
  17. {
  18. $content = $this->content($request->get("version", 0))->first();
  19. $whitelist=make_array_list($this->whitelist??'');
  20. $whitelistName=User::query()->whereIn('id',$whitelist)->get();
  21. $mailto=$this->mailto;
  22. $mailtoName=User::query()->whereIn('id',$mailto)->get();
  23. // $library = Library::query()->where('id',$this->library_id)->first();
  24. if ($this->library->type == 'project'){
  25. $project = Project::query()->where('id',$this->library->project_id)->first();
  26. }else if($this->library->type == 'asset'){
  27. $asset = Asset::query()->where('id',$this->library->asset_id)->first();
  28. }
  29. return [
  30. 'id' => $this->id,
  31. 'name' => $content?->name,
  32. 'library' => new LibrarySimpleResource($this->library),
  33. 'naming_rule' => new NamingRuleSimpleResource($this->namingRule),
  34. 'naming_rules' => $this->naming_rules,
  35. "mailto" => $mailto,
  36. "mailto_name" =>UserProfileResource::collection($mailtoName),
  37. "email_subject" => $this->email_subject,
  38. "doc_stage" => $this->doc_stage,
  39. "doc_type" => $this->doc_type,
  40. "description" => $content?->description ? (new \App\Services\File\ImageUrlService)->getImageUrl($content?->description) : "",
  41. "acl" => $this->acl,
  42. "whitelist" =>$whitelist,
  43. "whitelist_name"=>UserProfileResource::collection($whitelistName),
  44. "version" => $this->version,
  45. "created_at" => (string)$this->created_at,
  46. "created_by" => new UserProfileResource($this->createdBy),
  47. 'approval_status'=>$this->approval_status,
  48. 'project' => isset($project) ? ['id' => $project?->id ,'name' => $project?->name] : null,
  49. 'asset' => isset($asset) ? ['id' => $asset?->id ,'name' => $asset?->name] : null,
  50. ];
  51. }
  52. }