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

[TASK] Introduce ImageGalleryRepository (#36) #37

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
83 changes: 67 additions & 16 deletions Classes/Controller/GalleryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,49 @@
* The TYPO3 project - inspiring people to share!
*/

use Fab\NaturalGallery\Domain\Repository\CategoryRepository;
use Fab\NaturalGallery\Domain\Repository\ImageGalleryRepository;
use Fab\NaturalGallery\Persistence\MatcherFactory;
use Fab\NaturalGallery\Persistence\OrderFactory;
use Fab\Vidi\Domain\Repository\ContentRepositoryFactory;
use Fab\NaturalGallery\Utility\ConfigurationUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;

/**
* Controller
*/
class GalleryController extends ActionController
{
protected CategoryRepository $categoryRepository;
protected ImageGalleryRepository $galleryRepository;

protected MatcherFactory $matcherFactory;
protected OrderFactory $orderFactory;

protected array $configuration = array();

protected $settings = array();
protected $settings = array(
'folders'=> '',
'additionalEquals'=> '',
'sorting'=> '',
'direction'=> '',
'categories'=> '',

);
protected array $allowedColumns = [
'crdate',
'tstamp',
'title',
'uid',
];


public function initializeAction(): void
{
$this->galleryRepository = GeneralUtility::makeInstance(ImageGalleryRepository::class);
$this->orderFactory = GeneralUtility::makeInstance(OrderFactory::class);
$this->matcherFactory = GeneralUtility::makeInstance(MatcherFactory::class);
}


/**
* @return void|string
Expand All @@ -41,31 +67,56 @@ public function listAction()
return '<strong style="color: red">Please save your plugin settings in the BE beforehand.</strong>';
}

$this->settings['folders'] = ConfigurationUtility::getInstance()->get('folders');
$this->settings['additionalEquals'] = ConfigurationUtility::getInstance()->get('additionalEquals');
$this->settings['sorting'] = ConfigurationUtility::getInstance()->get('sorting');
$this->settings['direction'] = ConfigurationUtility::getInstance()->get('direction');
$this->settings['categories'] = ConfigurationUtility::getInstance()->get('categories');

// Initialize some objects related to the query.
$matcher = MatcherFactory::getInstance()->getMatcher($this->settings);
$order = OrderFactory::getInstance()->getOrder($this->settings);
$matcher = $this->matcherFactory->getMatcher($this->settings);
// $order = $this->orderFactory->getOrder($this->settings);

// Fetch the adequate repository for a known data type.
$contentRepository = ContentRepositoryFactory::getInstance('sys_file');
// Fetch and count files
$images = $this->galleryRepository->findByDemand($matcher, $this->getOrderings());

// Fetch and count files
$images = $contentRepository->findBy($matcher, $order);


// Assign template variables
$this->view->assign('settings', $this->settings);
$this->view->assign('data', $this->configurationManager->getcontentObject()->data);
$this->view->assign('images', $images);

$identifiers = GeneralUtility::trimExplode(',', $this->settings['categories'], TRUE);
$this->view->assign('categories', $this->categoryRepository->findByIdentifiers($identifiers));
$this->view->assign('categories', $this->galleryRepository->findByCategories($identifiers));
}

/**
* @param CategoryRepository $categoryRepository
*/
public function injectCategoryRepository(CategoryRepository $categoryRepository): void
protected function getOrderings(): array
{
$this->categoryRepository = $categoryRepository;
$sortBy = $this->settings['sorting'] ?? 'tstamp';
if (!in_array($sortBy, $this->allowedColumns)) {
$sortBy = 'tstamp';
}
$defaultDirection = QueryInterface::ORDER_DESCENDING;
$direction = $this->settings['direction'] ?? $defaultDirection;
if ($this->settings['direction'] && strtoupper($direction) === 'DESC') {
$defaultDirection = QueryInterface::ORDER_ASCENDING;
}
return [
$sortBy => $defaultDirection,
];
}

// protected function getDemand(): array
// {
// $searchTerm = $this->request->hasArgument('searchTerm') ? $this->request->getArgument('searchTerm') : '';
// $demand = [];
// if (strlen($searchTerm) > 0) {
// foreach ($this->demandFields as $field) {
// $demand[$field] = $searchTerm;
// }
// }
// return $demand;
// }

}
161 changes: 161 additions & 0 deletions Classes/Domain/Repository/ImageGalleryRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?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();
$queryBuilder->select('*')->from($this->tableName);

$constraints = [];
foreach ($demand as $field => $value) {
$constraints[] = $queryBuilder
->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 ($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);
}











}
Loading
Loading