8000 Extract palette and boxes building in data container by aschempp · Pull Request #8285 · contao/contao · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Extract palette and boxes building in data container #8285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core-bundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ services:
- '@security.helper'
- '@router'

contao.data_container.palette_builder:
class: Contao\CoreBundle\DataContainer\PaletteBuilder
public: true
arguments:
- '@contao.framework'
- '@request_stack'
- '@security.helper'
- '@database_connection'

contao.data_container.record_labeler:
class: Contao\CoreBundle\DataContainer\RecordLabeler
public: true
Expand Down
23 changes: 7 additions & 16 deletions core-bundle/contao/classes/DataContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,26 +811,17 @@ public function help($strClass='', $strDescription=null)
* @param array $names
*
* @return array
*
* @deprecated Deprecated in Contao 5.6, to be removed in Contao 6.0.
*/
protected function combiner($names)
{
$return = array('');
$names = array_values($names);

for ($i=0, $c=\count($names); $i<$c; $i++)
{
$buffer = array();

foreach ($return as $k=>$v)
{
$buffer[] = ($k%2 == 0) ? $v : $v . $names[$i];
$buffer[] = ($k%2 == 0) ? $v . $names[$i] : $v;
}

$return = $buffer;
}
trigger_deprecation('contao/core-bundle', '5.6', __METHOD__ . ' is deprecated.');

return array_filter($return);
return System::getContainer()
->get('contao.data_container.palette_builder')
->combiner($names)
;
}

