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.

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