| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace Opentalent\OtRouter\Routing;
- use Opentalent\OtCore\Page\OtPageRepository;
- use Opentalent\OtRouter\Utility\RouteNormalizer;
- use TYPO3\CMS\Core\Database\ConnectionPool;
- /**
- * Provides methods to index the Typo3 pages and populate the routing db table
- *
- * @package Opentalent\OtRouter\Routing
- */
- class Indexer
- {
- const INDEX_TABLENAME = 'tx_opentalent_router';
- /**
- * @var ConnectionPool
- */
- private ConnectionPool $connectionPool;
- public function injectConnectionPool(ConnectionPool $connectionPool)
- {
- $this->connectionPool = $connectionPool;
- }
- /**
- * @var OtPageRepository
- */
- protected OtPageRepository $otPageRepository;
- public function injectOtPageRepository(OtPageRepository $otPageRepository) {
- $this->otPageRepository = $otPageRepository;
- }
- /**
- * Clear then repopulate the whole index
- */
- public function indexAllRoutes() {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $rootPages = $queryBuilder
- ->select('uid')
- ->from('pages')
- ->where($queryBuilder->expr()->eq('is_siteroot', 1))
- ->execute()
- ->fetchAll();
- foreach ($rootPages as $rootPage) {
- $this->indexRoutesForWebsite($rootPage);
- }
- }
- /**
- * Clear and recreate the index entry for the target website
- *
- * @param int $rootUid
- */
- public function indexRoutesForWebsite(int $rootUid) {
- foreach ($this->otPageRepository->getAllSitePages($rootUid) as $page) {
- $this->registerIndexEntry(
- $page['uid'],
- $page['tx_opentalent_structure_domain'],
- $page['slug'],
- $rootUid
- );
- }
- }
- public function indexRouteForPage(int $pageUid) {
- $page = $this->otPageRepository->getPage($pageUid);
- $rootPage = $this->otPageRepository->getRootPageFor($pageUid);
- $this->registerIndexEntry(
- $page['uid'],
- $page['tx_opentalent_structure_domain'],
- $page['slug'],
- $rootPage['uid']
- );
- }
- private function registerIndexEntry(int $pageUid,
- string $domain,
- string $slug,
- ?int $rootUid = null) {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::INDEX_TABLENAME);
- $queryBuilder
- ->delete(self::INDEX_TABLENAME)
- ->where($queryBuilder->expr()->eq('page_uid', $pageUid))
- ->execute();
- $entry = [
- 'domain' => RouteNormalizer::normalizeDomain($domain),
- 'root_uid' => $rootUid,
- 'slug' => RouteNormalizer::normalizeSlug($slug),
- 'page_uid' => $pageUid,
- 'last_update' => date('Y-m-d H:i:s')
- ];
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::INDEX_TABLENAME);
- $queryBuilder
- ->insert(self::INDEX_TABLENAME)
- ->values($entry)
- ->execute();
- }
- }
|