浏览代码

Add bbcode management 3

Visman 4 年之前
父节点
当前提交
0ea771e2ac

+ 6 - 0
app/Controllers/Routing.php

@@ -557,6 +557,12 @@ class Routing
                 'AdminParserBBCode:edit',
                 'AdminParserBBCode:edit',
                 'AdminBBCodeNew'
                 'AdminBBCodeNew'
             );
             );
+            $r->add(
+                $r::GET,
+                '/admin/parser/bbcode/default/{id:[1-9]\d*}/{token}',
+                'AdminParserBBCode:default',
+                'AdminBBCodeDefault'
+            );
             $r->add(
             $r->add(
                 $r::DUO,
                 $r::DUO,
                 '/admin/parser/smilies',
                 '/admin/parser/smilies',

+ 33 - 1
app/Models/BBCodeList/Structure.php

@@ -27,6 +27,38 @@ class Structure extends ParentModel
         ];
         ];
     }
     }
 
 
+    public function isInDefault(): bool
+    {
+        if (empty($this->tag)) {
+            return false;
+        }
+
+        $bbcode = include $this->c->bbcode->fileDefault;
+
+        foreach ($bbcode as $cur) {
+            if ($this->tag === $cur['tag']) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    public function setDefault(): Structure
+    {
+        if (! $this->isInDefault()) {
+            throw new RuntimeException("There is no default for the '{$this->tag}' tag");
+        }
+
+        $bbcode = include $this->c->bbcode->fileDefault;
+
+        foreach ($bbcode as $cur) {
+            if ($this->tag === $cur['tag']) {
+                return $this->setAttrs($cur);
+            }
+        }
+    }
+
     public function fromString(string $data): Structure
     public function fromString(string $data): Structure
     {
     {
         return $this->setAttrs(\json_decode($data, true, 512, \JSON_THROW_ON_ERROR));
         return $this->setAttrs(\json_decode($data, true, 512, \JSON_THROW_ON_ERROR));
@@ -513,7 +545,7 @@ class Structure extends ParentModel
             $result = @eval($testCode);
             $result = @eval($testCode);
 
 
             if (true !== $result) {
             if (true !== $result) {
-                $error = error_get_last();
+                $error = \error_get_last();
                 $message = $error['message'] ?? 'Unknown error';
                 $message = $error['message'] ?? 'Unknown error';
                 $line    = $error['line'] ?? '';
                 $line    = $error['line'] ?? '';
 
 

+ 45 - 1
app/Models/Pages/Admin/Parser/BBCode.php

@@ -117,6 +117,12 @@ class BBCode extends Parser
                 ],
                 ],
             ],
             ],
             'btns'   => [
             'btns'   => [
+                'new' => [
+                    'type'      => 'btn',
+                    'value'     => __('New BBCode'),
+                    'link'      => $this->c->Router->link('AdminBBCodeNew'),
+//                    'accesskey' => 'n',
+                ],
                 'save' => [
                 'save' => [
                     'type'      => 'submit',
                     'type'      => 'submit',
                     'value'     => __('Save changes'),
                     'value'     => __('Save changes'),
@@ -240,9 +246,11 @@ class BBCode extends Parser
         }
         }
 
 
         $bbTypes = [];
         $bbTypes = [];
+        $bbNames = [];
         foreach ($this->c->bbcode->bbcodeTable as $cur) {
         foreach ($this->c->bbcode->bbcodeTable as $cur) {
             $type = $this->c->BBStructure->fromString($cur['bb_structure'])->type;
             $type = $this->c->BBStructure->fromString($cur['bb_structure'])->type;
             $bbTypes[$type] = $type;
             $bbTypes[$type] = $type;
+            $bbNames[$cur['bb_tag']] = $cur['bb_tag'];
         }
         }
         $this->bbTypes = $bbTypes;
         $this->bbTypes = $bbTypes;
 
 
@@ -264,7 +272,7 @@ class BBCode extends Parser
                     'check_all'                 => [$this, 'vCheckAll'],
                     'check_all'                 => [$this, 'vCheckAll'],
                 ])->addRules([
                 ])->addRules([
                     'token'                     => 'token:' . $page,
                     'token'                     => 'token:' . $page,
-                    'tag'                       => $id > 0 ? 'absent' : 'required|string:trim|regex:%^[a-z\*][a-z\d-]{0,10}$%',
+                    'tag'                       => $id > 0 ? 'absent' : 'required|string:trim|regex:%^[a-z\*][a-z\d-]{0,10}$%|not_in:' . \implode(',', $bbNames),
                     'type'                      => 'required|string|in:' . \implode(',', $bbTypes),
                     'type'                      => 'required|string|in:' . \implode(',', $bbTypes),
                     'type_new'                  => 'string:trim|regex:%^[a-z][a-z\d-]{0,19}$%',
                     'type_new'                  => 'string:trim|regex:%^[a-z][a-z\d-]{0,19}$%',
                     'parents.*'                 => 'required|string|in:' . \implode(',', $bbTypes),
                     'parents.*'                 => 'required|string|in:' . \implode(',', $bbTypes),
@@ -382,6 +390,18 @@ class BBCode extends Parser
             ],
             ],
             'sets' => [],
             'sets' => [],
             'btns'   => [
             'btns'   => [
+                'reset' => [
+                    'type'      => 'btn',
+                    'value'     => __('Default structure'),
+                    'link'      => $this->c->Router->link(
+                        'AdminBBCodeDefault',
+                        [
+                            'id'    => $id,
+                            'token' => null,
+                        ]
+                    ),
+//                    'accesskey' => 'r',
+                ],
                 'save' => [
                 'save' => [
                     'type'      => 'submit',
                     'type'      => 'submit',
                     'value'     => __('Save'),
                     'value'     => __('Save'),
@@ -390,6 +410,10 @@ class BBCode extends Parser
             ],
             ],
         ];
         ];
 
 
+        if (! $structure->isInDefault()) {
+            unset($form['btns']['reset']);
+        }
+
         $yn = [1 => __('Yes'), 0 => __('No')];
         $yn = [1 => __('Yes'), 0 => __('No')];
 
 
         $form['sets']['structure'] = [
         $form['sets']['structure'] = [
@@ -600,4 +624,24 @@ class BBCode extends Parser
             'fields' => $fields,
             'fields' => $fields,
         ];
         ];
     }
     }
