-
Notifications
You must be signed in to change notification settings - Fork 2
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
[TASK] Introduce ImageGalleryRepository (#36) #37
Open
Lebeau09
wants to merge
9
commits into
master
Choose a base branch
from
feat/introduce-image-gallery-repo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
12905c2
task: introduce ImageGalleryRepository
Lebeau09 04321b1
[TASK] Delete settings from Flexform
Lebeau09 3791a5a
fix: configuration imageGalleryRepository
Lebeau09 07fb8c3
fix: vidi files refactoring
Lebeau09 d798055
fix: further adjustments to vidi files
Lebeau09 9ae5e3e
fixup! fix: further adjustments to vidi files
fabarea 8e9d957
fix: reintegration of settings into flexform
Lebeau09 c2b38a6
fix: replace matcherfactory with demandfactory
Lebeau09 5b844a8
fix: refactor galleryrepository
Lebeau09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
namespace Fab\NaturalGallery\Domain\Repository; | ||
|
||
|
||
use Fab\NaturalGallery\Persistence\Matcher; | ||
use Fab\NaturalGallery\Persistence\Order; | ||
use Fab\NaturalGallery\Utility\ConfigurationUtility; | ||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
use TYPO3\CMS\Core\Database\Query\QueryBuilder; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
|
||
class ImageGalleryRepository | ||
{ | ||
|
||
protected string $tableName = 'sys_file'; | ||
|
||
protected array $settings; | ||
|
||
public function getDefaultData(string $field):string | ||
{ | ||
return ConfigurationUtility::getInstance()->get($field); | ||
} | ||
|
||
public function findByUid(int $uid): array | ||
{ | ||
$query = $this->getQueryBuilder(); | ||
$query | ||
->select('*') | ||
->from($this->tableName) | ||
->where( | ||
$this->getQueryBuilder() | ||
->expr() | ||
->eq('uid', $this->getQueryBuilder()->expr()->literal($uid)), | ||
); | ||
|
||
$messages = $query->execute()->fetchOne(); | ||
|
||
return is_array($messages) ? $messages : []; | ||
} | ||
|
||
// public function findByCategory(int $category): array | ||
// { | ||
// /** @var QueryBuilder $query */ | ||
// $queryBuilder = $this->getQueryBuilder(); | ||
// $queryBuilder->select('*') | ||
// ->from($this->tableName) | ||
// ->innerJoin( | ||
// 'sys_file', | ||
// 'sys_file_metadata', | ||
// 'sys_file_metadata', | ||
// 'sys_file.uid = sys_file_metadata.file' | ||
// ) | ||
// ->innerJoin( | ||
// 'sys_file_metadata', | ||
// 'sys_category_record_mm', | ||
// 'sys_category_record_mm', | ||
// 'sys_category_record_mm.uid_foreign = sys_file_metadata.uid AND tablenames = "sys_file_metadata" AND fieldname = "categories"' | ||
// ) | ||
// ->where( | ||
// $queryBuilder->expr()->eq('sys_category_record_mm.uid_local', $category) | ||
// ) | ||
// ->addOrderBy('sys_file_metadata.year', 'DESC') | ||
// ->addOrderBy('sys_file_metadata.title', 'ASC'); | ||
|
||
// return $queryBuilder | ||
// ->execute() | ||
// ->fetchAllAssociative(); | ||
// } | ||
|
||
public function findByCategories(array $categories): array | ||
{ | ||
$queryBuilder = $this->getQueryBuilder(); | ||
$queryBuilder->select('*') | ||
->from($this->tableName) | ||
->innerJoin( | ||
'sys_file', | ||
'sys_file_metadata', | ||
'sys_file_metadata', | ||
'sys_file.uid = sys_file_metadata.file' | ||
) | ||
->innerJoin( | ||
'sys_file_metadata', | ||
'sys_category_record_mm', | ||
'sys_category_record_mm', | ||
'sys_category_record_mm.uid_foreign = sys_file_metadata.uid AND tablenames = "sys_file_metadata" AND fieldname = "categories"' | ||
); | ||
|
||
if (!empty($categories)) { | ||
$queryBuilder->where( | ||
$queryBuilder->expr()->in('sys_category_record_mm.uid_local', $categories) | ||
); | ||
} | ||
|
||
$queryBuilder->addOrderBy('sys_file_metadata.year', 'DESC') | ||
->addOrderBy('sys_file_metadata.title', 'ASC'); | ||
|
||
return $queryBuilder | ||
->execute() | ||
->fetchAllAssociative(); | ||
|
||
} | ||
|
||
public function findByDemand(array|Matcher $demand = [], array $orderings = [], int $offset = 0, int $limit = 0): array | ||
{ | ||
$queryBuilder = $this->getQueryBuilder(); | ||
$constraints = []; | ||
|
||
|
||
if ($demand['likes']) { | ||
foreach ($demand['likes'] as $field => $value) { | ||
$constraints[] = $queryBuilder->select('*')->from($this->tableName) | ||
->expr() | ||
->like( | ||
$field, | ||
$queryBuilder->createNamedParameter('%' . $queryBuilder->escapeLikeWildcards($value) . '%'), | ||
); | ||
} | ||
|
||
if ($constraints) { | ||
$queryBuilder->where($queryBuilder->expr()->orX(...$constraints)); | ||
} | ||
|
||
# We handle the sorting | ||
$queryBuilder->addOrderBy(key($orderings), current($orderings)); | ||
|
||
} | ||
|
||
|
||
if ($demand['identifiers']) { | ||
$queryBuilder->select('*') | ||
->from($this->tableName) | ||
->innerJoin( | ||
'sys_file', | ||
'sys_file_metadata', | ||
'sys_file_metadata', | ||
'sys_file.uid = sys_file_metadata.file' | ||
) | ||
->innerJoin( | ||
'sys_file_metadata', | ||
'sys_category_record_mm', | ||
'sys_category_record_mm', | ||
'sys_category_record_mm.uid_foreign = sys_file_metadata.uid AND tablenames = "sys_file_metadata" AND fieldname = "categories"' | ||
); | ||
|
||
if (!empty($demand['identifiers'])) { | ||
$queryBuilder->where( | ||
$queryBuilder->expr()->in('sys_category_record_mm.uid_local', $demand['identifiers']) | ||
); | ||
} | ||
|
||
$queryBuilder->addOrderBy('sys_file_metadata.year', 'DESC') | ||
->addOrderBy('sys_file_metadata.title', 'ASC'); | ||
} | ||
|
||
if ($limit > 0) { | ||
$queryBuilder->setMaxResults($limit); | ||
} | ||
|
||
return $queryBuilder->execute()->fetchAllAssociative(); | ||
} | ||
|
||
|
||
public function findByUids(array $uids): array | ||
{ | ||
$query = $this->getQueryBuilder(); | ||
$query | ||
->select('*') | ||
->from($this->tableName) | ||
->where($this->getQueryBuilder()->expr()->in('uid', $uids)); | ||
|
||
return $query->execute()->fetchAllAssociative(); | ||
} | ||
|
||
protected function getQueryBuilder(): QueryBuilder | ||
{ | ||
/** @var ConnectionPool $connectionPool */ | ||
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); | ||
return $connectionPool->getQueryBuilderForTable($this->tableName); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
namespace Fab\NaturalGallery\Persistence; | ||
|
||
/** | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
use TYPO3\CMS\Core\Database\Query\QueryBuilder; | ||
use TYPO3\CMS\Core\Resource\File; | ||
use TYPO3\CMS\Core\Resource\ResourceFactory; | ||
use TYPO3\CMS\Core\SingletonInterface; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
|
||
/** | ||
* Factory class related to Matcher object. | ||
*/ | ||
class DemandFactory implements SingletonInterface | ||
{ | ||
|
||
protected array $settings = []; | ||
|
||
protected string $tableName = 'sys_file'; | ||
|
||
protected function applyCriteriaFromFolders(\Fab\NaturalGallery\Persistence\Matcher $matcher): \Fab\NaturalGallery\Persistence\Matcher | ||
{ | ||
$folders = ''; | ||
if (!empty($this->settings['folders'])) { | ||
|
||
if (str_contains($this->settings['folders'], 't3://')) { | ||
$decodedUrl = urldecode($this->settings['folders']); | ||
|
||
// In case identifier=/ is missing. | ||
if (!str_contains($decodedUrl, 'identifier=')) { | ||
$decodedUrl .= '&identifier=/'; | ||
} | ||
preg_match("/storage=([\d]+)&identifier=(.+)/", $decodedUrl, $matches); | ||
if (count($matches) === 3) { | ||
$folders = $matches[1] . ':' . ltrim($matches[2], '/'); | ||
} | ||
} else { | ||
$folders = $this->settings['folders']; | ||
} | ||
$folderIdentifiers = GeneralUtility::trimExplode(',', $folders, true); | ||
$fileUids = []; | ||
foreach ($folderIdentifiers as $folderIdentifier) { | ||
$folderIdentifier = str_replace('file:', '', $folderIdentifier); | ||
$folder = GeneralUtility::makeInstance(ResourceFactory::class)->getFolderObjectFromCombinedIdentifier($folderIdentifier); | ||
$files = $folder->getFiles(); | ||
foreach ($files as $file) { | ||
$fileUids[] = $file->getUid(); | ||
} | ||
} | ||
|
||
$matcher->in('uid', $fileUids); | ||
} | ||
|
||
return $matcher; | ||
} | ||
protected function applyCriteriaFromAdditionalConstraints(Matcher $matcher): Matcher | ||
{ | ||
|
||
if (!empty($this->settings['additionalEquals'])) { | ||
$constraints = GeneralUtility::trimExplode(',', $this->settings['additionalEquals'], TRUE); | ||
foreach ($constraints as $constraint) { | ||
|
||
if (preg_match('/.+=.+/isU', $constraint, $matches)) { | ||
$constraintParts = GeneralUtility::trimExplode('=', $constraint, TRUE); | ||
if (count($constraintParts) === 2) { | ||
$matcher->equals(trim($constraintParts[0]), trim($constraintParts[1])); | ||
} | ||
} elseif (preg_match('/.+like.+/isU', $constraint, $matches)) { | ||
$constraintParts = GeneralUtility::trimExplode('like', $constraint, TRUE); | ||
if (count($constraintParts) === 2) { | ||
$matcher->like(trim($constraintParts[0]), trim($constraintParts[1])); | ||
} | ||
} | ||
|
||
} | ||
} | ||
return $matcher; | ||
} | ||
|
||
public function get(array $settings): Matcher | ||
{ | ||
$this->settings = $settings; | ||
$matcher = GeneralUtility::makeInstance(Matcher::class); | ||
// We only want files of type images, consider it as a prerequisite. | ||
$matcher->equals('type', File::FILETYPE_IMAGE); | ||
$matcher = $this->applyCriteriaFromFolders($matcher); | ||
return $this->applyCriteriaFromAdditionalConstraints($matcher); | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Lebeau09 on peut enlever cette partie