Project.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Casts\Attribute;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. use Illuminate\Support\Facades\Storage;
  7. use Spatie\Translatable\HasTranslations;
  8. class Project extends Model
  9. {
  10. use HasTranslations, SoftDeletes;
  11. protected $guarded = ['id'];
  12. protected $translatable = ['name', 'sub_name', 'summaries', 'img_alt', 'address', 'floor_plan',
  13. 'building_structure', 'design_unit', 'contact_unit', 'contact_phone', 'inversment_phone', 'district',
  14. ];
  15. protected $casts = ['img_url' => 'array', 'mobile_img_url' => 'array'];
  16. protected $appends = ['first_list_img_url', 'img_list', 'thumbnail_url', 'first_mobile_img_url', 'mobile_img_list'];
  17. public function region()
  18. {
  19. return $this->belongsTo(Region::class);
  20. }
  21. public function tags()
  22. {
  23. return $this->morphToMany(Tag::class, 'taggable');
  24. }
  25. public function badges()
  26. {
  27. return $this->morphToMany(Badge::class, 'badgeable')
  28. ->withPivot('award_date', 'award_type', 'sort_order')
  29. ->orderByPivot('sort_order')
  30. ->withTimestamps();
  31. }
  32. /**
  33. * 永續目標
  34. */
  35. public function targetBadges()
  36. {
  37. return $this->morphToMany(Badge::class, 'badgeable')
  38. ->withPivot('award_type', 'sort_order')
  39. ->wherePivot('award_type', 1)
  40. ->orderByPivot('sort_order')
  41. ->withTimestamps();
  42. }
  43. /**
  44. * 取得標章
  45. */
  46. public function awardBadges()
  47. {
  48. return $this->morphToMany(Badge::class, 'badgeable')
  49. ->withPivot('award_type', 'award_date', 'sort_order')
  50. ->wherePivot('award_type', 2)
  51. ->orderByPivot('sort_order')
  52. ->withTimestamps();
  53. }
  54. public function histories()
  55. {
  56. return $this->hasMany(ProjectHistory::class)->orderBy('order');
  57. }
  58. public function spaceInfos()
  59. {
  60. return $this->hasMany(ProjectSpaceInfo::class)->orderBy('order');
  61. }
  62. public function firstListImgUrl(): Attribute
  63. {
  64. return Attribute::make(
  65. get: fn ($value) => ! empty($this->img_url) ? Storage::url($this->img_url[0]) : null,
  66. );
  67. }
  68. public function imgList(): Attribute
  69. {
  70. $imgList = [];
  71. if (! is_null($this->img_url) && count($this->img_url) > 0) {
  72. foreach ($this->img_url as $img) {
  73. $imgList[] = Storage::url($img);
  74. }
  75. }
  76. return Attribute::make(
  77. get: fn ($value) => $imgList,
  78. );
  79. }
  80. public function thumbnailUrl(): Attribute
  81. {
  82. return Attribute::make(
  83. get: fn ($value) => ! empty($this->thumbnail) ? Storage::url($this->thumbnail) : null,
  84. );
  85. }
  86. public function firstMobileImgUrl(): Attribute
  87. {
  88. return Attribute::make(
  89. get: fn ($value) => ! empty($this->mobile_img_url) ? Storage::url($this->mobile_img_url[0]) : null,
  90. );
  91. }
  92. public function mobileImgList(): Attribute
  93. {
  94. $imgList = [];
  95. if (! is_null($this->mobile_img_url) && count($this->mobile_img_url) > 0) {
  96. foreach ($this->mobile_img_url as $img) {
  97. $imgList[] = Storage::url($img);
  98. }
  99. }
  100. return Attribute::make(
  101. get: fn ($value) => $imgList,
  102. );
  103. }
  104. public function getBadges($locale): array
  105. {
  106. $badges = [];
  107. if ($this->badges->count() > 0) {
  108. foreach ($this->badges as $badge) {
  109. $badges[] = [
  110. 'imgUrl' => $badge->imgUrlLink,
  111. 'name' => $badge->getTranslation('title', $locale),
  112. 'awardDate' => '取得時間 : '.date('Y.m', strtotime($badge->pivot->award_date)),
  113. ];
  114. }
  115. }
  116. return $badges;
  117. }
  118. public function getBadgesByType($locale, $badgeType): array
  119. {
  120. $badges = [];
  121. switch ($badgeType) {
  122. case '1':
  123. $badgesData = $this->targetBadges;
  124. foreach ($badgesData as $badge) {
  125. $badges[] = [
  126. 'imgUrl' => $badge->imgUrlLink,
  127. 'name' => $badge->getTranslation('title', $locale),
  128. 'awardDate' => null,
  129. ];
  130. }
  131. break;
  132. case '2':
  133. $badgesData = $this->awardBadges;
  134. foreach ($badgesData as $badge) {
  135. $badges[] = [
  136. 'imgUrl' => $badge->imgUrlLink,
  137. 'name' => $badge->getTranslation('title', $locale),
  138. 'awardDate' => '取得時間 : '.date('Y.m', strtotime($badge->pivot->award_date)),
  139. ];
  140. }
  141. break;
  142. }
  143. return $badges;
  144. }
  145. public function getTags($locale): array
  146. {
  147. $tags = [];
  148. if ($this->tags->count() > 0) {
  149. foreach ($this->tags as $tag) {
  150. $tags[] = [
  151. 'id' => $tag->id,
  152. 'name' => $tag->getTranslation('name', $locale),
  153. ];
  154. }
  155. }
  156. return $tags;
  157. }
  158. }