Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
[TASK] Refactor WizardItemsHookSubscriber
Browse files Browse the repository at this point in the history
This change splits the logic of the wizard items hook subscriber into multiple smaller methods. A corresponding change will be made for Flux after which Fluidcontent's wizard hook subscriber can be trimmed a bit.
  • Loading branch information
NamelessCoder committed Feb 5, 2015
1 parent 406f931 commit c92c2c3
Showing 1 changed file with 107 additions and 54 deletions.
161 changes: 107 additions & 54 deletions Classes/Hooks/WizardItemsHookSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,92 @@ public function manipulateWizardItems(&$items, &$parentObject) {
* @return array
*/
protected function filterPermittedFluidContentTypesByInsertionPosition(array $items, $parentObject) {
list ($whitelist, $blacklist) = $this->getWhiteAndBlackListsFromPageAndContentColumn(
$parentObject->id,
$parentObject->colPos,
$parentObject->uid_pid
);
$items = $this->applyWhitelist($items, $whitelist);
$items = $this->applyBlacklist($items, $blacklist);
$items = $this->cleanEmptySections($items);
return $items;
}

/**
* @param array $items
* @return array
*/
protected function cleanEmptySections(array $items) {
$preserveHeaders = array();
foreach ($items as $name => $item) {
if (FALSE !== strpos($name, '_')) {
array_push($preserveHeaders, reset(explode('_', $name)));
}
}
foreach ($items as $name => $item) {
if (FALSE === strpos($name, '_') && FALSE === in_array($name, $preserveHeaders)) {
unset($items[$name]);
}
}
return $items;
}

/**
* @param array $items
* @param array $whitelist
* @return array
*/
protected function applyWhitelist(array $items, array $whitelist) {
if (0 < count($whitelist)) {
foreach ($items as $name => $item) {
if (FALSE !== strpos($name, '_') && 'fluidcontent_content' === $item['tt_content_defValues']['CType'] && FALSE === in_array($item['tt_content_defValues']['tx_fed_fcefile'], $whitelist)) {
unset($items[$name]);
}
}
}
return $items;
}

/**
* @param array $items
* @param array $blacklist
* @return array
*/
protected function applyBlacklist(array $items, array $blacklist) {
if (0 < count($blacklist)) {
foreach ($blacklist as $contentElementType) {
foreach ($items as $name => $item) {
if ('fluidcontent_content' === $item['tt_content_defValues']['CType'] && $item['tt_content_defValues']['tx_fed_fcefile'] === $contentElementType) {
unset($items[$name]);
}
}
}
}
return $items;
}

/**
* @param integer $pageUid
* @param integer $columnPosition
* @param integer $relativeUid
* @return array
*/
protected function getWhiteAndBlackListsFromPageAndContentColumn($pageUid, $columnPosition, $relativeUid) {
$whitelist = array();
$blacklist = array();
// if a Provider is registered for the "pages" table, try to get a Grid from it. If the Grid
// returned contains a Column which matches the desired colPos value, attempt to read a list
// of allowed/denied content element types from it.
$pageRecord = $this->recordService->getSingle('pages', '*', $parentObject->id);
$pageRecord = (array) $this->recordService->getSingle('pages', '*', $pageUid);
$pageProviders = $this->configurationService->resolveConfigurationProviders('pages', NULL, $pageRecord);
foreach ($pageProviders as $pageProvider) {
$grid = $pageProvider->getGrid($pageRecord);
if (NULL === $grid) {
continue;
}
foreach ($grid->getRows() as $row) {
foreach ($row->getColumns() as $column) {
if ($column->getColumnPosition() === $parentObject->colPos) {
list ($whitelist, $blacklist) = $this->appendToWhiteAndBlacklistFromComponent($column, $whitelist, $blacklist);
}
}
}
}
$this->appendToWhiteAndBlacklistFromProviders($pageProviders, $pageRecord, $whitelist, $blacklist, $columnPosition);
// Detect what was clicked in order to create the new content element; decide restrictions
// based on this.
$defaultValues = GeneralUtility::_GET('defVals');
if (0 > $parentObject->uid_pid) {
if (0 > $relativeUid) {
// pasting after another element means we should try to resolve the Flux content relation
// from that element instead of GET parameters (clicked: "create new" icon after other element)
$parentRecord = $this->recordService->getSingle('tt_content', '*', abs($parentObject->uid_pid));
$parentRecord = $this->recordService->getSingle('tt_content', '*', abs($relativeUid));
$fluxAreaName = (string) $parentRecord['tx_flux_column'];
$parentRecordUid = (integer) $parentRecord['tx_flux_parent'];
} elseif (TRUE === isset($defaultValues['tt_content']['tx_flux_column'])) {
Expand All @@ -129,56 +188,50 @@ protected function filterPermittedFluidContentTypesByInsertionPosition(array $it
// (admitted, that's quite a mouthful - but it's not that different from reading the values from
// a page template like above; it's the same principle).
if (0 < $parentRecordUid && FALSE === empty($fluxAreaName)) {
$parentRecord = $this->recordService->getSingle('tt_content', '*', $parentRecordUid);
$parentRecord = (array) $this->recordService->getSingle('tt_content', '*', $parentRecordUid);
$contentProviders = $this->configurationService->resolveConfigurationProviders('tt_content', NULL, $parentRecord);
foreach ($contentProviders as $contentProvider) {
$grid = $contentProvider->getGrid($parentRecord);
if (NULL === $grid) {
continue;
}
foreach ($grid->getRows() as $row) {
foreach ($row->getColumns() as $column) {
if ($column->getName() === $fluxAreaName) {
list ($whitelist, $blacklist) = $this->appendToWhiteAndBlacklistFromComponent($column, $whitelist, $blacklist);
}
}
}
}
$this->appendToWhiteAndBlacklistFromProviders($contentProviders, $parentRecord, $whitelist, $blacklist, NULL, $fluxAreaName);
}
// White/blacklist filtering. If whitelist contains elements, filter the list
// of possible types by whitelist first. Then apply the blacklist, removing
// any element types recorded herein.
$whitelist = array_unique($whitelist);
$blacklist = array_unique($blacklist);
if (0 < count($whitelist)) {
foreach ($items as $name => $item) {
if (FALSE !== strpos($name, '_') && 'fluidcontent_content' === $item['tt_content_defValues']['CType'] && FALSE === in_array($item['tt_content_defValues']['tx_fed_fcefile'], $whitelist)) {
unset($items[$name]);
}
return array($whitelist, $blacklist);
}

/**
* @param array $providers
* @param array $record
* @param integer $columnPosition
* @param string $fluxAreaName
*/
protected function appendToWhiteAndBlacklistFromProviders(
array $providers,
array $record,
array &$whitelist,
array &$blacklist,
$columnPosition,
$fluxAreaName = NULL
) {
foreach ($providers as $provider) {
$grid = $provider->getGrid($record);
if (NULL === $grid) {
continue;
}
}
if (0 < count($blacklist)) {
foreach ($blacklist as $contentElementType) {
foreach ($items as $name => $item) {
if ('fluidcontent_content' === $item['tt_content_defValues']['CType'] && $item['tt_content_defValues']['tx_fed_fcefile'] === $contentElementType) {
unset($items[$name]);
foreach ($grid->getRows() as $row) {
foreach ($row->getColumns() as $column) {
if (FALSE === empty($fluxAreaName)) {
if ($column->getName() === $fluxAreaName) {
list ($whitelist, $blacklist) = $this->appendToWhiteAndBlacklistFromComponent($column, $whitelist, $blacklist);
}
} elseif ($column->getColumnPosition() === $columnPosition) {
list ($whitelist, $blacklist) = $this->appendToWhiteAndBlacklistFromComponent($column, $whitelist, $blacklist);
}
}
}
}
// Finally, loop through the items list and clean up any tabs with zero element types inside.
$preserveHeaders = array();
foreach ($items as $name => $item) {
if (FALSE !== strpos($name, '_')) {
array_push($preserveHeaders, reset(explode('_', $name)));
}
}
foreach ($items as $name => $item) {
if (FALSE === strpos($name, '_') && FALSE === in_array($name, $preserveHeaders)) {
unset($items[$name]);
}
}
return $items;

}

/**
Expand Down

0 comments on commit c92c2c3

Please sign in to comment.