diff --git a/classes/tl_content_extended.php b/classes/tl_content_extended.php index e91d792..4e760ff 100644 --- a/classes/tl_content_extended.php +++ b/classes/tl_content_extended.php @@ -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; + } } diff --git a/dca/tl_content.php b/dca/tl_content.php index db6cdf7..3cd6846 100755 --- a/dca/tl_content.php +++ b/dca/tl_content.php @@ -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