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

Add attach callbacks for non-js files form; rearrange filebrowser submit functions

parent d2a9b67b
......@@ -130,6 +130,8 @@ $form = pieform(array(
'select' => true,
),
'selectlistcallback' => 'load_attachments',
'selectcallback' => 'add_attachment',
'unselectcallback' => 'delete_attachment',
),
'draft' => array(
'type' => 'checkbox',
......@@ -390,5 +392,19 @@ function load_attachments() {
}
return array();
}
function add_attachment($attachmentid) {
global $blogpostobj;
if ($blogpostobj) {
$blogpostobj->attach($attachmentid);
}
}
function delete_attachment($attachmentid) {
global $blogpostobj;
if ($blogpostobj) {
$blogpostobj->detach($attachmentid);
}
}
?>
......@@ -117,7 +117,11 @@ function pieform_element_filebrowser_get_path($folder) {
return $path;
}
function pieform_element_filebrowser_build_path($element, $folder) {
function pieform_element_filebrowser_build_path($form, $element, $folder) {
if (!$form->submitted_by_js()) {
return;
}
$path = pieform_element_filebrowser_get_path($folder);
$foldername = $path[0]->title;
......@@ -128,6 +132,11 @@ function pieform_element_filebrowser_build_path($element, $folder) {
}
function pieform_element_filebrowser_build_filelist($form, $element, $folder, $highlight=null) {
if (!$form->submitted_by_js()) {
// We're going to rebuild the page from scratch anyway.
return;
}
global $USER;
$group = $form->get_property('group');
......@@ -152,47 +161,59 @@ function pieform_element_filebrowser_build_filelist($form, $element, $folder, $h
function pieform_element_filebrowser_get_value(Pieform $form, $element) {
// This bypasses the rest of the submit process when we're not affecting
// the rest of the form
$result = pieform_element_filebrowser_doupdate($form, $element);
log_debug($_POST);
$value = array();
// Check if the user tried to make a change to the filebrowser element
if ($form->is_submitted()) {
// The following filebrowser update actions are only used when js is not available
if (!empty($_POST['edit']) && is_array($_POST['edit'])) {
// redirect
$value['action'] = 'edit';
$keys = array_keys($_POST['edit']);
$value['artefact'] = (int) ($keys[0]);
}
else if (!empty($_POST['canceledit'])) {
$value['action'] = 'cancel';
}
else if (!empty($_POST['select']) && is_array($_POST['select'])) {
$value['action'] = 'select';
$keys = array_keys($_POST['select']);
$value['artefact'] = (int) ($keys[0]);
}
else if (!empty($_POST['unselect']) && is_array($_POST['unselect'])) {
$value['action'] = 'unselect';
$keys = array_keys($_POST['unselect']);
$value['artefact'] = (int) ($keys[0]);
}
if (isset($_POST['folder'])) {
$folder = (int) $_POST['folder'];
}
// When files are being selected, this element has a real value
if (!empty($_POST['selected']) && is_array($_POST['selected'])) {
$value['selected'] = array_keys($_POST['selected']);
}
$result = pieform_element_filebrowser_doupdate($form, $element, $folder);
return $value;
}
if (is_array($result)) {
// We did something. If js, replace the filebrowser now and
// don't continue form submission.
if ($form->submitted_by_js()) {
$replacehtml = false; // Don't replace the entire form when replying with json data.
$form->json_reply(empty($result['error']) ? PIEFORM_OK : PIEFORM_ERR, $result, $replacehtml);
}
// Not js. Remember this change and submit it with the
// rest of the form.
return $result;
}
$result = array('folder' => $folder);
function pieform_element_filebrowser_doupdate(Pieform $form, $element) {
if (isset($_POST['folder'])) {
$folder = (int) $_POST['folder'];
if (!empty($_POST['select']) && is_array($_POST['select']) && is_callable($element['selectcallback'])) {
$keys = array_keys($_POST['select']);
// try
$element['selectcallback']((int) $keys[0]);
$result['message'] = get_string('fileadded', 'artefact.file');
}
else if (!empty($_POST['unselect']) && is_array($_POST['unselect']) && is_callable($element['unselectcallback'])) {
$keys = array_keys($_POST['unselect']);
// try
$element['unselectcallback']((int) $keys[0]);
$result['message'] = get_string('fileremoved', 'artefact.file');
}
else if (!empty($_POST['edit']) && is_array($_POST['edit'])) {
// Non-js update that needs to be passed back as a parameter
$keys = array_keys($_POST['edit']);
$result['edit'] = (int) $keys[0];
}
else if (!empty($_POST['selected']) && is_array($_POST['selected'])) {
// When files are being selected, this element has a real value
$result['selected'] = array_keys($_POST['selected']);
}
return $result;
}
}
function pieform_element_filebrowser_doupdate(Pieform $form, $element, $folder) {
$result = null;
if (!empty($_POST['delete']) && is_array($_POST['delete'])) {
......@@ -270,20 +291,14 @@ function pieform_element_filebrowser_doupdate(Pieform $form, $element) {
$folder = $newfolder;
}
if (is_array($result) && $form->submitted_by_js()) {
// We have just updated the filebrowser element, and can leave the rest of the form alone
if (is_array($result)) {
$result['folder'] = $folder;
// Don't replace the entire form when replying with json data.
$replacehtml = false;
$form->json_reply(empty($result['error']) ? PIEFORM_OK : PIEFORM_ERR, $result, $replacehtml);
}
}
// For non-js users:
function pieform_element_filebrowser_submit(Pieform $form, $values) {
return $result;
}
function pieform_element_filebrowser_upload(Pieform $form, $element, $data) {
global $USER;
......@@ -572,8 +587,9 @@ function pieform_element_filebrowser_changefolder(Pieform $form, $element, $fold
return array(
'error' => false,
'changedfolder' => true,
'folder' => $folder,
'newlist' => pieform_element_filebrowser_build_filelist($form, $element, $folder),
'newpath' => pieform_element_filebrowser_build_path($element, $folder),
'newpath' => pieform_element_filebrowser_build_path($form, $element, $folder),
);
}
......
......@@ -705,9 +705,10 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
}
// Pieforms doesn't seem to like a static class method here
// We only get this far for non-js submissions:
function files_submit(Pieform $form, $values) {
global $SESSION;
log_debug($values);
$group = $form->get_property('group');
$institution = $form->get_property('institution');
if ($group) {
......@@ -727,32 +728,29 @@ function files_submit(Pieform $form, $values) {
$params = array();
}
if (!empty($values['filebrowser']['action'])) {
// Updates on the filebrowser for non-js users that need to cause a
// redirect back to this page.
if (!empty($values['filebrowser']['folder'])) {
$params['folder'] = $values['filebrowser']['folder'];
}
if ($values['filebrowser']['action'] == 'edit') {
$params['edit'] = $values['filebrowser']['artefact'];
}
// $result = pieform_element_filebrowser_submit($form->get_element('filebrowser'), $values['filebrowser']);
$result['action'] = $values['filebrowser']['action'];
if (!empty($result['highlight'])) {
$params['file'] = $result['highlight'];
}
// Updates on the filebrowser for non-js users that need to cause a
// redirect back to this page.
if (isset($values['filebrowser']['folder'])) {
$params['folder'] = $values['filebrowser']['folder'];
}
if (isset($values['filebrowser']['edit'])) {
$params['edit'] = $values['filebrowser']['edit'];
}
if (isset($values['filebrowser']['highlight'])) {
$params['file'] = $values['filebrowser']['highlight'];
}
if ($params) {
foreach ($params as $k => $v) {
$params[$k] = $k . '=' . $v;
}
$redirect .= (strpos($redirect, '?') === false ? '?' : '&') . join('&', $params);
}
if (!empty($values['filebrowser']['action'])) {
$result['goto'] = $redirect;
$form->reply(empty($result['error']) ? PIEFORM_OK : PIEFORM_ERR, $result);
}
redirect($redirect);
$result = $values['filebrowser'];
$result['goto'] = $redirect;
$form->reply(empty($result['error']) ? PIEFORM_OK : PIEFORM_ERR, $result);
}
class ArtefactTypeFile extends ArtefactTypeFileBase {
......
......@@ -214,6 +214,7 @@ button.link {
overflow: visible;
margin: 0 !important;
padding: 0 !important;
font-size: 12px !important;
}
button.link:hover {
color: #84aa00;
......@@ -1798,13 +1799,13 @@ tr.uploadform th {
font-style: italic;
color: #999;
}
.directory-item.parentfolder td.filename {
.directory-item.parentfolder td.filename button.link {
font-weight: normal;
}
.filelist tr.folderhover.directory-item td {
background-color: #ffc !important;
}
.filelist tr.folderhover.directory-item a {
.filelist tr.folderhover.directory-item button.changefolder {
color: #000;
text-decoration: none;
}
......
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