Commit 0544da18 authored by Aaron Wells's avatar Aaron Wells Committed by Robert Lyon

Rewrite the navigation block's collection pointer, after leap2a import

Bug 1333424

Change-Id: I6ab58a154513fbe3185be91f251c5c1aa213a7a1
(cherry picked from commit 3658fa88)
parent ca583b9f
......@@ -260,6 +260,23 @@ abstract class PluginBlocktype extends Plugin implements IPluginBlocktype {
return $configdata;
}
/**
* Rewrite a block instance's relationships to views & collections at the end of the leap import process.
*
* (For instance the navigation block stores a collection ID, and needs to know the new ID the
* collection wound up with.)
*
* This method is called at the end of the import process. You will probably want to access the
* $importer->viewids, $importer->collectionids, and/or $importer->artefactids fields
*
* @param int $blockinstanceid ID of the block instance.
* @param PluginImportLeap $importer The importer object.
*/
public static function import_rewrite_blockinstance_relationships_leap($blockinstanceid, $importer) {
// Do nothing, in the default case
}
/*
* 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.
......
......@@ -33,7 +33,7 @@ class PluginBlocktypeNavigation extends SystemBlocktype {
$configdata = $bi->get('configdata');
if (!empty($configdata['collection'])) {
return $bi->get_data('collection', $configdata['collection'])->get('name');
return $bi->get_data('collection', (int) $configdata['collection'])->get('name');
}
return '';
}
......@@ -43,7 +43,7 @@ class PluginBlocktypeNavigation extends SystemBlocktype {
$smarty = smarty_core();
if (!empty($configdata['collection'])) {
$views = $instance->get_data('collection', $configdata['collection'])->views();
$views = $instance->get_data('collection', (int) $configdata['collection'])->views();
if (!empty($views)) {
$smarty->assign('views', $views['views']);
}
......@@ -129,4 +129,60 @@ class PluginBlocktypeNavigation extends SystemBlocktype {
return 'full';
}
/**
* Change the collection ID format to match the ID format we use in Leap2A,
* e.g.: portfolio:collection23
*
* @param BlockInstance $bi The blockinstance to export the config for.
* @return array The config for the blockinstance
*/
public static function export_blockinstance_config_leap(BlockInstance $bi) {
$jsonconfigdata = parent::export_blockinstance_config_leap($bi);
if (isset($jsonconfigdata['collection'])) {
// It should be a collection...
$collection = json_decode($jsonconfigdata['collection']);
if (is_array($collection)) {
$collection = $collection[0];
}
$jsonconfigdata['collection'] = json_encode(array('portfolio:collection' . (int) $collection));
}
return $jsonconfigdata;
}
/**
* After a leap2a import, rewrite the block instance's collection ID to the collection's new ID.
* (If the collection was part of this import. If it's not, just remove it.)
*
* @param int $blockinstanceid
* @param PluginLeapImport $importer
*/
public static function import_rewrite_blockinstance_relationships_leap($blockinstanceid, $importer) {
$bi = new BlockInstance($blockinstanceid);
$configdata = $bi->get('configdata');
// Rewrite the collection ID from the old one to the new one.
if (isset($configdata['collection'])) {
$oldcollectionid = $configdata['collection'];
// Backwards-compatibility for Leap2a files before we started rewriting the collection ID
if (strpos($oldcollectionid, 'portfolio:collection') !== 0) {
$oldcollectionid = 'portfolio:collection' . (int) $oldcollectionid;
}
if (isset($importer->collectionids[$oldcollectionid])) {
// If the collection was present in this import, point to its new ID
$configdata['collection'] = $importer->collectionids[$oldcollectionid];
}
else {
// If the collection was not present, then deactivate this block
// TODO: Make some guesses about what it should point at?
unset($configdata['collection']);
}
}
$bi->set('configdata', $configdata);
$bi->commit();
}
}
......@@ -24,10 +24,10 @@ class PluginImportLeap extends PluginImport {
private $strategylisting = array();
private $loadmapping = array();
private $coreloadmapping = array();
private $artefactids = array();
private $viewids = array();
private $collectionids = array();
private $collectionviewentries = array();
public $artefactids = array();
public $viewids = array();
public $collectionids = array();
public $collectionviewentries = array();
protected $filename;
protected $persondataid = null;
......@@ -337,8 +337,12 @@ class PluginImportLeap extends PluginImport {
}
}
}
// Allow each plugin to load relationships to views if they need to
// Allow each artefact plugin to load relationships to views if they need to
$this->call_import_method_plugins('setup_view_relationships_from_requests');
// Allow each blocktype plugin to load relationships to views if they need to
$this->rewrite_blockinstance_relationships();
$this->import_completed();
$this->delete_import_entry_requests();
......@@ -742,6 +746,9 @@ class PluginImportLeap extends PluginImport {
$entry, $this, $strategydata['strategy'], $strategydata['other_required_entries']);
}
}
// Allow each blocktype plugin to load relationships to views if they need to
$this->rewrite_blockinstance_relationships();
}
public function entry_has_strategy($entryid, $strategyid, $artefactplugin=null) {
......@@ -1493,6 +1500,29 @@ class PluginImportLeap extends PluginImport {
return $config;
}
/**
* This method is called late in the import process, after views, collections, and artefacts have been set up, to give collections the opportunity
* to rewrite any references they have to old view, collection, or artefact IDs.
*
* Blocktypes that use this API should define an "import_rewrite_blockinstance_relationships_leap" method.
*/
private function rewrite_blockinstance_relationships() {
foreach($this->viewids as $entryid => $viewid) {
$records = get_records_array('block_instance', 'view', $viewid, 'view, id');
if ($records) {
foreach ($records as $blockrec) {
// Let blocktype plugin rewrite relationships now that all views and collections are set up
safe_require('blocktype', $blockrec->blocktype);
$classname = generate_class_name('blocktype', $blockrec->blocktype);
$method = 'import_rewrite_blockinstance_relationships_leap';
$blockinstance['config'] = call_static_method($classname, $method, $blockrec->id, $this);
}
}
}
}
/**
* Given an artefact record, looks through it for any Leap2A style
* references to other artefacts, and rewrite those to point at the created
......
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