Commit 5d9385d6 authored by Son Nguyen's avatar Son Nguyen Committed by Gerrit Code Review
Browse files

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



This will update
 - the block content which have links to these embedded images
 - the tables 'artefact_file_embedded' and 'view_artefact' in database

Change-Id: I7b97e0828fd89609f9bd43e786c5ff5ad3e53156
Signed-off-by: default avatarSon Nguyen <son.nguyen@catalyst.net.nz>
parent 2cd4ec57
......@@ -903,7 +903,7 @@ class ArtefactTypeHtml 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 note.
// 1. Attach copies of the files that were attached to the old note.
if (isset($artefactcopies[$oldid]->oldattachments)) {
foreach ($artefactcopies[$oldid]->oldattachments as $a) {
if (isset($artefactcopies[$a])) {
......@@ -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 {
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) {
// If artefact attachment table has 'item' column utilised.
if ($this->can_have_attachments()) {
......
......@@ -1386,6 +1386,7 @@ class BlockInstance {
// We need the artefact instance before we can get its attachments
$tocopy = array();
$attachmentlists = array();
$embedlists = array();
foreach ($descendants as $d) {
if (!isset($artefactcopies[$d])) {
$tocopy[$d] = artefact_instance_from_id($d);
......@@ -1396,6 +1397,13 @@ class BlockInstance {
$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 {
if (!empty($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'));
}
......@@ -1413,7 +1424,7 @@ class BlockInstance {
if (isset($configdata['artefactid'])) {
$configdata['artefactid'] = $artefactcopies[$configdata['artefactid']]->newid;
}
else {
if (isset($configdata['artefactids'])) {
foreach ($configdata['artefactids'] as &$oldid) {
$oldid = $artefactcopies[$oldid]->newid;
}
......
......@@ -435,6 +435,10 @@ class View {
$blocks = get_records_array('block_instance', 'view', $view->get('id'));
if ($blocks) {
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);
if (!isset($configdata['artefactid'])) {
continue;
......
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