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] Api basic search #105

Open
wants to merge 4 commits into
base: master
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
92 changes: 92 additions & 0 deletions Classes/Controller/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Http\Stream;
Expand Down Expand Up @@ -644,6 +645,97 @@ public function deleteResourcesAction(ServerRequestInterface $request): JsonResp
return new FileOperationResponse($resources);
}

/**
* Return item list (folders, files, images) of a storage:path
* FAL folder identifier. GET request with identifier argument.
* Query parameter
* identifier string combined identifier of search starting point
* query string serach string
*
* @param ServerRequestInterface $request
*
* @return JsonResponse
*/
public function searchAction(ServerRequestInterface $request): JsonResponse
{
try {
$identifier = $request->getQueryParams()['identifier'] ?? '';
$query = $request->getQueryParams()['query'] ?? '';
if (empty($identifier)) {
throw new ControllerException('Identifier needed', 1553699828);
}
if (empty($identifier)) {
throw new ControllerException('Query string needed', 1554452377);
}
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
$folderObject = $resourceFactory->getObjectFromCombinedIdentifier($identifier);
if (!$folderObject instanceof Folder) {
throw new ControllerException('Identifier is not a folder', 1553701684);
}
$allFiles = $this->searchFiles($identifier, $query);
$folders = [];
$files = [];
$images = [];
foreach ($allFiles as $file) {
// If file is an image or media, create image object, else file object
$fileExtension = strtolower($file->getExtension());
if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileExtension)
|| GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['mediafile_ext'], $fileExtension)
) {
$images[] = new FolderItemImage($file);
} else {
$files[] = new FolderItemFile($file);
}
}
return new FolderItemsResponse($folders, $files, $images);
} catch (ResourceException $e) {
return new JsonExceptionResponse($e);
} catch (ControllerException $e) {
return new JsonExceptionResponse($e);
}
}

/**
* returns an array of files in a current path
*
* @param string $combinedIdentifier
* @param string $searchWord
*
* @return array
* @throws ResourceDoesNotExistException
*/
protected function searchFiles($combinedIdentifier = '', $searchWord = ''): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file');
/** @var ResourceFactory $resourceFactory */
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
$storage = $resourceFactory->getStorageObjectFromCombinedIdentifier($combinedIdentifier);
$identifier = substr($combinedIdentifier, strpos($combinedIdentifier, ':') + 1);
$constraints = [];
$constraints[] = $queryBuilder->expr()->eq('storage', $storage->getUid());
$constraints[] = $queryBuilder->expr()->like('sys_file.name', $queryBuilder->createNamedParameter('%' . $queryBuilder->escapeLikeWildcards($searchWord) . '%') );
if ($identifier !== '/') {
$constraints[] = $queryBuilder->expr()->like('identifier', $queryBuilder->createNamedParameter( $queryBuilder->escapeLikeWildcards($identifier) . '%') );
}
$statement = $queryBuilder->select('*')
->from('sys_file')
->join(
'sys_file',
'sys_file_metadata',
'file_metadata',
$queryBuilder->expr()->eq('sys_file.uid', 'file_metadata.file')
)
->where($queryBuilder->expr()->andX(...$constraints))
->execute();
$files = [];
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
while ($row = $statement->fetch()) {
$files[] = $resourceFactory->getObjectFromCombinedIdentifier($row['storage'] . ':' . $row['identifier']);
}
return $files;
}


/**
* @return BackendUserAuthentication
*/
Expand Down
4 changes: 4 additions & 0 deletions Configuration/Backend/AjaxRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@
'path' => '/dam/deleteResources',
'target' => TYPO3\CMS\DigitalAssetManagement\Controller\AjaxController::class . '::deleteResourcesAction',
],
'damSearch' => [
'path' => '/dam/search',
'target' => TYPO3\CMS\DigitalAssetManagement\Controller\AjaxController::class . '::searchAction',
],
];