SearchAvailabilityExtension.php 2.34 KB
Newer Older
1
<?php
2 3 4 5 6
/**
 * Copyright (c) 2016 by the ACP3 Developers.
 * See the LICENCE file at the top-level module directory for licencing details.
 */

7
namespace ACP3\Modules\ACP3\Files\Extension;
8

9 10

use ACP3\Core\Date;
11
use ACP3\Core\Router\RouterInterface;
12
use ACP3\Modules\ACP3\Files\Installer\Schema;
13
use ACP3\Modules\ACP3\Files\Model\Repository\FilesRepository;
14
use ACP3\Modules\ACP3\Search\Extension\SearchAvailabilityExtensionInterface;
15

16
class SearchAvailabilityExtension implements SearchAvailabilityExtensionInterface
17 18 19 20 21 22
{
    /**
     * @var \ACP3\Core\Date
     */
    private $date;
    /**
23
     * @var \ACP3\Core\Router\RouterInterface
24 25 26
     */
    private $router;
    /**
27
     * @var \ACP3\Modules\ACP3\Files\Model\Repository\FilesRepository
28
     */
29
    private $filesRepository;
30 31

    /**
32 33 34 35
     * SearchAvailability constructor.
     * @param Date $date
     * @param RouterInterface $router
     * @param FilesRepository $filesRepository
36 37 38
     */
    public function __construct(
        Date $date,
39
        RouterInterface $router,
40
        FilesRepository $filesRepository
41
    ) {
42 43
        $this->date = $date;
        $this->router = $router;
44
        $this->filesRepository = $filesRepository;
45 46 47
    }

    /**
48 49 50 51 52 53 54 55 56 57 58 59
     * @return string
     */
    public function getModuleName()
    {
        return Schema::MODULE_NAME;
    }

    /**
     * @param string $searchTerm
     * @param string $areas
     * @param string $sortDirection
     * @return array
60
     */
61
    public function fetchSearchResults($searchTerm, $areas, $sortDirection)
62
    {
63
        $fields = $this->mapSearchAreasToFields($areas);
64

65 66 67 68 69 70 71
        $results = $this->filesRepository->getAllSearchResults(
            $fields,
            $searchTerm,
            $sortDirection,
            $this->date->getCurrentDateTime()
        );
        $cResults = count($results);
72

73 74
        for ($i = 0; $i < $cResults; ++$i) {
            $results[$i]['hyperlink'] = $this->router->route('files/index/details/id_' . $results[$i]['id']);
75
        }
76

77
        return $results;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }

    /**
     * @param string $areas
     *
     * @return string
     */
    protected function mapSearchAreasToFields($areas)
    {
        switch ($areas) {
            case 'title':
                return 'title, file';
            case 'content':
                return 'text';
            default:
                return 'title, file, text';
        }
    }
96
}