Commit de4e9ec3 authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review

Merge "Bug 1476059: Duplicate blog block's embedded images"

parents 799d57f6 e975c5ee
......@@ -273,6 +273,10 @@ class ArtefactTypeBlog extends ArtefactType {
public static function collapse_config() {
}
public function can_have_attachments() {
return true;
}
/**
* This function returns a list of the given user's blogs.
*
......@@ -458,6 +462,30 @@ class ArtefactTypeBlog extends ArtefactType {
));
}
public function update_artefact_references(&$view, &$template, &$artefactcopies, $oldid) {
parent::update_artefact_references($view, $template, $artefactcopies, $oldid);
// Update <img> tags in the blog description to refer to the new image artefacts.
$regexp = array();
$replacetext = array();
if (isset($artefactcopies[$oldid]->oldembeds)) {
foreach ($artefactcopies[$oldid]->oldembeds as $a) {
if (isset($artefactcopies[$a])) {
// Change the old image id to the new one
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '(&|&amp;)embedded=1(.*?"[^>]+)#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '$2embedded=1$3';
}
}
require_once('embeddedimage.php');
$newdescription = EmbeddedImage::prepare_embedded_images(
preg_replace($regexp, $replacetext, $this->get('description')),
'blog',
$this->get('id'),
$view->get('group')
);
$this->set('description', $newdescription);
}
}
/**
* During the copying of a view, we might be allowed to copy
* blogs. Users need to have multipleblogs enabled for these
......@@ -1027,19 +1055,33 @@ class ArtefactTypeBlogPost extends ArtefactType {
public function update_artefact_references(&$view, &$template, &$artefactcopies, $oldid) {
parent::update_artefact_references($view, $template, $artefactcopies, $oldid);
// Attach copies of the files that were attached to the old post.
// Update <img> tags in the post body to refer to the new image artefacts.
$regexp = array();
$replacetext = array();
// 1. Attach copies of the files that were attached to the old post.
if (isset($artefactcopies[$oldid]->oldattachments)) {
foreach ($artefactcopies[$oldid]->oldattachments as $a) {
if (isset($artefactcopies[$a])) {
$this->attach($artefactcopies[$a]->newid);
}
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '"#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '"';
}
$this->set('description', preg_replace($regexp, $replacetext, $this->get('description')));
}
// 2. Update embedded images in the post body field
$regexp = array();
$replacetext = array();
if (isset($artefactcopies[$oldid]->oldembeds)) {
foreach ($artefactcopies[$oldid]->oldembeds as $a) {
if (isset($artefactcopies[$a])) {
// Change the old image id to the new one
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '(&|&amp;)embedded=1(.*?"[^>]+)#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '$2embedded=1$3';
}
}
require_once('embeddedimage.php');
$newdescription = EmbeddedImage::prepare_embedded_images(
preg_replace($regexp, $replacetext, $this->get('description')),
'blogpost',
$this->get('id'),
$view->get('group')
);
$this->set('description', $newdescription);
}
}
......
......@@ -214,6 +214,7 @@ function editpost_cancel_submit() {
function editpost_submit(Pieform $form, $values) {
global $USER, $SESSION, $blogpost, $blog;
require_once('embeddedimage.php');
db_begin();
$postobj = new ArtefactTypeBlogPost($blogpost, null);
$postobj->set('title', $values['title']);
......@@ -233,6 +234,9 @@ function editpost_submit(Pieform $form, $values) {
$postobj->commit();
$blogpost = $postobj->get('id');
// Need to wait until post is saved in case we are a new blogpost before we can sort out embedded images as we need an id
$postobj->set('description', EmbeddedImage::prepare_embedded_images($values['description'], 'blogpost', $postobj->get('id')));
// Attachments
$old = $postobj->attachment_id_list();
// $new = is_array($values['filebrowser']['selected']) ? $values['filebrowser']['selected'] : array();
......
......@@ -918,8 +918,8 @@ class ArtefactTypeHtml extends ArtefactType {
foreach ($artefactcopies[$oldid]->oldembeds as $a) {
if (isset($artefactcopies[$a])) {
// Change the old image id to the new one
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '&embedded=1([^"]+)"#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '&embedded=1"';
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '(&|&amp;)embedded=1(.*?"[^>]+)#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '$2embedded=1$3';
}
}
require_once('embeddedimage.php');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment