Commit b4cb838d authored by Richard Mansfield's avatar Richard Mansfield Committed by Richard Mansfield
Browse files

Allow copying of blogs when copying views with blog/recent post blocks

parent ec850509
......@@ -137,11 +137,11 @@ class PluginBlocktypeBlog extends PluginBlocktype {
//}
public static function copy_allowed($newowner=null) {
return false;
return true;
}
public static function copy_artefacts_allowed($newowner=null) {
return false;
return true;
}
}
......
......@@ -113,11 +113,11 @@ class PluginBlocktypeRecentposts extends PluginBlocktype {
}
public static function copy_allowed($newowner=null) {
return false;
return true;
}
public static function copy_artefacts_allowed($newowner=null) {
return false;
return true;
}
}
......
......@@ -762,6 +762,26 @@ class ArtefactTypeBlogPost extends ArtefactType {
'_default' => $wwwroot . 'artefact/blog/post.php?blogpost=' . $id,
);
}
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 post.
// Update <img> tags in the post body to refer to the new image artefacts.
$regexp = array();
$replacetext = array();
if (isset($artefactcopies[$oldid]->oldattachments)) {
foreach ($artefactcopies[$oldid]->oldattachments as $a) {
if (isset($artefactcopies[$a])) {
$this->attach_file($artefactcopies[$a]->newid);
}
$regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '"#';
$replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '"';
}
$this->set('description', preg_replace($regexp, $replacetext, $this->get('description')));
}
}
}
?>
......@@ -679,6 +679,44 @@ JAVASCRIPT;
return $path;
}
public function default_parent_for_copy(&$view, &$template) {
static $folderid;
if (!empty($folderid)) {
return $folderid;
}
$viewfilesfolder = ArtefactTypeFolder::get_folder_id(get_string('viewfilesdirname', 'view'), get_string('viewfilesdirdesc', 'view'),
null, true, $view->get('owner'), $view->get('group'), $view->get('institution'));
$foldername = $template->get('id');
$existing = get_column_sql("
SELECT title
FROM {artefact}
WHERE parent = ? AND title LIKE ? || '%'", array($viewfilesfolder, $foldername));
$sep = '';
$ext = '';
if ($existing) {
while (in_array($foldername . $sep . $ext, $existing)) {
$sep = '-';
$ext++;
}
}
$data = (object) array(
'title' => $foldername . $sep . $ext,
'description' => get_string('filescopiedfromviewtemplate', 'view', $template->get('title')),
'owner' => $view->get('owner'),
'group' => $view->get('group'),
'institution' => $view->get('institution'),
'parent' => $viewfilesfolder,
);
$folder = new ArtefactTypeFolder(0, $data);
$folder->commit();
$folderid = $folder->get('id');
return $folderid;
}
}
class ArtefactTypeFile extends ArtefactTypeFileBase {
......@@ -1009,22 +1047,21 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
}
public function copy_extra($new) {
$new->set('fileid', $this->get('fileid'));
$new->set('size', $this->get('size'));
$new->set('oldextension', $this->get('oldextension'));
$new->set('filetype', $this->get('filetype'));
global $USER;
if ($new->get('owner') && $new->get('owner') == $USER->get('id')) {
$USER->quota_add($new->get('size'));
$USER->commit();
}
return;
}
// Only changes to group files get put in the artefact_log table
public function can_be_logged() {
return (bool) $this->get('group');
}
public function default_parent_for_copy(&$view, &$template) {
return parent::default_parent_for_copy($view, $template);
}
}
class ArtefactTypeFolder extends ArtefactTypeFileBase {
......@@ -1202,6 +1239,9 @@ class ArtefactTypeFolder extends ArtefactTypeFileBase {
}
}
public function default_parent_for_copy(&$view, &$template) {
return parent::default_parent_for_copy($view, $template);
}
}
class ArtefactTypeImage extends ArtefactTypeFile {
......@@ -1309,6 +1349,10 @@ class ArtefactTypeImage extends ArtefactTypeFile {
. '" alt=""></div>' . $result['html'];
return $result;
}
public function default_parent_for_copy(&$view, &$template) {
return parent::default_parent_for_copy($view, $template);
}
}
class ArtefactTypeProfileIcon extends ArtefactTypeImage {
......@@ -1363,6 +1407,11 @@ class ArtefactTypeProfileIcon extends ArtefactTypeImage {
return filesize(get_config('dataroot') . 'artefact/file/profileicons/originals/'
. ($artefact % 256) . '/' . $artefact);
}
public function default_parent_for_copy(&$view, &$template) {
return null;
}
}
?>
......@@ -680,7 +680,6 @@ abstract class ArtefactType {
}
public function copy_extra($new) {
return;
}
public function copy_for_new_owner($user, $group, $institution) {
......@@ -697,6 +696,31 @@ abstract class ArtefactType {
return $copy->get('id');
}
/**
* Called after a view has been copied to rewrite all artefact
* references in the new artefact (which still point to the
* original artefacts) so that they also point to new artefacts
* that were copied during the view copy.
*
* @param View $view The newly copied view
* @param View $template The old view
* @param array $artefactcopies The mapping between old artefact ids and new ones (created in blockinstance copy)
* @param integer $oldid id of the artefact this artefact was copied from
*/
public function update_artefact_references(&$view, &$template, &$artefactcopies, $oldid) {
$copyinfo = $artefactcopies[$oldid];
if (isset($artefactcopies[$copyinfo->oldparent])) {
$this->set('parent', $artefactcopies[$copyinfo->oldparent]->newid);
}
else {
$this->set('parent', $this->default_parent_for_copy($view, $template));
}
}
public function default_parent_for_copy(&$view, &$template) {
return null;
}
public function can_be_logged() {
return false;
}
......@@ -733,6 +757,10 @@ abstract class ArtefactType {
insert_record('artefact_log', $entry);
}
}
public function attachment_id_list() {
return array();
}
}
/**
......
......@@ -739,19 +739,39 @@ class BlockInstance {
}
$artefactids = PluginBlockType::get_artefacts($this);
if (!empty($artefactids) && call_static_method($blocktypeclass, 'copy_artefacts_allowed', $copyconfig->ownertype)) {
// Copy artefacts & put the new artefact ids into the new
// block.
// Get all children of the artefacts.
// Copy artefacts & put the new artefact ids into the new block.
// Artefacts may have children (defined using the parent column of the artefact table) and attachments (currently
// only for blogposts). If we copy an artefact we must copy all its descendents & attachments too.
$descendants = artefact_get_descendants($artefactids);
foreach ($descendants as $aid) {
if (!isset($artefactcopies[$aid])) {
// Copy the artefact
$a = artefact_instance_from_id($aid);
// Save the id of the original artefact's parent
$artefactcopies[$aid] = (object) array('oldid' => $aid, 'oldparent' => $a->get('parent'));
$artefactcopies[$aid]->newid = $a->copy_for_new_owner($view->get('owner'), $view->get('group'), $view->get('institution'));
// We need the artefact instance before we can get its attachments
$tocopy = array();
$attachmentlists = array();
foreach ($descendants as $d) {
if (!isset($artefactcopies[$d])) {
$tocopy[$d] = artefact_instance_from_id($d);
// Get attachments.
$attachmentlists[$d] = $tocopy[$d]->attachment_id_list();
foreach ($attachmentlists[$d] as $a) {
if (!isset($artefactcopies[$a]) && !isset($tocopy[$a])) {
$tocopy[$a] = artefact_instance_from_id($a);
}
}
}
}
// Copy all the artefacts we haven't copied yet
foreach ($tocopy as $aid => $a) {
// Save the id of the original artefact's parent
$artefactcopies[$aid] = (object) array('oldid' => $aid, 'oldparent' => $a->get('parent'));
if (!empty($attachmentlists[$aid])) {
$artefactcopies[$aid]->oldattachments = $attachmentlists[$aid];
}
$artefactcopies[$aid]->newid = $a->copy_for_new_owner($view->get('owner'), $view->get('group'), $view->get('institution'));
}
// Record new artefact ids in the new block
if (isset($configdata['artefactid'])) {
$configdata['artefactid'] = $artefactcopies[$configdata['artefactid']]->newid;
}
......
......@@ -1965,46 +1965,13 @@ class View {
$numcopied['blocks']++;
}
}
// Go back and fix up the parents of the new artefacts so
// Go back and fix up artefact references in the new artefacts so
// they also point to new artefacts.
// Create parent folder for files that have no parent
if ($artefactcopies) {
safe_require('artefact', 'file');
$viewfilesfolder = ArtefactTypeFolder::get_folder_id(get_string('viewfilesdirname', 'view'), get_string('viewfilesdirdesc', 'view'),
null, true, $this->get('owner'), $this->get('group'), $this->get('institution'));
$foldername = $template->get('id');
$existing = get_column_sql("
SELECT title
FROM {artefact}
WHERE parent = ? AND title LIKE ? || '%'", array($viewfilesfolder, $foldername));
$sep = '';
$ext = '';
if ($existing) {
while (in_array($foldername . $sep . $ext, $existing)) {
$sep = '-';
$ext++;
}
}
$data = (object) array(
'title' => $foldername . $sep . $ext,
'description' => get_string('filescopiedfromviewtemplate', 'view', $template->get('title')),
'owner' => $this->get('owner'),
'group' => $this->get('group'),
'institution' => $this->get('institution'),
'parent' => $viewfilesfolder,
);
$folder = new ArtefactTypeFolder(0, $data);
$folder->commit();
foreach ($artefactcopies as $c) {
$a = artefact_instance_from_id($c->newid);
if (isset($artefactcopies[$c->oldparent])) {
$a->set('parent', $artefactcopies[$c->oldparent]->newid);
$a->commit();
}
else if (in_array($a->get('artefacttype'), array_diff(PluginArtefactFile::get_artefact_types(), array('profileicon')))) {
$a->set('parent', $folder->get('id'));
$a->commit();
}
foreach ($artefactcopies as $oldid => $copyinfo) {
$a = artefact_instance_from_id($copyinfo->newid);
$a->update_artefact_references($this, $template, $artefactcopies, $oldid);
$a->commit();
}
}
$numcopied['artefacts'] = count($artefactcopies);
......
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