/**
Expand Down
168 changes: 15 additions & 153 deletions core-bundle/contao/drivers/DC_File.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,85 +110,35 @@ public function edit()
$ajaxId = func_get_arg(1);
}

// Build an array from boxes and rows
$this->strPalette = $this->getPalette();
$boxes = StringUtil::trimsplit(';', $this->strPalette);
$legends = array();
$boxes = System::getContainer()->get('contao.data_container.palette_builder')->getBoxes($this->strPalette, $this->strTable);

if (!empty($boxes))
{
foreach ($boxes as $k=>$v)
{
$boxes[$k] = StringUtil::trimsplit(',', $v);

foreach ($boxes[$k] as $kk=>$vv)
{
if (preg_match('/^\[.*]$/', $vv))
{
continue;
}

if (preg_match('/^{.*}$/', $vv))
{
$legends[$k] = substr($vv, 1, -1);
unset($boxes[$k][$kk]);
}
elseif (!\is_array($GLOBALS['TL_DCA'][$this->strTable]['fields'][$vv] ?? null))
{
unset($boxes[$k][$kk]);
}
}

// Unset a box if it does not contain any fields
if (empty($boxes[$k]))
{
unset($boxes[$k]);
}
}

$objSessionBag = System::getContainer()->get('request_stack')->getSession()->getBag('contao_backend');

// Render boxes
$class = 'tl_tbox';
$fs = $objSessionBag->get('fieldset_states');

foreach ($boxes as $k=>$v)
foreach ($boxes as $box)
{
$strAjax = '';
$blnAjax = false;
$key = '';
$cls = '';
$key = $box['key'];
$legend = '';

if (isset($legends[$k]))
if ($key)
{
list($key, $cls) = explode(':', $legends[$k]) + array(null, null);

$legend = "\n" . '<legend><button type="button" data-action="contao--toggle-fieldset#toggle">' . ($GLOBALS['TL_LANG'][$this->strTable][$key] ?? $key) . '</button></legend>';
}

if ($legend)
{
if (isset($fs[$this->strTable][$key]))
if ($box['class'])
{
$class .= ($fs[$this->strTable][$key] ? '' : ' collapsed');
}
elseif ($cls)
{
// Convert the ":hide" suffix from the DCA
if ($cls == 'hide')
{
$cls = 'collapsed';
}

$class .= ' ' . $cls;
$class .= ' ' . $box['class'];
}
}

$return .= "\n\n" . '<fieldset class="' . $class . ($legend ? '' : ' nolegend') . '" data-controller="contao--toggle-fieldset" data-contao--toggle-fieldset-id-value="' . $key . '" data-contao--toggle-fieldset-table-value="' . $this->strTable . '" data-contao--toggle-fieldset-collapsed-class="collapsed" data-contao--jump-targets-target="section" data-contao--jump-targets-label-value="' . ($GLOBALS['TL_LANG'][$this->strTable][$key] ?? $key) . '" data-action="contao--jump-targets:scrollto->contao--toggle-fieldset#open">' . $legend . "\n" . '<div class="widget-group">';

// Build rows of the current box
foreach ($v as $vv)
foreach ($box['fields'] as $vv)
{
if ($vv == '[EOF]')
{
Expand Down Expand Up @@ -422,102 +372,14 @@ protected function save($varValue)
*/
public function getPalette()
{
$strPalette = $GLOBALS['TL_DCA'][$this->strTable]['palettes']['default'] ?? '';

// Check whether there are selector fields
if (!empty($GLOBALS['TL_DCA'][$this->strTable]['palettes']['__selector__']))
{
$sValues = array();
$subpalettes = array();

foreach ($GLOBALS['TL_DCA'][$this->strTable]['palettes']['__selector__'] as $name)
{
$trigger = Config::get($name);

// Overwrite the trigger if the page is not reloaded
if (Input::post('FORM_SUBMIT') == $this->strTable)
{
$key = (Input::get('act') == 'editAll') ? $name . '_' . $this->intId : $name;

if (!($GLOBALS['TL_DCA'][$this->strTable]['fields'][$name]['eval']['submitOnChange'] ?? null))
{
$trigger = Input::post($key);
}
}

if ($trigger)
{
if (($GLOBALS['TL_DCA'][$this->strTable]['fields'][$name]['inputType'] ?? null) == 'checkbox' && !($GLOBALS['TL_DCA'][$this->strTable]['fields'][$name]['eval']['multiple'] ?? null))
{
$sValues[] = $name;

// Look for a subpalette
if (isset($GLOBALS['TL_DCA'][$this->strTable]['subpalettes'][$name]))
{
$subpalettes[$name] = $GLOBALS['TL_DCA'][$this->strTable]['subpalettes'][$name];
}
}
else
{
$sValues[] = $trigger;
$key = $name . '_' . $trigger;

// Look for a subpalette
if (isset($GLOBALS['TL_DCA'][$this->strTable]['subpalettes'][$key]))
{
$subpalettes[$name] = $GLOBALS['TL_DCA'][$this->strTable]['subpalettes'][$key];
}
}
}
}

// Build possible palette names from the selector values
if (empty($sValues))
{
$names = array('default');
}
elseif (\count($sValues) > 1)
{
$names = $this->combiner($sValues);
}
else
{
$names = array($sValues[0]);
}

// Get an existing palette
foreach ($names as $paletteName)
{
if (isset($GLOBALS['TL_DCA'][$this->strTable]['palettes'][$paletteName]))
{
$strPalette = $GLOBALS['TL_DCA'][$this->strTable]['palettes'][$paletteName];
break;
}
}

// Include sub-palettes
foreach ($subpalettes as $k=>$v)
{
$strPalette = preg_replace('/\b' . preg_quote($k, '/') . '\b/i', $k . ',[' . $k . '],' . $v . ',[EOF]', $strPalette);
}
}

// Call onpalette_callback
if (\is_array($GLOBALS['TL_DCA'][$this->strTable]['config']['onpalette_callback'] ?? null))
{
foreach ($GLOBALS['TL_DCA'][$this->strTable]['config']['onpalette_callback'] as $callback)
{
if (\is_array($callback))
{
$strPalette = System::importStatic($callback[0])->{$callback[1]}($strPalette, $this);
}
elseif (\is_callable($callback))
{
$strPalette = $callback($strPalette, $this);
}
}
}
return System::getContainer()
->get('contao.data_container.palette_builder')
->getPalette($this->strTable, (int) $this->intId, $this)
;
}

