4
0

tagController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 (null === $tagDAO->searchByName($name)) {
  88. $tagDAO->addTag(['name' => $name]);
  89. Minz_Request::good('feedback.tag.created', ['c' => 'tag', 'a' => 'index'], true);
  90. }
  91. Minz_Request::bad('feedback.tag.name_exists', ['c' => 'tag', 'a' => 'index'], true);
  92. }
  93. public function renameAction() {
  94. if (!Minz_Request::isPost()) {
  95. Minz_Error::error(405);
  96. }
  97. $name = Minz_Request::param('name');
  98. $tagDAO = FreshRSS_Factory::createTagDao();
  99. $newTag = $tagDAO->searchByName($name);
  100. if (null === $newTag) {
  101. $tagDAO->updateTag(Minz_Request::param('id_tag'), ['name' => $name]);
  102. } else {
  103. $tagDAO->updateEntryTag(Minz_Request::param('id_tag'), $newTag->id());
  104. $tagDAO->deleteTag(Minz_Request::param('id_tag'));
  105. }
  106. Minz_Request::good('feedback.tag.renamed', ['c' => 'tag', 'a' => 'index'], true);
  107. }
  108. public function indexAction() {
  109. $tagDAO = FreshRSS_Factory::createTagDao();
  110. $this->view->tags = $tagDAO->listTags();
  111. }
  112. }