Browse Source

Another teaspoon of code for the extension system

Visman 1 year ago
parent
commit
dff71bcace

+ 1 - 1
app/Controllers/Routing.php

@@ -839,7 +839,7 @@ class Routing
                 'AdminAntispam'
                 'AdminAntispam'
             );
             );
             $r->add(
             $r->add(
-                $r::GET,
+                $r::DUO,
                 '/admin/extensions',
                 '/admin/extensions',
                 'AdminExtensions:info',
                 'AdminExtensions:info',
                 'AdminExtensions'
                 'AdminExtensions'

+ 35 - 0
app/Models/Extension/Extension.php

@@ -39,6 +39,11 @@ class Extension extends Model
         return $this->dbData['version'] ?? $this->fileData['version'];
         return $this->dbData['version'] ?? $this->fileData['version'];
     }
     }
 
 
+    protected function getfileVersion(): string
+    {
+        return $this->fileData['version'] ?? '-';
+    }
+
     protected function getname(): string
     protected function getname(): string
     {
     {
         return $this->dbData['name'] ?? $this->fileData['name'];
         return $this->dbData['name'] ?? $this->fileData['name'];
@@ -102,4 +107,34 @@ class Extension extends Model
                 throw new RuntimeException("Error in {$this->name} extension status");
                 throw new RuntimeException("Error in {$this->name} extension status");
         }
         }
     }
     }
+
+    protected function getcanInstall(): bool
+    {
+        return self::NOT_INSTALLED === $this->status;
+    }
+
+    protected function getcanUninstall(): bool
+    {
+        return \in_array($this->status, [self::DISABLED, self::DISABLED_DOWN, self::DISABLED_UP], true);
+    }
+
+    protected function getcanUpdate(): bool
+    {
+        return \in_array($this->status, [self::DISABLED_UP, self::ENABLED_UP], true);
+    }
+
+    protected function getcanDowndate(): bool
+    {
+        return \in_array($this->status, [self::DISABLED_DOWN, self::ENABLED_DOWN], true);
+    }
+
+    protected function getcanEnable(): bool
+    {
+        return self::DISABLED === $this->status;
+    }
+
+    protected function getcanDisable(): bool
+    {
+        return \in_array($this->status, [self::ENABLED, self::ENABLED_DOWN, self::ENABLED_UP, self::CRASH], true);
+    }
 }
 }

+ 2 - 1
app/Models/Extension/Extensions.php

