Config.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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\Profile;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Pages\Profile;
  13. use function \ForkBB\__;
  14. use function \ForkBB\dt;
  15. class Config extends Profile
  16. {
  17. /**
  18. * Подготавливает данные для шаблона настройки форума
  19. */
  20. public function config(array $args, string $method): Page
  21. {
  22. if (
  23. false === $this->initProfile($args['id'])
  24. || ! $this->rules->editConfig
  25. ) {
  26. return $this->c->Message->message('Bad request');
  27. }
  28. $this->c->Lang->load('validator');
  29. $this->c->Lang->load('profile_other');
  30. if ('POST' === $method) {
  31. $v = $this->c->Validator->reset()
  32. ->addValidators([
  33. 'to_zero' => [$this, 'vToZero'],
  34. ])->addRules([
  35. 'token' => 'token:EditUserBoardConfig',
  36. 'language' => 'required|string:trim|in:' . \implode(',', $this->c->Func->getLangs()),
  37. 'style' => 'required|string:trim|in:' . \implode(',', $this->c->Func->getStyles()),
  38. 'timezone' => 'required|numeric|in:-12,-11,-10,-9.5,-9,-8.5,-8,-7,-6,-5,-4,-3.5,-3,-2,-1,0,1,2,3,3.5,4,4.5,5,5.5,5.75,6,6.5,7,8,8.75,9,9.5,10,10.5,11,11.5,12,12.75,13,14',
  39. 'dst' => 'required|integer|in:0,1',
  40. 'time_format' => 'required|integer|in:' . \implode(',', \array_keys($this->c->TIME_FORMATS)),
  41. 'date_format' => 'required|integer|in:' . \implode(',', \array_keys($this->c->DATE_FORMATS)),
  42. 'show_smilies' => 'required|integer|in:0,1',
  43. 'show_sig' => 'required|integer|in:0,1',
  44. 'show_avatars' => 'required|integer|in:0,1',
  45. 'show_img' => 'required|integer|in:0,1',
  46. 'show_img_sig' => 'required|integer|in:0,1',
  47. 'disp_topics' => 'integer|min:0|max:50|to_zero',
  48. 'disp_posts' => 'integer|min:0|max:50|to_zero',
  49. 'ip_check_type' => 'required|integer|in:' . (
  50. $this->rules->editIpCheckType
  51. ? '0,1,2'
  52. : (int) $this->curUser->ip_check_type
  53. ),
  54. 'save' => 'required|string',
  55. ])->addAliases([
  56. 'language' => 'Language',
  57. 'style' => 'Style',
  58. 'timezone' => 'Time zone',
  59. 'dst' => 'DST label',
  60. 'time_format' => 'Time format',
  61. 'date_format' => 'Date format',
  62. 'show_smilies' => 'Smilies label',
  63. 'show_sig' => 'Sigs label',
  64. 'show_avatars' => 'Avatars label',
  65. 'show_img' => 'Images label',
  66. 'show_img_sig' => 'Images sigs label',
  67. 'disp_topics' => 'Topics per page label',
  68. 'disp_posts' => 'Posts per page label',
  69. 'ip_check_type' => 'IP check',
  70. ])->addArguments([
  71. 'token' => $args,
  72. ])->addMessages([
  73. ]);
  74. if ($this->rules->viewSubscription) { // ???? модераторы?
  75. $v = $this->c->Validator
  76. ->addRules([
  77. 'notify_with_post' => 'required|integer|in:0,1',
  78. 'auto_notify' => 'required|integer|in:0,1',
  79. ])->addAliases([
  80. 'notify_with_post' => 'Notify label',
  81. 'auto_notify' => 'Auto notify label',
  82. ]);
  83. }
  84. if ($v->validation($_POST)) {
  85. $data = $v->getData();
  86. unset($data['token']);
  87. $this->curUser->replAttrs($data, true);
  88. if ($this->curUser->isModified('ip_check_type')) {
  89. $this->c->users->updateLoginIpCache($this->curUser); // ????
  90. }
  91. $this->c->users->update($this->curUser);
  92. return $this->c->Redirect->page('EditUserBoardConfig', $args)->message('Board configuration redirect');
  93. }
  94. $this->fIswev = $v->getErrors();
  95. }
  96. $this->crumbs = $this->crumbs(
  97. [
  98. $this->c->Router->link('EditUserBoardConfig', $args),
  99. 'Board configuration',
  100. ]
  101. );
  102. $this->form = $this->form($args);
  103. $this->actionBtns = $this->btns('config');
  104. return $this;
  105. }
  106. /**
  107. * Преобразовывает число меньше 10 в 0
  108. */
  109. public function vToZero(Validator $v, int $value): int
  110. {
  111. return $value < 10 ? 0 : $value;
  112. }
  113. /**
  114. * Создает массив данных для формы
  115. */
  116. protected function form(array $args): array
  117. {
  118. $form = [
  119. 'action' => $this->c->Router->link('EditUserBoardConfig', $args),
  120. 'hidden' => [
  121. 'token' => $this->c->Csrf->create('EditUserBoardConfig', $args),
  122. ],
  123. 'sets' => [],
  124. 'btns' => [
  125. 'save' => [
  126. 'type' => 'submit',
  127. 'value' => __('Save changes'),
  128. ],
  129. ],
  130. ];
  131. $yn = [1 => __('Yes'), 0 => __('No')];
  132. $langs = $this->c->Func->getNameLangs();
  133. $styles = $this->c->Func->getStyles();
  134. $timeFormat = [];
  135. foreach ($this->c->TIME_FORMATS as $key => $value) {
  136. $timeFormat[$key] = dt(\time(), false, null, $value, true, true)
  137. . (
  138. $key > 1
  139. ? ''
  140. : ' (' . __('Default for language') . ')'
  141. );
  142. }
  143. $dateFormat = [];
  144. foreach ($this->c->DATE_FORMATS as $key => $value) {
  145. $dateFormat[$key] = dt(\time(), true, $value, null, false, true)
  146. . (
  147. $key > 1
  148. ? ''
  149. : ' (' . __('Default for language') . ')'
  150. );
  151. }
  152. $form['sets']['essentials'] = [
  153. 'legend' => 'Essentials',
  154. 'class' => ['data-edit'],
  155. 'fields' => [
  156. 'language' => [
  157. 'type' => 'select',
  158. 'options' => $langs,
  159. 'value' => $this->curUser->language,
  160. 'caption' => 'Language',
  161. ],
  162. 'style' => [
  163. 'type' => 'select',
  164. 'options' => $styles,
  165. 'value' => $this->curUser->style,
  166. 'caption' => 'Style',
  167. ],
  168. 'timezone' => [
  169. 'type' => 'select',
  170. 'options' => [
  171. '-12' => __('UTC-12:00'),
  172. '-11' => __('UTC-11:00'),
  173. '-10' => __('UTC-10:00'),
  174. '-9.5' => __('UTC-09:30'),
  175. '-9' => __('UTC-09:00'),
  176. '-8.5' => __('UTC-08:30'),
  177. '-8' => __('UTC-08:00'),
  178. '-7' => __('UTC-07:00'),
  179. '-6' => __('UTC-06:00'),
  180. '-5' => __('UTC-05:00'),
  181. '-4' => __('UTC-04:00'),
  182. '-3.5' => __('UTC-03:30'),
  183. '-3' => __('UTC-03:00'),
  184. '-2' => __('UTC-02:00'),
  185. '-1' => __('UTC-01:00'),
  186. '0' => __('UTC'),
  187. '1' => __('UTC+01:00'),
  188. '2' => __('UTC+02:00'),
  189. '3' => __('UTC+03:00'),
  190. '3.5' => __('UTC+03:30'),
  191. '4' => __('UTC+04:00'),
  192. '4.5' => __('UTC+04:30'),
  193. '5' => __('UTC+05:00'),
  194. '5.5' => __('UTC+05:30'),
  195. '5.75' => __('UTC+05:45'),
  196. '6' => __('UTC+06:00'),
  197. '6.5' => __('UTC+06:30'),
  198. '7' => __('UTC+07:00'),
  199. '8' => __('UTC+08:00'),
  200. '8.75' => __('UTC+08:45'),
  201. '9' => __('UTC+09:00'),
  202. '9.5' => __('UTC+09:30'),
  203. '10' => __('UTC+10:00'),
  204. '10.5' => __('UTC+10:30'),
  205. '11' => __('UTC+11:00'),
  206. '11.5' => __('UTC+11:30'),
  207. '12' => __('UTC+12:00'),
  208. '12.75' => __('UTC+12:45'),
  209. '13' => __('UTC+13:00'),
  210. '14' => __('UTC+14:00'),
  211. ],
  212. 'value' => $this->curUser->timezone,
  213. 'caption' => 'Time zone',
  214. ],
  215. 'dst' => [
  216. 'type' => 'radio',
  217. 'value' => $this->curUser->dst,
  218. 'values' => $yn,
  219. 'caption' => 'DST label',
  220. 'help' => 'DST help',
  221. ],
  222. 'time_format' => [
  223. 'type' => 'select',
  224. 'options' => $timeFormat,
  225. 'value' => $this->curUser->time_format,
  226. 'caption' => 'Time format',
  227. ],
  228. 'date_format' => [
  229. 'type' => 'select',
  230. 'options' => $dateFormat,
  231. 'value' => $this->curUser->date_format,
  232. 'caption' => 'Date format',
  233. ],
  234. ],
  235. ];
  236. $form['sets']['viewing-posts'] = [
  237. 'legend' => 'Viewing posts',
  238. 'class' => ['data-edit'],
  239. 'fields' => [
  240. 'show_smilies' => [
  241. 'type' => 'radio',
  242. 'value' => $this->curUser->show_smilies,
  243. 'values' => $yn,
  244. 'caption' => 'Smilies label',
  245. 'help' => 'Smilies info',
  246. ],
  247. 'show_sig' => [
  248. 'type' => 'radio',
  249. 'value' => $this->curUser->show_sig,
  250. 'values' => $yn,
  251. 'caption' => 'Sigs label',
  252. 'help' => 'Sigs info',
  253. ],
  254. 'show_avatars' => [
  255. 'type' => 'radio',
  256. 'value' => $this->curUser->show_avatars,
  257. 'values' => $yn,
  258. 'caption' => 'Avatars label',
  259. 'help' => 'Avatars info',
  260. ],
  261. 'show_img' => [
  262. 'type' => 'radio',
  263. 'value' => $this->curUser->show_img,
  264. 'values' => $yn,
  265. 'caption' => 'Images label',
  266. 'help' => 'Images info',
  267. ],
  268. 'show_img_sig' => [
  269. 'type' => 'radio',
  270. 'value' => $this->curUser->show_img_sig,
  271. 'values' => $yn,
  272. 'caption' => 'Images sigs label',
  273. 'help' => 'Images sigs info',
  274. ],
  275. ],
  276. ];
  277. $form['sets']['pagination'] = [
  278. 'legend' => 'Pagination',
  279. 'class' => ['data-edit'],
  280. 'fields' => [
  281. 'disp_topics' => [
  282. 'type' => 'number',
  283. 'min' => '0',
  284. 'max' => '50',
  285. 'value' => $this->curUser->__disp_topics,
  286. 'caption' => 'Topics per page label',
  287. 'help' => 'For default',
  288. ],
  289. 'disp_posts' => [
  290. 'type' => 'number',
  291. 'min' => '0',
  292. 'max' => '50',
  293. 'value' => $this->curUser->__disp_posts,
  294. 'caption' => 'Posts per page label',
  295. 'help' => 'For default',
  296. ],
  297. ],
  298. ];
  299. $form['sets']['security'] = [
  300. 'legend' => 'Security',
  301. 'class' => ['data-edit'],
  302. 'fields' => [
  303. 'ip_check_type' => [
  304. 'type' => 'select',
  305. 'options' => [
  306. '0' => __('Disable check'),
  307. '1' => __('Not strict check'),
  308. '2' => __('Strict check'),
  309. ],
  310. 'value' => $this->curUser->ip_check_type,
  311. 'caption' => 'IP check',
  312. 'help' => 'IP check info',
  313. 'disabled' => $this->rules->editIpCheckType ? null : true,
  314. ],
  315. ],
  316. ];
  317. if ($this->rules->viewSubscription) { // ???? модераторы?
  318. $form['sets']['subscriptions'] = [
  319. 'legend' => 'Subscription options',
  320. 'class' => ['data-edit'],
  321. 'fields' => [
  322. 'notify_with_post' => [
  323. 'type' => 'radio',
  324. 'value' => $this->curUser->notify_with_post,
  325. 'values' => $yn,
  326. 'caption' => 'Notify label',
  327. 'help' => 'Notify info',
  328. ],
  329. 'auto_notify' => [
  330. 'type' => 'radio',
  331. 'value' => $this->curUser->auto_notify,
  332. 'values' => $yn,
  333. 'caption' => 'Auto notify label',
  334. 'help' => 'Auto notify info',
  335. ],
  336. ],
  337. ];
  338. }
  339. return $form;
  340. }
  341. }