Просмотр файла app/Models/User.php

Размер файла: 3.62Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. /**
  8. * Class User
  9. *
  10. * @property int $id
  11. * @property string $login
  12. * @property string $password
  13. * @property string $email
  14. * @property string $role
  15. * @property string $name
  16. * @property string $picture
  17. * @property string $avatar
  18. * @property bool $confirmed
  19. * @property string $confirm_code
  20. * @property int $created_at
  21. */
  22. class User extends Model
  23. {
  24. /**
  25. * Table name
  26. */
  27. protected string $table = 'users.csv';
  28.  
  29. public const BOSS = 'boss'; // Владелец
  30. public const ADMIN = 'admin'; // Админ
  31. public const MODER = 'moder'; // Модератор
  32. public const EDITOR = 'editor'; // Редактор
  33. public const USER = 'user'; // Пользователь
  34. public const PENDED = 'pended'; // Ожидающий
  35. public const BANNED = 'banned'; // Забаненный
  36.  
  37. /**
  38. * All group
  39. */
  40. public const ROLES = [
  41. self::BOSS,
  42. self::ADMIN,
  43. self::MODER,
  44. self::EDITOR,
  45. self::USER,
  46. self::PENDED,
  47. self::BANNED,
  48. ];
  49.  
  50. /**
  51. * All group
  52. */
  53. public const ADMIN_ROLES = [
  54. self::BOSS,
  55. self::ADMIN,
  56. self::MODER,
  57. self::EDITOR,
  58. ];
  59.  
  60. /**
  61. * Genders
  62. */
  63. public const MALE = 'male';
  64. public const FEMALE = 'female';
  65.  
  66. /**
  67. * The attributes that should be cast.
  68. */
  69. protected array $casts = [
  70. 'confirmed' => 'bool',
  71. ];
  72.  
  73. /**
  74. * Get name
  75. *
  76. * @return string
  77. */
  78. public function getName(): string
  79. {
  80. if (! $this->id) {
  81. return setting('main.delete_name');
  82. }
  83.  
  84. return escape($this->name ?? $this->login);
  85. }
  86.  
  87. /**
  88. * Get profile link
  89. *
  90. * @return string
  91. */
  92. public function getProfile(): string
  93. {
  94. if (! $this->id) {
  95. return setting('main.delete_name');
  96. }
  97.  
  98. return '<a href="/users/' . $this->login . '">' . $this->getName() . '</a>';
  99. }
  100.  
  101. /**
  102. * Get avatar
  103. *
  104. * @return string
  105. */
  106. public function getAvatar(): string
  107. {
  108. if (! $this->id) {
  109. return '<img class="avatar-default rounded-circle" src="/assets/images/avatar_guest.png" alt="Аватар">';
  110. }
  111.  
  112. if ($this->avatar && file_exists(publicPath($this->avatar))) {
  113. $avatar = '<img class="avatar-default rounded-circle" src="' . $this->avatar . '" alt="Аватар">';
  114. } else {
  115. $avatar = '<img class="avatar-default rounded-circle" src="/assets/images/avatar_default.png" alt="Аватар">';
  116. }
  117.  
  118. return $avatar;
  119. }
  120.  
  121. /**
  122. * Get role
  123. *
  124. * @return string
  125. */
  126. public function getRole(): string
  127. {
  128. if (! $this->id) {
  129. return setting('roles.user');
  130. }
  131.  
  132. return setting('roles.' . $this->role);
  133. }
  134.  
  135. /**
  136. * Check is banned
  137. *
  138. * @return bool
  139. */
  140. public function isBanned(): bool
  141. {
  142. return $this->role === self::BANNED;
  143. }
  144.  
  145. /**
  146. * Check is pended
  147. *
  148. * @return bool
  149. */
  150. public function isPended(): bool
  151. {
  152. return $this->role === self::PENDED;
  153. }
  154.  
  155. /**
  156. * Delete story
  157. *
  158. * @return int
  159. */
  160. public function delete(): int
  161. {
  162. // delete photo
  163. if ($this->picture && file_exists(publicPath($this->picture))) {
  164. unlink(publicPath($this->picture));
  165. }
  166.  
  167. // delete avatar
  168. if ($this->avatar && file_exists(publicPath($this->avatar))) {
  169. unlink(publicPath($this->avatar));
  170. }
  171.  
  172. return parent::delete();
  173. }
  174. }