Commit 393e1608 authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review
Browse files

Merge "Update embed images in 'Note' blocks when copying. Bug 1424284"

parents ac276c78 5d9385d6
...@@ -903,7 +903,7 @@ class ArtefactTypeHtml extends ArtefactType { ...@@ -903,7 +903,7 @@ class ArtefactTypeHtml extends ArtefactType {
public function update_artefact_references(&$view, &$template, &$artefactcopies, $oldid) { public function update_artefact_references(&$view, &$template, &$artefactcopies, $oldid) {
parent::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 note. // 1. Attach copies of the files that were attached to the old note.
if (isset($artefactcopies[$oldid]->oldattachments)) { if (isset($artefactcopies[$oldid]->oldattachments)) {
foreach ($artefactcopies[$oldid]->oldattachments as $a) { foreach ($artefactcopies[$oldid]->oldattachments as $a) {
if (isset($artefactcopies[$a])) { if (isset($artefactcopies[$a])) {
...@@ -911,6 +911,26 @@ class ArtefactTypeHtml extends ArtefactType { ...@@ -911,6 +911,26 @@ class ArtefactTypeHtml extends ArtefactType {
} }
} }
} }
// 2. Update embedded images in the note and db
$regexp = array();
$replacetext = array();
if (!empty($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 . '&embedded=1([^"]+)"#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '&embedded=1"';
}
}
require_once('embeddedimage.php');
$newdescription = EmbeddedImage::prepare_embedded_images(
preg_replace($regexp, $replacetext, $this->get('description')),
'textbox',
$this->get('id'),
$view->get('group')
);
$this->set('description', $newdescription);
}
} }
} }
......
...@@ -1112,6 +1112,21 @@ abstract class ArtefactType implements IArtefactType { ...@@ -1112,6 +1112,21 @@ abstract class ArtefactType implements IArtefactType {
return array(); return array();
} }
/**
* Returns a list of embedded image artefact ids
* This function is called when copying a view
*
* @return array
*/
public function embed_id_list() {
if ($this->can_have_attachments()) {
if ($list = get_column('artefact_file_embedded', 'fileid', 'resourceid', $this->get('id'))) {
return $list;
}
}
return array();
}
public function attachment_id_list_with_item($itemid) { public function attachment_id_list_with_item($itemid) {
// If artefact attachment table has 'item' column utilised. // If artefact attachment table has 'item' column utilised.
if ($this->can_have_attachments()) { if ($this->can_have_attachments()) {
......
...@@ -1386,6 +1386,7 @@ class BlockInstance { ...@@ -1386,6 +1386,7 @@ class BlockInstance {
// We need the artefact instance before we can get its attachments // We need the artefact instance before we can get its attachments
$tocopy = array(); $tocopy = array();
$attachmentlists = array(); $attachmentlists = array();
$embedlists = array();
foreach ($descendants as $d) { foreach ($descendants as $d) {
if (!isset($artefactcopies[$d])) { if (!isset($artefactcopies[$d])) {
$tocopy[$d] = artefact_instance_from_id($d); $tocopy[$d] = artefact_instance_from_id($d);
...@@ -1396,6 +1397,13 @@ class BlockInstance { ...@@ -1396,6 +1397,13 @@ class BlockInstance {
$tocopy[$a] = artefact_instance_from_id($a); $tocopy[$a] = artefact_instance_from_id($a);
} }
} }
// Get embedded file artefacts
$embedlists[$d] = $tocopy[$d]->embed_id_list();
foreach ($embedlists[$d] as $a) {
if (!isset($artefactcopies[$a]) && !isset($tocopy[$a])) {
$tocopy[$a] = artefact_instance_from_id($a);
}
}
} }
} }
...@@ -1406,6 +1414,9 @@ class BlockInstance { ...@@ -1406,6 +1414,9 @@ class BlockInstance {
if (!empty($attachmentlists[$aid])) { if (!empty($attachmentlists[$aid])) {
$artefactcopies[$aid]->oldattachments = $attachmentlists[$aid]; $artefactcopies[$aid]->oldattachments = $attachmentlists[$aid];
} }
if (!empty($embedlists[$aid])) {
$artefactcopies[$aid]->oldembeds= $embedlists[$aid];
}
$artefactcopies[$aid]->newid = $a->copy_for_new_owner($view->get('owner'), $view->get('group'), $view->get('institution')); $artefactcopies[$aid]->newid = $a->copy_for_new_owner($view->get('owner'), $view->get('group'), $view->get('institution'));
} }
...@@ -1413,7 +1424,7 @@ class BlockInstance { ...@@ -1413,7 +1424,7 @@ class BlockInstance {
if (isset($configdata['artefactid'])) { if (isset($configdata['artefactid'])) {
$configdata['artefactid'] = $artefactcopies[$configdata['artefactid']]->newid; $configdata['artefactid'] = $artefactcopies[$configdata['artefactid']]->newid;
} }
else { if (isset($configdata['artefactids'])) {
foreach ($configdata['artefactids'] as &$oldid) { foreach ($configdata['artefactids'] as &$oldid) {
$oldid = $artefactcopies[$oldid]->newid; $oldid = $artefactcopies[$oldid]->newid;
} }
......
...@@ -435,6 +435,10 @@ class View { ...@@ -435,6 +435,10 @@ class View {
$blocks = get_records_array('block_instance', 'view', $view->get('id')); $blocks = get_records_array('block_instance', 'view', $view->get('id'));
if ($blocks) { if ($blocks) {
foreach ($blocks as $b) { foreach ($blocks as $b) {
// As some artefact references have been changed, e.g embedded images
// we need to rebuild the artefact list for each block
$bi = new BlockInstance($b->id);
$bi->rebuild_artefact_list();
$configdata = unserialize($b->configdata); $configdata = unserialize($b->configdata);
if (!isset($configdata['artefactid'])) { if (!isset($configdata['artefactid'])) {
continue; continue;
......
Supports Markdown
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