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.

184 lines
5.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Models;
  3. use App\Models\File;
  4. use App\Models\Model;
  5. use App\Models\SoftDeletes;
  6. use Illuminate\Validation\Rule;
  7. use Illuminate\Http\UploadedFile;
  8. use Spatie\MediaLibrary\HasMedia;
  9. use App\Models\ReportableRelation;
  10. use Illuminate\Auth\Authenticatable;
  11. use Spatie\MediaLibrary\InteractsWithMedia;
  12. use Illuminate\Foundation\Auth\Access\Authorizable;
  13. use Spatie\MediaLibrary\MediaCollections\Models\Media;
  14. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  15. use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  16. class User extends Model implements AuthenticatableContract, AuthorizableContract, HasMedia
  17. {
  18. use SoftDeletes,
  19. Authorizable,
  20. Authenticatable,
  21. InteractsWithMedia;
  22. const CONVERSION_NAME = 'avatar';
  23. const COLLECTION_NAME = 'avatars';
  24. public $casts = [
  25. 'has_avatar' => 'boolean',
  26. ];
  27. protected $fillable = ['name', 'email','mobile', 'username','password','active','has_avatar'];
  28. protected $fillable_relations = ['projects'];
  29. protected $reportable = [
  30. 'name', 'username', 'mobile', 'email', // fields
  31. ['projects' => 'project_user']
  32. ];
  33. public $detach_relation = false;
  34. public function updateRelations()
  35. {
  36. // projects relations
  37. if (!empty($this->filled_relations['projects']) || $this->detach_relation) {
  38. $this->dirties['projects'] = $this->projects()->sync($this->filled_relations['projects'], $this->detach_relation);
  39. }
  40. }
  41. /** =============================== Validations ======================== */
  42. public function rules()
  43. {
  44. return [
  45. 'name' => 'required|string|max:225|min:2',
  46. 'username' => ['required', Rule::unique('users', 'username')->ignore($this->id)],
  47. 'email' => ['required', 'email', Rule::unique('users', 'email')->ignore($this->id)],
  48. 'password' => ['required','string','min:8']
  49. ];
  50. }
  51. /** =============================== End Validations ==================== */
  52. /** =============================== Relations ========================== */
  53. public function fingerprints()
  54. {
  55. return $this->hasMany(Fingerprint::class, 'user_id', 'id');
  56. }
  57. public function businesses()
  58. {
  59. return $this->belongsToMany(
  60. Business::class,
  61. 'business_user',
  62. 'user_id',
  63. 'business_id',
  64. 'id',
  65. 'id',
  66. __FUNCTION__
  67. );
  68. }
  69. public function tasks()
  70. {
  71. return $this->hasMany(Task::class, 'user_id', 'id');
  72. }
  73. public function projects()
  74. {
  75. return $this->belongsToMany(
  76. Project::class,
  77. 'project_user',
  78. 'user_id',
  79. 'project_id',
  80. 'id',
  81. 'id',
  82. __FUNCTION__
  83. )->using(ReportableRelation::class);
  84. }
  85. public function files()
  86. {
  87. return $this->hasMany(File::class, 'user_id','id');
  88. }
  89. /** =============================== End Relations ====================== */
  90. public function getValueOf(?string $key)
  91. {
  92. $values = [
  93. 'business_id' => request('_business_info')['id'] ?? null,
  94. 'user_id' => $this->id,
  95. 'workflow_id' => null,
  96. 'project_id' => null,
  97. 'sprint_id' => null,
  98. 'system_id' => null,
  99. 'status_id' => null,
  100. 'task_id' => null,
  101. 'subject_id' => $this->id,
  102. ];
  103. if ($key && isset($values, $key)) {
  104. return $values[$key];
  105. }
  106. return $values;
  107. }
  108. public function registerMediaCollections(): void
  109. {
  110. $this->addMediaCollection(static::COLLECTION_NAME)
  111. ->acceptsMimeTypes([
  112. 'image/jpeg',
  113. 'image/png',
  114. 'image/tiff',
  115. 'image/gif',
  116. ])
  117. ->useDisk('public')
  118. ->singleFile();
  119. }
  120. public function registerMediaConversions(Media $media = null): void
  121. {
  122. $this->addMediaConversion(static::CONVERSION_NAME)
  123. ->width(200)
  124. ->height(200)
  125. ->queued()
  126. ->nonOptimized()
  127. ->performOnCollections(static::COLLECTION_NAME);
  128. }
  129. public function saveAsAvatar(UploadedFile $avatar): void
  130. {
  131. $this->addMedia($avatar)->toMediaCollection(static::COLLECTION_NAME);
  132. $this->update([
  133. 'has_avatar' => true,
  134. ]);
  135. @unlink($this->getFirstMedia(static::COLLECTION_NAME)->getPath());
  136. }
  137. public function deleteAvatar(): void
  138. {
  139. $path = $this->getFirstMedia(static::COLLECTION_NAME)->getPath();
  140. $this->getFirstMedia(static::COLLECTION_NAME)->delete();
  141. $this->update([
  142. 'has_avatar' => false,
  143. ]);
  144. @unlink($path);
  145. }
  146. public function getAvatarUrl(): ?string
  147. {
  148. if ($url = $this->getFirstMediaUrl(static::COLLECTION_NAME, static::CONVERSION_NAME)) {
  149. return $url;
  150. }
  151. return null;
  152. }
  153. }