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.

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