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.

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