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
{
$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
*/
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;
+ }
}