tagController.php 4.1 KB

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