Misc.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Pages;
  10. use ForkBB\Models\Page;
  11. use ForkBB\Models\Forum\Forum;
  12. use ForkBB\Models\Topic\Topic;
  13. use function \ForkBB\__;
  14. class Misc extends Page
  15. {
  16. /**
  17. * Пометка раздела прочитанным
  18. */
  19. public function markread(array $args): Page
  20. {
  21. $forum = $this->c->forums->loadTree($args['id']);
  22. if (! $forum instanceof Forum) {
  23. return $this->c->Message->message('Bad request');
  24. }
  25. if (! $this->c->Csrf->verify($args['token'], 'MarkRead', $args)) {
  26. return $this->c->Redirect->url($forum->link)->message($this->c->Csrf->getError());
  27. }
  28. $this->c->forums->markread($forum, $this->user);
  29. $this->c->Lang->load('misc');
  30. $message = $forum->id ? 'Mark forum read redirect' : 'Mark read redirect';
  31. return $this->c->Redirect->url($forum->link)->message($message);
  32. }
  33. /**
  34. * Подписка на форум и отписка от него
  35. */
  36. public function forumSubscription(array $args): Page
  37. {
  38. if (! $this->c->Csrf->verify($args['token'], 'ForumSubscription', $args)) {
  39. return $this->c->Message->message($this->c->Csrf->getError());
  40. }
  41. $forum = $this->c->forums->get($args['fid']);
  42. if (! $forum instanceof Forum) {
  43. return $this->c->Message->message('Bad request');
  44. }
  45. $this->c->Lang->load('misc');
  46. if ('subscribe' === $args['type']) {
  47. if (! $this->user->email_confirmed) {
  48. return $this->confirmMessage();
  49. }
  50. $this->c->subscriptions->subscribe($this->user, $forum);
  51. $message = 'Subscribe redirect';
  52. } else {
  53. $this->c->subscriptions->unsubscribe($this->user, $forum);
  54. $message = 'Unsubscribe redirect';
  55. }
  56. return $this->c->Redirect->url($forum->link)->message($message);
  57. }
  58. /**
  59. * Подписка на топик и отписка от него
  60. */
  61. public function topicSubscription(array $args): Page
  62. {
  63. if (! $this->c->Csrf->verify($args['token'], 'TopicSubscription', $args)) {
  64. return $this->c->Message->message($this->c->Csrf->getError());
  65. }
  66. $topic = $this->c->topics->load($args['tid']);
  67. if (! $topic instanceof Topic) {
  68. return $this->c->Message->message('Bad request');
  69. }
  70. $this->c->Lang->load('misc');
  71. if ('subscribe' === $args['type']) {
  72. if (! $this->user->email_confirmed) {
  73. return $this->confirmMessage();
  74. }
  75. $this->c->subscriptions->subscribe($this->user, $topic);
  76. $message = 'Subscribe redirect';
  77. } else {
  78. $this->c->subscriptions->unsubscribe($this->user, $topic);
  79. $message = 'Unsubscribe redirect';
  80. }
  81. return $this->c->Redirect->url($topic->link)->message($message);
  82. }
  83. protected function confirmMessage(): Page
  84. {
  85. $link = $this->c->Router->link(
  86. 'EditUserEmail',
  87. [
  88. 'id' => $this->user->id,
  89. ]
  90. );
  91. return $this->c->Message->message(['Confirm your email address', $link], true, 100);
  92. }
  93. }