tagController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. //Create new tag
  35. $id_tag = $tagDAO->addTag(array('name' => $name_tag));
  36. }
  37. if ($id_tag != 0) {
  38. $tagDAO->tagEntry($id_tag, $id_entry, $checked);
  39. }
  40. }
  41. } else {
  42. Minz_Error::error(405);
  43. }
  44. if (!$this->ajax) {
  45. Minz_Request::forward(array(
  46. 'c' => 'index',
  47. 'a' => 'index',
  48. ), true);
  49. }
  50. }
  51. public function deleteAction() {
  52. if (Minz_Request::isPost()) {
  53. $id_tag = Minz_Request::param('id_tag');
  54. if ($id_tag != false) {
  55. $tagDAO = FreshRSS_Factory::createTagDao();
  56. $tagDAO->deleteTag($id_tag);
  57. }
  58. } else {
  59. Minz_Error::error(405);
  60. }
  61. if (!$this->ajax) {
  62. Minz_Request::forward(array(
  63. 'c' => 'tag',
  64. 'a' => 'index',
  65. ), true);
  66. }
  67. }
  68. public function getTagsForEntryAction() {
  69. $this->view->_layout(false);
  70. header('Content-Type: application/json; charset=UTF-8');
  71. header('Cache-Control: private, no-cache, no-store, must-revalidate');
  72. $id_entry = Minz_Request::param('id_entry', 0);
  73. $tagDAO = FreshRSS_Factory::createTagDao();
  74. $this->view->tags = $tagDAO->getTagsForEntry($id_entry);
  75. }
  76. public function addAction() {
  77. if (!Minz_Request::isPost()) {
  78. Minz_Error::error(405);
  79. }
  80. $name = Minz_Request::param('name');
  81. $tagDAO = FreshRSS_Factory::createTagDao();
  82. if (null === $tagDAO->searchByName($name)) {
  83. $tagDAO->addTag(['name' => $name]);
  84. Minz_Request::good('feedback.tag.created', ['c' => 'tag', 'a' => 'index'], true);
  85. }
  86. Minz_Request::bad('feedback.tag.name_exists', ['c' => 'tag', 'a' => 'index'], true);
  87. }
  88. public function renameAction() {
  89. if (!Minz_Request::isPost()) {
  90. Minz_Error::error(405);
  91. }
  92. $name = Minz_Request::param('name');
  93. $tagDAO = FreshRSS_Factory::createTagDao();
  94. $newTag = $tagDAO->searchByName($name);
  95. if (null === $newTag) {
  96. $tagDAO->updateTag(Minz_Request::param('id_tag'), ['name' => $name]);
  97. } else {
  98. $tagDAO->updateEntryTag(Minz_Request::param('id_tag'), $newTag->id());
  99. $tagDAO->deleteTag(Minz_Request::param('id_tag'));
  100. }
  101. Minz_Request::good('feedback.tag.renamed', ['c' => 'tag', 'a' => 'index'], true);
  102. }
  103. public function indexAction() {
  104. $tagDAO = FreshRSS_Factory::createTagDao();
  105. $this->view->tags = $tagDAO->listTags();
  106. }
  107. }