]> _ Git - cubist_cms-back.git/commitdiff
#2878
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 11 Jul 2019 12:58:33 +0000 (14:58 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 11 Jul 2019 12:58:33 +0000 (14:58 +0200)
src/app/Magic/PageData.php

index 6b62066dbdcdd5bc0169488c5fe5d9eedef3d03e..a590abfd90192c55305652c2819ca688aad23d38 100644 (file)
@@ -7,6 +7,7 @@ namespace Cubist\Backpack\app\Magic;
 use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel;
 use Cubist\Backpack\app\Magic\Models\CubistMagicModel;
 use Illuminate\Support\Arr;
+use Spatie\MediaLibrary\Models\Media;
 
 class PageData implements \ArrayAccess
 {
@@ -145,6 +146,60 @@ class PageData implements \ArrayAccess
         $this->unset($name);
     }
 
+    public function getMediaCollection($offset)
+    {
+        if (!$this->exists($offset)) {
+            return false;
+        }
+        if (null === $this->getEntity()) {
+            return false;
+        }
+        $collection = $this->get($offset);
+        if (null === $collection || !$collection) {
+            return false;
+        }
+
+        return $collection;
+    }
+
+    /**
+     * @param $offset
+     * @return array|\Illuminate\Support\Collection
+     */
+    public function getMedia($offset)
+    {
+        $collection = $this->getMediaCollection($offset);
+        if (!$collection) {
+            return [];
+        }
+        $media = $this->getEntity()->getMedia($collection);
+        return $media;
+    }
+
+    /**
+     * @param $offset
+     * @return bool
+     */
+    public function hasMedia($offset)
+    {
+        return count($this->getMedia($offset)) > 0;
+    }
+
+    public function getMediaURL($offset, $default = null)
+    {
+        return $this->getImageURL($offset, '', $default);
+    }
+
+    public function getMediaURLList($offset, $default = [])
+    {
+        return $this->getImageURLList($offset, '', $default);
+    }
+
+    public function getMediaURLAt($offset, $at = 0, $default = null)
+    {
+        return $this->getImageURLAt($offset, $at, '', $default);
+    }
+
     /**
      * @param string $offset
      * @param string $conversionName
@@ -153,22 +208,58 @@ class PageData implements \ArrayAccess
      */
     public function getImageURL($offset, $conversionName = '', $default = null)
     {
-        if (!$this->exists($offset)) {
+        $media = $this->getMedia($offset);
+        if (!$media) {
             return $default;
         }
-        if (null === $this->getEntity()) {
+
+        foreach ($media as $m) {
+            /** @var Media $m */
+            $res = $m->getUrl($conversionName);
+            if (!$res) {
+                continue;
+            }
+            return $res;
+        }
+
+        return $default;
+    }
+
+    public function getImageURLAt($offset, $at = 0, $conversionName = '', $default = null)
+    {
+        $media = $this->getMedia($offset);
+        if (!$media) {
             return $default;
         }
-        $collection = $this->get($offset);
-        if (null === $collection || !$collection) {
+        if ($at + 1 > count($media)) {
             return $default;
         }
 
-        $res = $this->getEntity()->getFirstMediaUrl($collection, $conversionName);
+        $res = $media[$at]->getUrl($conversionName);
         if (!$res) {
             return $default;
         }
-
         return $res;
     }
+
+    public function getImageURLList($offset, $conversionName = '', $default = [])
+    {
+        $media = $this->getMedia($offset);
+        if (!$media) {
+            return $default;
+        }
+
+        $res = [];
+
+        foreach ($media as $m) {
+            /** @var Media $m */
+            $url = $m->getUrl($conversionName);
+            if (!$url) {
+                continue;
+            }
+            return $res;
+        }
+
+        return $default;
+    }
 }