Kaynağa Gözat

Add PM\PMBlock page

Visman 3 yıl önce
ebeveyn
işleme
62b5af7c3e

+ 201 - 0
app/Models/Pages/PM/PMBlock.php

@@ -0,0 +1,201 @@
+<?php
+/**
+ * This file is part of the ForkBB <https://github.com/forkbb>.
+ *
+ * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
+ * @license   The MIT License (MIT)
+ */
+
+declare(strict_types=1);
+
+namespace ForkBB\Models\Pages\PM;
+
+use ForkBB\Core\Validator;
+use ForkBB\Models\Page;
+use ForkBB\Models\Pages\PM\AbstractPM;
+use ForkBB\Models\PM\Cnst;
+use ForkBB\Models\PM\PPost;
+use ForkBB\Models\PM\PTopic;
+use ForkBB\Models\User\Model as User;
+use InvalidArgumentException;
+use function \ForkBB\__;
+
+class PMBlock extends AbstractPM
+{
+    const BLOCK   = 'block';
+    const UNBLOCK = 'unblock';
+
+    /**
+     * Подготовка данных для шаблона
+     */
+    protected function view(array $args, string $method): Page
+    {
+        $this->nameTpl    = 'pm/block';
+        $this->blockList  = $this->c->pms->block->list;
+        $this->pmCrumbs[] = [
+            $this->c->Router->link('PMAction', $args),
+            __('Blocked users'),
+        ];
+
+        return $this;
+    }
+
+    /**
+     * (Раз)блокировка пользователя или отображение списка заблокированных
+     */
+    public function block(array $args, string $method): Page
+    {
+        $this->args    = $args;
+        $this->pmIndex = Cnst::ACTION_BLOCK;
+
+        if (! isset($args['more1'])) {
+            return $this->view($args, $method);
+        }
+
+        if (isset($args['more2'])) {
+            if ('' !== \trim($args['more2'], '1234567890')) {
+                return $this->c->Message->message('Bad request');
+            }
+
+            $post = $this->pms->load(Cnst::PPOST, (int) $args['more2']);
+
+            if (! $post instanceof PPost
+                || $args['more1'] !== $post->poster_id
+            ) {
+                return $this->c->Message->message('Bad request');
+            }
+        } else {
+            $post = null;
+        }
+
+        $blockUser = $this->c->users->load($args['more1']);
+
+        if (! $blockUser instanceof User) {
+            return $this->c->Message->message('Bad request');
+        }
+
+        $blockStatus = $this->pms->block->isBlock($blockUser);
+
+        if (
+            ! $blockStatus
+            && (
+                ! $post
+                || ! $this->pms->block->canBlock($blockUser)
+            )
+        ) {
+            return $this->c->Message->message('Invalid action');
+        }
+
+        $this->c->Lang->load('validator');
+
+        $this->linkPMBlk = $this->c->Router->link('PMAction', ['action' => Cnst::ACTION_BLOCK]);
+        $this->linkBack  = $post instanceof PPost ? $post->link : $this->linkPMBlk;
+
+        if ('POST' === $method) {
+            $v = $this->c->Validator->reset()
+                ->addRules([
+                    'token'       => 'token:PMAction',
+                    'confirm'     => 'checkbox',
+                    self::BLOCK   => 'string',
+                    self::UNBLOCK => 'string',
+                ])->addAliases([
+                ])->addArguments([
+                    'token' => $args,
+                ]);
+
+            if (
+                ! $v->validation($_POST)
+                || '1' !== $v->confirm
+            ) {
+                return $this->c->Redirect->url($this->linkBack)->message('No confirm redirect');
+            } elseif (
+                (
+                    ! $v->{self::BLOCK}
+                    && ! $v->{self::UNBLOCK}
+                )
+                || (
+                    $v->{self::BLOCK}
+                    && $blockStatus
+                )
+                || (
+                    $v->{self::UNBLOCK}
+                    && ! $blockStatus
+                )
+            ) {
+                return $this->c->Message->message('Invalid action');
+            }
+
+            if ($v->{self::BLOCK}) {
+                $this->pms->block->add($blockUser);
+
+                $message = 'User is blocked redirect';
+            } else {
+                $this->pms->block->remove($blockUser);
+
+                $message = 'User is unblocked redirect';
+            }
+
+            return $this->c->Redirect->url($this->linkBack)->message($message);
+        }
+
+        $this->nameTpl    = 'pm/form';
+        $this->formTitle  = $blockStatus ? 'Unblock user title' : 'Block user title';
+        $this->formClass  = 'block';
+        $this->form       = $this->formBlock($args, $blockStatus, $blockUser);
+        $this->pmCrumbs[] = [
+            $this->c->Router->link('PMAction', $args),
+            __([$blockStatus ? 'Unblock user %s crumb' : 'Block user %s crumb', $blockUser->username]),
+        ];
+        $this->pmCrumbs[] = [
+            $this->linkPMBlk,
+            __('Blocked users'),
+        ];
+
+        return $this;
+    }
+
+    /**
+     * Подготавливает массив данных для формы
+     */
+    protected function formBlock(array $args, bool $status, User $user): array
+    {
+        $btn = $status ? self::UNBLOCK : self::BLOCK;
+
+        return [
+            'action' => $this->c->Router->link('PMAction', $args),
+            'hidden' => [
+                'token' => $this->c->Csrf->create('PMAction', $args),
+            ],
+            'sets'   => [
+                'info' => [
+                    'info' => [
+                        [
+                            'value' => __([$status ? 'Unblock user %s' : 'Block user %s', $user->username]),
+                        ],
+                    ],
+                ],
+                'confirm' => [
+                    'fields' => [
+                        'confirm' => [
+                            'type'    => 'checkbox',
+                            'label'   => __('Confirm action'),
+                            'value'   => '1',
+                            'checked' => false,
+                        ],
+                    ],
+                ],
+            ],
+            'btns'   => [
+                $btn     => [
+                    'type'  => 'submit',
+                    'value' => __($status ? 'Unblock' : 'Block'),
+                ],
+                'cancel' => [
+                    'type'  => 'btn',
+                    'value' => __('Cancel'),
+                    'link'  => $this->linkBack,
+                ],
+            ],
+        ];
+    }
+}

+ 20 - 0
app/templates/pm/block.forkbb.php

@@ -0,0 +1,20 @@
+@extends ('layouts/pm')
+      <section id="fork-pm-bl" class="f-pm f-forum @empty ($p->blockList) f-pm-bl-empty @endempty">
+        <h2>{!! __('Blocked users title') !!}</h2>
+@if (empty($p->blockList) && $iswev = ['i' => ['No blocked users']])
+        @include ('layouts/iswev')
+@else
+        <div>
+          <fieldset>
+            <ol>
+@foreach ($p->blockList as $user)
+              <li>
+                <a href="{{ $user->link }}">{{ $user->username }}</a>
+                <a class="f-btn" href="{{ '?' }}" title="{{ __('Unblock') }}">{!! __('Unblock') !!}</a>
+              </li>
+@endforeach
+            </ol>
+          </fieldset>
+        </div>
+@endif
+      </section>

+ 4 - 0
public/style/ForkBB/style.css

@@ -2416,6 +2416,10 @@ body,
   padding: 0;
 }
 
+#fork .f-block-form .f-fbtn {
+  width: auto;
+}
+
 @media screen and (min-width: 36rem) {
   #forkpm > .f-pmconfig-form dl {
     display: flex;