Skip to content

Commit

Permalink
Merge pull request #12 from ErdmannFreunde/feature/auto-end-element
Browse files Browse the repository at this point in the history
Automatically create end elements.
  • Loading branch information
denniserdmann authored Jun 3, 2019
2 parents 0c316b8 + 87831a9 commit c386cda
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
46 changes: 46 additions & 0 deletions classes/tl_content_extended.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,50 @@ public function addCteType($arrRow)
return $return;
}

public function onsubmitCallback(\DataContainer $dc)
{
if (!in_array($dc->activeRecord->type, array('rowStart', 'colStart'))) {
return;
}

if ('auto' !== \Contao\Input::post('SUBMIT_TYPE') && $this->siblingStopElmentIsMissing(
$dc->activeRecord->pid,
$dc->activeRecord->ptable,
$dc->activeRecord->sorting,
substr($dc->activeRecord->type, 0, 3)
)) {
$data = $dc->activeRecord->row();
unset($data['id']);
$data['type'] = str_replace('Start', 'End', $dc->activeRecord->type);
$data['sorting'] += 1;

$newElement = new \Contao\ContentModel();
$newElement->setRow($data);
$newElement->save();
}
}

private function siblingStopElmentIsMissing($pid, $ptable, $sorting, $rowOrCol)
{
if (!in_array($rowOrCol, array('row', 'col'))) {
throw new InvalidArgumentException('Argument $rowOrCol must be either "row" or "col"');
}
$statement = \Contao\Database::getInstance()
->prepare(
'SELECT * FROM tl_content WHERE pid=? AND ptable=? AND sorting>? AND type IN("' . $rowOrCol . 'Start", "'
. $rowOrCol . 'End") ORDER BY sorting'
)
->limit(1)
->execute($pid, $ptable, $sorting);

if (false === $row = $statement->fetchAssoc()) {
return true;
}

if ($rowOrCol . 'End' !== $row['type']) {
return true;
}

return false;
}
}
2 changes: 2 additions & 0 deletions dca/tl_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
$GLOBALS['TL_DCA']['tl_content']['list']['sorting']['child_record_callback'] = array('tl_content_extended', 'addCteType');

$GLOBALS['TL_DCA']['tl_content']['config']['onsubmit_callback'][] = array('tl_content_extended', 'onsubmitCallback');


/*
* Palettes
Expand Down

0 comments on commit c386cda

Please sign in to comment.