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.

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