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

Размер файла: 2.4Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use Illuminate\Database\Eloquent\Casts\AsArrayObject;
  8. use Illuminate\Support\Facades\Cache;
  9.  
  10. /**
  11. * Class PaidAdvert
  12. *
  13. * @property int id
  14. * @property string place
  15. * @property string site
  16. * @property array names
  17. * @property string color
  18. * @property int bold
  19. * @property int user_id
  20. * @property int created_at
  21. * @property int deleted_at
  22. */
  23. class PaidAdvert extends BaseModel
  24. {
  25. public const TOP_ALL = 'top_all';
  26. public const TOP = 'top';
  27. public const FORUM = 'forum';
  28. public const BOTTOM_ALL = 'bottom_all';
  29. public const BOTTOM = 'bottom';
  30.  
  31. /**
  32. * Места размещения
  33. */
  34. public const PLACES = [
  35. self::TOP_ALL,
  36. self::TOP,
  37. self::FORUM,
  38. self::BOTTOM_ALL,
  39. self::BOTTOM,
  40. ];
  41.  
  42. /**
  43. * The attributes that should be cast to native types.
  44. *
  45. * @var array
  46. */
  47. protected $casts = [
  48. 'names' => 'array',
  49. ];
  50.  
  51. /**
  52. * Indicates if the model should be timestamped.
  53. *
  54. * @var bool
  55. */
  56. public $timestamps = false;
  57.  
  58. /**
  59. * The attributes that aren't mass assignable.
  60. *
  61. * @var array
  62. */
  63. protected $guarded = [];
  64.  
  65. /**
  66. * Кэширует ссылки платной рекламы
  67. *
  68. * @return array Список ссылок
  69. */
  70. public static function statAdverts(): array
  71. {
  72. return Cache::remember('paidAdverts', 3600, static function () {
  73. $data = self::query()->where('deleted_at', '>', SITETIME)->orderBy('created_at')->get();
  74.  
  75. $links = [];
  76. if ($data->isNotEmpty()) {
  77. foreach ($data as $val) {
  78. $names = check($val->names);
  79.  
  80. $sites = [];
  81. foreach ($names as $name) {
  82. if ($val->color) {
  83. $name = '<span style="color:' . $val->color . '">' . $name . '</span>';
  84. }
  85.  
  86. $link = '<a href="' . $val->site . '" target="_blank">' . $name . '</a>';
  87.  
  88. if ($val->bold) {
  89. $link = '<b>' . $link . '</b>';
  90. }
  91.  
  92. $sites[] = $link;
  93. }
  94.  
  95. $links[$val->place][] = $sites;
  96. }
  97. }
  98.  
  99. return $links;
  100. });
  101. }
  102. }