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

Don't lose attachments when there are errors in the form

parent 66f4e94e
......@@ -56,6 +56,7 @@ if (!$blogpost) {
$checked = '';
$pagetitle = get_string('newblogpost', 'artefact.blog', get_field('artefact', 'title', 'id', $blog));
$focuselement = 'title';
$attachments = array();
define('TITLE', $pagetitle);
}
else {
......@@ -68,6 +69,7 @@ else {
$checked = !$blogpostobj->get('published');
$pagetitle = get_string('editblogpost', 'artefact.blog');
$focuselement = 'description'; // Doesn't seem to work with tinyMCE.
$attachments = $blogpostobj->attachment_id_list();
define('TITLE', get_string('editblogpost','artefact.blog'));
}
......@@ -141,7 +143,8 @@ $form = pieform(array(
'edit' => false,
'select' => true,
),
'selectlistcallback' => 'load_attachments',
'defaultvalue' => $attachments,
'selectlistcallback' => 'artefact_get_records_by_id',
'selectcallback' => 'add_attachment',
'unselectcallback' => 'delete_attachment',
),
......@@ -392,14 +395,6 @@ function editpost_submit(Pieform $form, $values) {
$form->reply(PIEFORM_OK, $result);
}
function load_attachments() {
global $blogpostobj;
if ($blogpostobj) {
return $blogpostobj->get_attachments(true);
}
return array();
}
function add_attachment($attachmentid) {
global $blogpostobj;
if ($blogpostobj) {
......
......@@ -73,12 +73,21 @@ function pieform_element_filebrowser(Pieform $form, $element) {
$smarty->assign('groupinfo', pieform_element_filebrowser_get_groupinfo($group));
}
$formid = $form->get_name();
$prefix = $formid . '_' . $element['name'];
if ($config['select']) {
if (is_array($element['selectlistcallback'])) {
$selected = call_user_func_array($element['selectlistcallback']['name'], $element['selectlistcallback']['args']);
}
else {
$selected = $element['selectlistcallback']();
if (function_exists($element['selectlistcallback'])) {
if ($form->is_submitted() && $form->has_errors() && isset($_POST[$prefix . '_selected']) && is_array($_POST[$prefix . '_selected'])) {
$value = array_keys($_POST[$prefix . '_selected']);
}
else if (isset($element['defaultvalue'])) {
$value = $element['defaultvalue'];
}
else {
$value = null;
}
$selected = $element['selectlistcallback']($value);
}
$smarty->assign('selectedlist', $selected);
$selectedliststr = json_encode($selected);
......@@ -108,9 +117,6 @@ function pieform_element_filebrowser(Pieform $form, $element) {
$configstr = json_encode($config);
$fileliststr = json_encode($filedata);
$formid = $form->get_name();
$prefix = $formid . '_' . $element['name'];
$smarty->assign('prefix', $prefix);
$initjs = "{$prefix} = new FileBrowser('{$prefix}', {$folder}, {$configstr}, config);
......
......@@ -789,10 +789,8 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
'publishing' => true,
),
'tabs' => $instance->get_view()->ownership(),
'selectlistcallback' => array(
'name' => 'artefact_get_records_by_id',
'args' => array($default),
),
'defaultvalue' => $default,
'selectlistcallback' => 'artefact_get_records_by_id',
);
}
......
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