4
0

categoryController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Controller to handle actions relative to categories.
  5. * User needs to be connected.
  6. */
  7. class FreshRSS_category_Controller extends FreshRSS_ActionController {
  8. /**
  9. * This action is called before every other action in that class. It is
  10. * the common boiler plate for every action. It is triggered by the
  11. * underlying framework.
  12. *
  13. */
  14. #[\Override]
  15. public function firstAction(): void {
  16. if (!FreshRSS_Auth::hasAccess()) {
  17. Minz_Error::error(403);
  18. }
  19. $catDAO = FreshRSS_Factory::createCategoryDao();
  20. $catDAO->checkDefault();
  21. }
  22. /**
  23. * This action creates a new category.
  24. *
  25. * Request parameter is:
  26. * - new-category
  27. */
  28. public function createAction(): void {
  29. $catDAO = FreshRSS_Factory::createCategoryDao();
  30. $tagDAO = FreshRSS_Factory::createTagDao();
  31. $url_redirect = ['c' => 'subscription', 'a' => 'add'];
  32. $limits = FreshRSS_Context::systemConf()->limits;
  33. $this->view->categories = $catDAO->listCategories(false) ?: [];
  34. if (count($this->view->categories) >= $limits['max_categories']) {
  35. Minz_Request::bad(_t('feedback.sub.category.over_max', $limits['max_categories']), $url_redirect);
  36. }
  37. if (Minz_Request::isPost()) {
  38. invalidateHttpCache();
  39. $cat_name = Minz_Request::paramString('new-category');
  40. if ($cat_name === '') {
  41. Minz_Request::bad(_t('feedback.sub.category.no_name'), $url_redirect);
  42. }
  43. $cat = new FreshRSS_Category($cat_name);
  44. if ($catDAO->searchByName($cat->name()) != null) {
  45. Minz_Request::bad(_t('feedback.sub.category.name_exists'), $url_redirect);
  46. }
  47. if ($tagDAO->searchByName($cat->name()) != null) {
  48. Minz_Request::bad(_t('feedback.tag.name_exists', $cat->name()), $url_redirect);
  49. }
  50. $opml_url = checkUrl(Minz_Request::paramString('opml_url'));
  51. if ($opml_url != '') {
  52. $cat->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML);
  53. $cat->_attribute('opml_url', $opml_url);
  54. } else {
  55. $cat->_kind(FreshRSS_Category::KIND_NORMAL);
  56. $cat->_attribute('opml_url', null);
  57. }
  58. if ($catDAO->addCategoryObject($cat)) {
  59. $url_redirect['a'] = 'index';
  60. Minz_Request::good(_t('feedback.sub.category.created', $cat->name()), $url_redirect);
  61. } else {
  62. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  63. }
  64. }
  65. Minz_Request::forward($url_redirect, true);
  66. }
  67. /**
  68. * This action updates the given category.
  69. */
  70. public function updateAction(): void {
  71. if (Minz_Request::paramBoolean('ajax')) {
  72. $this->view->_layout(null);
  73. }
  74. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  75. $id = Minz_Request::paramInt('id');
  76. $category = $categoryDAO->searchById($id);
  77. if ($id === 0 || null === $category) {
  78. Minz_Error::error(404);
  79. return;
  80. }
  81. $this->view->category = $category;
  82. FreshRSS_View::prependTitle($category->name() . ' · ' . _t('sub.title') . ' · ');
  83. if (Minz_Request::isPost()) {
  84. if (Minz_Request::paramBoolean('enable_read_when_same_title_in_category')) {
  85. $category->_attribute('read_when_same_title_in_category', Minz_Request::paramInt('read_when_same_title_in_category'));
  86. } else {
  87. $category->_attribute('read_when_same_title_in_category', null);
  88. }
  89. $category->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read'));
  90. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  91. $category->_attribute('archiving', null);
  92. } else {
  93. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  94. $keepMax = false;
  95. } elseif (($keepMax = Minz_Request::paramInt('keep_max')) !== 0) {
  96. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  97. }
  98. if (Minz_Request::paramBoolean('enable_keep_period')) {
  99. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  100. if (is_numeric(Minz_Request::paramString('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::paramString('keep_period_unit'))) {
  101. $keepPeriod = str_replace('1', Minz_Request::paramString('keep_period_count'), Minz_Request::paramString('keep_period_unit'));
  102. }
  103. } else {
  104. $keepPeriod = false;
  105. }
  106. $category->_attribute('archiving', [
  107. 'keep_period' => $keepPeriod,
  108. 'keep_max' => $keepMax,
  109. 'keep_min' => Minz_Request::paramInt('keep_min'),
  110. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  111. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  112. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  113. ]);
  114. }
  115. $position = Minz_Request::paramInt('position') ?: null;
  116. $category->_attribute('position', $position);
  117. $opml_url = checkUrl(Minz_Request::paramString('opml_url'));
  118. if ($opml_url != '') {
  119. $category->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML);
  120. $category->_attribute('opml_url', $opml_url);
  121. } else {
  122. $category->_kind(FreshRSS_Category::KIND_NORMAL);
  123. $category->_attribute('opml_url', null);
  124. }
  125. $values = [
  126. 'kind' => $category->kind(),
  127. 'name' => Minz_Request::paramString('name'),
  128. 'attributes' => $category->attributes(),
  129. ];
  130. invalidateHttpCache();
  131. $url_redirect = ['c' => 'subscription', 'params' => ['id' => $id, 'type' => 'category']];
  132. if (false !== $categoryDAO->updateCategory($id, $values)) {
  133. Minz_Request::good(_t('feedback.sub.category.updated'), $url_redirect);
  134. } else {
  135. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  136. }
  137. }
  138. }
  139. /**
  140. * This action deletes a category.
  141. * Feeds in the given category are moved in the default category.
  142. * Related user queries are deleted too.
  143. *
  144. * Request parameter is:
  145. * - id (of a category)
  146. */
  147. public function deleteAction(): void {
  148. $feedDAO = FreshRSS_Factory::createFeedDao();
  149. $catDAO = FreshRSS_Factory::createCategoryDao();
  150. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  151. if (Minz_Request::isPost()) {
  152. invalidateHttpCache();
  153. $id = Minz_Request::paramInt('id');
  154. if ($id === 0) {
  155. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  156. }
  157. if ($id === FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  158. Minz_Request::bad(_t('feedback.sub.category.not_delete_default'), $url_redirect);
  159. }
  160. if ($feedDAO->changeCategory($id, FreshRSS_CategoryDAO::DEFAULTCATEGORYID) === false) {
  161. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  162. }
  163. if ($catDAO->deleteCategory($id) === false) {
  164. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  165. }
  166. // Remove related queries.
  167. /** @var array<array{'get'?:string,'name'?:string,'order'?:string,'search'?:string,'state'?:int,'url'?:string}> $queries */
  168. $queries = remove_query_by_get('c_' . $id, FreshRSS_Context::userConf()->queries);
  169. FreshRSS_Context::userConf()->queries = $queries;
  170. FreshRSS_Context::userConf()->save();
  171. Minz_Request::good(_t('feedback.sub.category.deleted'), $url_redirect);
  172. }
  173. Minz_Request::forward($url_redirect, true);
  174. }
  175. /**
  176. * This action deletes all the feeds relative to a given category.
  177. * Feed-related queries are deleted.
  178. *
  179. * Request parameter is:
  180. * - id (of a category)
  181. * - muted (truthy to remove only muted feeds, or falsy otherwise)
  182. */
  183. public function emptyAction(): void {
  184. $feedDAO = FreshRSS_Factory::createFeedDao();
  185. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  186. if (Minz_Request::isPost()) {
  187. invalidateHttpCache();
  188. $id = Minz_Request::paramInt('id');
  189. if ($id === 0) {
  190. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  191. }
  192. $muted = Minz_Request::paramTernary('muted');
  193. $errored = Minz_Request::paramTernary('errored');
  194. // List feeds to remove then related user queries.
  195. $feeds = $feedDAO->listByCategory($id, $muted, $errored);
  196. if ($feedDAO->deleteFeedByCategory($id, $muted, $errored)) {
  197. // TODO: Delete old favicons
  198. // Remove related queries
  199. foreach ($feeds as $feed) {
  200. /** @var array<array{'get'?:string,'name'?:string,'order'?:string,'search'?:string,'state'?:int,'url'?:string}> */
  201. $queries = remove_query_by_get('f_' . $feed->id(), FreshRSS_Context::userConf()->queries);
  202. FreshRSS_Context::userConf()->queries = $queries;
  203. }
  204. FreshRSS_Context::userConf()->save();
  205. Minz_Request::good(_t('feedback.sub.category.emptied'), $url_redirect);
  206. } else {
  207. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  208. }
  209. }
  210. Minz_Request::forward($url_redirect, true);
  211. }
  212. /**
  213. * Request parameter is:
  214. * - id (of a category)
  215. */
  216. public function refreshOpmlAction(): void {
  217. $catDAO = FreshRSS_Factory::createCategoryDao();
  218. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  219. if (Minz_Request::isPost()) {
  220. invalidateHttpCache();
  221. $id = Minz_Request::paramInt('id');
  222. if ($id === 0) {
  223. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  224. return;
  225. }
  226. $category = $catDAO->searchById($id);
  227. if ($category === null) {
  228. Minz_Request::bad(_t('feedback.sub.category.not_exist'), $url_redirect);
  229. return;
  230. }
  231. invalidateHttpCache();
  232. $ok = $category->refreshDynamicOpml();
  233. if (Minz_Request::paramBoolean('ajax')) {
  234. Minz_Request::setGoodNotification(_t('feedback.sub.category.updated'));
  235. $this->view->_layout(null);
  236. } else {
  237. if ($ok) {
  238. Minz_Request::good(_t('feedback.sub.category.updated'), $url_redirect);
  239. } else {
  240. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  241. }
  242. Minz_Request::forward($url_redirect, true);
  243. }
  244. }
  245. }
  246. /** @return array<string,int> */
  247. public static function refreshDynamicOpmls(): array {
  248. $successes = 0;
  249. $errors = 0;
  250. $catDAO = FreshRSS_Factory::createCategoryDao();
  251. $categories = $catDAO->listCategoriesOrderUpdate(FreshRSS_Context::userConf()->dynamic_opml_ttl_default ?? 86400);
  252. foreach ($categories as $category) {
  253. if ($category->refreshDynamicOpml()) {
  254. $successes++;
  255. } else {
  256. $errors++;
  257. }
  258. }
  259. return [
  260. 'successes' => $successes,
  261. 'errors' => $errors,
  262. ];
  263. }
  264. }