Просмотр файла app/Models/Offer.php

Размер файла: 3.62Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8. use Illuminate\Database\Eloquent\Relations\HasMany;
  9. use Illuminate\Database\Eloquent\Relations\MorphMany;
  10. use Illuminate\Database\Eloquent\Relations\MorphOne;
  11. use Illuminate\Support\HtmlString;
  12.  
  13. /**
  14. * Class Offer
  15. *
  16. * @property int id
  17. * @property string type
  18. * @property string title
  19. * @property string text
  20. * @property int user_id
  21. * @property int rating
  22. * @property int created_at
  23. * @property string status
  24. * @property int count_comments
  25. * @property int closed
  26. * @property string reply
  27. * @property int reply_user_id
  28. * @property int updated_at
  29. */
  30. class Offer extends BaseModel
  31. {
  32. public const DONE = 'done';
  33. public const WAIT = 'wait';
  34. public const CANCEL = 'cancel';
  35. public const PROCESS = 'process';
  36.  
  37. /**
  38. * Статусы
  39. */
  40. public const STATUSES = [
  41. self::DONE,
  42. self::WAIT,
  43. self::CANCEL,
  44. self::PROCESS,
  45. ];
  46.  
  47. public const OFFER = 'offer';
  48. public const ISSUE = 'issue';
  49.  
  50. /**
  51. * Типы
  52. */
  53. public const TYPES = [
  54. self::OFFER,
  55. self::ISSUE,
  56. ];
  57.  
  58. /**
  59. * Indicates if the model should be timestamped.
  60. *
  61. * @var bool
  62. */
  63. public $timestamps = false;
  64.  
  65. /**
  66. * The attributes that aren't mass assignable.
  67. *
  68. * @var array
  69. */
  70. protected $guarded = [];
  71.  
  72. /**
  73. * Morph name
  74. *
  75. * @var string
  76. */
  77. public static $morphName = 'offers';
  78.  
  79. /**
  80. * Возвращает связь с голосованием
  81. *
  82. * @return morphOne
  83. */
  84. public function polling(): morphOne
  85. {
  86. return $this->morphOne(Polling::class, 'relate')->where('user_id', getUser('id'));
  87. }
  88.  
  89. /**
  90. * Возвращает связь с комментариями
  91. *
  92. * @return MorphMany
  93. */
  94. public function comments(): MorphMany
  95. {
  96. return $this->morphMany(Comment::class, 'relate')->with('relate', 'user');
  97. }
  98.  
  99. /**
  100. * Возвращает связь пользователей
  101. *
  102. * @return BelongsTo
  103. */
  104. public function replyUser(): BelongsTo
  105. {
  106. return $this->belongsTo(User::class, 'reply_user_id')->withDefault();
  107. }
  108.  
  109. /**
  110. * Возвращает последнии комментарии
  111. *
  112. * @param int $limit
  113. * @return HasMany
  114. */
  115. public function lastComments(int $limit = 15): HasMany
  116. {
  117. return $this->hasMany(Comment::class, 'relate_id')
  118. ->where('relate_type', self::$morphName)
  119. ->orderBy('created_at', 'desc')
  120. ->with('user')
  121. ->limit($limit);
  122. }
  123.  
  124. /**
  125. * Возвращает статус записи
  126. *
  127. * @return HtmlString
  128. */
  129. public function getStatus(): HtmlString
  130. {
  131. switch ($this->status) {
  132. case 'process':
  133. $status = '<span class="font-weight-bold text-primary"><i class="fa fa-spinner"></i> ' . __('offers.process') . '</span>';
  134. break;
  135. case 'done':
  136. $status = '<span class="font-weight-bold text-success"><i class="fa fa-check-circle"></i> ' . __('offers.done') . '</span>';
  137. break;
  138. case 'cancel':
  139. $status = '<span class="font-weight-bold text-danger"><i class="fa fa-times-circle"></i> ' . __('offers.cancel') . '</span>';
  140. break;
  141. default:
  142. $status = '<span class="font-weight-bold text-warning"><i class="fa fa-question-circle"></i> ' . __('offers.wait') . '</span>';
  143. }
  144.  
  145. return new HtmlString($status);
  146. }
  147. }