瀏覽代碼

2018-01-05

Visman 7 年之前
父節點
當前提交
5ca0ec778e

+ 67 - 0
app/Models/Forum/Delete.php

@@ -0,0 +1,67 @@
+<?php
+
+namespace ForkBB\Models\Forum;
+
+use ForkBB\Models\Action;
+use ForkBB\Models\Forum\Model as Forum;
+use InvalidArgumentException;
+use RuntimeException;
+
+class Delete extends Action
+{
+    /**
+     * Удаляет раздел(ы) 
+     *
+     * @param mixed ...$args
+     *
+     * @throws InvalidArgumentException
+     * @throws RuntimeException
+     */
+    public function delete(...$args)
+    {
+        if (empty($args)) {
+            throw new InvalidArgumentException('No arguments, expected forum(s)');
+        }
+
+        $forums = [];
+        $all    = [];
+
+        foreach ($args as $arg) {
+            if ($arg instanceof Forum) {
+                if (! $this->c->forums->get($arg->id) instanceof Forum) {
+                    throw new RuntimeException('Forum unavailable');
+                }
+                $forums[$arg->id] = $arg;
+                $all[$arg->id]    = true;
+                foreach (array_keys($arg->descendants) as $id) { //???? а если не админ?
+                    $all[$id] = true;
+                }
+            } else {
+                throw new InvalidArgumentException('Expected forum(s)');
+            }
+        }
+
+        if (array_diff_key($all, $forums)) {
+            throw new RuntimeException('Descendants should not be or they should be deleted too');
+        }
+
+        $this->c->topics->delete(...$args);
+
+        //???? подписки, опросы, предупреждения, поисковый индекс, метки посещения тем
+
+        foreach ($forums as $forum) {
+            $this->c->groups->Perm->reset($forum);
+        }
+
+        $vars = [
+            ':forums' => array_keys($forums),
+        ];
+        $sql = 'DELETE FROM ::mark_of_forum 
+                WHERE fid IN (?ai:forums)';
+        $this->c->DB->exec($sql, $vars);
+
+        $sql = 'DELETE FROM ::forums 
+                WHERE id IN (?ai:forums)';
+        $this->c->DB->exec($sql, $vars);
+    }
+}

+ 1 - 1
app/Models/Forum/Manager.php

@@ -100,7 +100,7 @@ class Manager extends ManagerModel
      * 
      * @return int
      */
-    public function insert(Topic $forum)
+    public function insert(Forum $forum)
     {
         $id = $this->Save->insert($forum);
         $this->set($id, $forum);

+ 22 - 5
app/Models/Group/Perm.php

@@ -25,12 +25,8 @@ class Perm extends Action
      */
     public function get(Forum $forum)
     {
-        if ($forum->id < 1) {
-            throw new RuntimeException('The forum does not have ID');
-        }
-
         $vars = [
-            ':fid' => $forum->id,
+            ':fid' => $forum->id > 0 ? $forum->id : 0,
             ':adm' => $this->c->GROUP_ADMIN,
         ];
         $sql = 'SELECT g.g_id, fp.read_forum, fp.post_replies, fp.post_topics 
@@ -117,4 +113,25 @@ class Perm extends Action
             }
         }
     }
+
+    /**
+     * Обновление разрешений для раздела
+     *
+     * @param Forum $forum
+     * 
+     * @throws RuntimeException
+     */
+    public function reset(Forum $forum)
+    {
+        if ($forum->id < 1) {
+            throw new RuntimeException('The forum does not have ID');
+        }
+
+        $vars = [
+            ':fid' => $forum->id,
+        ];
+        $sql = 'DELETE FROM ::forum_perms 
+                WHERE forum_id=?i:fid';
+        $this->c->DB->exec($sql, $vars);
+    }
 }

+ 43 - 20
app/Models/Pages/Admin/Forums.php

@@ -113,6 +113,12 @@ class Forums extends Admin
             ],
             'sets'   => [],
             'btns'   => [
+                'new'  => [
+                    'type'      => 'btn',
+                    'value'     => \ForkBB\__('New forum'),
+                    'link'      => $this->c->Router->link('AdminForumsNew'),
+                    'accesskey' => 'n',
+                ],
                 'update'  => [
                     'type'      => 'submit',
                     'value'     => \ForkBB\__('Update positions'),
@@ -160,12 +166,14 @@ class Forums extends Admin
                 'value' => $forum->disp_position,
                 'title' => \ForkBB\__('Position label'),
             ];
+            $disabled = (bool) $forum->subforums;
             $fieldset[] = [
-                'dl'    => ['delete', 'inline'],
-                'type'  => 'btn',
-                'value' => '❌',
-                'title' => \ForkBB\__('Delete'),
-                'link'  => $this->c->Router->link('AdminForumsDelete', ['id' => $forum->id]),
+                'dl'       => ['delete', 'inline'],
+                'type'     => 'btn',
+                'value'    => '❌',
+                'title'    => \ForkBB\__('Delete'),
+                'link'     => $disabled ? '#' : $this->c->Router->link('AdminForumsDelete', ['id' => $forum->id]),
+                'disabled' => $disabled,
             ];
         }
 
