Skip to content

Commit

Permalink
Merge pull request #15 from pKallert/feature/tags
Browse files Browse the repository at this point in the history
FEATURE: Use Canto custom fields as tags and collections
  • Loading branch information
kdambekalns committed Oct 26, 2021
2 parents ec8efe3 + 1faf9ff commit 9e1b844
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 40 deletions.
159 changes: 136 additions & 23 deletions Classes/AssetSource/CantoAssetProxyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
*/

use Flownative\Canto\Exception\AuthenticationFailedException;
use Flownative\Canto\Exception\ConnectionException;
use Flownative\OAuth2\Client\OAuthClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Neos\Flow\Annotations\Inject;
use Neos\Flow\Annotations as Flow;
use Neos\Media\Domain\Model\AssetCollection;
use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryResultInterface;
use Neos\Media\Domain\Model\Tag;
use Psr\Log\LoggerInterface as SystemLoggerInterface;

/**
Expand All @@ -37,6 +40,21 @@ final class CantoAssetProxyQuery implements AssetProxyQueryInterface
*/
private $searchTerm = '';

/**
* @var Tag
*/
private $activeTag;

/**
* @var string
*/
private $tagQuery = "";

/**
* @var AssetCollection
*/
private $activeAssetCollection;

/**
* @var string
*/
Expand All @@ -58,12 +76,13 @@ final class CantoAssetProxyQuery implements AssetProxyQueryInterface
private $limit = 30;

/**
* @var string
* @Flow\InjectConfiguration(path="mapping", package="Flownative.Canto")
* @var array
*/
private $parentFolderIdentifier = '';
protected $mapping = [];

/**
* @Inject
* @Flow\Inject
* @var SystemLoggerInterface
*/
protected $logger;
Expand Down Expand Up @@ -124,6 +143,38 @@ public function getSearchTerm(): string
return $this->searchTerm;
}

/**
* @param Tag $tag
*/
public function setActiveTag(Tag $tag): void
{
$this->activeTag = $tag;
}

/**
* @return Tag
*/
public function getActiveTag(): Tag
{
return $this->activeTag;
}

/**
* @param AssetCollection|null $assetCollection
*/
public function setActiveAssetCollection(AssetCollection $assetCollection = null): void
{
$this->activeAssetCollection = $assetCollection;
}

/**
* @return AssetCollection|null
*/
public function getActiveAssetCollection(): ?AssetCollection
{
return $this->activeAssetCollection;
}

/**
* @param string $assetTypeFilter
*/
Expand Down Expand Up @@ -156,22 +207,6 @@ public function setOrderings(array $orderings): void
$this->orderings = $orderings;
}

/**
* @return string
*/
public function getParentFolderIdentifier(): string
{
return $this->parentFolderIdentifier;
}

/**
* @param string $parentFolderIdentifier
*/
public function setParentFolderIdentifier(string $parentFolderIdentifier): void
{
$this->parentFolderIdentifier = $parentFolderIdentifier;
}

/**
* @return AssetProxyQueryResultInterface
*/
Expand All @@ -182,6 +217,10 @@ public function execute(): AssetProxyQueryResultInterface

