Просмотр файла app/Console/Commands/AddBirthdays.php

Размер файла: 2.13Kb
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use App\Models\Mailing;
  6. use App\Models\User;
  7. use Illuminate\Console\Command;
  8.  
  9. class AddBirthdays extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'add:birthdays';
  17.  
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Add birthdays';
  24.  
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34.  
  35. /**
  36. * Добавляет именинников в рассылку
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. $deliveryUsers = User::query()
  43. ->where('point', '>', 0)
  44. ->whereIn('level', User::USER_GROUPS)
  45. ->whereRaw('substr(birthday, 1, 5) = ?', date('d.m', SITETIME))
  46. ->whereNotNull('subscribe')
  47. ->get();
  48.  
  49. if ($deliveryUsers->isNotEmpty()) {
  50. foreach ($deliveryUsers as $user) {
  51. $subject = 'С днем рождения от ' . setting('title');
  52.  
  53. $text = 'Здравствуйте ' . e($user->getName()) . '!<br>Поздравляем Вас с Днём рождения и желаем счастья, здоровья, новых идей, творческого настроения и побольше радости и смеха!<br><br>Администрация сайта ' . setting('title') . '<br><br><small>Если вы не хотите получать эти email, пожалуйста, <a href="' . config('app.url') . '/unsubscribe?key=' . $user->subscribe . '">откажитесь от подписки</a></small>';
  54.  
  55. Mailing::query()->create([
  56. 'user_id' => $user->id,
  57. 'type' => 'birthdays',
  58. 'subject' => $subject,
  59. 'text' => $text,
  60. 'created_at' => SITETIME,
  61. ]);
  62. }
  63. }
  64.  
  65. $this->info('Birthdays successfully added.');
  66.  
  67. return 0;
  68. }
  69. }