tagController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Controller to handle every tag actions.
  5. */
  6. class FreshRSS_tag_Controller extends FreshRSS_ActionController {
  7. /**
  8. * JavaScript request or not.
  9. */
  10. private bool $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. #[\Override]
  17. public function firstAction(): void {
  18. // If ajax request, we do not print layout
  19. $this->ajax = Minz_Request::paramBoolean('ajax');
  20. if ($this->ajax) {
  21. $this->view->_layout(null);
  22. }
  23. }
  24. /**
  25. * This action adds (checked=true) or removes (checked=false) a tag to an entry.
  26. */
  27. public function tagEntryAction(): void {
  28. if (!FreshRSS_Auth::hasAccess()) {
  29. Minz_Error::error(403);
  30. }
  31. if (Minz_Request::isPost()) {
  32. $id_tag = Minz_Request::paramInt('id_tag');
  33. $name_tag = Minz_Request::paramString('name_tag');
  34. $id_entry = Minz_Request::paramString('id_entry');
  35. $checked = Minz_Request::paramBoolean('checked');
  36. if ($id_entry != '') {
  37. $tagDAO = FreshRSS_Factory::createTagDao();
  38. if ($id_tag == 0 && $name_tag !== '' && $checked) {
  39. if (($existing_tag = $tagDAO->searchByName($name_tag)) !== null) {
  40. // Use existing tag
  41. $tagDAO->tagEntry($existing_tag->id(), $id_entry, $checked);
  42. } else {
  43. //Create new tag
  44. $id_tag = $tagDAO->addTag(['name' => $name_tag]);
  45. }
  46. }
  47. if ($id_tag != false) {
  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([
  56. 'c' => 'index',
  57. 'a' => 'index',
  58. ], true);
  59. }
  60. }
  61. public function deleteAction(): void {
  62. if (!FreshRSS_Auth::hasAccess()) {
  63. Minz_Error::error(403);
  64. }
  65. if (Minz_Request::isPost()) {
  66. $id_tag = Minz_Request::paramInt('id_tag');
  67. if ($id_tag !== 0) {
  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([
  76. 'c' => 'tag',
  77. 'a' => 'index',
  78. ], true);
  79. }
  80. }
  81. /**
  82. * This action updates the given tag.
  83. */
  84. public function updateAction(): void {
  85. if (!FreshRSS_Auth::hasAccess()) {
  86. Minz_Error::error(403);
  87. return;
  88. }
  89. if (Minz_Request::paramBoolean('ajax')) {
  90. $this->view->_layout(null);
  91. }
  92. $tagDAO = FreshRSS_Factory::createTagDao();
  93. $id = Minz_Request::paramInt('id');
  94. $tag = $tagDAO->searchById($id);
  95. if ($id === 0 || $tag === null) {
  96. Minz_Error::error(404);
  97. return;
  98. }
  99. $this->view->tag = $tag;
  100. FreshRSS_View::prependTitle($tag->name() . ' · ' . _t('sub.title') . ' · ');
  101. if (Minz_Request::isPost()) {
  102. invalidateHttpCache();
  103. $ok = true;
  104. if ($tag->name() !== Minz_Request::paramString('name')) {
  105. $ok = $tagDAO->updateTagName($tag->id(), Minz_Request::paramString('name')) !== false;
  106. }
  107. if ($ok) {
  108. $tag->_filtersAction('label', Minz_Request::paramTextToArray('filteractions_label', plaintext: true));
  109. $ok = $tagDAO->updateTagAttributes($tag->id(), $tag->attributes()) !== false;
  110. }
  111. invalidateHttpCache();
  112. $prev_controller = 'tag';
  113. $from = Minz_Request::paramStringNull('from') ?? 'update';
  114. $params = ['id' => $id];
  115. if ($from === 'normal' || $from === 'reader') {
  116. $prev_controller = 'index';
  117. $params['type'] = 'tag';
  118. }
  119. $url_redirect = ['c' => $prev_controller, 'a' => $from, 'params' => $params];
  120. if ($ok) {
  121. Minz_Request::good(
  122. _t('feedback.tag.updated'),
  123. $url_redirect,
  124. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  125. );
  126. } else {
  127. Minz_Request::bad(_t('feedback.tag.error'), $url_redirect);
  128. }
  129. }
  130. }
  131. public function getTagsForEntryAction(): void {
  132. if (!FreshRSS_Auth::hasAccess() && !FreshRSS_Auth::allowAnonymous()) {
  133. Minz_Error::error(403);
  134. }
  135. $this->view->_layout(null);
  136. header('Content-Type: application/json; charset=UTF-8');
  137. header('Cache-Control: private, no-cache, no-store, must-revalidate');
  138. $id_entry = Minz_Request::paramString('id_entry');
  139. $tagDAO = FreshRSS_Factory::createTagDao();
  140. $tagsForEntry = $tagDAO->getTagsForEntry($id_entry);
  141. foreach ($tagsForEntry as $key => $tag) {
  142. $tagsForEntry[$key]['name'] = htmlspecialchars_decode($tag['name'], ENT_QUOTES);
  143. }
  144. $this->view->tagsForEntry = $tagsForEntry;
  145. }
  146. public function addAction(): void {
  147. if (!FreshRSS_Auth::hasAccess()) {
  148. Minz_Error::error(403);
  149. }
  150. if (!Minz_Request::isPost()) {
  151. Minz_Error::error(405);
  152. }
  153. $url_redirect = ['c' => 'tag', 'a' => 'index'];
  154. $name = Minz_Request::paramString('name');
  155. $catDAO = FreshRSS_Factory::createCategoryDao();
  156. if ($catDAO->searchByName($name) !== null) {
  157. Minz_Request::bad(_t('feedback.sub.category.name_exists'), $url_redirect);
  158. }
  159. $tagDAO = FreshRSS_Factory::createTagDao();
  160. if ($tagDAO->searchByName($name) !== null) {
  161. Minz_Request::bad(_t('feedback.tag.name_exists', $name), $url_redirect);
  162. }
  163. $tagDAO->addTag(['name' => $name]);
  164. Minz_Request::good(
  165. _t('feedback.tag.created', $name),
  166. $url_redirect,
  167. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  168. );
  169. }
  170. /**
  171. * @throws Minz_ConfigurationNamespaceException
  172. * @throws Minz_PDOConnectionException
  173. */
  174. public function renameAction(): void {
  175. if (!FreshRSS_Auth::hasAccess()) {
  176. Minz_Error::error(403);
  177. }
  178. if (!Minz_Request::isPost()) {
  179. Minz_Error::error(405);
  180. }
  181. $targetName = Minz_Request::paramString('name');
  182. $sourceId = Minz_Request::paramInt('id_tag');
  183. if ($targetName == '' || $sourceId == 0) {
  184. Minz_Error::error(400);
  185. return;
  186. }
  187. $tagDAO = FreshRSS_Factory::createTagDao();
  188. $sourceTag = $tagDAO->searchById($sourceId);
  189. $sourceName = $sourceTag === null ? '' : $sourceTag->name();
  190. $targetTag = $tagDAO->searchByName($targetName);
  191. if ($targetTag === null) {
  192. // There is no existing tag with the same target name
  193. $tagDAO->updateTagName($sourceId, $targetName);
  194. } else {
  195. // There is an existing tag with the same target name
  196. $tagDAO->updateEntryTag($sourceId, $targetTag->id());
  197. $tagDAO->deleteTag($sourceId);
  198. }
  199. Minz_Request::good(
  200. _t('feedback.tag.renamed', $sourceName, $targetName),
  201. ['c' => 'tag', 'a' => 'index'],
  202. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0);
  203. }
  204. public function indexAction(): void {
  205. FreshRSS_View::prependTitle(_t('sub.menu.label_management') . ' · ');
  206. if (!FreshRSS_Auth::hasAccess()) {
  207. Minz_Error::error(403);
  208. }
  209. $tagDAO = FreshRSS_Factory::createTagDao();
  210. $this->view->tags = $tagDAO->listTags(precounts: true);
  211. $id = Minz_Request::paramInt('id');
  212. if ($id !== 0) {
  213. $this->view->displaySlider = true;
  214. $this->view->tag = $tagDAO->searchById($id);
  215. }
  216. }
  217. public static function escapeForSearch(string $tag): string {
  218. $tag = htmlspecialchars_decode($tag, ENT_QUOTES);
  219. return str_replace([' ', '(', ')'], ['+', '\\(', '\\)'], $tag);
  220. }
  221. }