tagController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Controller to handle every tag actions.
  5. */
  6. class FreshRSS_tag_Controller extends FreshRSS_ActionController {
  7. /**
  8. * JavaScript request or not.
  9. */
  10. private bool $ajax = false;
  11. /**
  12. * This action is called before every other action in that class. It is
  13. * the common boilerplate for every action. It is triggered by the
  14. * underlying framework.
  15. */
  16. public function firstAction(): void {
  17. // If ajax request, we do not print layout
  18. $this->ajax = Minz_Request::paramBoolean('ajax');
  19. if ($this->ajax) {
  20. $this->view->_layout(null);
  21. }
  22. }
  23. /**
  24. * This action adds (checked=true) or removes (checked=false) a tag to an entry.
  25. */
  26. public function tagEntryAction(): void {
  27. if (!FreshRSS_Auth::hasAccess()) {
  28. Minz_Error::error(403);
  29. }
  30. if (Minz_Request::isPost()) {
  31. $id_tag = Minz_Request::paramInt('id_tag');
  32. $name_tag = Minz_Request::paramString('name_tag');
  33. $id_entry = Minz_Request::paramString('id_entry');
  34. $checked = Minz_Request::paramBoolean('checked');
  35. if ($id_entry != '') {
  36. $tagDAO = FreshRSS_Factory::createTagDao();
  37. if ($id_tag == 0 && $name_tag !== '' && $checked) {
  38. if ($existing_tag = $tagDAO->searchByName($name_tag)) {
  39. // Use existing tag
  40. $tagDAO->tagEntry($existing_tag->id(), $id_entry, $checked);
  41. } else {
  42. //Create new tag
  43. $id_tag = $tagDAO->addTag(['name' => $name_tag]);
  44. }
  45. }
  46. if ($id_tag != false) {
  47. $tagDAO->tagEntry($id_tag, $id_entry, $checked);
  48. }
  49. }
  50. } else {
  51. Minz_Error::error(405);
  52. }
  53. if (!$this->ajax) {
  54. Minz_Request::forward([
  55. 'c' => 'index',
  56. 'a' => 'index',
  57. ], true);
  58. }
  59. }
  60. public function deleteAction(): void {
  61. if (!FreshRSS_Auth::hasAccess()) {
  62. Minz_Error::error(403);
  63. }
  64. if (Minz_Request::isPost()) {
  65. $id_tag = Minz_Request::paramInt('id_tag');
  66. if ($id_tag !== 0) {
  67. $tagDAO = FreshRSS_Factory::createTagDao();
  68. $tagDAO->deleteTag($id_tag);
  69. }
  70. } else {
  71. Minz_Error::error(405);
  72. }
  73. if (!$this->ajax) {
  74. Minz_Request::forward([
  75. 'c' => 'tag',
  76. 'a' => 'index',
  77. ], true);
  78. }
  79. }
  80. /**
  81. * This action updates the given tag.
  82. */
  83. public function updateAction(): void {
  84. if (Minz_Request::paramBoolean('ajax')) {
  85. $this->view->_layout(null);
  86. }
  87. $tagDAO = FreshRSS_Factory::createTagDao();
  88. $id = Minz_Request::paramInt('id');
  89. $tag = $tagDAO->searchById($id);
  90. if ($id === 0 || $tag === null) {
  91. Minz_Error::error(404);
  92. return;
  93. }
  94. $this->view->tag = $tag;
  95. FreshRSS_View::prependTitle($tag->name() . ' · ' . _t('sub.title') . ' · ');
  96. if (Minz_Request::isPost()) {
  97. invalidateHttpCache();
  98. $ok = true;
  99. if ($tag->name() !== Minz_Request::paramString('name')) {
  100. $ok = $tagDAO->updateTagName($tag->id(), Minz_Request::paramString('name')) !== false;
  101. }
  102. if ($ok) {
  103. $tag->_filtersAction('label', Minz_Request::paramTextToArray('filteractions_label'));
  104. $ok = $tagDAO->updateTagAttributes($tag->id(), $tag->attributes()) !== false;
  105. }
  106. invalidateHttpCache();
  107. $url_redirect = ['c' => 'tag', 'a' => 'update', 'params' => ['id' => $id]];
  108. if ($ok) {
  109. Minz_Request::good(_t('feedback.tag.updated'), $url_redirect);
  110. } else {
  111. Minz_Request::bad(_t('feedback.tag.error'), $url_redirect);
  112. }
  113. }
  114. }
  115. public function getTagsForEntryAction(): void {
  116. if (!FreshRSS_Auth::hasAccess() && !FreshRSS_Context::systemConf()->allow_anonymous) {
  117. Minz_Error::error(403);
  118. }
  119. $this->view->_layout(null);
  120. header('Content-Type: application/json; charset=UTF-8');
  121. header('Cache-Control: private, no-cache, no-store, must-revalidate');
  122. $id_entry = Minz_Request::paramString('id_entry');
  123. $tagDAO = FreshRSS_Factory::createTagDao();
  124. $this->view->tagsForEntry = $tagDAO->getTagsForEntry($id_entry) ?: [];
  125. }
  126. public function addAction(): void {
  127. if (!FreshRSS_Auth::hasAccess()) {
  128. Minz_Error::error(403);
  129. }
  130. if (!Minz_Request::isPost()) {
  131. Minz_Error::error(405);
  132. }
  133. $name = Minz_Request::paramString('name');
  134. $tagDAO = FreshRSS_Factory::createTagDao();
  135. if (strlen($name) > 0 && null === $tagDAO->searchByName($name)) {
  136. $tagDAO->addTag(['name' => $name]);
  137. Minz_Request::good(_t('feedback.tag.created', $name), ['c' => 'tag', 'a' => 'index']);
  138. }
  139. Minz_Request::bad(_t('feedback.tag.name_exists', $name), ['c' => 'tag', 'a' => 'index']);
  140. }
  141. /**
  142. * @throws Minz_ConfigurationNamespaceException
  143. * @throws Minz_PDOConnectionException
  144. */
  145. public function renameAction(): void {
  146. if (!FreshRSS_Auth::hasAccess()) {
  147. Minz_Error::error(403);
  148. }
  149. if (!Minz_Request::isPost()) {
  150. Minz_Error::error(405);
  151. }
  152. $targetName = Minz_Request::paramString('name');
  153. $sourceId = Minz_Request::paramInt('id_tag');
  154. if ($targetName == '' || $sourceId == 0) {
  155. Minz_Error::error(400);
  156. return;
  157. }
  158. $tagDAO = FreshRSS_Factory::createTagDao();
  159. $sourceTag = $tagDAO->searchById($sourceId);
  160. $sourceName = $sourceTag === null ? '' : $sourceTag->name();
  161. $targetTag = $tagDAO->searchByName($targetName);
  162. if ($targetTag === null) {
  163. // There is no existing tag with the same target name
  164. $tagDAO->updateTagName($sourceId, $targetName);
  165. } else {
  166. // There is an existing tag with the same target name
  167. $tagDAO->updateEntryTag($sourceId, $targetTag->id());
  168. $tagDAO->deleteTag($sourceId);
  169. }
  170. Minz_Request::good(_t('feedback.tag.renamed', $sourceName, $targetName), ['c' => 'tag', 'a' => 'index']);
  171. }
  172. public function indexAction(): void {
  173. if (!FreshRSS_Auth::hasAccess()) {
  174. Minz_Error::error(403);
  175. }
  176. $tagDAO = FreshRSS_Factory::createTagDao();
  177. $this->view->tags = $tagDAO->listTags() ?: [];
  178. }
  179. }