Pārlūkot izejas kodu

Add weight to pageHeaders for sorting

Visman 4 gadi atpakaļ
vecāks
revīzija
317b7f6034

+ 14 - 5
app/Models/Page.php

@@ -65,7 +65,7 @@ abstract class Page extends Model
         }
         $this->user         = $this->c->user; // передача текущего юзера в шаблон
 
-        $this->pageHeader('mainStyle', 'link', [
+        $this->pageHeader('mainStyle', 'link', 10000, [
             'rel'  => 'stylesheet',
             'type' => 'text/css',
             'href' => $this->publicLink("/style/{$this->user->style}/style.css"),
@@ -86,7 +86,7 @@ abstract class Page extends Model
      */
     public function prepare(): void
     {
-        $this->pageHeader('commonJS', 'script', [
+        $this->pageHeader('commonJS', 'script', 10000, [
             'src' => $this->publicLink('/js/common.js'),
         ]);
 
@@ -301,12 +301,13 @@ abstract class Page extends Model
     /**
      * Задает/получает заголовок страницы
      */
-    public function pageHeader(string $name, string $type, array $values = null) /* : mixed */
+    public function pageHeader(string $name, string $type, int $weight = 0, array $values = null) /* : mixed */
     {
         if (null === $values) {
             return $this->pageHeaders["{$name}_{$type}"] ?? null;
         } else {
             $this->pageHeaders["{$name}_{$type}"] = [
+                'weight' => $weight,
                 'type'   => $type,
                 'values' => $values
             ];
@@ -322,18 +323,26 @@ abstract class Page extends Model
     protected function getpageHeaders(): array
     {
         if ($this->canonical) {
-            $this->pageHeader('canonical', 'link', [
+            $this->pageHeader('canonical', 'link', 0, [
                 'rel'  => 'canonical',
                 'href' => $this->canonical,
             ]);
         }
         if ($this->robots) {
-            $this->pageHeader('robots', 'meta', [
+            $this->pageHeader('robots', 'meta', 11000, [
                 'name'    => 'robots',
                 'content' => $this->robots,
             ]);
         }
 
+        \uasort($this->pageHeaders, function (array $a, array $b) {
+            if ($a['weight'] === $b['weight']) {
+                return 0;
+            } else {
+                return $a['weight'] > $b['weight'] ? -1 : 1;
+            }
+        });
+
         return $this->pageHeaders;
     }
 

+ 1 - 1
app/Models/Pages/Admin/Statistics.php

@@ -50,7 +50,7 @@ class Statistics extends Admin
                     $matches[1]
                 );
                 $this->c->Cache->set('phpinfoCSS', $style);
-                $this->pageHeader('phpinfoStyle', 'link', [
+                $this->pageHeader('phpinfoStyle', 'link', 0, [
                     'rel'  => 'stylesheet',
                     'type' => 'text/css',
                     'href' => $this->c->Router->link('AdminInfoCSS', ['time' => \time()] ),

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

@@ -68,7 +68,7 @@ class Forum extends Page
 
         if ($this->c->config->i_feed_type > 0) {
             $feedType = 2 === $this->c->config->i_feed_type ? 'atom' : 'rss';
-            $this->pageHeader('feed', 'link', [
+            $this->pageHeader('feed', 'link', 0, [
                 'rel'  => 'alternate',
                 'type' => "application/{$feedType}+xml",
                 'href' => $this->c->Router->link('Feed', ['type' => $feedType, 'fid' => $forum->id]),

+ 1 - 1
app/Models/Pages/Index.php

@@ -69,7 +69,7 @@ class Index extends Page
 
         if ($this->c->config->i_feed_type > 0) {
             $feedType = 2 === $this->c->config->i_feed_type ? 'atom' : 'rss';
-            $this->pageHeader('feed', 'link', [
+            $this->pageHeader('feed', 'link', 0, [
                 'rel'  => 'alternate',
                 'type' => "application/{$feedType}+xml",
                 'href' => $this->c->Router->link('Feed', ['type' => $feedType]),

+ 1 - 1
app/Models/Pages/PostFormTrait.php

@@ -268,7 +268,7 @@ trait PostFormTrait
                 ];
             }
 
-            $this->pageHeader('pollJS', 'script', [
+            $this->pageHeader('pollJS', 'script', 9000, [
                 'src' => $this->publicLink('/js/poll.js'),
             ]);
 

+ 1 - 1
app/Models/Pages/Topic.php

@@ -167,7 +167,7 @@ class Topic extends Page
 
         if ($this->c->config->i_feed_type > 0) {
             $feedType = 2 === $this->c->config->i_feed_type ? 'atom' : 'rss';
-            $this->pageHeader('feed', 'link', [
+            $this->pageHeader('feed', 'link', 0, [
                 'rel'  => 'alternate',
                 'type' => "application/{$feedType}+xml",
                 'href' => $this->c->Router->link('Feed', ['type' => $feedType, 'tid' => $topic->id]),

+ 1 - 1
app/templates/layouts/main.forkbb.php

@@ -8,7 +8,7 @@
     @if ('style' === $pageHeader['type'])
   <style>{!! $pageHeader['values'][0] !!}</style>
     @elseif ('script' !== $pageHeader['type'])
-  <{!! $pageHeader['type'] !!} @foreach ($pageHeader['values'] as $key => $val) {!! $key !!}="{{ $val }}" @endforeach>
+  <{{ $pageHeader['type'] }} @foreach ($pageHeader['values'] as $key => $val) {{ $key }}="{{ $val }}" @endforeach>
     @endif
 @endforeach
 </head>