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.

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