]> _ Git - fluidbook_tools.git/commitdiff
wait #6274 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 13 Sep 2023 17:35:22 +0000 (19:35 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 13 Sep 2023 17:35:22 +0000 (19:35 +0200)
src/Links/CustomLink.php

index d6b5205263ff6565fa106869c1d066e9585bb23b..7e62d46c33db4fe97ef23d4154e7c679cc0931a8 100644 (file)
@@ -8,9 +8,12 @@ class CustomLink extends NormalLink
 {
     protected $role = 'link';
 
+    protected static $_all = [];
+
     public function getURL()
     {
-        return static::_getURL($this->to);
+        $res = static::_getURL($this->to);
+        return $res;
     }
 
     public function getTarget()
@@ -39,25 +42,41 @@ class CustomLink extends NormalLink
     {
         $ref = str_replace('custom:', '', $ref);
         $e = explode(':', $ref, 2);
-        if (count($e) == 2 && $e[0] == $type) {
+        if (count($e) == 2) {
             $ref = $e[1];
             $type = $e[0];
         }
         $nospaceref = str_replace(' ', '', $ref);
 
-        $res = DB::table('fluidbook_reference_url')->where('type', $type)->whereIn('ref', [$ref, $nospaceref])->get();
-        if ($res->count() > 0) {
-            $url = $res->get('url');
-            if (!$url || $url === '-') {
-                return $default;
-            }
-            return $url;
+        $all = self::getAllURLOf($type);
+        if (isset($all[$ref])) {
+            $res = $all[$ref];
+        } else if (isset($all[$nospaceref])) {
+            $res = $all[$nospaceref];
+        } else {
+            return $default;
         }
-        return $default;
+
+        if (!$res || $res === '-') {
+            return $default;
+        }
+        return $res;
     }
 
     public function ignore()
     {
         return parent::ignore() || !$this->getURL() || $this->getURL() === '-';
     }
+
+    protected static function getAllURLOf($type)
+    {
+        if (!isset(self::$_all[$type])) {
+            self::$_all[$type] = [];
+            $res = DB::table('fluidbook_reference_url')->where('type', $type)->get(['url', 'ref']);
+            foreach ($res as $item) {
+                self::$_all[$type][$item->ref] = $item->url;
+            }
+        }
+        return self::$_all[$type];
+    }
 }