AbstractPM.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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\PM;
  10. use ForkBB\Core\Container;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\PM\Cnst;
  13. use ForkBB\Models\User\User;
  14. use function \ForkBB\__;
  15. abstract class AbstractPM extends Page
  16. {
  17. /**
  18. * @var array
  19. */
  20. protected $pmCrumbs = [];
  21. /**
  22. * @var ForkBB\Models\PM\Manager
  23. */
  24. protected $pms;
  25. public function __construct(Container $container)
  26. {
  27. parent::__construct($container);
  28. $this->pms = $container->pms;
  29. $this->pmIndex = Cnst::ACTION_CURRENT; # string Указатель на активный пункт навигации в меню ЛС
  30. $this->fIndex = self::FI_PM;
  31. $this->onlinePos = 'pm';
  32. $this->robots = 'noindex, nofollow';
  33. $this->hhsLevel = 'secure';
  34. }
  35. /**
  36. * Подготовка страницы к отображению
  37. */
  38. public function prepare(): void
  39. {
  40. $this->pmNavigation = $this->pmNavigation();
  41. $this->crumbs = $this->crumbs(...$this->pmCrumbs);
  42. if (1 !== $this->user->u_pm) {
  43. $this->fIswev = ['w', 'PM off'];
  44. }
  45. parent::prepare();
  46. }
  47. /**
  48. * Возвращает массив ссылок с описанием для построения навигации админки
  49. */
  50. protected function pmNavigation(): array
  51. {
  52. $r = $this->c->Router;
  53. $args = [
  54. 'second' => $this->pms->second,
  55. ];
  56. $nav = [
  57. 'pm-boxes' => [true, 'PM Folders'],
  58. Cnst::ACTION_NEW => [
  59. $r->link('PMAction', $args + ['action' => Cnst::ACTION_NEW]),
  60. $this->pms->numNew > 0 ? ['New messages %s', $this->pms->numNew] : 'New messages',
  61. ],
  62. Cnst::ACTION_CURRENT => [
  63. $r->link('PMAction', $args + ['action' => Cnst::ACTION_CURRENT]),
  64. $this->pms->numCurrent > 0 ? ['My talks %s', $this->pms->numCurrent] : 'My talks',
  65. ],
  66. Cnst::ACTION_ARCHIVE => [
  67. $r->link('PMAction', $args + ['action' => Cnst::ACTION_ARCHIVE]),
  68. $this->pms->numArchive > 0 ? ['Archive messages %s', $this->pms->numArchive] : 'Archive messages',
  69. ],
  70. // 'pm-sp1' => [null, null],
  71. ];
  72. if ($this->user->g_pm_limit > 0) {
  73. $nav += [
  74. 'pm-storage' => [true, 'PM Storage'],
  75. 'pm-active' => [
  76. false,
  77. [
  78. 'Active: %s',
  79. $this->user->g_pm_limit < 1 ? 0 : (int) (100 * $this->pms->totalCurrent / $this->user->g_pm_limit),
  80. ],
  81. ],
  82. 'pm-archive' => [
  83. false,
  84. [
  85. 'Archive: %s',
  86. $this->user->g_pm_limit < 1 ? 0 : (int) (100 * $this->pms->totalArchive / $this->user->g_pm_limit),
  87. ],
  88. ],
  89. // 'pm-sp2' => [null, null],
  90. ];
  91. }
  92. $nav += [
  93. 'pm-options' => [true, 'PM Options'],
  94. Cnst::ACTION_CONFIG => [
  95. $r->link('PMAction', ['action' => Cnst::ACTION_CONFIG]),
  96. 'PM Config',
  97. ],
  98. Cnst::ACTION_BLOCK => [
  99. $r->link('PMAction', ['action' => Cnst::ACTION_BLOCK]),
  100. 'Blocked users',
  101. ],
  102. ];
  103. return $nav;
  104. }
  105. /**
  106. * Возвращает массив хлебных крошек
  107. * Заполняет массив титула страницы
  108. */
  109. protected function crumbs(/* mixed */ ...$crumbs): array
  110. {
  111. $pms = $this->pms;
  112. $action = $this->args['action'] ?? ($this->user->u_pm_num_new > 0 ? Cnst::ACTION_NEW : Cnst::ACTION_CURRENT);
  113. $viewArea = false;
  114. switch ($action) {
  115. case Cnst::ACTION_NEW:
  116. case Cnst::ACTION_CURRENT:
  117. case Cnst::ACTION_ARCHIVE:
  118. case Cnst::ACTION_SEND:
  119. case Cnst::ACTION_TOPIC:
  120. case Cnst::ACTION_POST:
  121. case Cnst::ACTION_EDIT:
  122. case Cnst::ACTION_DELETE:
  123. $viewArea = true;
  124. break;
  125. case Cnst::ACTION_BLOCK:
  126. case Cnst::ACTION_CONFIG:
  127. break;
  128. default:
  129. $crumbs[] = [null, ['%s', 'unknown']];
  130. }
  131. if ($viewArea) {
  132. if (null !== $pms->second) {
  133. if (\is_int($pms->second)) {
  134. if (
  135. ($user = $this->c->users->load($pms->second)) instanceof User
  136. && ! $user->isGuest
  137. ) {
  138. $name = $user->username;
  139. } else {
  140. $name = 'unknown'; // ????
  141. }
  142. } else {
  143. $name = \substr($pms->second, 1, -1);
  144. }
  145. switch ($pms->area) {
  146. case Cnst::ACTION_NEW: $m = ['New messages with %s', $name]; break;
  147. case Cnst::ACTION_CURRENT: $m = ['My talks with %s', $name]; break;
  148. case Cnst::ACTION_ARCHIVE: $m = ['Archive messages with %s', $name]; break;
  149. }
  150. } else {
  151. if ($this->targetUser instanceof User) {
  152. $crumbs[] = [
  153. $this->c->Router->link(
  154. 'PMAction',
  155. [
  156. 'second' => $this->targetUser->isGuest
  157. ? '"' . $this->targetUser->username . '"'
  158. : $this->targetUser->id,
  159. 'action' => $pms->area,
  160. ]
  161. ),
  162. ['"%s"', $this->targetUser->username],
  163. ];
  164. }
  165. switch ($pms->area) {
  166. case Cnst::ACTION_NEW: $m = 'New messages'; break;
  167. case Cnst::ACTION_CURRENT: $m = 'My talks'; break;
  168. case Cnst::ACTION_ARCHIVE: $m = 'Archive messages'; break;
  169. }
  170. }
  171. $crumbs[] = [
  172. $this->c->Router->link(
  173. 'PMAction',
  174. [
  175. 'second' => $pms->second,
  176. 'action' => $pms->area,
  177. ]
  178. ),
  179. $m,
  180. ];
  181. if (null === $this->title) {
  182. $this->title = $m;
  183. }
  184. }
  185. $crumbs[] = [$this->c->Router->link('PM'), 'PM'];
  186. return parent::crumbs(...$crumbs);
  187. }
  188. }