Skip to content

Commit 8e0a9e4

Browse files
committed
Added copy callback for article, fixed wrong sh5_pid update when saving html5End elements
1 parent 2b082c0 commit 8e0a9e4

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

contao/dca/tl_article.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* Contao Open Source CMS
5+
* Copyright (c) 2005-2015 Leo Feyer
6+
*
7+
* @package semantic_html5
8+
* @copyright MEN AT WORK 2016
9+
* @author David Maack <david.maack@arcor.de>
10+
* @license LGPL-3.0+
11+
*/
12+
13+
/**
14+
* Callbacks
15+
*/
16+
$GLOBALS['TL_DCA']['tl_article']['config']['oncopy_callback'][] = array('SemanticHTML5\Backend\Callbacks', 'oncopyArticleCallback');

src/Backend/Callbacks.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ public static function ondeleteCallback(\DataContainer $dc, $id)
9191
}
9292
}
9393

94+
/**
95+
* This methods corrects the hml5-elements after using the copy function of
96+
* the tl_article table
97+
*
98+
* @param type $id
99+
* @param \DataContainer $dc
100+
*/
101+
public static function oncopyArticleCallback($id, \DataContainer $dc)
102+
{
103+
104+
//fetch all html5 start elemnts and update them the end elements will be corrected automatically
105+
$elements = \Database::getInstance()
106+
->prepare('SELECT * FROM tl_content WHERE type = "sHtml5Start" AND pid = ?')
107+
->execute($id);
108+
109+
//return if no elements were found
110+
if ($elements->numRows == 0) {
111+
return;
112+
}
113+
114+
$util = new TagUtils('tl_content');
115+
116+
while ($elements->next()) {
117+
$util->createOrUpdateCorresppondingTag($elements, true);
118+
}
119+
}
120+
94121
/**
95122
* This methods corrects the hml5-elements after using the copy function of
96123
* the tl_content table

src/Backend/TagUtils.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@ public function __construct($table) {
4646
* @param Result The DB-Result of the element to update
4747
* @return NULL or the id of the new element
4848
*/
49-
public function createOrUpdateCorresppondingTag(Result $item)
49+
public function createOrUpdateCorresppondingTag(Result $item, $fixSh5Pid = false)
5050
{
5151
$cTags = $this->getcorrespondingTag($item);
5252

53+
//correct the sh5_pid if needed and the update flag is set
54+
if ($fixSh5Pid && $item->type == 'sHtml5Start' && $item->id != $item->sh5_pid) {
55+
56+
$data = array('sh5_pid' => $item->id);
57+
$item = $this->updateTag($item->id, $data);
58+
59+
}
60+
5361
if ($cTags == null) {
5462
//create a new tag
5563
$data = $item->row();
@@ -59,7 +67,7 @@ public function createOrUpdateCorresppondingTag(Result $item)
5967
//update the sh5_pid for end tags
6068
if ($item->type == 'sHtml5End') {
6169
$data = array('sh5_pid' => $newId);
62-
$this->updateTag($item->id, $data);
70+
$item = $this->updateTag($item->id, $data);
6371
}
6472

6573
return $newId;
@@ -73,7 +81,7 @@ public function createOrUpdateCorresppondingTag(Result $item)
7381

7482
//set the new data
7583
$data = array(
76-
'sh5_pid' => $item->id,
84+
'sh5_pid' => ($cTags->type == 'sHtml5Start') ? $item->sh5_pid : $item->id,
7785
'sh5_type' => $item->sh5_type
7886
);
7987

0 commit comments

Comments
 (0)