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

Set up some default artefacts in profile blocks when copying from a template

parent d1d0d59d
......@@ -148,6 +148,20 @@ class PluginBlocktypeContactinfo extends PluginBlocktype {
return false;
}
public static function default_artefact_config($ownertype=null, $ownerid=null, $configdata) {
if ($ownertype == 'user') {
$artefacttypes = array_diff(PluginArtefactInternal::get_contactinfo_artefact_types(), array('email'));
$artefactids = get_column_sql('
SELECT id FROM {artefact}
WHERE owner = ? AND artefacttype IN (' . join(',', array_map('db_quote', $artefacttypes)) . ')', array($ownerid));
$configdata['artefactids'] = $artefactids;
}
else {
$configdata['artefactids'] = array();
}
return $configdata;
}
}
?>
......@@ -209,6 +209,20 @@ class PluginBlocktypeProfileinfo extends PluginBlocktype {
return false;
}
public static function default_artefact_config($ownertype=null, $ownerid=null, $configdata) {
if ($ownertype == 'user') {
$artefacttypes = array_diff(PluginArtefactInternal::get_artefact_types(), array('profileicon', 'email'));
$artefactids = get_column_sql('
SELECT id FROM {artefact}
WHERE owner = ? AND artefacttype IN (' . join(',', array_map('db_quote', $artefacttypes)) . ')', array($ownerid));
$configdata['artefactids'] = $artefactids;
}
else {
$configdata['artefactids'] = array();
}
return $configdata;
}
}
?>
......@@ -132,6 +132,21 @@ class PluginBlocktypeResumefield extends PluginBlocktype {
return false;
}
public static function default_artefact_config($ownertype=null, $ownerid=null, $configdata) {
$artefactid = null;
if ($ownertype == 'user') {
if (!empty($configdata['artefactid'])) {
$artefacttype = get_field('artefact', 'artefacttype', 'id', $configdata['artefactid']);
}
// @todo get artefacttype from a different field when copying from institution or group view.
if ($artefacttype) {
$artefactid = get_field('artefact', 'id', 'artefacttype', $artefacttype, 'owner', $ownerid);
}
}
$configdata['artefactid'] = $artefactid;
return $configdata;
}
}
?>
......@@ -164,6 +164,16 @@ abstract class PluginBlocktype extends Plugin {
return true;
}
public static function default_artefact_config($ownertype=null, $ownerid=null, $configdata) {
if (isset($configdata['artefactid'])) {
$configdata['artefactid'] = null;
}
if (isset($configdata['artefactids'])) {
$configdata['artefactids'] = array();
}
return $configdata;
}
}
abstract class SystemBlockType extends PluginBlockType {
......@@ -671,21 +681,12 @@ class BlockInstance {
return $a;
}
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($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) {
$blocktypeclass = generate_class_name('blocktype', $this->get('blocktype'));
$copyconfig = $view->get('copyconfig');
if (!$copyconfig->sameowner && !$this->copy_allowed($copyconfig->ownertype)) {
if (!$copyconfig->sameowner && !call_static_method($blocktypeclass, 'copy_allowed', $copyconfig->ownertype)) {
return false;
}
$newblock = new BlockInstance(0, array(
......@@ -696,9 +697,14 @@ class BlockInstance {
'order' => $this->get('order'),
));
$configdata = $this->get('configdata');
if ($copyconfig->sameowner) {
$newblock->set('configdata', $configdata);
$newblock->commit();
return true;
}
$artefactids = PluginBlockType::get_artefacts($this);
if (!$copyconfig->sameowner && !empty($artefactids) && $this->copy_artefacts_allowed($copyconfig->ownertype)) {
$configdata = $this->get('configdata');
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.
......@@ -720,12 +726,11 @@ class BlockInstance {
$oldid = $artefactcopies[$oldid]->newid;
}
}
$newblock->set('configdata', $configdata);
}
else {
$newblock->set_default_configdata();
$configdata = call_static_method($blocktypeclass, 'default_artefact_config', $copyconfig->ownertype, $copyconfig->ownerid, $configdata);
}
$newblock->set('configdata', $configdata);
$newblock->commit();
return true;
}
......
......@@ -1854,6 +1854,7 @@ class View {
$oldowner = $template->ownership();
$this->set('copyconfig', (object) array(
'ownertype' => $newowner['type'],
'ownerid' => $newowner['id'],
'sameowner' => $newowner['type'] == $oldowner['type'] && $newowner['id'] == $oldowner['id'],
));
$artefactcopies = array(); // Correspondence between original artefact ids and id of the copy
......
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