@@ -194,7 +202,7 @@ class Forums extends Admin
     public function delete(array $args, $method)
     {
         $forum = $this->c->forums->get((int) $args['id']);
-        if (! $forum instanceof Forum) {
+        if (! $forum instanceof Forum || $forum->subforums) {
             return $this->c->Message->message('Bad request');
         }
 
@@ -321,6 +329,8 @@ class Forums extends Admin
                 'perms.*.read_forum'   => 'checkbox',
                 'perms.*.post_replies' => 'checkbox',
                 'perms.*.post_topics'  => 'checkbox',
+                'submit'               => 'string',
+                'reset'                => empty($forum->id) ? 'absent' : 'string',
             ])->setArguments([
                 'token' => $args,
             ]);
@@ -342,16 +352,21 @@ class Forums extends Admin
             if ($valid) {
                 $this->c->DB->beginTransaction();
 
-                if (empty($args['id'])) {
-                    $message = 'Forum added redirect';
-                    $this->c->forums->insert($forum);
+                if ($v->reset) {
+                    $message = 'Perms reverted redirect';
+                    $this->c->groups->Perm->reset($forum);
                 } else {
-                    $message = 'Forum updated redirect';
-                    $this->c->forums->update($forum);
+                    if (empty($args['id'])) {
+                        $message = 'Forum added redirect';
+                        $this->c->forums->insert($forum);
+                    } else {
+                        $message = 'Forum updated redirect';
+                        $this->c->forums->update($forum);
+                    }
+    
+                    $this->c->groups->Perm->update($forum, $v->perms);
                 }
 
-                $this->c->groups->Perm->update($forum, $v->perms);
-
                 $this->c->DB->commit();
 
                 $this->c->Cache->delete('forums_mark');
@@ -386,13 +401,21 @@ class Forums extends Admin
                 'token' => $this->c->Csrf->create($marker, $args),
             ],
             'sets'   => [],
-            'btns'   => [
-                'submit'  => [
-                    'type'      => 'submit',
-                    'value'     => empty($forum->id) ? \ForkBB\__('Add') : \ForkBB\__('Update'),
-                    'accesskey' => 's',
-                ],
-            ],
+            'btns'   => [],
+        ];
+
+        if ($forum->id > 0) {
+            $form['btns']['reset'] = [
+                'type'      => 'submit',
+                'value'     => \ForkBB\__('Revert to default'),
+                'accesskey' => 'r',
+            ];
+        }
+
+        $form['btns']['submit'] = [
+            'type'      => 'submit',
+            'value'     => empty($forum->id) ? \ForkBB\__('Add') : \ForkBB\__('Update'),
+            'accesskey' => 's',
         ];
 
         $form['sets'][] = [

+ 5 - 1
app/templates/layouts/form.tpl

@@ -54,7 +54,7 @@
       @elseif ('password' === $cur['type'])
                 <input @if (isset($cur['required'])) required @endif @if (! empty($cur['disabled'])) disabled @endif @if (isset($cur['autofocus'])) autofocus @endif class="f-ctrl" id="id-{{ $key }}" name="{{ $key }}" type="password" @if (! empty($cur['maxlength'])) maxlength="{{ $cur['maxlength'] }}" @endif @if (isset($cur['pattern'])) pattern="{{ $cur['pattern'] }}" @endif @if (isset($cur['value'])) value="{{ $cur['value'] }}" @endif>
       @elseif ('btn' === $cur['type'])
-                <a class="f-btn" href="{!! $cur['link'] !!}">{{ $cur['value'] }}</a>
+                <a class="f-btn @if (! empty($cur['disabled'])) f-disabled @endif" href="{!! $cur['link'] !!}" @if (! empty($cur['disabled'])) tabindex="-1" @endif>{{ $cur['value'] }}</a>
       @endif
       @if (isset($cur['info']))
                 <p class="f-child4">{!! $cur['info'] !!}</p>
@@ -67,7 +67,11 @@
 @endforeach
           <p class="f-btns">
 @foreach ($form['btns'] as $key => $cur)
+  @if ('submit' === $cur['type'])
             <input class="f-btn @if(isset($cur['class'])) {{ $cur['class'] }} @endif" type="{{ $cur['type'] }}" name="{{ $key }}" value="{{ $cur['value'] }}" @if (isset($cur['accesskey'])) accesskey="{{ $cur['accesskey'] }}" @endif>
+  @elseif ('btn'=== $cur['type'])          
+            <a class="f-btn @if(isset($cur['class'])) {{ $cur['class'] }} @endif" data-name="{{ $key }}" href="{!! $cur['link'] !!}" @if (isset($cur['accesskey'])) accesskey="{{ $cur['accesskey'] }}" @endif>{{ $cur['value'] }}</a>
+  @endif
 @endforeach
           </p>
         </form>

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

@@ -1698,4 +1698,16 @@ li + li .f-btn {
   left: 0;
   right: 0;
   background: gray;
+}
+
+.f-editforums-form .f-btn[data-name=new],
+.f-editforum-form .f-btn[name=reset] {
+  float: right;
+  width: auto;
+}
+
+.f-btn.f-disabled {
+  pointer-events: none;
+  cursor: default;
+  opacity: 0.5;
 }