Delete.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Poll;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\DataModel;
  12. use ForkBB\Models\Forum\Forum;
  13. use ForkBB\Models\Poll\Poll;
  14. use ForkBB\Models\Topic\Topic;
  15. use InvalidArgumentException;
  16. use RuntimeException;
  17. class Delete extends Action
  18. {
  19. /**
  20. * Удаление индекса
  21. */
  22. public function delete(DataModel ...$args): void
  23. {
  24. if (empty($args)) {
  25. throw new InvalidArgumentException('No arguments, expected Poll(s) or Topic(s)');
  26. }
  27. $tids = [];
  28. $isPoll = 0;
  29. $isTopic = 0;
  30. foreach ($args as $arg) {
  31. if ($arg instanceof Poll) {
  32. $arg->parent; // проверка доступности опроса
  33. $tids[$arg->tid] = $arg->tid;
  34. $isPoll = 1;
  35. } elseif ($arg instanceof Topic) {
  36. if (! $arg->parent instanceof Forum) {
  37. throw new RuntimeException('Parent unavailable');
  38. }
  39. $tids[$arg->id] = $arg->id;
  40. $isTopic = 1;
  41. } else {
  42. throw new InvalidArgumentException('Expected Poll(s) or Topic(s)');
  43. }
  44. }
  45. if ($isPoll + $isTopic > 1) {
  46. throw new InvalidArgumentException('Expected only Poll(s) or Topic(s)');
  47. }
  48. $vars = [
  49. ':tids' => $tids,
  50. ];
  51. $query = 'DELETE
  52. FROM ::poll
  53. WHERE tid IN (?ai:tids)';
  54. $this->c->DB->exec($query, $vars);
  55. $query = 'DELETE
  56. FROM ::poll_voted
  57. WHERE tid IN (?ai:tids)';
  58. $this->c->DB->exec($query, $vars);
  59. foreach ($tids as $tid) {
  60. $this->manager->reset($tid);
  61. }
  62. }
  63. }