Commit d1d0d59d authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Move block copying stuff into BlockInstance class

parent 29ee9888
......@@ -140,11 +140,11 @@ class PluginBlocktypeContactinfo extends PluginBlocktype {
return '';
}
public static function copy_allowed($newowner=null) {
return $newowner['type'] == 'user';
public static function copy_allowed($ownertype=null) {
return $ownertype == 'user';
}
public static function copy_artefacts_allowed($newowner=null) {
public static function copy_artefacts_allowed($ownertype=null) {
return false;
}
......
......@@ -201,11 +201,11 @@ class PluginBlocktypeProfileinfo extends PluginBlocktype {
return '';
}
public static function copy_allowed($newowner=null) {
return $newowner['type'] == 'user';
public static function copy_allowed($ownertype=null) {
return $ownertype == 'user';
}
public static function copy_artefacts_allowed($newowner=null) {
public static function copy_artefacts_allowed($ownertype=null) {
return false;
}
......
......@@ -129,8 +129,8 @@ class PluginBlocktypeEntireresume extends PluginBlocktype {
}
}
public static function copy_allowed($newowner=null) {
return $newowner['type'] == 'user';
public static function copy_allowed($ownertype=null) {
return $ownertype == 'user';
}
public static function copy_artefacts_allowed($newowner=null) {
......
......@@ -124,8 +124,8 @@ class PluginBlocktypeResumefield extends PluginBlocktype {
return '';
}
public static function copy_allowed($newowner=null) {
return $newowner['type'] == 'user';
public static function copy_allowed($ownertype=null) {
return $ownertype == 'user';
}
public static function copy_artefacts_allowed($newowner=null) {
......
......@@ -156,11 +156,11 @@ abstract class PluginBlocktype extends Plugin {
return $blocktypes;
}
public static function copy_allowed($newowner=null) {
public static function copy_allowed($ownertype=null) {
return true;
}
public static function copy_artefacts_allowed($newowner=null) {
public static function copy_artefacts_allowed($ownertype=null) {
return true;
}
......@@ -671,12 +671,63 @@ class BlockInstance {
return $a;
}
public function copy_allowed($newowner=null) {
return call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'copy_allowed', $newowner);
public function copy_allowed($ownertype=null) {
return call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'copy_allowed', $ownertype);
}
public function copy_artefacts_allowed($newowner=null) {
return call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'copy_artefacts_allowed', $newowner);
public function copy_artefacts_allowed($ownertype=null) {
return call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'copy_artefacts_allowed', $ownertype);
}
public function set_default_configdata() {
return;
}
public function copy(&$view, &$artefactcopies) {
$copyconfig = $view->get('copyconfig');
if (!$copyconfig->sameowner && !$this->copy_allowed($copyconfig->ownertype)) {
return false;
}
$newblock = new BlockInstance(0, array(
'blocktype' => $this->get('blocktype'),
'title' => $this->get('title'),
'view' => $view->get('id'),
'column' => $this->get('column'),
'order' => $this->get('order'),
));
$artefactids = PluginBlockType::get_artefacts($this);
if (!$copyconfig->sameowner && !empty($artefactids) && $this->copy_artefacts_allowed($copyconfig->ownertype)) {
$configdata = $this->get('configdata');
// Copy artefacts & put the new artefact ids into the new
// block.
// Get all children of the artefacts.
$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'));
}
}
if (isset($configdata['artefactid'])) {
$configdata['artefactid'] = $artefactcopies[$configdata['artefactid']]->newid;
}
else {
foreach ($configdata['artefactids'] as &$oldid) {
$oldid = $artefactcopies[$oldid]->newid;
}
}
$newblock->set('configdata', $configdata);
}
else {
$newblock->set_default_configdata();
}
$newblock->commit();
return true;
}
}
......
......@@ -58,6 +58,7 @@ class View {
private $template;
private $copynewuser = 0;
private $copynewgroups;
private $copyconfig;
public function __construct($id=0, $data=null) {
if (!empty($id)) {
......@@ -1851,54 +1852,15 @@ class View {
if ($blocks) {
$newowner = $this->ownership();
$oldowner = $template->ownership();
$sameowner = $newowner['type'] == $oldowner['type'] && $newowner['id'] == $oldowner['id'];
$this->set('copyconfig', (object) array(
'ownertype' => $newowner['type'],
'sameowner' => $newowner['type'] == $oldowner['type'] && $newowner['id'] == $oldowner['id'],
));
$artefactcopies = array(); // Correspondence between original artefact ids and id of the copy
foreach ($blocks as $b) {
safe_require('blocktype', $b->blocktype);
$oldblock = new BlockInstance($b->id, $b);
if ($sameowner || $oldblock->copy_allowed($newowner)) {
$newblock = new BlockInstance(0, array(
'blocktype' => $oldblock->get('blocktype'),
'title' => $oldblock->get('title'),
'view' => $this->get('id'),
'column' => $oldblock->get('column'),
'order' => $oldblock->get('order'),
));
$configdata = $oldblock->get('configdata');
$artefactids = PluginBlockType::get_artefacts($oldblock);
if (!$sameowner && !empty($artefactids)) {
if ($oldblock->copy_artefacts_allowed($newowner)) {
// Copy artefacts & put the new artefact
// ids into the new block
// Get all children of the artefacts
$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($this->owner, $this->group, $this->institution);
}
}
if (isset($configdata['artefactid'])) {
$configdata['artefactid'] = $artefactcopies[$configdata['artefactid']]->newid;
}
else {
foreach ($configdata['artefactids'] as &$oldid) {
$oldid = $artefactcopies[$oldid]->newid;
}
}
}
else if (isset($configdata['artefactid'])) {
$configdata['artefactid'] = null;
}
else if (isset($configdata['artefactids'])) {
$configdata['artefactids'] = array();
}
}
$newblock->set('configdata', $configdata);
$newblock->commit();
if ($oldblock->copy($this, $artefactcopies)) {
$numcopied['blocks']++;
}
}
......
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