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.

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