tagController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. Minz_Request::_param('ajax');
  22. }
  23. }
  24. /**
  25. * This action adds (checked=true) or removes (checked=false) a tag to an entry.
  26. */
  27. public function tagEntryAction(): void {
  28. if (!FreshRSS_Auth::hasAccess()) {
  29. Minz_Error::error(403);
  30. }
  31. if (Minz_Request::isPost()) {
  32. $id_tag = Minz_Request::paramInt('id_tag');
  33. $name_tag = Minz_Request::paramString('name_tag');
  34. $id_entry = Minz_Request::paramString('id_entry');
  35. $checked = Minz_Request::paramBoolean('checked');
  36. if ($id_entry != '') {
  37. $tagDAO = FreshRSS_Factory::createTagDao();
  38. if ($id_tag == 0 && $name_tag !== '' && $checked) {
  39. if ($existing_tag = $tagDAO->searchByName($name_tag)) {
  40. // Use existing tag
  41. $tagDAO->tagEntry($existing_tag->id(), $id_entry, $checked);
  42. } else {
  43. //Create new tag
  44. $id_tag = $tagDAO->addTag(['name' => $name_tag]);
  45. }
  46. }
  47. if ($id_tag != false) {
  48. $tagDAO->tagEntry($id_tag, $id_entry, $checked);
  49. }
  50. }
  51. } else {
  52. Minz_Error::error(405);
  53. }
  54. if (!$this->ajax) {
  55. Minz_Request::forward([
  56. 'c' => 'index',
  57. 'a' => 'index',
  58. ], true);
  59. }
  60. }
  61. public function deleteAction(): void {
  62. if (!FreshRSS_Auth::hasAccess()) {
  63. Minz_Error::error(403);
  64. }
  65. if (Minz_Request::isPost()) {
  66. $id_tag = Minz_Request::paramInt('id_tag');
  67. if ($id_tag !== 0) {
  68. $tagDAO = FreshRSS_Factory::createTagDao();
  69. $tagDAO->deleteTag($id_tag);
  70. }
  71. } else {
  72. Minz_Error::error(405);
  73. }
  74. if (!$this->ajax) {
  75. Minz_Request::forward([
  76. 'c' => 'tag',
  77. 'a' => 'index',
  78. ], true);
  79. }
  80. }
  81. public function getTagsForEntryAction(): void {
  82. if (!FreshRSS_Auth::hasAccess() && !FreshRSS_Context::systemConf()->allow_anonymous) {
  83. Minz_Error::error(403);
  84. }
  85. $this->view->_layout(null);
  86. header('Content-Type: application/json; charset=UTF-8');
  87. header('Cache-Control: private, no-cache, no-store, must-revalidate');
  88. $id_entry = Minz_Request::paramString('id_entry');
  89. $tagDAO = FreshRSS_Factory::createTagDao();
  90. $this->view->tagsForEntry = $tagDAO->getTagsForEntry($id_entry) ?: [];
  91. }
  92. public function addAction(): void {
  93. if (!FreshRSS_Auth::hasAccess()) {
  94. Minz_Error::error(403);
  95. }
  96. if (!Minz_Request::isPost()) {
  97. Minz_Error::error(405);
  98. }
  99. $name = Minz_Request::paramString('name');
  100. $tagDAO = FreshRSS_Factory::createTagDao();
  101. if (strlen($name) > 0 && null === $tagDAO->searchByName($name)) {
  102. $tagDAO->addTag(['name' => $name]);
  103. Minz_Request::good(_t('feedback.tag.created', $name), ['c' => 'tag', 'a' => 'index']);
  104. }
  105. Minz_Request::bad(_t('feedback.tag.name_exists', $name), ['c' => 'tag', 'a' => 'index']);
  106. }
  107. /**
  108. * @throws Minz_ConfigurationNamespaceException
  109. * @throws Minz_PDOConnectionException|JsonException
  110. */
  111. public function renameAction(): void {
  112. if (!FreshRSS_Auth::hasAccess()) {
  113. Minz_Error::error(403);
  114. }
  115. if (!Minz_Request::isPost()) {
  116. Minz_Error::error(405);
  117. }
  118. $targetName = Minz_Request::paramString('name');
  119. $sourceId = Minz_Request::paramInt('id_tag');
  120. if ($targetName == '' || $sourceId == 0) {
  121. Minz_Error::error(400);
  122. return;
  123. }
  124. $tagDAO = FreshRSS_Factory::createTagDao();
  125. $sourceTag = $tagDAO->searchById($sourceId);
  126. $sourceName = $sourceTag === null ? '' : $sourceTag->name();
  127. $targetTag = $tagDAO->searchByName($targetName);
  128. if ($targetTag === null) {
  129. // There is no existing tag with the same target name
  130. $tagDAO->updateTagName($sourceId, $targetName);
  131. } else {
  132. // There is an existing tag with the same target name
  133. $tagDAO->updateEntryTag($sourceId, $targetTag->id());
  134. $tagDAO->deleteTag($sourceId);
  135. }
  136. Minz_Request::good(_t('feedback.tag.renamed', $sourceName, $targetName), ['c' => 'tag', 'a' => 'index']);
  137. }
  138. public function indexAction(): void {
  139. if (!FreshRSS_Auth::hasAccess()) {
  140. Minz_Error::error(403);
  141. }
  142. $tagDAO = FreshRSS_Factory::createTagDao();
  143. $this->view->tags = $tagDAO->listTags() ?: [];
  144. }
  145. }