Commit 6339949c authored by Penny Leach's avatar Penny Leach
Browse files

leap2a #528423 make the blog importer import attached files



This fixes the problem that just linked files (rather than linked file
entries) were not being imported.
Signed-off-by: default avatarPenny Leach <penny@mjollnir.org>
parent 2f7e57f6
......@@ -165,6 +165,7 @@ class LeapImportBlog extends LeapImportArtefactPlugin {
* entry having out-of-line content), we attach that file to the entry.
*/
public static function setup_relationships(SimpleXMLElement $entry, PluginImportLeap $importer, $strategy, array $otherentries) {
$newartefactmapping = array();
switch ($strategy) {
case self::STRATEGY_IMPORT_AS_BLOG:
foreach ($otherentries as $entryid) {
......@@ -184,6 +185,11 @@ class LeapImportBlog extends LeapImportArtefactPlugin {
$artefactids = $importer->get_artefactids_imported_by_entryid((string)$blogpostlink['href']);
if (isset($artefactids[0])) {
$blogpost->attach($artefactids[0]);
} else { // it may be just an attached file, with no leap2a element in its own right ....
if ($id = self::attach_linked_file($blogpostentry, $blogpostlink, $importer)) {
$blogpost->attach($id);
$newartefactmapping[(string)$entry->id][] = $id;
}
}
}
if ($blogpost) {
......@@ -200,6 +206,33 @@ class LeapImportBlog extends LeapImportArtefactPlugin {
default:
throw new ImportException($importer, 'TODO: get_string: unknown strategy chosen for importing entry');
}
return $newartefactmapping;
}
/**
* Attaches a file to a blogpost entry that was just linked directly, rather than having a leap2a entry
* See http://wiki.cetis.ac.uk/LEAP2A_relationships#Attachments
*
* @param SimpleXMLElement $blogpostentry
* @param SimpleXMLElement $blogpostlink
* @param PluginImportLeap $importer
*/
private static function attach_linked_file($blogpostentry, $blogpostlink, PluginImportLeap $importer) {
$importer->trace($blogpostlink);
$pathname = urldecode((string)$blogpostlink['href']);
$dir = dirname($importer->get('filename'));
$pathname = $dir . DIRECTORY_SEPARATOR . $pathname;
if (!file_exists($pathname)) {
return false;
}
// Note: this data is passed (eventually) to ArtefactType->__construct,
// which calls strtotime on the dates for us
$data = (object)array(
'title' => (string)$blogpostentry->title . ' ' . get_string('attachment', 'artefact.blog'),
'owner' => $importer->get('usr'),
'filetype' => mime_content_type($pathname),
);
return ArtefactTypeFile::save_file($pathname, $data, $importer->get('usrobj'), true);
}
/**
......
......@@ -37,6 +37,7 @@ $string['alignment'] = 'Alignment';
$string['attach'] = 'Attach';
$string['attachedfilelistloaded'] = 'Attached file list loaded';
$string['attachedfiles'] = 'Attached files';
$string['attachment'] = 'Attachment';
$string['attachments'] = 'Attachments';
$string['blogcopiedfromanotherview'] = 'Note: This block has been copied from another View. You may move it around or remove it, but you cannot change what %s is in it.';
$string['blogdesc'] = 'Description';
......
......@@ -369,7 +369,7 @@ class PluginImportLeap extends PluginImport {
throw new SystemException("import_from_load_mapping(): $classname::import_using_strategy has not return a list");
}
$this->artefactids = array_merge($this->artefactids, $artefactmapping);
$this->artefactids = array_merge_recursive($this->artefactids, $artefactmapping);
$usedlist[] = $entryid;
if (isset($strategydata['other_required_entries'])) {
......@@ -400,11 +400,15 @@ class PluginImportLeap extends PluginImport {
$strategydata = $this->loadmapping[$entryid];
$classname = 'LeapImport' . ucfirst($strategydata['artefactplugin']);
$entry = $this->get_entry_by_id($entryid);
call_static_method($classname, 'setup_relationships',
$maybeartefacts = call_static_method($classname, 'setup_relationships',
$entry, $this, $strategydata['strategy'], $strategydata['other_required_entries']);
if (is_array($maybeartefacts)) { // some might add new artefacts (eg files attached by relpath, rather than leap id)
$this->artefactids = array_merge_recursive($this->artefactids, $maybeartefacts);
}
}
// Fix up any artefact references in the content of imported artefacts
// TODO: restrict this to the ones that were imported right now
if ($artefacts = get_records_array('artefact', 'owner', $this->get('usr'), '', 'id, title, description')) {
foreach ($artefacts as $artefact) {
$this->fix_artefact_references($artefact);
......
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