Load.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Report;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\Report\Report;
  12. use InvalidArgumentException;
  13. class Load extends Action
  14. {
  15. /**
  16. * Загружает сигнал из БД
  17. */
  18. public function load(int $id): ?Report
  19. {
  20. if ($id < 1) {
  21. throw new InvalidArgumentException('Expected a positive report id');
  22. }
  23. $vars = [
  24. ':id' => $id,
  25. ];
  26. $query = 'SELECT r.*
  27. FROM ::reports AS r
  28. WHERE r.id=?i:id';
  29. $data = $this->c->DB->query($query, $vars)->fetch();
  30. if (empty($data)) {
  31. return null;
  32. }
  33. $report = $this->manager->create($data);
  34. return $report;
  35. }
  36. /**
  37. * Загрузка сигналов из БД
  38. */
  39. public function loadList(bool $noZapped): array
  40. {
  41. $result = [];
  42. $vars = [];
  43. if ($noZapped) {
  44. $query = 'SELECT r.*
  45. FROM ::reports AS r
  46. WHERE r.zapped=0
  47. ORDER BY r.id DESC';
  48. } else {
  49. $query = 'SELECT r.*
  50. FROM ::reports AS r
  51. WHERE r.zapped!=0
  52. ORDER BY r.zapped DESC'; // LIMIT 10 не нужен, если при обработке сигнала будут удалены старые
  53. }
  54. $data = $this->c->DB->query($query, $vars)->fetchAll();
  55. foreach ($data as $row) {
  56. $result[] = $this->manager->create($row);
  57. }
  58. return $result;
  59. }
  60. }