Save.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\Poll\Poll;
  12. use ForkBB\Models\Topic\Topic;
  13. use RuntimeException;
  14. class Save extends Action
  15. {
  16. /**
  17. * Обновляет опрос в БД
  18. */
  19. public function update(Poll $poll): Poll
  20. {
  21. $poll->itWasModified = false;
  22. if (true !== $this->manager->revision($poll, true)) {
  23. throw new RuntimeException('The poll model has errors');
  24. }
  25. $old = $this->manager->Load->load($poll->tid);
  26. if (! $old instanceof Poll) {
  27. throw new RuntimeException('No such poll found');
  28. }
  29. $vars = [
  30. ':tid' => $poll->tid,
  31. ];
  32. $queryIn = 'INSERT INTO ::poll (tid, question_id, field_id, qna_text)
  33. VALUES (?i:tid, ?i:qid, ?i:fid, ?s:qna)';
  34. $queryU1 = 'UPDATE ::poll
  35. SET qna_text=?s:qna
  36. WHERE tid=?i:tid AND question_id=?i:qid AND field_id=?i:fid';
  37. $queryD1 = 'DELETE FROM ::poll
  38. WHERE tid=?i:tid AND question_id IN(?ai:qids)';
  39. $queryD2 = 'DELETE FROM ::poll
  40. WHERE tid=?i:tid AND question_id=?i:qid AND field_id IN(?ai:fid)';
  41. $modified = false;
  42. $oldQuestion = $old->question;
  43. $oldType = $old->type;
  44. foreach ($poll->question as $qid => $qna) {
  45. $vars[':qid'] = $qid;
  46. $vars[':fid'] = 0;
  47. $vars[':qna'] = $poll->type[$qid] . '|' . $qna;
  48. if (! isset($oldQuestion[$qid])) {
  49. $modified = true;
  50. $this->c->DB->exec($queryIn, $vars);
  51. } elseif (
  52. $qna !== $oldQuestion[$qid]
  53. || $poll->type[$qid] !== $oldType[$qid]
  54. ) {
  55. $modified = true;
  56. $this->c->DB->exec($queryU1, $vars);
  57. }
  58. $oldAnswer = $old->answer[$qid] ?? [];
  59. foreach ($poll->answer[$qid] as $fid => $qna) {
  60. $vars[':fid'] = $fid;
  61. $vars[':qna'] = $qna;
  62. if (! isset($oldAnswer[$fid])) {
  63. $modified = true;
  64. $this->c->DB->exec($queryIn, $vars);
  65. } elseif ($qna !== $oldAnswer[$fid]) {
  66. $modified = true;
  67. $this->c->DB->exec($queryU1, $vars);
  68. }
  69. unset($oldAnswer[$fid]);
  70. }
  71. if (! empty($oldAnswer)) {
  72. $modified = true;
  73. $vars[':fid'] = \array_keys($oldAnswer);
  74. $this->c->DB->exec($queryD2, $vars);
  75. }
  76. unset($oldQuestion[$qid]);
  77. }
  78. if (! empty($oldQuestion)) {
  79. $modified = true;
  80. $vars[':qid'] = \array_keys($oldQuestion);
  81. $this->c->DB->exec($queryD1, $vars);
  82. }
  83. $poll->itWasModified = $modified;
  84. $poll->resModified();
  85. return $poll;
  86. }
  87. /**
  88. * Добавляет новый опрос в БД
  89. */
  90. public function insert(Poll $poll): int
  91. {
  92. if (true !== $this->manager->revision($poll, true)) {
  93. throw new RuntimeException('The poll model has errors');
  94. }
  95. if (null !== $this->manager->Load->load($poll->tid)) {
  96. throw new RuntimeException('Such the poll already exists');
  97. }
  98. $vars = [
  99. ':tid' => $poll->tid,
  100. ];
  101. $query = 'INSERT INTO ::poll (tid, question_id, field_id, qna_text)
  102. VALUES (?i:tid, ?i:qid, ?i:fid, ?s:qna)';
  103. foreach ($poll->question as $qid => $qna) {
  104. $vars[':qid'] = $qid;
  105. $vars[':fid'] = 0;
  106. $vars[':qna'] = $poll->type[$qid] . '|' . $qna;
  107. $this->c->DB->exec($query, $vars);
  108. foreach ($poll->answer[$qid] as $fid => $qna) {
  109. $vars[':fid'] = $fid;
  110. $vars[':qna'] = $qna;
  111. $this->c->DB->exec($query, $vars);
  112. }
  113. }
  114. $poll->resModified();
  115. return $poll->tid;
  116. }
  117. }