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

Remove need for some filebrowser-element-specific stuff in pieform submit functions

parent 0888c32d
......@@ -352,70 +352,48 @@ function editpost_cancel_submit() {
function editpost_submit(Pieform $form, $values) {
global $USER, $SESSION, $blogpost, $blog;
// save the post if the user clicked submit or has no js
$submitted = !empty($values['submitpost']);
if ($submitted || !$form->submitted_by_js()) {
db_begin();
$postobj = new ArtefactTypeBlogPost($blogpost, null);
$postobj->set('title', $values['title']);
$postobj->set('description', $values['description']);
$postobj->set('tags', $values['tags']);
$postobj->set('published', !$values['draft']);
if (!$blogpost) {
$postobj->set('parent', $blog);
$postobj->set('owner', $USER->id);
}
$postobj->commit();
$blogpost = $postobj->get('id');
// Attachments
$old = $postobj->attachment_id_list();
$new = is_array($values['filebrowser']['selected']) ? $values['filebrowser']['selected'] : array();
if (!empty($new) || !empty($old)) {
foreach ($old as $o) {
if (!in_array($o, $new)) {
$postobj->detach($o);
}
}
foreach ($new as $n) {
if (!in_array($n, $old)) {
$postobj->attach($n);
}
db_begin();
$postobj = new ArtefactTypeBlogPost($blogpost, null);
$postobj->set('title', $values['title']);
$postobj->set('description', $values['description']);
$postobj->set('tags', $values['tags']);
$postobj->set('published', !$values['draft']);
if (!$blogpost) {
$postobj->set('parent', $blog);
$postobj->set('owner', $USER->id);
}
$postobj->commit();
$blogpost = $postobj->get('id');
// Attachments
$old = $postobj->attachment_id_list();
// $new = is_array($values['filebrowser']['selected']) ? $values['filebrowser']['selected'] : array();
$new = is_array($values['filebrowser']) ? $values['filebrowser'] : array();
if (!empty($new) || !empty($old)) {
foreach ($old as $o) {
if (!in_array($o, $new)) {
$postobj->detach($o);
}
$filebrowser['selectedlist'] = $postobj->get_attachments(true);
}
db_commit();
if ($submitted) {
$result = array(
'error' => false,
'message' => get_string('blogpostsaved', 'artefact.blog'),
'goto' => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog,
);
if ($form->submitted_by_js()) {
// Redirect back to the blog page from within the iframe
$SESSION->add_ok_msg($result['message']);
$form->json_reply(PIEFORM_OK, $result, false);
foreach ($new as $n) {
if (!in_array($n, $old)) {
$postobj->attach($n);
}
$form->reply(PIEFORM_OK, $result);
}
}
db_commit();
// Non-js filebrowser submission
$result = array(
'error' => false,
'goto' => get_config('wwwroot') . 'artefact/blog/post.php?id=' . $blogpost,
'message' => get_string('blogpostsaved', 'artefact.blog'),
'goto' => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog,
);
if (isset($values['filebrowser']['browse'])) {
$result['goto'] .= '&browse=1';
}
if (isset($values['filebrowser']['folder'])) {
$result['goto'] .= '&folder=' . $values['filebrowser']['folder'];
}
if (isset($values['filebrowser']['highlight'])) {
$result['goto'] .= '&file=' . $values['filebrowser']['highlight'];
if ($form->submitted_by_js()) {
// Redirect back to the blog page from within the iframe
$SESSION->add_ok_msg($result['message']);
$form->json_reply(PIEFORM_OK, $result, false);
}
$form->reply(empty($result['error']) ? PIEFORM_OK : PIEFORM_ERR, $result);
$form->reply(PIEFORM_OK, $result);
}
function load_attachments() {
......
......@@ -106,18 +106,10 @@ class PluginBlocktypeFiledownload extends PluginBlocktype {
safe_require('artefact', 'file');
$instance->set('artefactplugin', 'file');
return array(
'filebrowser' => self::filebrowser_element($instance, (isset($configdata['artefactids'])) ? $configdata['artefactids'] : null, $istemplate),
'artefactids' => self::filebrowser_element($instance, (isset($configdata['artefactids'])) ? $configdata['artefactids'] : null),
);
}
public static function instance_config_save($values) {
if (isset($values['filebrowser']['selected'])) {
$values['artefactids'] = $values['filebrowser']['selected'];
}
unset($values['filebrowser']);
return $values;
}
public static function artefactchooser_element($default=null, $istemplate=false) {
$element = array(
'name' => 'artefactids',
......@@ -147,9 +139,10 @@ class PluginBlocktypeFiledownload extends PluginBlocktype {
return ArtefactTypeFileBase::artefactchooser_get_file_data($artefact);
}
public static function filebrowser_element(&$instance, $default=array(), $istemplate=false) {
$element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default, $istemplate);
public static function filebrowser_element(&$instance, $default=array()) {
$element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
$element['title'] = get_string('Files', 'blocktype.file/filedownload');
$element['name'] = 'artefactids';
$element['config']['selectone'] = false;
return $element;
}
......
......@@ -67,18 +67,10 @@ class PluginBlocktypeHtml extends PluginBlocktype {
safe_require('artefact', 'file');
$instance->set('artefactplugin', 'file');
return array(
'filebrowser' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null, $istemplate),
'artefactid' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null),
);
}
public static function instance_config_save(&$values) {
if (isset($values['filebrowser']['selected'])) {
$values['artefactid'] = $values['filebrowser']['selected'][0];
}
unset($values['filebrowser']);
return $values;
}
private static function get_allowed_mimetypes() {
static $mimetypes = array();
if (!$mimetypes) {
......@@ -87,9 +79,10 @@ class PluginBlocktypeHtml extends PluginBlocktype {
return $mimetypes;
}
public static function filebrowser_element(&$instance, $default=array(), $istemplate=false) {
$element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default, $istemplate);
public static function filebrowser_element(&$instance, $default=array()) {
$element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
$element['title'] = get_string('file', 'artefact.file');
$element['name'] = 'artefactid';
$element['config']['selectone'] = true;
$element['filters'] = array(
'artefacttype' => array('file'),
......
......@@ -91,7 +91,7 @@ class PluginBlocktypeImage extends PluginBlocktype {
safe_require('artefact', 'file');
$instance->set('artefactplugin', 'file');
return array(
'filebrowser' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null, $istemplate),
'artefactid' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null),
'showdescription' => array(
'type' => 'checkbox',
'title' => get_string('showdescription', 'blocktype.file/image'),
......@@ -111,17 +111,10 @@ class PluginBlocktypeImage extends PluginBlocktype {
);
}
public static function instance_config_save($values) {
if (isset($values['filebrowser']['selected'])) {
$values['artefactid'] = $values['filebrowser']['selected'][0];
}
unset($values['filebrowser']);
return $values;
}
public static function filebrowser_element(&$instance, $default=array(), $istemplate=false) {
$element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default, $istemplate);
public static function filebrowser_element(&$instance, $default=array()) {
$element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
$element['title'] = get_string('image');
$element['name'] = 'artefactid';
$element['config']['selectone'] = true;
$element['filters'] = array(
'artefacttype' => array('image'),
......
......@@ -84,7 +84,7 @@ class PluginBlocktypeInternalmedia extends PluginBlocktype {
safe_require('artefact', 'file');
$instance->set('artefactplugin', 'file');
return array(
'filebrowser' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null, $istemplate),
'artefactid' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null),
'width' => array(
'type' => 'text',
'title' => get_string('width'),
......@@ -100,14 +100,6 @@ class PluginBlocktypeInternalmedia extends PluginBlocktype {
);
}
public static function instance_config_save($values) {
if (isset($values['filebrowser']['selected'])) {
$values['artefactid'] = $values['filebrowser']['selected'][0];
}
unset($values['filebrowser']);
return $values;
}
public static function get_artefacts(BlockInstance $instance) {
$configdata = $instance->get('configdata');
if (isset($configdata['artefactid'])) {
......@@ -116,9 +108,10 @@ class PluginBlocktypeInternalmedia extends PluginBlocktype {
return false;
}
public static function filebrowser_element(&$instance, $default=array(), $istemplate=false) {
$element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default, $istemplate);
public static function filebrowser_element(&$instance, $default=array()) {
$element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
$element['title'] = get_string('media', 'blocktype.file/internalmedia');
$element['name'] = 'artefactid';
$element['config']['selectone'] = true;
$element['filters'] = array(
'artefacttype' => array('file'),
......
......@@ -68,6 +68,7 @@ function pieform_element_filebrowser(Pieform $form, $element) {
}
$config = array_map('intval', $element['config']);
if ($group && $config['edit']) {
$smarty->assign('groupinfo', pieform_element_filebrowser_get_groupinfo($group));
}
......@@ -82,6 +83,7 @@ function pieform_element_filebrowser(Pieform $form, $element) {
$smarty->assign('selectedlist', $selected);
$selectedliststr = json_encode($selected);
}
if ($config['uploadagreement']) {
if (get_config_plugin('artefact', 'file', 'usecustomagreement')) {
$smarty->assign('agreementtext', get_field('site_content', 'content', 'name', 'uploadcopyright'));
......@@ -90,11 +92,13 @@ function pieform_element_filebrowser(Pieform $form, $element) {
$smarty->assign('agreementtext', get_string('uploadcopyrightdefaultcontent', 'install'));
}
}
if (!empty($element['browsehelp'])) {
$config['plugintype'] = $form->get_property('plugintype');
$config['pluginname'] = $form->get_property('pluginname');
$config['browsehelp'] = $element['browsehelp'];
}
$smarty->assign('config', $config);
$filters = isset($element['filters']) ? $element['filters'] : null;
......@@ -156,6 +160,7 @@ function pieform_element_filebrowser_get_path($folder) {
return $path;
}
function pieform_element_filebrowser_build_path($form, $element, $folder, $owner=null, $ownerid=null) {
if (!$form->submitted_by_js()) {
return;
......@@ -173,6 +178,7 @@ function pieform_element_filebrowser_build_path($form, $element, $folder, $owner
return array('html' => $smarty->fetch('artefact:file:form/folderpath.tpl'), 'foldername' => $foldername);
}
function pieform_element_filebrowser_build_filelist($form, $element, $folder, $highlight=null, $group=null, $institution=null) {
if (!$form->submitted_by_js()) {
// We're going to rebuild the page from scratch anyway.
......@@ -200,7 +206,7 @@ function pieform_element_filebrowser_build_filelist($form, $element, $folder, $h
$userid = ($group || $institution) ? null : $USER->get('id');
$editable = (int) $element['config']['edit'];
$selectable = (int) $element['config']['select'];
$publishing = (int) $element['config']['publishing'];
$publishing = (int) !empty($element['config']['publishing']);
$querybase = $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&');
$prefix = $form->get_name() . '_' . $element['name'];
......@@ -222,6 +228,7 @@ function pieform_element_filebrowser_build_filelist($form, $element, $folder, $h
);
}
function pieform_element_filebrowser_configure_tabs($viewowner) {
if ($viewowner['type'] == 'institution' && $viewowner['id'] == 'mahara') {
// No filebrowser tabs for site views
......@@ -280,91 +287,87 @@ function pieform_element_filebrowser_configure_tabs($viewowner) {
return array('tabs' => $tabs, 'subtabs' => $subtabs, 'owner' => $selectedtab, 'ownerid' => $selectedsubtab, 'upload' => $upload);
}
function pieform_element_filebrowser_get_value(Pieform $form, $element) {
// Check if the user tried to make a change to the filebrowser element
if ($form->is_submitted()) {
$prefix = $form->get_name() . '_' . $element['name'];
// The value of this element is the list of selected artefact ids
$selected = param_variable($prefix . '_selected', null);
if (is_array($selected)) {
$selected = array_keys($selected);
}
$result = pieform_element_filebrowser_doupdate($form, $element);
$prefix = $form->get_name() . '_' . $element['name'];
// Process actions that must occur before form validation and
// which can safely occur without affecting the element's value
$result = pieform_element_filebrowser_doupdate($form, $element);
if (is_array($result)) {
// We did something. If js, replace the filebrowser now and
// don't continue form submission.
if (!isset($result['folder'])) {
$result['folder'] = $element['folder'];
if (is_array($result)) {
// We did something. If js, replace the filebrowser now and
// don't continue form submission.
if (!isset($result['folder'])) {
$result['folder'] = $element['folder'];
}
if ($form->submitted_by_js()) {
$replacehtml = false; // Don't replace the entire form when replying with json data.
$result['formelement'] = $prefix;
if ($result['error']) {
$result['formelementerror'] = $prefix . '.success';
}
if ($form->submitted_by_js()) {
$replacehtml = false; // Don't replace the entire form when replying with json data.
$result['formelement'] = $prefix;
if ($result['error']) {
$result['formelementerror'] = $prefix . '.success';
}
else {
$result['formelementsuccess'] = $prefix . '.success';
}
$form->json_reply(empty($result['error']) ? PIEFORM_OK : PIEFORM_ERR, $result, $replacehtml);
else {
$result['formelementsuccess'] = $prefix . '.success';
}
// Not js. Remember this change and submit it with the
// rest of the form.
return $result;
$form->json_reply(empty($result['error']) ? PIEFORM_OK : PIEFORM_ERR, $result, $replacehtml);
}
$selected = param_variable($prefix . '_selected', null);
if (is_array($selected)) {
$selected = array_keys($selected);
// Not js. Add some params & redirect back to the page
$params = array();
if (!empty($result['folder'])) {
$params[] = 'folder=' . $result['folder'];
}
if (!empty($result['edit'])) {
$params[] = 'edit=' . $result['edit'];
}
if (!empty($result['highlight'])) {
$params[] = 'file=' . $result['highlight'];
}
if (!empty($result['browse'])) {
$params[] = 'browse=1';
}
// We only care about the following when js is off:
if (!$form->submitted_by_js()) {
$select = param_variable($prefix . '_select', null);
$unselect = param_variable($prefix . '_unselect', null);
$edit = param_variable($prefix . '_edit', null);
if (is_array($select)) {
$keys = array_keys($select);
$add = (int) $keys[0];
if (isset($element['selectcallback']) && is_callable($element['selectcallback'])) {
$element['selectcallback']($add);
}
else if ($element['config']['selectone']) {
$result['selected'] = array($add);
}
else {
$result['selected'] = is_array($selected) ? $selected : array();
$result['selected'][] = $add;
}
$result['message'] = get_string('fileadded', 'artefact.file');
$result['browse'] = 1;
}
else if (is_array($unselect)) {
$keys = array_keys($unselect);
$del = (int) $keys[0];
if (isset($element['unselectcallback']) && is_callable($element['unselectcallback'])) {
$element['unselectcallback']($del);
}
else {
$result['selected'] = is_array($selected) ? array_diff($selected, array($del)) : array();
}
$result['message'] = get_string('fileremoved', 'artefact.file');
}
else if (is_array($edit)) {
// Non-js update that needs to be passed back as a parameter
$keys = array_keys($edit);
$result['edit'] = (int) $keys[0];
$result['goto'] = $element['page'];
if (!empty($params)) {
$result['goto'] .= (strpos($element['page'], '?') === false ? '?' : '&') . join('&', $params);
}
if (empty($result['select']) && empty($result['unselect'])) {
$form->reply(empty($result['error']) ? PIEFORM_OK : PIEFORM_ERR, $result);
}
// If we got to this point, the doupdate function couldn't select or unselect a file,
// so we need to let it go through to the form's submit function to deal with.
if ($result['select']) {
if ($element['config']['selectone']) {
$selected = array($result['select']);
}
else if (param_variable('browse', 0) && !param_variable($prefix . '_cancelbrowse', 0)) {
$result['browse'] = 1;
else {
$selected = is_array($selected) ? $selected : array();
$selected[] = $result['select'];
}
}
if (!is_array($result) && is_array($selected)) {
// When files are being selected, this element has a real value
$result['selected'] = $selected;
else if ($result['unselect']) {
$selected = is_array($selected) ? array_diff($selected, array($result['unselect'])) : array();
}
}
$result['folder'] = $element['folder'];
return $result;
if (is_array($selected) && !empty($selected)) {
if ($element['config']['selectone']) {
return $selected[0];
}
return $selected;
}
return null;
}
......@@ -480,14 +483,53 @@ function pieform_element_filebrowser_doupdate(Pieform $form, $element) {
$element['selectcallback']($result['highlight']);
}
else {
$selected = param_variable($prefix . '_selected', array());
$result['selected'] = array_keys($selected);
$result['selected'][] = $result['highlight'];
$result['select'] = $result['highlight'];
}
}
return $result;
}
$select = param_variable($prefix . '_select', null);
if (is_array($select)) {
$keys = array_keys($select);
$add = (int) $keys[0];
if (isset($element['selectcallback']) && is_callable($element['selectcallback'])) {
$element['selectcallback']($add);
}
else {
$result['select'] = $add;
}
$result['message'] = get_string('fileadded', 'artefact.file');
$result['browse'] = 1;
return $result;
}
$unselect = param_variable($prefix . '_unselect', null);
if (is_array($unselect)) {
$keys = array_keys($unselect);
$del = (int) $keys[0];
if (isset($element['unselectcallback']) && is_callable($element['unselectcallback'])) {
$element['unselectcallback']($del);
}
else {
$result['unselect'] = $del;
}
$result['message'] = get_string('fileremoved', 'artefact.file');
return $result;
}
$edit = param_variable($prefix . '_edit', null);
if (is_array($edit)) {
$keys = array_keys($edit);
$result['edit'] = (int) $keys[0];
return $result;
}
if (param_variable('browse', 0) && !param_variable($prefix . '_cancelbrowse', 0)) {
$result['browse'] = 1;
return $result;
}
$changeowner = param_variable($prefix . '_changeowner', null);
if (!empty($changeowner)) {
$result = pieform_element_filebrowser_changeowner($form, $element);
......@@ -648,6 +690,7 @@ function prepare_upload_failed_message(&$result, $exception, $parentfoldername,
$result['message'] .= ': ' . $exception->getMessage();
}
function pieform_element_filebrowser_createfolder(Pieform $form, $element, $data) {
global $USER;
......@@ -703,6 +746,7 @@ function pieform_element_filebrowser_createfolder(Pieform $form, $element, $data
);
}
function pieform_element_filebrowser_update(Pieform $form, $element, $data) {
global $USER;
$collide = !empty($data['collide']) ? $data['collide'] : 'fail';
......@@ -741,6 +785,7 @@ function pieform_element_filebrowser_update(Pieform $form, $element, $data) {
);
}
function pieform_element_filebrowser_delete(Pieform $form, $element, $artefact) {
global $USER;
$artefact = artefact_instance_from_id($artefact);
......@@ -760,6 +805,7 @@ function pieform_element_filebrowser_delete(Pieform $form, $element, $artefact)
);
}
function pieform_element_filebrowser_move(Pieform $form, $element, $data) {
global $USER;
$artefactid = $data['artefact']; // Artefact being moved
......@@ -827,6 +873,7 @@ function pieform_element_filebrowser_move(Pieform $form, $element, $data) {
return array('error' => true, 'message' => get_string('movefailed', 'artefact.file'));
}
function pieform_element_filebrowser_changeowner(Pieform $form, $element) {
$newtabdata = pieform_element_filebrowser_configure_tabs($element['tabs']);
$smarty = smarty_core();
......@@ -866,6 +913,7 @@ function pieform_element_filebrowser_changeowner(Pieform $form, $element) {
);
}
function pieform_element_filebrowser_changefolder(Pieform $form, $element, $folder) {
$owner = $ownerid = $group = $institution = null;
......@@ -893,6 +941,7 @@ function pieform_element_filebrowser_changefolder(Pieform $form, $element, $fold
);
}
function pieform_element_filebrowser_views_js(Pieform $form, $element) {
global $_PIEFORM_FILEBROWSERS;
$formname = $form->get_name();
......@@ -923,6 +972,7 @@ function pieform_element_filebrowser_get_headdata($element) {
return $headdata;
}
function pieform_element_filebrowser_set_attributes($element) {/*{{{*/
$element['needsmultipart'] = true;
return $element;
......
......@@ -411,6 +411,17 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
}
/**
* Gets a list of files in one folder
*
* @param integer $parentfolderid Artefact id of the folder
* @param integer $userid Id of the owner, if the owner is a user
* @param integer $group Id of the owner, if the owner is a group
* @param string $institution Id of the owner, if the owner is a institution
* @param array $filters Filters to apply to the results. An array with keys 'artefacttype', 'filetype',
* where array values are arrays of artefacttype or mimetype strings.
* @return array A list of artefacts
*/
public static function get_my_files_data($parentfolderid, $userid, $group=null, $institution=null, $filters=null) {