Initial commit - Typo3 11.5.41
This commit is contained in:
@@ -0,0 +1,519 @@
|
||||
<?php
|
||||
namespace MiniFranske\FsMediaGallery\Domain\Model;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2014 Frans Saris <franssaris@gmail.com>
|
||||
*
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
|
||||
use TYPO3\CMS\Core\Resource\FileCollectionRepository;
|
||||
use MiniFranske\FsMediaGallery\Domain\Repository\MediaAlbumRepository;
|
||||
use TYPO3\CMS\Core\Resource\File;
|
||||
use TYPO3\CMS\Core\Resource\FileInterface;
|
||||
use TYPO3\CMS\Core\Resource\FileReference;
|
||||
|
||||
/**
|
||||
* Media album
|
||||
*/
|
||||
class MediaAlbum extends AbstractEntity
|
||||
{
|
||||
|
||||
/**
|
||||
* fileCollectionRepository
|
||||
*
|
||||
* @var \TYPO3\CMS\Core\Resource\FileCollectionRepository
|
||||
*/
|
||||
protected $fileCollectionRepository;
|
||||
|
||||
/**
|
||||
* mediaAlbumRepository
|
||||
*
|
||||
* @var \MiniFranske\FsMediaGallery\Domain\Repository\MediaAlbumRepository
|
||||
*/
|
||||
protected $mediaAlbumRepository;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $assetCache;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $allowedMimeTypes = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $assetsOrderBy = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $assetsOrderDirection = 'asc';
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $excludeEmptyAlbums = false;
|
||||
|
||||
/**
|
||||
* Assets
|
||||
* An array of File or FileReference
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $assets;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $assetsCount;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $hidden;
|
||||
|
||||
/**
|
||||
* Title
|
||||
*
|
||||
* @var \string
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* Description visible online
|
||||
*
|
||||
* @var \string
|
||||
*/
|
||||
protected $webdescription;
|
||||
|
||||
/**
|
||||
* @var \MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum|NULL
|
||||
* @TYPO3\CMS\Extbase\Annotation\ORM\Lazy
|
||||
*/
|
||||
protected $parentalbum;
|
||||
|
||||
/**
|
||||
* Main asset
|
||||
*
|
||||
* @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
|
||||
*/
|
||||
protected $mainAsset;
|
||||
|
||||
/**
|
||||
* Child albums
|
||||
*
|
||||
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum>
|
||||
* @TYPO3\CMS\Extbase\Annotation\ORM\Lazy
|
||||
*/
|
||||
public $albumCache;
|
||||
|
||||
/**
|
||||
* @var \DateTime
|
||||
*/
|
||||
protected $datetime;
|
||||
|
||||
public function injectFileCollectionRepository(FileCollectionRepository $fileCollectionRepository): void
|
||||
{
|
||||
$this->fileCollectionRepository = $fileCollectionRepository;
|
||||
}
|
||||
|
||||
public function injectMediaAlbumRepository(MediaAlbumRepository $mediaAlbumRepository): void
|
||||
{
|
||||
$this->mediaAlbumRepository = $mediaAlbumRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set allowedMimeTypes
|
||||
*
|
||||
* @param array $allowedMimeTypes
|
||||
*/
|
||||
public function setAllowedMimeTypes($allowedMimeTypes)
|
||||
{
|
||||
$this->allowedMimeTypes = $allowedMimeTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get allowedMimeTypes
|
||||
*
|
||||
* @return array $allowedMimeTypes
|
||||
*/
|
||||
public function getAllowedMimeTypes()
|
||||
{
|
||||
return $this->allowedMimeTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get assetsOrderBy
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAssetsOrderBy()
|
||||
{
|
||||
return $this->assetsOrderBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set assetsOrderBy
|
||||
*
|
||||
* @param string $assetsOrderBy
|
||||
*/
|
||||
public function setAssetsOrderBy($assetsOrderBy)
|
||||
{
|
||||
$this->assetsOrderBy = $assetsOrderBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get assetsOrderDirection
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAssetsOrderDirection()
|
||||
{
|
||||
return $this->assetsOrderDirection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set assetsOrderDirection
|
||||
*
|
||||
* @param string $assetsOrderDirection
|
||||
*/
|
||||
public function setAssetsOrderDirection($assetsOrderDirection)
|
||||
{
|
||||
$this->assetsOrderDirection = strtolower($assetsOrderDirection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get excludeEmptyAlbums
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getExcludeEmptyAlbums()
|
||||
{
|
||||
return $this->excludeEmptyAlbums;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set excludeEmptyAlbums
|
||||
*
|
||||
* @param bool $excludeEmptyAlbums
|
||||
*/
|
||||
public function setExcludeEmptyAlbums($excludeEmptyAlbums)
|
||||
{
|
||||
$this->excludeEmptyAlbums = (bool)$excludeEmptyAlbums;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set hidden
|
||||
*
|
||||
* @param boolean $hidden
|
||||
*/
|
||||
public function setHidden($hidden)
|
||||
{
|
||||
$this->hidden = $hidden;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get hidden
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getHidden()
|
||||
{
|
||||
return $this->hidden;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the title
|
||||
*
|
||||
* @return \string $title
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the title
|
||||
*
|
||||
* @param \string $title
|
||||
* @return void
|
||||
*/
|
||||
public function setTitle($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the webdescription
|
||||
*
|
||||
* @return \string $webdescription
|
||||
*/
|
||||
public function getWebdescription()
|
||||
{
|
||||
return $this->webdescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the webdescription
|
||||
*
|
||||
* @param \string $webdescription
|
||||
* @return void
|
||||
*/
|
||||
public function setWebdescription($webdescription)
|
||||
{
|
||||
$this->webdescription = $webdescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set parentalbum
|
||||
*
|
||||
* @param \MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum $parentalbum
|
||||
*/
|
||||
public function setParentalbum(\MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum $parentalbum)
|
||||
{
|
||||
$this->parentalbum = $parentalbum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parentalbum
|
||||
*
|
||||
* @return \MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum
|
||||
*/
|
||||
public function getParentalbum()
|
||||
{
|
||||
return $this->parentalbum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return File[]|FileReference[]
|
||||
*/
|
||||
public function getAssets()
|
||||
{
|
||||
if ($this->assetCache === null) {
|
||||
try {
|
||||
/** @var $fileCollection \TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection */
|
||||
$fileCollection = $this->fileCollectionRepository->findByUid($this->getUid());
|
||||
$fileCollection->loadContents();
|
||||
$files = $fileCollection->getItems();
|
||||
// check if file has right mimeType
|
||||
if (count($this->allowedMimeTypes) > 0) {
|
||||
foreach ($files as $key => $fileObject) {
|
||||
/** @var $fileObject File|FileReference */
|
||||
if (!in_array($fileObject->getMimeType(), $this->allowedMimeTypes)) {
|
||||
unset($files[$key]);
|
||||
}
|
||||
}
|
||||
// reset keys
|
||||
$files = array_values($files);
|
||||
}
|
||||
if (trim($this->assetsOrderBy) !== '') {
|
||||
$files = $this->orderAssets($files, $this->assetsOrderBy, $this->assetsOrderDirection);
|
||||
}
|
||||
$this->assetCache = $files;
|
||||
} catch (\Exception $exception) {
|
||||
// failing albums get disabled
|
||||
$this->setHidden(true);
|
||||
$this->mediaAlbumRepository->update($this);
|
||||
$this->assetCache = [];
|
||||
}
|
||||
}
|
||||
return $this->assetCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get asset by uid
|
||||
*
|
||||
* @param int $assetUid
|
||||
* @return File|FileReference|NULL
|
||||
*/
|
||||
public function getAssetByUid($assetUid)
|
||||
{
|
||||
foreach ($assets = $this->getAssets() as $asset) {
|
||||
/** @var $asset File|FileReference */
|
||||
if ((int)$assetUid === (int)$asset->getUid()) {
|
||||
return $asset;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pevious, current and next asset by assetUid
|
||||
*
|
||||
* @param $assetUid
|
||||
* @return FileInterface[]
|
||||
*/
|
||||
public function getPreviousCurrentAndNext($assetUid)
|
||||
{
|
||||
$previous = $last = $current = $next = null;
|
||||
|
||||
foreach ($assets = $this->getAssets() as $asset) {
|
||||
if ($current !== null) {
|
||||
$next = $asset;
|
||||
break;
|
||||
}
|
||||
$previous = $last;
|
||||
$last = $asset;
|
||||
/** @var $asset File|FileReference */
|
||||
if ((int)$assetUid === (int)$asset->getUid()) {
|
||||
$current = $asset;
|
||||
}
|
||||
}
|
||||
|
||||
return [$previous, $current, $next];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @deprecated Will be removed in next major version 2.*
|
||||
*/
|
||||
public function getAssetsUids()
|
||||
{
|
||||
trigger_error('MediaAlbum::getAssetsUid is deprecated and will be removed with next major version 2.*. Use getAssets() as this method can not handle static file collections', E_USER_DEPRECATED);
|
||||
$assetsUids = [];
|
||||
foreach ($assets = $this->getAssets() as $asset) {
|
||||
/** @var $asset FileInterface */
|
||||
$assetsUids[] = $asset->getUid();
|
||||
}
|
||||
return $assetsUids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get assetsCount
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getAssetsCount()
|
||||
{
|
||||
if ($this->assetCache === null) {
|
||||
return count($this->getAssets());
|
||||
}
|
||||
return count($this->assetCache);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get child albums
|
||||
*
|
||||
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum>>
|
||||
*/
|
||||
public function getAlbums()
|
||||
{
|
||||
if ($this->albumCache === null) {
|
||||
$this->albumCache = $this->mediaAlbumRepository->findByParentalbum($this, $this->excludeEmptyAlbums);
|
||||
}
|
||||
return $this->albumCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get random child album
|
||||
*
|
||||
* @return MediaAlbum
|
||||
*/
|
||||
public function getRandomAlbum()
|
||||
{
|
||||
$albums = $this->getAlbums();
|
||||
return $albums[rand(0, count($albums) - 1)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return File|FileReference
|
||||
*/
|
||||
public function getMainAsset()
|
||||
{
|
||||
$mainAsset = null;
|
||||
if ($this->mainAsset) {
|
||||
$mainAsset = $this->mainAsset->getOriginalResource();
|
||||
} else {
|
||||
$assets = $this->getAssets();
|
||||
$mainAsset = $assets !== [] ? $assets[0] : null;
|
||||
}
|
||||
return $mainAsset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return File|FileReference
|
||||
*/
|
||||
public function getRandomAsset()
|
||||
{
|
||||
$assets = $this->getAssets();
|
||||
|
||||
// if there is an asset, return it
|
||||
if (count($assets)) {
|
||||
return $assets[rand(1, count($assets)) - 1];
|
||||
} else {
|
||||
// try to fetch it from child album
|
||||
$randomAlbum = $this->getRandomAlbum();
|
||||
if ($randomAlbum) {
|
||||
return $randomAlbum->getRandomAsset();
|
||||
}
|
||||
// album and child album are empty
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get datetime
|
||||
*
|
||||
* @return \DateTime
|
||||
*/
|
||||
public function getDatetime()
|
||||
{
|
||||
return $this->datetime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set date time
|
||||
*
|
||||
* @param \DateTime $datetime datetime
|
||||
* @return void
|
||||
*/
|
||||
public function setDatetime($datetime)
|
||||
{
|
||||
$this->datetime = $datetime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FileInterface[] $files
|
||||
* @param string $orderBy
|
||||
* @param string $direction
|
||||
* @return FileInterface[]
|
||||
*/
|
||||
protected function orderAssets($files, $orderBy, $direction)
|
||||
{
|
||||
usort($files, function ($a, $b) use ($orderBy, $direction) {
|
||||
if ($orderBy === 'crdate') {
|
||||
$compare = $a->getCreationTime() > $b->getCreationTime();
|
||||
} elseif (in_array($orderBy, ['content_creation_date', 'content_modification_date'], true)) {
|
||||
$compare = $a->getProperty($orderBy) > $b->getProperty($orderBy);
|
||||
} else {
|
||||
$compare = strnatcasecmp($a->getProperty($orderBy), $b->getProperty($orderBy));
|
||||
}
|
||||
return $direction === 'desc' ? -1 * $compare : $compare;
|
||||
});
|
||||
|
||||
return $files;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
<?php
|
||||
namespace MiniFranske\FsMediaGallery\Domain\Repository;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2014 Frans Saris <franssaris@gmail.com>
|
||||
*
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use TYPO3\CMS\Extbase\Persistence\Repository;
|
||||
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
|
||||
use MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum;
|
||||
|
||||
/**
|
||||
* MediaAlbumRepository
|
||||
*/
|
||||
class MediaAlbumRepository extends Repository
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array default ordering
|
||||
*/
|
||||
protected $defaultOrderings = [
|
||||
'sorting' => QueryInterface::ORDER_ASCENDING,
|
||||
'crdate' => QueryInterface::ORDER_DESCENDING,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $allowedAssetMimeTypes = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $albumUids = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $useAlbumUidsAsExclude = false;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $assetsOrderBy = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $assetsOrderDirection = 'asc';
|
||||
|
||||
/**
|
||||
* Set allowedAssetMimeTypes
|
||||
*
|
||||
* @param array $allowedAssetMimeTypes
|
||||
*/
|
||||
public function setAllowedAssetMimeTypes($allowedAssetMimeTypes)
|
||||
{
|
||||
$this->allowedAssetMimeTypes = $allowedAssetMimeTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get allowedAssetMimeTypes
|
||||
*
|
||||
* @return array $allowedAssetMimeTypes
|
||||
*/
|
||||
public function getAllowedAssetMimeTypes()
|
||||
{
|
||||
return $this->allowedAssetMimeTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get allowedAlbumUids
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAlbumUids()
|
||||
{
|
||||
return $this->albumUids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set allowedAlbumUids
|
||||
*
|
||||
* @param array $albumUids
|
||||
*/
|
||||
public function setAlbumUids($albumUids)
|
||||
{
|
||||
$this->albumUids = $albumUids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get useAlbumUidsAsExclude
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getUseAlbumUidsAsExclude()
|
||||
{
|
||||
return $this->useAlbumUidsAsExclude;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set useAlbumUidsAsExclude
|
||||
*
|
||||
* @param boolean $useAlbumUidsAsExclude
|
||||
*/
|
||||
public function setUseAlbumUidsAsExclude($useAlbumUidsAsExclude)
|
||||
{
|
||||
$this->useAlbumUidsAsExclude = $useAlbumUidsAsExclude;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get assetsOrderBy
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAssetsOrderBy()
|
||||
{
|
||||
return $this->assetsOrderBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set assetsOrderBy
|
||||
*
|
||||
* @param string $assetsOrderBy
|
||||
*/
|
||||
public function setAssetsOrderBy($assetsOrderBy)
|
||||
{
|
||||
$this->assetsOrderBy = $assetsOrderBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get assetsOrderDirection
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAssetsOrderDirection()
|
||||
{
|
||||
return $this->assetsOrderDirection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set assetsOrderDirection
|
||||
*
|
||||
* @param string $assetsOrderDirection
|
||||
*/
|
||||
public function setAssetsOrderDirection($assetsOrderDirection)
|
||||
{
|
||||
$this->assetsOrderDirection = $assetsOrderDirection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get random sub album
|
||||
*
|
||||
* @param MediaAlbum|bool $parent parent MediaAlbum, FALSE for parent = 0 or NULL for no restriction by parent
|
||||
* @return \MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum|NULL
|
||||
*/
|
||||
public function findRandom($parent = null)
|
||||
{
|
||||
|
||||
/** @var \TYPO3\CMS\Extbase\Persistence\Generic\Query $query */
|
||||
$query = $this->createQuery();
|
||||
$constraints = [];
|
||||
|
||||
// restrict by parent album
|
||||
if ($parent !== null) {
|
||||
$constraints[] = $query->equals('parentalbum', $parent ? $parent->getUid() : 0);
|
||||
}
|
||||
|
||||
// restrict by given uids
|
||||
if ($this->albumUids !== []) {
|
||||
if ($this->useAlbumUidsAsExclude) {
|
||||
$constraints[] = $query->logicalNot($query->in('uid', $this->albumUids));
|
||||
} else {
|
||||
$constraints[] = $query->in('uid', $this->albumUids);
|
||||
}
|
||||
}
|
||||
$query->matching($query->logicalAnd($constraints));
|
||||
$mediaAlbums = $query->execute()->toArray();
|
||||
|
||||
/** @var \MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum $mediaAlbum */
|
||||
$mediaAlbum = null;
|
||||
|
||||
if ($mediaAlbums) {
|
||||
$mediaAlbum = $mediaAlbums[array_rand($mediaAlbums, 1)];
|
||||
|
||||
// set allowed asset mime types
|
||||
$mediaAlbum->setAllowedMimeTypes($this->allowedAssetMimeTypes);
|
||||
// set assets order
|
||||
$mediaAlbum->setAssetsOrderBy($this->assetsOrderBy);
|
||||
$mediaAlbum->setAssetsOrderDirection($this->assetsOrderDirection);
|
||||
}
|
||||
|
||||
return $mediaAlbum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find albums by parent album
|
||||
*
|
||||
* @param MediaAlbum $parentAlbum
|
||||
* @param boolean $excludeEmptyAlbums
|
||||
* @param string $orderBy Sort albums by: datetime|crdate|sorting
|
||||
* @param string $orderDirection Sort order: asc|desc
|
||||
* @return MediaAlbum[]
|
||||
*/
|
||||
public function findByParentAlbum(
|
||||
MediaAlbum $parentAlbum = null,
|
||||
$excludeEmptyAlbums = true,
|
||||
$orderBy = 'sorting',
|
||||
$orderDirection = 'desc'
|
||||
) {
|
||||
$excludeEmptyAlbums = filter_var($excludeEmptyAlbums, FILTER_VALIDATE_BOOLEAN);
|
||||
$query = $this->createQuery();
|
||||
$constraints = [];
|
||||
$constraints[] = $query->equals('parentalbum', $parentAlbum ?: 0);
|
||||
|
||||
if ($this->albumUids !== []) {
|
||||
if ($this->useAlbumUidsAsExclude) {
|
||||
$constraints[] = $query->logicalNot($query->in('uid', $this->albumUids));
|
||||
} else {
|
||||
$constraints[] = $query->in('uid', $this->albumUids);
|
||||
}
|
||||
}
|
||||
|
||||
$query->matching($query->logicalAnd($constraints));
|
||||
$query->setOrderings($this->getOrderingsSettings($orderBy, $orderDirection));
|
||||
$mediaAlbums = $query->execute()->toArray();
|
||||
|
||||
foreach ($mediaAlbums as $key => $mediaAlbum) {
|
||||
/** @var $mediaAlbum \MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum */
|
||||
// set allowed asset mime types
|
||||
$mediaAlbum->setAllowedMimeTypes($this->allowedAssetMimeTypes);
|
||||
// set assets order
|
||||
$mediaAlbum->setAssetsOrderBy($this->assetsOrderBy);
|
||||
$mediaAlbum->setAssetsOrderDirection($this->assetsOrderDirection);
|
||||
$mediaAlbum->setExcludeEmptyAlbums($excludeEmptyAlbums);
|
||||
|
||||
// exclude if album is empty
|
||||
if (
|
||||
$excludeEmptyAlbums
|
||||
&&
|
||||
$mediaAlbum->getAssetsCount() === 0
|
||||
&&
|
||||
count($mediaAlbum->getAlbums()) === 0
|
||||
) {
|
||||
unset($mediaAlbums[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset array keys and return albums
|
||||
return array_values($mediaAlbums);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find album by Uid
|
||||
*
|
||||
* @param integer $uid The identifier of the MediaAlbum to find
|
||||
* @param bool $respectStorage possibility to disable storage restriction
|
||||
* @return MediaAlbum|NULL The matching media album if found, otherwise NULL
|
||||
*/
|
||||
public function findByUid($uid, $respectStorage = true)
|
||||
{
|
||||
|
||||
$query = $this->createQuery();
|
||||
$query->getQuerySettings()->setRespectSysLanguage(false);
|
||||
|
||||
if (!$respectStorage) {
|
||||
$query->getQuerySettings()->setRespectStoragePage(false);
|
||||
}
|
||||
|
||||
$constraints = [$query->equals('uid', (int)$uid)];
|
||||
|
||||
if ($this->albumUids !== []) {
|
||||
if ($this->useAlbumUidsAsExclude) {
|
||||
$constraints[] = $query->logicalNot($query->in('uid', $this->albumUids));
|
||||
} else {
|
||||
$constraints[] = $query->in('uid', $this->albumUids);
|
||||
}
|
||||
}
|
||||
|
||||
/** @var $mediaAlbum \MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum */
|
||||
$mediaAlbum = $query->matching($query->logicalAnd($constraints))->execute()->getFirst();
|
||||
|
||||
if ($mediaAlbum) {
|
||||
// set allowed asset mime types
|
||||
$mediaAlbum->setAllowedMimeTypes($this->allowedAssetMimeTypes);
|
||||
// set assets order
|
||||
$mediaAlbum->setAssetsOrderBy($this->assetsOrderBy);
|
||||
$mediaAlbum->setAssetsOrderDirection($this->assetsOrderDirection);
|
||||
}
|
||||
|
||||
return $mediaAlbum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all albums
|
||||
*
|
||||
* @param boolean $excludeEmptyAlbums
|
||||
* @param string $orderBy Sort albums by: datetime|crdate|sorting
|
||||
* @param string $orderDirection Sort order: asc|desc
|
||||
* @return MediaAlbum[]
|
||||
*/
|
||||
public function findAll($excludeEmptyAlbums = true, $orderBy = 'datetime', $orderDirection = 'desc')
|
||||
{
|
||||
$excludeEmptyAlbums = filter_var($excludeEmptyAlbums, FILTER_VALIDATE_BOOLEAN);
|
||||
$query = $this->createQuery();
|
||||
$query->setOrderings($this->getOrderingsSettings($orderBy, $orderDirection));
|
||||
$query->getQuerySettings()->setRespectSysLanguage(false);
|
||||
|
||||
if ($this->albumUids !== []) {
|
||||
if ($this->useAlbumUidsAsExclude) {
|
||||
$query->matching($query->logicalNot($query->in('uid', $this->albumUids)));
|
||||
} else {
|
||||
$query->matching($query->in('uid', $this->albumUids));
|
||||
}
|
||||
}
|
||||
|
||||
$mediaAlbums = $query->execute()->toArray();
|
||||
|
||||
foreach ($mediaAlbums as $key => $mediaAlbum) {
|
||||
/** @var $mediaAlbum \MiniFranske\FsMediaGallery\Domain\Model\MediaAlbum */
|
||||
// set allowed asset mime types
|
||||
$mediaAlbum->setAllowedMimeTypes($this->allowedAssetMimeTypes);
|
||||
// set assets order
|
||||
$mediaAlbum->setAssetsOrderBy($this->assetsOrderBy);
|
||||
$mediaAlbum->setAssetsOrderDirection($this->assetsOrderDirection);
|
||||
$mediaAlbum->setExcludeEmptyAlbums($excludeEmptyAlbums);
|
||||
|
||||
// exclude if album is empty
|
||||
if (
|
||||
$excludeEmptyAlbums
|
||||
&&
|
||||
$mediaAlbum->getAssetsCount() === 0
|
||||
&&
|
||||
count($mediaAlbum->getAlbums()) === 0
|
||||
) {
|
||||
unset($mediaAlbums[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset array keys and return albums
|
||||
return array_values($mediaAlbums);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get orderings settings. Returns an array like:
|
||||
* array(
|
||||
* 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
|
||||
* 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
|
||||
* )
|
||||
*
|
||||
* @param string $orderBy Sort albums by: datetime|crdate|sorting
|
||||
* @param string $orderDirection Sort order: asc|desc
|
||||
* @return array Orderings settings used by \TYPO3\CMS\Extbase\Persistence\QueryInterface->setOrderings()
|
||||
*/
|
||||
protected function getOrderingsSettings($orderBy = 'sorting', $orderDirection = 'asc')
|
||||
{
|
||||
|
||||
// check orderDirection
|
||||
if ($orderDirection === 'asc') {
|
||||
$orderDirection = QueryInterface::ORDER_ASCENDING;
|
||||
} else {
|
||||
$orderDirection = QueryInterface::ORDER_DESCENDING;
|
||||
}
|
||||
|
||||
// set $orderingsSettings by orderBy and orderDirection
|
||||
switch ($orderBy) {
|
||||
case 'datetime':
|
||||
$orderingsSettings = [
|
||||
'datetime' => $orderDirection,
|
||||
'crdate' => $orderDirection
|
||||
];
|
||||
break;
|
||||
case 'crdate':
|
||||
$orderingsSettings = ['crdate' => $orderDirection];
|
||||
break;
|
||||
default:
|
||||
// sorting
|
||||
$orderingsSettings = [
|
||||
'sorting' => $orderDirection,
|
||||
'crdate' => $orderDirection
|
||||
];
|
||||
}
|
||||
|
||||
return $orderingsSettings;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user