Commit 8ad48ebc authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review
Browse files

Merge "Fix embedded images in 'Text' block when copy and import. Bug 1425728"

parents 7bc9838d 2b259a85
......@@ -325,6 +325,46 @@ abstract class PluginBlocktype extends Plugin implements IPluginBlocktype {
return $configdata;
}
/**
* Takes extra config data for an existing blockinstance of this class
* and rewrites it so it can be used to configure a new block instance being put
* in a new view
*
* This is used at view copy time, to give blocktypes the chance to change
* the extra configuration for a block based on aspects about the new view
*
* As an example - when the 'Text' blocktype is copied, we
* want it so that all image urls in the $configdata['text'] are
* pointing to the new images.
*
* @param View $view The view that the blocktype will be placed into (e.g.
* the View being created as a result of the copy)
* @param BlockInstance $block The new block
* @param array $configdata The configuration data for the old blocktype
* @param array $artefactcopies The mapping of old artefact ids to new ones
* @return array The new configuration data.
*/
public static function rewrite_blockinstance_extra_config(View $view, BlockInstance $block, $configdata, $artefactcopies) {
return $configdata;
}
/**
* Rewrite extra config data for a blockinstance of this class when
* importing its view from Leap
*
* As an example - when the 'text' blocktype is imported, we
* want all image urls in the $configdata['text'] are
* pointing to the new images.
*
* @param array $artefactids The mapping of leap entries to their artefact ID
* see more PluginImportLeap->artefactids
* @param array $configdata The imported configuration data for the blocktype
* @return array The new configuration data.
*/
public static function import_rewrite_blockinstance_extra_config_leap(array $artefactids, array $configdata) {
return $configdata;
}
/*
* The copy_type of a block affects how it should be copied when its view gets copied.
* nocopy: The block doesn't appear in the new view at all.
......@@ -1448,6 +1488,10 @@ class BlockInstance {
else {
$configdata = call_static_method($blocktypeclass, 'rewrite_blockinstance_config', $view, $configdata);
}
// Rewrite the extra configuration of block
$configdata = call_static_method($blocktypeclass, 'rewrite_blockinstance_extra_config', $view, $newblock, $configdata, $artefactcopies);
$newblock->set('configdata', $configdata);
$newblock->commit();
return true;
......
......@@ -113,6 +113,40 @@ class PluginBlocktypeText extends SystemBlocktype {
return $bi;
}
/**
* Rewrite extra config data for a Text blockinstance
*
* See more PluginBlocktype::import_rewrite_blockinstance_extra_config_leap()
*/
public static function import_rewrite_blockinstance_extra_config_leap(array $artefactids, array $configdata) {
// Find all possible embedded image artefact ids in the import configdata
$ids = array();
if (isset($configdata['text'])
&& preg_match_all(
'#<img([^>]+)src="' . get_config('wwwroot')
. 'artefact/file/download.php\?file=([0-9]+)&embedded=1([^"]+)"#',
$configdata['text'],
$ids)
) {
$ids = $ids[2];
$regexp = array();
$replacetext = array();
foreach ($ids as $id) {
if (!empty($artefactids["portfolio:artefact$id"])) {
// Change the old image id to the new one
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot')
. 'artefact/file/download.php\?file='
. $id . '&embedded=1([^"]+)"#';
$replacetext[] = '<img$1src="' . get_config('wwwroot')
. 'artefact/file/download.php?file='
. $artefactids["portfolio:artefact$id"][0] . '&embedded=1"';
}
}
$configdata['text'] = preg_replace($regexp, $replacetext, $configdata['text']);
}
return $configdata;
}
/**
* Set the text property of the block config so that exports can be imported
* into older versions.
......@@ -234,4 +268,21 @@ class PluginBlocktypeText extends SystemBlocktype {
db_commit();
$form->json_reply(PIEFORM_OK, get_string('convertibleokmessage', 'blocktype.text', $countconverted));
}
/**
* Rewrites embedded image urls in the $configdata['text']
*
* See more in PluginBlocktype::rewrite_blockinstance_extra_config()
*/
public static function rewrite_blockinstance_extra_config(View $view, BlockInstance $block, $configdata, $artefactcopies) {
$regexp = array();
$replacetext = array();
foreach ($artefactcopies as $copyobj) {
// Change the old image id to the new one
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $copyobj->oldid . '&embedded=1([^"]+)"#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $copyobj->newid . '&embedded=1"';
}
$configdata['text'] = preg_replace($regexp, $replacetext, $configdata['text']);
return $configdata;
}
}
......@@ -997,7 +997,7 @@ class PluginImportLeap extends PluginImport {
*/
public function import_view_from_request($entry_request) {
$viewdata = unserialize($entry_request->entrycontent);
$view = View::import_from_config($this->rewrite_artefact_ids($viewdata), $this->get('usr'), 'leap');
$view = View::import_from_config($this->rewrite_blockinstance_config($viewdata), $this->get('usr'), 'leap');
if (isset($viewdata->ctime)) {
$view->set('ctime', $viewdata->ctime);
......@@ -1376,7 +1376,7 @@ class PluginImportLeap extends PluginImport {
return false;
}
$view = View::import_from_config($this->rewrite_artefact_ids($config), $this->get('usr'), 'leap');
$view = View::import_from_config($this->rewrite_blockinstance_config($config), $this->get('usr'), 'leap');
if ($published = strtotime((string)$entry->published)) {
$view->set('ctime', $published);
......@@ -1421,10 +1421,13 @@ class PluginImportLeap extends PluginImport {
/**
* Given the view config that we have built from the export, rewrite all
* the entry references in blockinstance artefactid fields to be actual
* artefact IDs.
* the entry references in the artefactid field of blockinstance config
* and call PluginBlocktype::import_rewrite_blockinstance_extra_config() to
* rewrite extra config
* For example, the 'Text' blocktype will rewrite the embedded image urls
* which is stored in $blockinstance['config']['text']
*/
private function rewrite_artefact_ids($config) {
private function rewrite_blockinstance_config($config) {
foreach ($config['rows'] as &$row) {
foreach ($row['columns'] as &$column) {
foreach ($column as &$blockinstance) {
......@@ -1464,6 +1467,12 @@ class PluginImportLeap extends PluginImport {
}
}
}
// Let blocktype plugin rewrite extra config
safe_require('blocktype', $blockinstance['type']);
$classname = generate_class_name('blocktype', $blockinstance['type']);
$method = 'import_rewrite_blockinstance_extra_config_leap';
$blockinstance['config'] = call_static_method($classname, $method, $this->artefactids, $blockinstance['config']);
}
} // cols
} // rows
......
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