From: Vincent Vanwaelscappel Date: Thu, 11 Jul 2019 12:58:33 +0000 (+0200) Subject: #2878 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=ed288a598e51b31834298251598fc218687c6f46;p=cubist_cms-back.git #2878 --- diff --git a/src/app/Magic/PageData.php b/src/app/Magic/PageData.php index 6b62066..a590abf 100644 --- a/src/app/Magic/PageData.php +++ b/src/app/Magic/PageData.php @@ -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; + } }