maj grav
This commit is contained in:
@@ -1,30 +1,55 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav.Common.Page
|
||||
* @package Grav\Common\Page
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Common\Page\Medium;
|
||||
|
||||
use Grav\Common\Getters;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
|
||||
use Grav\Common\Media\Interfaces\MediaObjectInterface;
|
||||
use Grav\Common\Page\Page;
|
||||
use Grav\Common\Utils;
|
||||
use RocketTheme\Toolbox\ArrayTraits\ArrayAccess;
|
||||
use RocketTheme\Toolbox\ArrayTraits\Countable;
|
||||
use RocketTheme\Toolbox\ArrayTraits\Export;
|
||||
use RocketTheme\Toolbox\ArrayTraits\ExportInterface;
|
||||
use RocketTheme\Toolbox\ArrayTraits\Iterator;
|
||||
|
||||
abstract class AbstractMedia extends Getters implements MediaCollectionInterface
|
||||
abstract class AbstractMedia implements ExportInterface, MediaCollectionInterface
|
||||
{
|
||||
protected $gettersVariable = 'instances';
|
||||
use ArrayAccess;
|
||||
use Countable;
|
||||
use Iterator;
|
||||
use Export;
|
||||
|
||||
protected $instances = [];
|
||||
protected $items = [];
|
||||
protected $path;
|
||||
protected $images = [];
|
||||
protected $videos = [];
|
||||
protected $audios = [];
|
||||
protected $files = [];
|
||||
protected $media_order;
|
||||
|
||||
/**
|
||||
* Return media path.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPath()
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
public function setPath(?string $path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get medium by filename.
|
||||
*
|
||||
@@ -48,73 +73,78 @@ abstract class AbstractMedia extends Getters implements MediaCollectionInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* Set file modification timestamps (query params) for all the media files.
|
||||
*
|
||||
* @return mixed
|
||||
* @param string|int|null $timestamp
|
||||
* @return $this
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
public function setTimestamps($timestamp = null)
|
||||
{
|
||||
$object = parent::offsetGet($offset);
|
||||
/** @var Medium $instance */
|
||||
foreach ($this->items as $instance) {
|
||||
$instance->setTimestamp($timestamp);
|
||||
}
|
||||
|
||||
// It would be nice if previous image modification would not affect the later ones.
|
||||
//$object = $object ? clone($object) : null;
|
||||
|
||||
return $object;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all media.
|
||||
*
|
||||
* @return array|MediaObjectInterface[]
|
||||
* @return MediaObjectInterface[]
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
$this->instances = $this->orderMedia($this->instances);
|
||||
$this->items = $this->orderMedia($this->items);
|
||||
|
||||
return $this->instances;
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all image media.
|
||||
*
|
||||
* @return array|MediaObjectInterface[]
|
||||
* @return MediaObjectInterface[]
|
||||
*/
|
||||
public function images()
|
||||
{
|
||||
$this->images = $this->orderMedia($this->images);
|
||||
|
||||
return $this->images;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all video media.
|
||||
*
|
||||
* @return array|MediaObjectInterface[]
|
||||
* @return MediaObjectInterface[]
|
||||
*/
|
||||
public function videos()
|
||||
{
|
||||
$this->videos = $this->orderMedia($this->videos);
|
||||
|
||||
return $this->videos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all audio media.
|
||||
*
|
||||
* @return array|MediaObjectInterface[]
|
||||
* @return MediaObjectInterface[]
|
||||
*/
|
||||
public function audios()
|
||||
{
|
||||
$this->audios = $this->orderMedia($this->audios);
|
||||
|
||||
return $this->audios;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all file media.
|
||||
*
|
||||
* @return array|MediaObjectInterface[]
|
||||
* @return MediaObjectInterface[]
|
||||
*/
|
||||
public function files()
|
||||
{
|
||||
$this->files = $this->orderMedia($this->files);
|
||||
|
||||
return $this->files;
|
||||
}
|
||||
|
||||
@@ -122,9 +152,12 @@ abstract class AbstractMedia extends Getters implements MediaCollectionInterface
|
||||
* @param string $name
|
||||
* @param MediaObjectInterface $file
|
||||
*/
|
||||
protected function add($name, $file)
|
||||
public function add($name, $file)
|
||||
{
|
||||
$this->instances[$name] = $file;
|
||||
if (!$file) {
|
||||
return;
|
||||
}
|
||||
$this->offsetSet($name, $file);
|
||||
switch ($file->type) {
|
||||
case 'image':
|
||||
$this->images[$name] = $file;
|
||||
@@ -143,13 +176,14 @@ abstract class AbstractMedia extends Getters implements MediaCollectionInterface
|
||||
/**
|
||||
* Order the media based on the page's media_order
|
||||
*
|
||||
* @param $media
|
||||
* @param array $media
|
||||
* @return array
|
||||
*/
|
||||
protected function orderMedia($media)
|
||||
{
|
||||
if (null === $this->media_order) {
|
||||
$page = Grav::instance()['pages']->get($this->path);
|
||||
/** @var Page $page */
|
||||
$page = Grav::instance()['pages']->get($this->getPath());
|
||||
|
||||
if ($page && isset($page->header()->media_order)) {
|
||||
$this->media_order = array_map('trim', explode(',', $page->header()->media_order));
|
||||
|
||||
Reference in New Issue
Block a user