tagController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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
  9. */
  10. private $ajax = false;
  11. /**
  12. * This action is called before every other action in that class. It is
  13. * the common boiler plate for every action. It is triggered by the
  14. * underlying framework.
  15. */
  16. public function firstAction() {
  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() {
  31. if (Minz_Request::isPost()) {
  32. $id_tag = Minz_Request::param('id_tag');
  33. $name_tag = trim(Minz_Request::param('name_tag'));
  34. $id_entry = Minz_Request::param('id_entry');
  35. $checked = Minz_Request::paramTernary('checked');
  36. if ($id_entry != false) {
  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(array('name' => $name_tag));
  45. }
  46. }
  47. if ($id_tag != 0) {
  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(array(
  56. 'c' => 'index',
  57. 'a' => 'index',
  58. ), true);
  59. }
  60. }
  61. public function deleteAction() {
  62. if (Minz_Request::isPost()) {
  63. $id_tag = Minz_Request::param('id_tag');
  64. if ($id_tag != false) {
  65. $tagDAO = FreshRSS_Factory::createTagDao();
  66. $tagDAO->deleteTag($id_tag);
  67. }
  68. } else {
  69. Minz_Error::error(405);
  70. }
  71. if (!$this->ajax) {
  72. Minz_Request::forward(array(
  73. 'c' => 'tag',
  74. 'a' => 'index',
  75. ), true);
  76. }
  77. }
  78. public function getTagsForEntryAction() {
  79. $this->view->_layout(false);
  80. header('Content-Type: application/json; charset=UTF-8');
  81. header('Cache-Control: private, no-cache, no-store, must-revalidate');
  82. $id_entry = Minz_Request::param('id_entry', 0);
  83. $tagDAO = FreshRSS_Factory::createTagDao();
  84. $this->view->tags = $tagDAO->getTagsForEntry($id_entry);
  85. }
  86. public function addAction() {
  87. if (!Minz_Request::isPost()) {
  88. Minz_Error::error(405);
  89. }
  90. $name = Minz_Request::param('name');
  91. $tagDAO = FreshRSS_Factory::createTagDao();
  92. if (strlen($name) > 0 && null === $tagDAO->searchByName($name)) {
  93. $tagDAO->addTag(['name' => $name]);
  94. Minz_Request::good(_t('feedback.tag.created', $name), ['c' => 'tag', 'a' => 'index']);
  95. }
  96. Minz_Request::bad(_t('feedback.tag.name_exists', $name), ['c' => 'tag', 'a' => 'index']);
  97. }
  98. public function renameAction() {
  99. if (!Minz_Request::isPost()) {
  100. Minz_Error::error(405);
  101. }
  102. $targetName = Minz_Request::param('name');
  103. $sourceId = Minz_Request::param('id_tag');
  104. if ($targetName == '' || $sourceId == '') {
  105. return Minz_Error::error(400);
  106. }
  107. $tagDAO = FreshRSS_Factory::createTagDao();
  108. $sourceTag = $tagDAO->searchById($sourceId);
  109. $sourceName = $sourceTag == null ? null : $sourceTag->name();
  110. $targetTag = $tagDAO->searchByName($targetName);
  111. if ($targetTag == null) {
  112. // There is no existing tag with the same target name
  113. $tagDAO->updateTag($sourceId, ['name' => $targetName]);
  114. } else {
  115. // There is an existing tag with the same target name
  116. $tagDAO->updateEntryTag($sourceId, $targetTag->id());
  117. $tagDAO->deleteTag($sourceId);
  118. }
  119. Minz_Request::good(_t('feedback.tag.renamed', $sourceName, $targetName), ['c' => 'tag', 'a' => 'index']);
  120. }
  121. public function indexAction() {
  122. $tagDAO = FreshRSS_Factory::createTagDao();
  123. $this->view->tags = $tagDAO->listTags();
  124. }
  125. }