|
| 1 | +<?php |
| 2 | +namespace FluidTYPO3\Fluidpages\UserFunction; |
| 3 | + |
| 4 | +use FluidTYPO3\Flux\Service\WorkspacesAwareRecordService;; |
| 5 | +use TYPO3\CMS\Core\Cache\CacheManager; |
| 6 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 7 | +use TYPO3\CMS\Backend\Form\FormDataProvider\EvaluateDisplayConditions; |
| 8 | +use TYPO3\CMS\Extbase\Object\ObjectManager; |
| 9 | + |
| 10 | +/** |
| 11 | + * Decide if fluidpages shall be used to render a page |
| 12 | + */ |
| 13 | +class LayoutSelect |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var WorkspacesAwareRecordService |
| 17 | + */ |
| 18 | + protected $workspacesAwareRecordService; |
| 19 | + |
| 20 | + /** |
| 21 | + * Decide if the "Page layouts" fields and "Page configuration" flexform |
| 22 | + * shall be shown in the page settings. |
| 23 | + * |
| 24 | + * The first conditionParameters parameter determines if the subpages |
| 25 | + * settings are meant or not. |
| 26 | + * |
| 27 | + * @param array $param Parameters with key: |
| 28 | + * - record (page db row) |
| 29 | + * - flexformValueKey |
| 30 | + * - conditionParameters (displayCond config) |
| 31 | + * @param object $caller Object calling the method |
| 32 | + * |
| 33 | + * @return boolean True if it shall be shown |
| 34 | + */ |
| 35 | + public function doShowPageConfiguration($param, EvaluateDisplayConditions $caller) |
| 36 | + { |
| 37 | + $sub = (boolean) $param['conditionParameters'][0]; |
| 38 | + |
| 39 | + $conf = $this->getPageTemplateConfiguration($param['record']['uid']); |
| 40 | + if (null === $conf) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + if ($sub) { |
| 45 | + return false === empty($conf['tx_fed_page_controller_action_sub']); |
| 46 | + } else { |
| 47 | + return false === empty($conf['tx_fed_page_controller_action']); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Process RootLine to find first usable, configured Fluid Page Template. |
| 53 | + * WARNING: do NOT use the output of this feature to overwrite $row - the |
| 54 | + * record returned may or may not be the same record as defined in $id. |
| 55 | + * |
| 56 | + * @param integer $pageUid |
| 57 | + * @return array|NULL Array with two keys: |
| 58 | + * - tx_fed_page_controller_action |
| 59 | + * - tx_fed_page_controller_action_sub |
| 60 | + * @api |
| 61 | + */ |
| 62 | + public function getPageTemplateConfiguration($pageUid) |
| 63 | + { |
| 64 | + $pageUid = (integer) $pageUid; |
| 65 | + if (1 > $pageUid) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + $cacheId = 'fluidpages-template-configuration-' . $pageUid; |
| 69 | + $runtimeCache = $this->getRuntimeCache(); |
| 70 | + $fromCache = $runtimeCache->get($cacheId); |
| 71 | + if ($fromCache) { |
| 72 | + return $fromCache; |
| 73 | + } |
| 74 | + $fieldList = 'tx_fed_page_controller_action_sub,backend_layout,backend_layout_next_level,t3ver_oid,pid,uid'; |
| 75 | + $page = $this->getWorkspacesAwareRecordService()->getSingle( |
| 76 | + 'pages', |
| 77 | + 'tx_fed_page_controller_action,' . $fieldList, |
| 78 | + $pageUid |
| 79 | + ); |
| 80 | + |
| 81 | + $checkUsage = true; |
| 82 | + $useMainTemplate = $this->isFluidpagesBackendLayout($page['backend_layout']); |
| 83 | + $useSubTemplate = $this->isFluidpagesBackendLayout($page['backend_layout_next_level']); |
| 84 | + $mainTemplate = $page['tx_fed_page_controller_action']; |
| 85 | + $subTemplate = $page['tx_fed_page_controller_action_sub']; |
| 86 | + $isFirstPage = true; |
| 87 | + |
| 88 | + do { |
| 89 | + if (false === $isFirstPage) { |
| 90 | + if ($checkUsage) { |
| 91 | + if ($this->isFluidpagesBackendLayout($page['backend_layout_next_level'])) { |
| 92 | + $useMainTemplate = true; |
| 93 | + $useSubTemplate = true; |
| 94 | + } else if (false === empty($page['backend_layout_next_level'])) { |
| 95 | + //we have a different layout in between, so do not look further up |
| 96 | + $checkUsage = false; |
| 97 | + } |
| 98 | + } |
| 99 | + $containsSubDefinition = (false !== strpos($page['tx_fed_page_controller_action_sub'], '->')); |
| 100 | + if ($containsSubDefinition) { |
| 101 | + if (empty($mainTemplate)) { |
| 102 | + $mainTemplate = $page['tx_fed_page_controller_action_sub']; |
| 103 | + } |
| 104 | + if (empty($subTemplate)) { |
| 105 | + $subTemplate = $page['tx_fed_page_controller_action_sub']; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + // Note: 't3ver_oid' is analysed in order to make versioned records inherit the original record's |
| 110 | + // configuration as an emulated first parent page. |
| 111 | + $resolveParentPageUid = (integer) (0 > $page['pid'] ? $page['t3ver_oid'] : $page['pid']); |
| 112 | + $page = $this->getWorkspacesAwareRecordService()->getSingle( |
| 113 | + 'pages', |
| 114 | + $fieldList, |
| 115 | + $resolveParentPageUid |
| 116 | + ); |
| 117 | + $isFirstPage = false; |
| 118 | + } while (null !== $page); |
| 119 | + |
| 120 | + if (empty($mainTemplate)) { |
| 121 | + $useMainTemplate = false; |
| 122 | + } |
| 123 | + if (empty($subTemplate)) { |
| 124 | + $useSubTemplate = false; |
| 125 | + } |
| 126 | + if (false === $useMainTemplate && false === $useSubTemplate) { |
| 127 | + //BC return value |
| 128 | + return null; |
| 129 | + } |
| 130 | + $configuration = [ |
| 131 | + 'tx_fed_page_controller_action' => $useMainTemplate ? $mainTemplate : null, |
| 132 | + 'tx_fed_page_controller_action_sub' => $useSubTemplate ? $subTemplate : null |
| 133 | + ]; |
| 134 | + $runtimeCache->set($cacheId, $configuration); |
| 135 | + return $configuration; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Determine if the given backend layout string is a fluidpages layout |
| 140 | + * |
| 141 | + * @param string $belayout Page row backend_layout value |
| 142 | + * |
| 143 | + * @return boolean True if fluidpages should be used to render |
| 144 | + */ |
| 145 | + public function isFluidpagesBackendLayout($belayout) |
| 146 | + { |
| 147 | + return substr($belayout, 0, 12) == 'fluidpages__'; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @return \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend |
| 152 | + */ |
| 153 | + protected function getRuntimeCache() |
| 154 | + { |
| 155 | + return GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime'); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @return use WorkspacesAwareRecordService |
| 160 | + */ |
| 161 | + protected function getWorkspacesAwareRecordService() |
| 162 | + { |
| 163 | + if (null === $this->workspacesAwareRecordService) { |
| 164 | + $this->workspacesAwareRecordService = GeneralUtility::makeInstance(ObjectManager::class) |
| 165 | + ->get(WorkspacesAwareRecordService::class); |
| 166 | + } |
| 167 | + return $this->workspacesAwareRecordService; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Used in unit tests. |
| 172 | + * |
| 173 | + * @param WorkspacesAwareRecordService $workspacesAwareRecordService |
| 174 | + * |
| 175 | + * @return void |
| 176 | + */ |
| 177 | + public function setWorkspacesAwareRecordService(WorkspacesAwareRecordService $workspacesAwareRecordService) |
| 178 | + { |
| 179 | + $this->workspacesAwareRecordService = $workspacesAwareRecordService; |
| 180 | + } |
| 181 | +} |
| 182 | +?> |
0 commit comments