return $strPalette;
public function getCurrentRecord(int|string|null $id = null, string|null $table = null): array|null
{
return $GLOBALS['TL_CONFIG'];
}
}
66 changes: 12 additions & 54 deletions core-bundle/contao/drivers/DC_Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1249,44 +1249,21 @@ public function edit()
$GLOBALS['TL_DCA'][$this->strTable]['fields'] = array_intersect_key($GLOBALS['TL_DCA'][$this->strTable]['fields'] ?? array(), array('name' => true, 'protected' => true, 'syncExclude' => true));
}

$security = System::getContainer()->get('security.helper');

// Build an array from boxes and rows (do not show excluded fields)
$this->strPalette = $this->getPalette();
$boxes = StringUtil::trimsplit(';', $this->strPalette);
$boxes = System::getContainer()->get('contao.data_container.palette_builder')->getBoxes($this->strPalette, $this->strTable);

if (!empty($boxes))
{
// Get fields
foreach ($boxes as $k=>$v)
{
$boxes[$k] = StringUtil::trimsplit(',', $v);

foreach ($boxes[$k] as $kk=>$vv)
{
if (!isset($GLOBALS['TL_DCA'][$this->strTable]['fields'][$vv]) || (DataContainer::isFieldExcluded($this->strTable, $vv) && !$security->isGranted(ContaoCorePermissions::USER_CAN_EDIT_FIELD_OF_TABLE, $this->strTable . '::' . $vv)))
{
unset($boxes[$k][$kk]);
}
}

// Unset a box if it does not contain any fields
if (empty($boxes[$k]))
{
unset($boxes[$k]);
}
}

// Render boxes
$class = 'tl_tbox';

foreach ($boxes as $v)
foreach ($boxes as $box)
{
$return .= '
<div class="' . $class . ' cf">';

// Build rows of the current box
foreach ($v as $vv)
foreach ($box['fields'] as $vv)
{
$this->strField = $vv;
$this->strInputName = $vv;
Expand Down Expand Up @@ -1491,7 +1468,9 @@ public function editAll()

$this->intId = $id;
$this->initialId = $id;
$this->strPalette = StringUtil::trimsplit('[;,]', $this->getPalette());
$this->strPalette = $this->getPalette();
$boxes = System::getContainer()->get('contao.data_container.palette_builder')->getBoxes($this->strPalette, $this->strTable);
$paletteFields = array_merge(...array_column($boxes, 'fields'));

$objModel = null;
$objVersions = null;
Expand All @@ -1517,7 +1496,7 @@ public function editAll()
else
{
// Unset the database fields
$this->strPalette = array_filter($this->strPalette, static function ($val) { return $val == 'name' || $val == 'protected'; });
$paletteFields = array_filter($paletteFields, static function ($val) { return $val == 'name' || $val == 'protected'; });
}

$return .= '
Expand All @@ -1526,19 +1505,13 @@ public function editAll()
$class = 'tl_box';
$strHash = md5($id);

foreach ($this->strPalette as $v)
foreach ($paletteFields as $v)
{
if (!\in_array($v, $fields))
{
continue;
}

// Check whether field is excluded
if (DataContainer::isFieldExcluded($this->strTable, $v) && !$security->isGranted(ContaoCorePermissions::USER_CAN_EDIT_FIELD_OF_TABLE, $this->strTable . '::' . $v))
{
continue;
}

$this->strField = $v;
$this->strInputName = $v . '_' . $strHash;

Expand Down Expand Up @@ -2204,25 +2177,10 @@ public function sync()
*/
public function getPalette()
{
$strPalette = $GLOBALS['TL_DCA'][$this->strTable]['palettes']['default'];

// Call onpalette_callback
if (\is_array($GLOBALS['TL_DCA'][$this->strTable]['config']['onpalette_callback'] ?? null))
{
foreach ($GLOBALS['TL_DCA'][$this->strTable]['config']['onpalette_callback'] as $callback)
{
if (\is_array($callback))
{
$strPalette = System::importStatic($callback[0])->{$callback[1]}($strPalette, $this);
}
elseif (\is_callable($callback))
{
$strPalette = $callback($strPalette, $this);
}
}
}

return $strPalette;
return System::getContainer()
->get('contao.data_container.palette_builder')
->getPalette($this->strTable, (int) $this->intId, $this)
;
}

/**
Expand Down
Loading
0