Skip to content

Commit

Permalink
Added copy callback for article, fixed wrong sh5_pid update when savi…
Browse files Browse the repository at this point in the history
…ng html5End elements
  • Loading branch information
davidmaack committed Aug 17, 2016
1 parent 2b082c0 commit 8e0a9e4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
16 changes: 16 additions & 0 deletions contao/dca/tl_article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* Contao Open Source CMS
* Copyright (c) 2005-2015 Leo Feyer
*
* @package semantic_html5
* @copyright MEN AT WORK 2016
* @author David Maack <[email protected]>
* @license LGPL-3.0+
*/

/**
* Callbacks
*/
$GLOBALS['TL_DCA']['tl_article']['config']['oncopy_callback'][] = array('SemanticHTML5\Backend\Callbacks', 'oncopyArticleCallback');
27 changes: 27 additions & 0 deletions src/Backend/Callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ public static function ondeleteCallback(\DataContainer $dc, $id)
}
}

/**
* This methods corrects the hml5-elements after using the copy function of
* the tl_article table
*
* @param type $id
* @param \DataContainer $dc
*/
public static function oncopyArticleCallback($id, \DataContainer $dc)
{

//fetch all html5 start elemnts and update them the end elements will be corrected automatically
$elements = \Database::getInstance()
->prepare('SELECT * FROM tl_content WHERE type = "sHtml5Start" AND pid = ?')
->execute($id);

//return if no elements were found
if ($elements->numRows == 0) {
return;
}

$util = new TagUtils('tl_content');

while ($elements->next()) {
$util->createOrUpdateCorresppondingTag($elements, true);
}
}

/**
* This methods corrects the hml5-elements after using the copy function of
* the tl_content table
Expand Down
14 changes: 11 additions & 3 deletions src/Backend/TagUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@ public function __construct($table) {
* @param Result The DB-Result of the element to update
* @return NULL or the id of the new element
*/
public function createOrUpdateCorresppondingTag(Result $item)
public function createOrUpdateCorresppondingTag(Result $item, $fixSh5Pid = false)
{
$cTags = $this->getcorrespondingTag($item);

//correct the sh5_pid if needed and the update flag is set
if ($fixSh5Pid && $item->type == 'sHtml5Start' && $item->id != $item->sh5_pid) {

$data = array('sh5_pid' => $item->id);
$item = $this->updateTag($item->id, $data);

}

if ($cTags == null) {
//create a new tag
$data = $item->row();
Expand All @@ -59,7 +67,7 @@ public function createOrUpdateCorresppondingTag(Result $item)
//update the sh5_pid for end tags
if ($item->type == 'sHtml5End') {
$data = array('sh5_pid' => $newId);
$this->updateTag($item->id, $data);
$item = $this->updateTag($item->id, $data);
}

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

//set the new data
$data = array(
'sh5_pid' => $item->id,
'sh5_pid' => ($cTags->type == 'sHtml5Start') ? $item->sh5_pid : $item->id,
'sh5_type' => $item->sh5_type
);

Expand Down

0 comments on commit 8e0a9e4

Please sign in to comment.