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.

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