tagController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Controller to handle every tag actions.
  4. */
  5. class FreshRSS_tag_Controller extends Minz_ActionController {
  6. /**
  7. * This action is called before every other action in that class. It is
  8. * the common boiler plate for every action. It is triggered by the
  9. * underlying framework.
  10. */
  11. public function firstAction() {
  12. if (!FreshRSS_Auth::hasAccess()) {
  13. Minz_Error::error(403);
  14. }
  15. // If ajax request, we do not print layout
  16. $this->ajax = Minz_Request::param('ajax');
  17. if ($this->ajax) {
  18. $this->view->_layout(false);
  19. Minz_Request::_param('ajax');
  20. }
  21. }
  22. /**
  23. * This action adds (checked=true) or removes (checked=false) a tag to an entry.
  24. */
  25. public function tagEntryAction() {
  26. if (Minz_Request::isPost()) {
  27. $id_tag = Minz_Request::param('id_tag');
  28. $name_tag = trim(Minz_Request::param('name_tag'));
  29. $id_entry = Minz_Request::param('id_entry');
  30. $checked = Minz_Request::paramTernary('checked');
  31. if ($id_entry != false) {
  32. $tagDAO = FreshRSS_Factory::createTagDao();
  33. if ($id_tag == 0 && $name_tag != '' && $checked) {
  34. if ($existing_tag = $tagDAO->searchByName($name_tag)) {
  35. // Use existing tag
  36. $tagDAO->tagEntry($existing_tag->id(), $id_entry, $checked);
  37. } else {
  38. //Create new tag
  39. $id_tag = $tagDAO->addTag(array('name' => $name_tag));
  40. }
  41. }
  42. if ($id_tag != 0) {
  43. $tagDAO->tagEntry($id_tag, $id_entry, $checked);
  44. }
  45. }
  46. } else {
  47. Minz_Error::error(405);
  48. }
  49. if (!$this->ajax) {
  50. Minz_Request::forward(array(
  51. 'c' => 'index',
  52. 'a' => 'index',
  53. ), true);
  54. }
  55. }
  56. public function deleteAction() {
  57. if (Minz_Request::isPost()) {
  58. $id_tag = Minz_Request::param('id_tag');
  59. if ($id_tag != false) {
  60. $tagDAO = FreshRSS_Factory::createTagDao();
  61. $tagDAO->deleteTag($id_tag);
  62. }
  63. } else {
  64. Minz_Error::error(405);
  65. }
  66. if (!$this->ajax) {
  67. Minz_Request::forward(array(
  68. 'c' => 'tag',
  69. 'a' => 'index',
  70. ), true);
  71. }
  72. }
  73. public function getTagsForEntryAction() {
  74. $this->view->_layout(false);
  75. header('Content-Type: application/json; charset=UTF-8');
  76. header('Cache-Control: private, no-cache, no-store, must-revalidate');
  77. $id_entry = Minz_Request::param('id_entry', 0);
  78. $tagDAO = FreshRSS_Factory::createTagDao();
  79. $this->view->tags = $tagDAO->getTagsForEntry($id_entry);
  80. }
  81. public function addAction() {
  82. if (!Minz_Request::isPost()) {
  83. Minz_Error::error(405);
  84. }
  85. $name = Minz_Request::param('name');
  86. $tagDAO = FreshRSS_Factory::createTagDao();
  87. if (strlen($name) > 0 && null === $tagDAO->searchByName($name)) {
  88. $tagDAO->addTag(['name' => $name]);
  89. Minz_Request::good(_t('feedback.tag.created', $name), ['c' => 'tag', 'a' => 'index'], true);
  90. }
  91. Minz_Request::bad(_t('feedback.tag.name_exists', $name), ['c' => 'tag', 'a' => 'index'], true);
  92. }
  93. public function renameAction() {
  94. if (!Minz_Request::isPost()) {
  95. Minz_Error::error(405);
  96. }
  97. $targetName = Minz_Request::param('name');
  98. $sourceId = Minz_Request::param('id_tag');
  99. if ($targetName == '' || $sourceId == '') {
  100. return Minz_Error::error(400);
  101. }
  102. $tagDAO = FreshRSS_Factory::createTagDao();
  103. $sourceTag = $tagDAO->searchById($sourceId);
  104. $sourceName = $sourceTag == null ? null : $sourceTag->name();
  105. $targetTag = $tagDAO->searchByName($targetName);
  106. if ($targetTag == null) {
  107. // There is no existing tag with the same target name
  108. $tagDAO->updateTag($sourceId, ['name' => $targetName]);
  109. } else {
  110. // There is an existing tag with the same target name
  111. $tagDAO->updateEntryTag($sourceId, $targetTag->id());
  112. $tagDAO->deleteTag($sourceId);
  113. }
  114. Minz_Request::good(_t('feedback.tag.renamed', $sourceName, $targetName), ['c' => 'tag', 'a' => 'index'], true);
  115. }
  116. public function indexAction() {
  117. $tagDAO = FreshRSS_Factory::createTagDao();
  118. $this->view->tags = $tagDAO->listTags();
  119. }
  120. }