/**
* @return int
* @throws AuthenticationFailedException
* @throws IdentityProviderException
* @throws OAuthClientException
* @throws GuzzleException
*/
public function count(): int
{
Expand All @@ -192,6 +231,12 @@ public function count(): int

/**
* @return CantoAssetProxy[]
* @throws AuthenticationFailedException
* @throws IdentityProviderException
* @throws OAuthClientException
* @throws GuzzleException
* @throws \Neos\Cache\Exception
* @throws \Neos\Cache\Exception\InvalidDataException
*/
public function getArrayResult(): array
{
Expand All @@ -216,11 +261,16 @@ public function getArrayResult(): array
* @param array $orderings
* @return Response
* @throws AuthenticationFailedException
* @throws ConnectionException
* @throws IdentityProviderException
* @throws OAuthClientException
* @throws GuzzleException
*/
private function sendSearchRequest(int $limit, array $orderings): Response
{
if (!empty($this->activeTag)) {
$this->prepareTagQuery();
}

$searchTerm = $this->searchTerm;

switch ($this->assetTypeFilter) {
Expand All @@ -241,7 +291,70 @@ private function sendSearchRequest(int $limit, array $orderings): Response
$formatTypes = ['image', 'video', 'audio', 'document', 'presentation', 'other'];
break;
}
return $this->assetSource->getCantoClient()->search($searchTerm, $formatTypes, $this->tagQuery, $this->offset, $limit, $orderings);
}

return $this->assetSource->getCantoClient()->search($searchTerm, $formatTypes, $this->offset, $limit, $orderings);
/**
* @return void
* @throws AuthenticationFailedException
* @throws IdentityProviderException
* @throws OAuthClientException
* @throws GuzzleException
*/
public function prepareTagQuery(): void
{
$assetCollectionTitlesToSearch = [];
if (!empty($this->activeAssetCollection)) {
$assetCollectionTitlesToSearch[] = $this->activeAssetCollection->getTitle();
} else {
foreach ($this->activeTag->getAssetCollections() as $collection) {
$assetCollectionTitlesToSearch[] = $collection->getTitle();
}
}

$this->tagQuery = '';

if (!empty($this->mapping['customFields'])) {
$cantoCustomFields = $this->assetSource->getCantoClient()->getCustomFields();

foreach ($cantoCustomFields as $cantoCustomField) {
// field should not be mapped if it does not exist in the settings or if asAssetCollection is set to false
if (!array_key_exists($cantoCustomField->id, $this->mapping['customFields']) || !$this->mapping['customFields'][$cantoCustomField->id]['asAssetCollection']) {
continue;
}
if (in_array($cantoCustomField->name, $assetCollectionTitlesToSearch, true)) {
$this->tagQuery .= '&' . $cantoCustomField->id . '.keyword="' . $this->activeTag->getLabel() . '"';
}
}
}
}

/**
* @return void
* @throws AuthenticationFailedException
* @throws IdentityProviderException
* @throws OAuthClientException
* @throws GuzzleException
*/
public function prepareUntaggedQuery(): void
{
$this->tagQuery = '';
if (!empty($this->mapping['customFields'])) {
if ($this->activeAssetCollection !== null) {
$cantoCustomFields = $this->assetSource->getCantoClient()->getCustomFields();
foreach ($cantoCustomFields as $cantoCustomField) {
if ($this->mapping['customFields'][$cantoCustomField->id]['asAssetCollection'] && $cantoCustomField->name === $this->activeAssetCollection->getTitle()) {
$this->tagQuery .= '&' . $cantoCustomField->id . '.keyword="__null__"';
}
}
} else {
foreach ($this->mapping['customFields'] as $customFieldId => $customFieldToBeMapped) {
if (!$customFieldToBeMapped['asAssetCollection']) {
continue;
}
$this->tagQuery .= '&' . $customFieldId . '.keyword="__null__"';
}
}
}
}
}
37 changes: 35 additions & 2 deletions Classes/AssetSource/CantoAssetProxyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@
use Exception;
use Flownative\Canto\Exception\AssetNotFoundException;
use Flownative\Canto\Exception\AuthenticationFailedException;
use Neos\Media\Domain\Model\AssetCollection;
use Neos\Media\Domain\Model\AssetSource\AssetNotFoundExceptionInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxyQueryResultInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxyRepositoryInterface;
use Neos\Media\Domain\Model\AssetSource\AssetSourceConnectionExceptionInterface;
use Neos\Media\Domain\Model\AssetSource\AssetTypeFilter;
use Neos\Media\Domain\Model\AssetSource\SupportsCollectionsInterface;
use Neos\Media\Domain\Model\AssetSource\SupportsSortingInterface;
use Neos\Media\Domain\Model\AssetSource\SupportsTaggingInterface;
use Neos\Media\Domain\Model\Tag;

/**
* CantoAssetProxyRepository
*/
class CantoAssetProxyRepository implements AssetProxyRepositoryInterface, SupportsSortingInterface
class CantoAssetProxyRepository implements AssetProxyRepositoryInterface, SupportsSortingInterface, SupportsTaggingInterface, SupportsCollectionsInterface
{
/**
* @var CantoAssetSource
*/
private $assetSource;

/**
* @var AssetCollection
*/
private $activeAssetCollection;

/**
* @param CantoAssetSource $assetSource
*/
Expand Down Expand Up @@ -120,7 +128,8 @@ public function findBySearchTerm(string $searchTerm): AssetProxyQueryResultInter
public function findByTag(Tag $tag): AssetProxyQueryResultInterface
{
$query = new CantoAssetProxyQuery($this->assetSource);
$query->setSearchTerm($tag->getLabel());
$query->setActiveTag($tag);
$query->setActiveAssetCollection($this->activeAssetCollection);
$query->setAssetTypeFilter($this->assetTypeFilter);
$query->setOrderings($this->orderings);
return new CantoAssetProxyQueryResult($query);
Expand All @@ -132,6 +141,8 @@ public function findByTag(Tag $tag): AssetProxyQueryResultInterface
public function findUntagged(): AssetProxyQueryResultInterface
{
$query = new CantoAssetProxyQuery($this->assetSource);
$query->setActiveAssetCollection($this->activeAssetCollection);
$query->prepareUntaggedQuery();
$query->setAssetTypeFilter($this->assetTypeFilter);
$query->setOrderings($this->orderings);
return new CantoAssetProxyQueryResult($query);
Expand Down Expand Up @@ -161,4 +172,26 @@ public function orderBy(array $orderings): void
{
$this->orderings = $orderings;
}

/**
* @return int
*/
public function countUntagged(): int
{
$query = new CantoAssetProxyQuery($this->assetSource);
$query->setActiveAssetCollection($this->activeAssetCollection);
$query->prepareUntaggedQuery();
$query->setAssetTypeFilter($this->assetTypeFilter);
$query->setOrderings($this->orderings);
return $query->count();
}

/**
* @param AssetCollection|null $assetCollection
* @return void
*/
public function filterByCollection(AssetCollection $assetCollection = null): void
{
$this->activeAssetCollection = $assetCollection;
}
}
Loading

0 comments on commit 9e1b844

Please sign in to comment.