tagController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Controller to handle every tag actions.
  4. */
  5. class FreshRSS_tag_Controller extends FreshRSS_ActionController {
  6. /**
  7. * JavaScript request or not.
  8. */
  9. private bool $ajax = false;
  10. /**
  11. * This action is called before every other action in that class. It is
  12. * the common boilerplate for every action. It is triggered by the
  13. * underlying framework.
  14. */
  15. public function firstAction(): void {
  16. // If ajax request, we do not print layout
  17. $this->ajax = Minz_Request::paramBoolean('ajax');
  18. if ($this->ajax) {
  19. $this->view->_layout(null);
  20. Minz_Request::_param('ajax');
  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::paramTernary('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. public function getTagsForEntryAction(): void {
  81. if (!FreshRSS_Auth::hasAccess() && !FreshRSS_Context::$system_conf->allow_anonymous) {
  82. Minz_Error::error(403);
  83. }
  84. $this->view->_layout(null);
  85. header('Content-Type: application/json; charset=UTF-8');
  86. header('Cache-Control: private, no-cache, no-store, must-revalidate');
  87. $id_entry = Minz_Request::paramString('id_entry');
  88. $tagDAO = FreshRSS_Factory::createTagDao();
  89. $this->view->tagsForEntry = $tagDAO->getTagsForEntry($id_entry) ?: [];
  90. }
  91. public function addAction(): void {
  92. if (!FreshRSS_Auth::hasAccess()) {
  93. Minz_Error::error(403);
  94. }
  95. if (!Minz_Request::isPost()) {
  96. Minz_Error::error(405);
  97. }
  98. $name = Minz_Request::paramString('name');
  99. $tagDAO = FreshRSS_Factory::createTagDao();
  100. if (strlen($name) > 0 && null === $tagDAO->searchByName($name)) {
  101. $tagDAO->addTag(['name' => $name]);
  102. Minz_Request::good(_t('feedback.tag.created', $name), ['c' => 'tag', 'a' => 'index']);
  103. }
  104. Minz_Request::bad(_t('feedback.tag.name_exists', $name), ['c' => 'tag', 'a' => 'index']);
  105. }
  106. /**
  107. * @throws Minz_ConfigurationNamespaceException
  108. * @throws Minz_PDOConnectionException|JsonException
  109. */
  110. public function renameAction(): void {
  111. if (!FreshRSS_Auth::hasAccess()) {
  112. Minz_Error::error(403);
  113. }
  114. if (!Minz_Request::isPost()) {
  115. Minz_Error::error(405);
  116. }
  117. $targetName = Minz_Request::paramString('name');
  118. $sourceId = Minz_Request::paramInt('id_tag');
  119. if ($targetName == '' || $sourceId == 0) {
  120. Minz_Error::error(400);
  121. return;
  122. }
  123. $tagDAO = FreshRSS_Factory::createTagDao();
  124. $sourceTag = $tagDAO->searchById($sourceId);
  125. $sourceName = $sourceTag === null ? null : $sourceTag->name();
  126. $targetTag = $tagDAO->searchByName($targetName);
  127. if ($targetTag === null) {
  128. // There is no existing tag with the same target name
  129. $tagDAO->updateTagName($sourceId, $targetName);
  130. } else {
  131. // There is an existing tag with the same target name
  132. $tagDAO->updateEntryTag($sourceId, $targetTag->id());
  133. $tagDAO->deleteTag($sourceId);
  134. }
  135. Minz_Request::good(_t('feedback.tag.renamed', $sourceName, $targetName), ['c' => 'tag', 'a' => 'index']);
  136. }
  137. public function indexAction(): void {
  138. if (!FreshRSS_Auth::hasAccess()) {
  139. Minz_Error::error(403);
  140. }
  141. $tagDAO = FreshRSS_Factory::createTagDao();
  142. $this->view->tags = $tagDAO->listTags() ?: [];
  143. }
  144. }