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.

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