You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.6 KiB

  1. <?php
  2. namespace App\Models;
  3. use App\Models\File;
  4. use App\Models\Model;
  5. use App\Models\SoftDeletes;
  6. use App\Models\ReportableRelation;
  7. use Illuminate\Validation\Rule;
  8. use Illuminate\Http\UploadedFile;
  9. use Spatie\MediaLibrary\HasMedia;
  10. use Spatie\MediaLibrary\InteractsWithMedia;
  11. use Spatie\MediaLibrary\MediaCollections\Models\Media;
  12. class Project extends Model implements HasMedia
  13. {
  14. use SoftDeletes,
  15. InteractsWithMedia;
  16. const CONVERSION_NAME = 'avatar';
  17. const COLLECTION_NAME = 'avatars';
  18. public static $permissions = ['level'];
  19. protected $table = 'projects';
  20. protected $fillable = [
  21. 'business_id', 'name', 'slug', 'private', 'budget', 'color', 'active', 'description', 'has_avatar', 'start', 'finish','members'
  22. ];
  23. protected $reportable = [
  24. 'business_id', 'name', 'slug', ['members' => 'project_user']
  25. ];
  26. protected $fillable_relations = ['members'];
  27. public $detach_relation = false;
  28. public function rules()
  29. {
  30. return [
  31. 'name' => 'required|string|min:2|max:225',
  32. 'slug' => ['required', 'string', 'min:2', 'max:225',
  33. Rule::unique($this->table, 'slug')
  34. ->where('business_id', request('business_id'))
  35. ->whereNull('deleted_at')
  36. ->ignore($this->id)],
  37. 'order' => 'nullable|numeric|min:0',
  38. 'private' => 'nullable|boolean',
  39. 'color' => 'nullable|string|min:2|max:255',
  40. 'active' => 'nullable|boolean',
  41. 'description' => 'nullable|string|min:2|max:1000',
  42. // 'members' => empty($this->id) ? '' : 'required|array'
  43. ];
  44. }
  45. protected $casts = [
  46. 'private' => 'boolean',
  47. 'start' => 'date',
  48. 'finish' => 'date',
  49. 'has_avatar' => 'boolean',
  50. ];
  51. public function getValueOf(?string $key)
  52. {
  53. $values = [
  54. 'business_id' => $this->business_id,
  55. 'project_id' => $this->id,
  56. 'sprint_id' => null,
  57. 'workflow_id' => null,
  58. 'status_id' => null,
  59. 'system_id' => null,
  60. 'actor_id' => auth()->id(),
  61. 'user_id' => null,
  62. 'task_id' => null,
  63. 'subject_id' => $this->id,
  64. ];
  65. if ($key && isset($values, $key)) {
  66. return $values[$key];
  67. }
  68. return $values;
  69. }
  70. public function members()
  71. {
  72. $permissions = self::$permissions;
  73. return $this->belongsToMany(User::class)->using(ReportableRelation::class)
  74. ->withPivot($permissions);
  75. }
  76. public function owners()
  77. {
  78. return $this->members()->wherePivot('level', '=', enum('levels.owner.id'));
  79. }
  80. public function tasks()
  81. {
  82. return $this->hasMany(Task::class, 'project_id', 'id');
  83. }
  84. public function business()
  85. {
  86. return $this->belongsTo(Business::class, 'business_id', 'id');
  87. }
  88. public function systems()
  89. {
  90. return $this->hasMany(System::class, 'project_id', 'id');
  91. }
  92. public function sprints()
  93. {
  94. return $this->hasMany(Sprint::class, 'project_id', 'id');
  95. }
  96. public function files()
  97. {
  98. return $this->hasMany(File::class, 'user_id', 'id');
  99. }
  100. public function updateRelations()
  101. {
  102. // members
  103. // if (!empty($this->filled_relations['members']) || $this->detach_relation) {
  104. // $this->dirties['members'] = $this->members()->sync($this->filled_relations['members'], $this->detach_relation);
  105. // }
  106. }
  107. public function reportActivity()
  108. {
  109. // foreach ($this->dirties as $name => $value) {
  110. // return \post('task', 'task/v1/log', [
  111. // 'user_id' => Auth::id(),
  112. // 'business_id' => $this->business_id,
  113. // 'loggable_id' => $this->id,
  114. // 'loggable_type' => $this->getTypeId(),
  115. // 'action' => $this->getAction(), // id of the action
  116. // 'data' => [$name => [
  117. // 'original' => $value['original'],
  118. // 'diff' => $value['diff'],
  119. // ]],
  120. // ]);
  121. // }
  122. }
  123. public function getPermissions($user_id)
  124. {
  125. return $this->members->where('id',$user_id)->first();
  126. }
  127. public function registerMediaCollections(): void
  128. {
  129. $this->addMediaCollection(static::COLLECTION_NAME)
  130. ->acceptsMimeTypes([
  131. 'image/jpeg',
  132. 'image/png',
  133. 'image/tiff',
  134. 'image/gif',
  135. ])
  136. ->useDisk('public')
  137. ->singleFile();
  138. }
  139. public function registerMediaConversions(Media $media = null): void
  140. {
  141. $this->addMediaConversion(static::CONVERSION_NAME)
  142. ->width(200)
  143. ->height(200)
  144. ->queued()
  145. ->nonOptimized()
  146. ->performOnCollections(static::COLLECTION_NAME);
  147. }
  148. public function saveAsAvatar(UploadedFile $avatar): void
  149. {
  150. $this->addMedia($avatar)->toMediaCollection(static::COLLECTION_NAME);
  151. $this->update([
  152. 'has_avatar' => true,
  153. ]);
  154. @unlink($this->getFirstMedia(static::COLLECTION_NAME)->getPath());
  155. }
  156. public function deleteAvatar(): void
  157. {
  158. $path = $this->getFirstMedia(static::COLLECTION_NAME)->getPath();
  159. $this->getFirstMedia(static::COLLECTION_NAME)->delete();
  160. $this->update([
  161. 'has_avatar' => false,
  162. ]);
  163. @unlink($path);
  164. }
  165. public function getAvatarUrl(): ?string
  166. {
  167. if ($url = $this->getFirstMediaUrl(static::COLLECTION_NAME, static::CONVERSION_NAME)) {
  168. return $url;
  169. }
  170. return null;
  171. }
  172. }