@@ -106,7 +106,8 @@ class Extensions extends Manager
      */
      */
     protected function prepare(array $files): array
     protected function prepare(array $files): array
     {
     {
-        $v = $this->c->Validator->reset()
+        $v = clone $this->c->Validator;
+        $v = $v->reset()
             ->addValidators([
             ->addValidators([
             ])->addRules([
             ])->addRules([
                 'name'               => 'required|string',
                 'name'               => 'required|string',

+ 57 - 1
app/Models/Pages/Admin/Extensions.php

@@ -10,6 +10,7 @@ declare(strict_types=1);
 
 
 namespace ForkBB\Models\Pages\Admin;
 namespace ForkBB\Models\Pages\Admin;
 
 
+use ForkBB\Models\Extension\Extension;
 use ForkBB\Models\Page;
 use ForkBB\Models\Page;
 use ForkBB\Models\Pages\Admin;
 use ForkBB\Models\Pages\Admin;
 use Throwable;
 use Throwable;
@@ -20,14 +21,69 @@ class Extensions extends Admin
     /**
     /**
      * Подготавливает данные для шаблона
      * Подготавливает данные для шаблона
      */
      */
-    public function info(): Page
+    public function info(array $args, string $method): Page
     {
     {
         $this->c->Lang->load('admin_extensions');
         $this->c->Lang->load('admin_extensions');
 
 
+        if ('POST' === $method) {
+            return $this->action($args, $method);
+        }
+
         $this->nameTpl    = 'admin/extensions';
         $this->nameTpl    = 'admin/extensions';
         $this->aIndex     = 'extensions';
         $this->aIndex     = 'extensions';
         $this->extensions = $this->c->extensions->repository;
         $this->extensions = $this->c->extensions->repository;
+        $this->actionLink = $this->c->Router->link('AdminExtensions');
+        $this->formsToken = $this->c->Csrf->create('AdminExtensions');
 
 
         return $this;
         return $this;
     }
     }
+
+    protected function action(array $args, string $method): Page
+    {
+        $v = $this->c->Validator->reset()
+            ->addRules([
+                'token'     => 'token:AdminExtensions',
+                'name'      => 'required|string',
+                'confirm'   => 'required|string|in:1',
+                'install'   => 'string',
+                'uninstall' => 'string',
+                'update'    => 'string',
+                'downdate'  => 'string',
+                'enable'    => 'string',
+                'disable'   => 'string',
+            ])->addAliases([
+            ])->addMessages([
+                'confirm' => [FORK_MESS_WARN, 'No confirm redirect'],
+            ])->addArguments([
+            ]);
+
+        if (! $v->validation($_POST)) {
+            $message         = $this->c->Message;
+            $message->fIswev = $v->getErrors();
+
+            return $message->message('');
+        }
+
+        $ext = $this->c->extensions->get($v->name);
+
+        if (! $ext instanceof Extension) {
+            return $this->c->Message->message('Extension not found');
+        }
+
+        $actions = $v->getData(false, ['token', 'name', 'confirm']);
+        $action  = \array_key_first($actions);
+
+        if (empty($action)) {
+            return $this->c->Message->message('Invalid action');
+        }
+
+        $property = 'can' . \ucfirst($action);
+
+        if (true !== $ext->{$property}) {
+            return $this->c->Message->message('Invalid action');
+        }
+
+        exit(var_dump('<pre>', $_POST, '</pre>'));
+
+    }
 }
 }

+ 30 - 3
app/lang/en/admin_extensions.po

@@ -13,7 +13,7 @@ msgstr ""
 "Language: en\n"
 "Language: en\n"
 
 
 msgid "Details"
 msgid "Details"
-msgstr "Details"
+msgstr "Details:"
 
 
 msgid "Name"
 msgid "Name"
 msgstr "Name of package"
 msgstr "Name of package"
@@ -31,10 +31,10 @@ msgid "Licence"
 msgstr "Licence"
 msgstr "Licence"
 
 
 msgid "Requirements"
 msgid "Requirements"
-msgstr "Requirements"
+msgstr "Requirements:"
 
 
 msgid "Authors"
 msgid "Authors"
-msgstr "Author(s)"
+msgstr "Author(s):"
 
 
 msgid "php"
 msgid "php"
 msgstr "PHP version"
 msgstr "PHP version"
@@ -59,3 +59,30 @@ msgstr "Enabled, but package changed!"
 
 
 msgid "Crash"
 msgid "Crash"
 msgstr "Crash, package not found!"
 msgstr "Crash, package not found!"
+
+msgid "Install_"
+msgstr "Install"
+
+msgid "Uninstall_"
+msgstr "Uninstall"
+
+msgid "Enable_"
+msgstr "Enable"
+
+msgid "Disable_"
+msgstr "Disable"
+
+msgid "Update_"
+msgstr "Update"
+
+msgid "Downdate_"
+msgstr "Downdate"
+
+msgid "Package version"
+msgstr "Package version"
+
+msgid "Extension not found"
+msgstr "Extension not found."
+
+msgid "Invalid action"
+msgstr "Invalid action."

+ 30 - 3
app/lang/ru/admin_extensions.po

@@ -13,7 +13,7 @@ msgstr ""
 "Language: ru\n"
 "Language: ru\n"
 
 
 msgid "Details"
 msgid "Details"
-msgstr "Подробности"
+msgstr "Подробности:"
 
 
 msgid "Name"
 msgid "Name"
 msgstr "Имя пакета"
 msgstr "Имя пакета"
@@ -31,10 +31,10 @@ msgid "Licence"
 msgstr "Лицензия"
 msgstr "Лицензия"
 
 
 msgid "Requirements"
 msgid "Requirements"
-msgstr "Требования"
+msgstr "Требования:"
 
 
 msgid "Authors"
 msgid "Authors"
-msgstr "Автор(ы)"
+msgstr "Автор(ы):"
 
 
 msgid "php"
 msgid "php"
 msgstr "Версия PHP"
 msgstr "Версия PHP"
@@ -59,3 +59,30 @@ msgstr "Включено, но пакет изменен!"
 
 
 msgid "Crash"
 msgid "Crash"
 msgstr "Сломано, пакет не найден!"
 msgstr "Сломано, пакет не найден!"
+
+msgid "Install_"
+msgstr "Установить"
+
+msgid "Uninstall_"
+msgstr "Удалить"
+
+msgid "Enable_"
+msgstr "Включить"
+
+msgid "Disable_"
+msgstr "Выключить"
+
+msgid "Update_"
+msgstr "Обновить"
+
+msgid "Downdate_"
+msgstr "Откатить"
+
+msgid "Package version"
+msgstr "Версия пакета"
+
+msgid "Extension not found"
+msgstr "Расширение не найдено."
+
+msgid "Invalid action"
+msgstr "Недопустимое действие."

+ 79 - 44
app/templates/_default/admin/extensions.forkbb.php

@@ -38,70 +38,105 @@
                     /<span>
                     /<span>
                   </summary>
                   </summary>
                   <div class="f-extdata f-fdiv">
                   <div class="f-extdata f-fdiv">
-                    <fieldset class="f-extfs-details">
-                      <legend class="f-fleg">{!! __('Details') !!}</legend>
-                      <dl>
-                        <dt>{!! __('Name') !!}</dt>
-                        <dd>{{ $ext->name }}</dd>
-                      </dl>
-                      <dl>
-                        <dt>{!! __('Description') !!}</dt>
-                        <dd>{{ $ext->description }}</dd>
-                      </dl>
+                    <form class="f-form" method="post" action="{{ $p->actionLink }}">
+                      <fieldset class="f-extfs-details">
+                        <legend class="f-fleg">{!! __('Details') !!}</legend>
+                        <dl>
+                          <dt>{!! __('Name') !!}</dt>
+                          <dd>{{ $ext->name }}</dd>
+                        </dl>
+                        <dl>
+                          <dt>{!! __('Package version') !!}</dt>
+                          <dd>{{ $ext->fileVersion }}</dd>
+                        </dl>
+                        <dl>
+                          <dt>{!! __('Description') !!}</dt>
+                          <dd>{{ $ext->description }}</dd>
+                        </dl>
         @if ($ext->time)
         @if ($ext->time)
-                      <dl>
-                        <dt>{!! __('Release date') !!}</dt>
-                        <dd>{{ $ext->time }}</dd>
-                      </dl>
+                        <dl>
+                          <dt>{!! __('Release date') !!}</dt>
+                          <dd>{{ $ext->time }}</dd>
+                        </dl>
         @endif
         @endif
         @if ($ext->homepage)
         @if ($ext->homepage)
-                      <dl>
-                        <dt>{!! __('Homepage') !!}</dt>
-                        <dd><a href="{{ url($ext->homepage) }}">{{ $ext->homepage }}</a></dd>
-                      </dl>
+                        <dl>
+                          <dt>{!! __('Homepage') !!}</dt>
+                          <dd><a href="{{ url($ext->homepage) }}">{{ $ext->homepage }}</a></dd>
+                        </dl>
         @endif
         @endif
         @if ($ext->license)
         @if ($ext->license)
-                      <dl>
-                        <dt>{!! __('Licence') !!}</dt>
-                        <dd>{{ $ext->license }}</dd>
-                      </dl>
+                        <dl>
+                          <dt>{!! __('Licence') !!}</dt>
+                          <dd>{{ $ext->license }}</dd>
+                        </dl>
         @endif
         @endif
-                    </fieldset>
-                    <fieldset class="f-extfs-requirements">
-                      <legend class="f-fleg">{!! __('Requirements') !!}</legend>
+                      </fieldset>
+                      <fieldset class="f-extfs-requirements">
+                        <legend class="f-fleg">{!! __('Requirements') !!}</legend>
         @foreach ($ext->requirements as $k => $v)
         @foreach ($ext->requirements as $k => $v)
-                      <dl>
-                        <dt>{!! __($k) !!}</dt>
-                        <dd>{{ $v }}</dd>
-                      </dl>
+                        <dl>
+                          <dt>{!! __($k) !!}</dt>
+                          <dd>{{ $v }}</dd>
+                        </dl>
         @endforeach
         @endforeach
-                    </fieldset>
-                    <fieldset class="f-extfs-authors">
-                      <legend class="f-fleg">{!! __('Authors') !!}</legend>
+                      </fieldset>
+                      <fieldset class="f-extfs-authors">
+                        <legend class="f-fleg">{!! __('Authors') !!}</legend>
         @foreach ($ext->authors as $author)
         @foreach ($ext->authors as $author)
-                      <dl>
-                        <dd class="f-extdd-author">
-                          <span>{{ $author['name'] }}</span>
+                        <dl>
+                          <dd class="f-extdd-author">
+                            <span>{{ $author['name'] }}</span>
             @if (! empty($author['email']) || ! empty($author['homepage']))
             @if (! empty($author['email']) || ! empty($author['homepage']))
-                          (
+                            (
                 @if ($author['email'])
                 @if ($author['email'])
-                          <a href="{{ url('mailto:'.$author['email']) }}">{{ $author['email'] }}</a>
+                            <a href="{{ url('mailto:'.$author['email']) }}">{{ $author['email'] }}</a>
                 @endif
                 @endif
                 @if ($author['homepage'])
                 @if ($author['homepage'])
                   @if ($author['email'])
                   @if ($author['email'])
-                          |
+                            |
                   @endif
                   @endif
-                          <a href="{{ url($author['homepage']) }}">{{ $author['homepage'] }}</a>
+                            <a href="{{ url($author['homepage']) }}">{{ $author['homepage'] }}</a>
                 @endif
                 @endif
-                          )
+                            )
             @endif
             @endif
             @if ($author['role'])
             @if ($author['role'])
-                          [ {{ $author['role'] }} ]
+                            [ {{ $author['role'] }} ]
             @endif
             @endif
-                        </dd>
-                      </dl>
+                          </dd>
+                        </dl>
         @endforeach
         @endforeach
-                    </fieldset>
+                      </fieldset>
+                      <fieldset calss="f-extfs-confirm">
+                        <dl>
+                          <dd>
+                            <label class="f-flblch"><input name="confirm" class="f-ychk" type="checkbox" value="1">{!! __('Confirm action') !!}</label>
+                          </dd>
+                        </dl>
+                      </fieldset>
+                      <input type="hidden" name="name" value="{{ $ext->name }}">
+                      <input type="hidden" name="token" value="{{ $p->formsToken }}">
+                      <p class="f-btns">
+        @if ($ext->canInstall)
+                        <button class="f-btn f-fbtn" name="install" value="install" title="{{ __('Install_') }}"><span>{!! __('Install_') !!}</span></button>
+        @endif
+        @if ($ext->canUninstall)
+                        <button class="f-btn f-fbtn" name="uninstall" value="uninstall" title="{{ __('Uninstall_') }}"><span>{!! __('Uninstall_') !!}</span></button>
+        @endif
+        @if ($ext->canUpdate)
+                        <button class="f-btn f-fbtn" name="update" value="update" title="{{ __('Update_') }}"><span>{!! __('Update_') !!}</span></button>
+        @endif
+        @if ($ext->canDowndate)
+                        <button class="f-btn f-fbtn" name="downdate" value="downdate" title="{{ __('Downdate_') }}"><span>{!! __('Downdate_') !!}</span></button>
+        @endif
+        @if ($ext->canEnable)
+                        <button class="f-btn f-fbtn" name="enable" value="enable" title="{{ __('Enable_') }}"><span>{!! __('Enable_') !!}</span></button>
+        @endif
+        @if ($ext->canDisable)
+                        <button class="f-btn f-fbtn" name="disable" value="disable" title="{{ __('Disable_') }}"><span>{!! __('Disable_') !!}</span></button>
+        @endif
+                      </p>
+                    </form>
                   </div>
                   </div>
                 </details>
                 </details>
               </li>
               </li>

+ 11 - 3
public/style/ForkBB/admin.css

@@ -410,7 +410,7 @@
 /* Админка/Пользователи */
 /* Админка/Пользователи */
 /************************/
 /************************/
 #fork-ausersrch-rs .f-btns {
 #fork-ausersrch-rs .f-btns {
-  text-align: end;
+  justify-content: flex-end;
 }
 }
 
 
 #fork-ausersrch-rs .f-btns .f-fbtn,
 #fork-ausersrch-rs .f-btns .f-fbtn,
@@ -1273,7 +1273,7 @@
 /****************************************/
 /****************************************/
 /* Админка/РАсширения                   */
 /* Админка/РАсширения                   */
 /****************************************/
 /****************************************/
-#fork #fork-extsinfo summary.f-extsu::after {
+#forka #fork-extsinfo summary.f-extsu::after {
   content: none;
   content: none;
 }
 }
 
 
@@ -1325,5 +1325,13 @@
 
 
 #fork-extsinfo .f-ext-status12 {
 #fork-extsinfo .f-ext-status12 {
   color: var(--c-log-in-c);
   color: var(--c-log-in-c);
-  background-color: #B71C1C;
+  background-color: #D32F2F;
+}
+
+#forka #fork-extsinfo .f-fbtn {
+  width: auto;
+}
+
+#forka .f-fbtn[data-name="uninstall"]:not(.origin) {
+  color: red;
 }
 }