+
+    /**
+     * Устанавливает структуру bb-кода по умолчанию
+     */
+    public function default(array $args, string $method): Page
+    {
+        if (! $this->c->Csrf->verify($args['token'], 'AdminBBCodeDefault', $args)) {
+            return $this->c->Message->message('Bad token');
+        }
+
+        $id = (int) $args['id'];
+
+        $structure = $this->c->BBStructure
+            ->fromString($this->c->bbcode->load()->bbcodeTable[$id]['bb_structure'])
+            ->setDefault();
+
+        $this->c->bbcode->update($id, $structure);
+
+        return $this->c->Redirect->page('AdminBBCodeEdit', ['id' => $id])->message('BBCode updated redirect');
+    }
 }
 }

+ 6 - 0
app/lang/en/admin_parser.po

@@ -317,3 +317,9 @@ msgstr "BBCode updated."
 
 
 msgid "BBCode added redirect"
 msgid "BBCode added redirect"
 msgstr "BBCode added."
 msgstr "BBCode added."
+
+msgid "New BBCode"
+msgstr "New BBCode"
+
+msgid "Default structure"
+msgstr "Default"

+ 6 - 0
app/lang/ru/admin_parser.po

@@ -317,3 +317,9 @@ msgstr "BB-код обновлен."
 
 
 msgid "BBCode added redirect"
 msgid "BBCode added redirect"
 msgstr "BB-код добавлен."
 msgstr "BB-код добавлен."
+
+msgid "New BBCode"
+msgstr "Новый BB-код"
+
+msgid "Default structure"
+msgstr "По умолчанию"

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

@@ -3015,6 +3015,19 @@ body,
   font-family: "DejaVu Sans Mono", Consolas, "Droid Sans Mono", Monospace, Monaco, "Courier New", Courier;
   font-family: "DejaVu Sans Mono", Consolas, "Droid Sans Mono", Monospace, Monaco, "Courier New", Courier;
 }
 }
 
 
+#fork .f-bbcode-form .f-btns,
+#fork .f-editbbcode-form .f-btns {
+  display: flex;
+  flex-direction: column;
+}
+
+#fork .f-bbcode-form .f-btn[data-name=new],
+#fork .f-editbbcode-form  .f-btn[data-name=reset]{
+  width: auto;
+  margin-bottom: 0.625rem;
+  margin-left: auto;
+}
+
 @media screen and (min-width: 25rem) {
 @media screen and (min-width: 25rem) {
   #fork .f-bbcode-form .f-fs-bbcode {
   #fork .f-bbcode-form .f-fs-bbcode {
     display: flex;
     display: flex;