Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Implement actual Canto tags to be used as Neos tags #31

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Classes/AssetSource/CantoAssetProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public static function fromJsonObject(stdClass $jsonObject, CantoAssetSource $as
$assetProxy->lastModified = \DateTime::createFromFormat('YmdHisv', $jsonObject->default->{'Date modified'});
$assetProxy->fileSize = (int)$jsonObject->size;
$assetProxy->mediaType = MediaTypes::getMediaTypeFromFilename($jsonObject->name);
$assetProxy->tags = $jsonObject->tag ?? [];

$assetProxy->iptcProperties['CopyrightNotice'] = $jsonObject->copyright ?? ($jsonObject->default->Copyright ?? '');

Expand Down Expand Up @@ -202,7 +201,8 @@ public function getLocalAssetIdentifier(): ?string

public function getTags(): array
{
return $this->tags;
return [];
// return $this->tags;
}

public function isImported(): bool
Expand Down
4 changes: 4 additions & 0 deletions Classes/AssetSource/CantoAssetProxyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ public function prepareTagQuery(): void
}
}
}

if (!empty($this->mapping['tags'])) {
$this->tagQuery .= '&tags="' . $this->activeTag->getLabel() . '"';
}
}

/**
Expand Down
56 changes: 56 additions & 0 deletions Classes/Command/CantoCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,60 @@ public function importCustomFieldsAsCollectionsAndTagsCommand(string $assetSourc

!$quiet && $this->outputLine('<success>Import done.</success>');
}

/**
* Import Canto Tags as Tags
*
* @param string $assetSourceIdentifier Name of the canto asset source
* @param bool $quiet If set, only errors will be displayed.
* @throws GuzzleException
* @throws IllegalObjectTypeException
* @throws OAuthClientException
* @throws StopCommandException
* @throws AuthenticationFailedException
*/
public function importTagsCommand(string $assetSourceIdentifier = CantoAssetSource::ASSET_SOURCE_IDENTIFIER, bool $quiet = true): void
{
!$quiet && $this->outputLine('<b>Importing tags via Canto API</b>');

$tagMapping = $this->mapping['tags'];
if (empty($tagMapping)) {
$this->outputLine('<error>No tags mapping configured</error>');
$this->quit(1);
}

try {
/** @var CantoAssetSource $cantoAssetSource */
$cantoAssetSource = $this->assetSourceService->getAssetSources()[$assetSourceIdentifier];
$cantoClient = $cantoAssetSource->getCantoClient();
$cantoClient->allowClientCredentialsAuthentication(true);
} catch (\Exception) {
$this->outputLine('<error>Canto client could not be created</error>');
$this->quit(1);
}

$tags = $cantoClient->getFacetValues($tagMapping['field']);
$relevantTags = array_chunk($tags, $tagMapping['limit']);
foreach ($relevantTags[0] as $tagLabel) {

if (!empty($tagMapping['include']) && !in_array($tagLabel, $tagMapping['include'], true)) {
continue;
}
if (!empty($tagMapping['exclude']) && in_array($tagLabel, $tagMapping['exclude'], true)) {
continue;
}

$tag = $this->tagRepository->findOneByLabel($tagLabel);

if ($tag === null) {
$tag = new Tag($tagLabel);
$this->tagRepository->add($tag);
!$quiet && $this->outputLine(' + %s', [$tagLabel]);
}

!$quiet && $this->outputLine();
}

!$quiet && $this->outputLine('<success>Import done.</success>');
}
}
27 changes: 26 additions & 1 deletion Classes/Service/CantoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function getCurrentUri(): UriInterface
private function redirectToUri(string $uri): void
{
header('Location: ' . $uri);
throw new StopActionException('Canto login required', 1625222167);
throw new AuthenticationFailedException('Canto login required', 1625222167);
}

/**
Expand Down Expand Up @@ -240,6 +240,31 @@ public function getCustomFields(): array
return [];
}

/**
* @throws AuthenticationFailedException
* @throws GuzzleException
* @throws HttpException
* @throws IdentityProviderException
* @throws MissingActionNameException
* @throws MissingClientSecretException
* @throws OAuthClientException
* @todo perhaps cache the result
*/
public function getFacetValues(string $facetName): array
{
$response = $this->sendAuthenticatedRequest('search');
if ($response->getStatusCode() === 200) {
$result = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);

foreach ($result['facets'] as $facet) {
if ($facet['name'] === $facetName) {
return $facet['value'];
}
}
}
return [];
}

/**
* @throws AuthenticationFailedException
* @throws GuzzleException
Expand Down
7 changes: 7 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
Flownative:
Canto:
# At the moment, this is "either or", so use custom fields or tags for mapping, not both
mapping:
# map "Custom Fields" from Canto to Neos
customFields: []
# map "Tags" from Canto to Neos
tags:
field: 'tags'
limit: 20
include: []
exclude: ['Untagged']
webhook:
pathPrefix: '/flownative-canto/webhook/'
# A token that can be used to secure webhook invocations; used only if set
Expand Down
8 changes: 6 additions & 2 deletions Resources/Private/Templates/Canto/Index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="neos-content neos-container-fluid">
<h2>{neos:backend.translate(id: 'cantoConnectionSettings', source: 'Main', package: 'Flownative.Canto')}</h2>
<br/>
<f:form action="updateRefreshToken" method="post">
<f:form controller="Authorization" action="start" additionalAttributes="{'target': '_blank'}" method="post">
<div class="neos-row-fluid">
<div class="neos-span6">
<fieldset>
Expand All @@ -19,13 +19,17 @@ <h2>{neos:backend.translate(id: 'cantoConnectionSettings', source: 'Main', packa
<tr>
<td>{user.firstName} {user.lastName}</td>
<td>{user.email}</td>
</tr>
</table>
</div>
</div>
</f:if>
</fieldset>
<f:if condition="{authenticationError}">
<p><i class="fa fa-exclamation-triangle"></i> {authenticationError}</p>
<div class="neos-control-group">
<p><i class="fa fa-exclamation-triangle"></i> {authenticationError}</p>
</div>
<button type="submit" class="neos-button neos-button-primary">Authorize with Canto login</button>
</f:if>
</div>
</div>
Expand Down