1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\System\Configuration; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2010-2016 Timo Schmidt <[email protected] |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache; |
28
|
|
|
use TYPO3\CMS\Core\Database\DatabaseConnection; |
29
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
30
|
|
|
use \TYPO3\CMS\Frontend\Page\PageRepository; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* This class is responsible to find the closest page id from the rootline where |
34
|
|
|
* a typoscript template is stored on. |
35
|
|
|
* |
36
|
|
|
* @package ApacheSolrForTypo3\Solr\System\Configuration |
37
|
|
|
* @author Timo Hund <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class ConfigurationPageResolver |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var PageRepository |
43
|
|
|
*/ |
44
|
|
|
protected $pageRepository; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var TwoLevelCache |
48
|
|
|
*/ |
49
|
|
|
protected $twoLevelCache; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* ConfigurationPageResolver constructor. |
53
|
|
|
* @param PageRepository|null $pageRepository |
54
|
|
|
* @param TwoLevelCache|null $twoLevelCache |
55
|
|
|
*/ |
56
|
1 |
|
public function __construct(PageRepository $pageRepository = null, TwoLevelCache $twoLevelCache = null) |
57
|
|
|
{ |
58
|
1 |
|
$this->pageRepository = isset($pageRepository) ? $pageRepository : GeneralUtility::makeInstance(PageRepository::class); |
59
|
1 |
|
$this->runtimeCache = isset($twoLevelCache) ? $twoLevelCache : GeneralUtility::makeInstance(TwoLevelCache::class, 'cache_runtime'); |
|
|
|
|
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* This method fetches the rootLine and calculates the id of the closest template in the rootLine. |
64
|
|
|
* The result is stored in the runtime cache. |
65
|
|
|
* |
66
|
|
|
* @param integer $startPageId |
67
|
|
|
* @return integer |
68
|
|
|
*/ |
69
|
1 |
|
public function getClosestPageIdWithActiveTemplate($startPageId) |
70
|
|
|
{ |
71
|
1 |
|
if ($startPageId === 0) { |
72
|
|
|
return 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$cacheId = 'ConfigurationPageResolver' . '_' . 'getClosestPageIdWithActiveTemplate' . '_' . $startPageId; |
76
|
1 |
|
$methodResult = $this->runtimeCache->get($cacheId); |
77
|
1 |
|
if (!empty($methodResult)) { |
78
|
|
|
return $methodResult; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$methodResult = $this->calculateClosestPageIdWithActiveTemplate($startPageId); |
82
|
1 |
|
$this->runtimeCache->set($cacheId, $methodResult); |
83
|
|
|
|
84
|
1 |
|
return $methodResult; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* This method fetches the rootLine and calculates the id of the closest template in the rootLine. |
89
|
|
|
* |
90
|
|
|
* @param integer $startPageId |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
1 |
|
protected function calculateClosestPageIdWithActiveTemplate($startPageId) |
94
|
|
|
{ |
95
|
1 |
|
$rootLine = $this->pageRepository->getRootLine($startPageId); |
96
|
|
|
// when no rootline is present the startpage it's self is the closest page |
97
|
1 |
|
if (!is_array($rootLine)) { |
98
|
|
|
return $startPageId; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
$closestPageIdWithTemplate = $this->getPageIdsWithTemplateInRootLineOrderedByDepth($rootLine); |
102
|
1 |
|
if ($closestPageIdWithTemplate === 0) { |
103
|
|
|
return $startPageId; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return (int)$closestPageIdWithTemplate; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Retrieves the closest pageId with a template on and 0 when non is found. |
111
|
|
|
* |
112
|
|
|
* @param array $rootLine |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
1 |
|
protected function getPageIdsWithTemplateInRootLineOrderedByDepth($rootLine) |
116
|
|
|
{ |
117
|
1 |
|
$rootLinePageIds = [0]; |
118
|
1 |
|
foreach ($rootLine as $rootLineItem) { |
119
|
1 |
|
$rootLinePageIds[] = (int)$rootLineItem['uid']; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
$pageIdsClause = implode(",", $rootLinePageIds); |
123
|
1 |
|
$where = 'pid IN (' . $pageIdsClause . ') AND deleted = 0 AND hidden = 0'; |
124
|
1 |
|
$res = $this->getDatabaseConnection()->exec_SELECTgetRows('uid,pid', 'sys_template', $where); |
125
|
|
|
|
126
|
1 |
|
$firstTemplateRow = $res[0]; |
127
|
1 |
|
return isset($firstTemplateRow['pid']) ? $firstTemplateRow['pid'] : 0; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return DatabaseConnection |
132
|
|
|
*/ |
133
|
1 |
|
protected function getDatabaseConnection() |
134
|
|
|
{ |
135
|
1 |
|
return $GLOBALS['TYPO3_DB']; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: