Commit 8d1ddb86 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Remove old myfiles_js function, move myfiles form function into filebase class

parent 3f33fcfe
......@@ -35,8 +35,8 @@ require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('artefact', 'file');
define('TITLE', get_string('sitefiles', 'admin'));
$form = pieform(files_form(null, 'mahara'));
$js = files_js();
$form = pieform(ArtefactTypeFileBase::files_form(null, 'mahara'));
$js = ArtefactTypeFileBase::files_js();
$smarty = smarty();
$smarty->assign('descriptionstrargs', array('<a href="' . get_config('wwwroot') . 'admin/site/menu.php">', '</a>'));
......
......@@ -41,8 +41,8 @@ define('TITLE', $group->name . ' - ' . get_string('groupfiles', 'artefact.file')
require_once(get_config('docroot') . 'interaction/lib.php');
$form = pieform(files_form($group->id, null));
$js = files_js();
$form = pieform(ArtefactTypeFileBase::files_form($group->id, null));
$js = ArtefactTypeFileBase::files_js();
$smarty = smarty();
$smarty->assign('heading', $group->name);
......
......@@ -34,8 +34,8 @@ require(dirname(dirname(dirname(__FILE__))) . '/init.php');
define('TITLE', get_string('myfiles', 'artefact.file'));
safe_require('artefact', 'file');
$form = pieform(files_form(null, null));
$js = files_js();
$form = pieform(ArtefactTypeFileBase::files_form(null, null));
$js = ArtefactTypeFileBase::files_js();
$smarty = smarty(
array(),
......
......@@ -44,8 +44,8 @@ $s = institution_selector_for_page($institution,
$institution = $s['institution'];
$form = pieform(files_form(null, $institution));
$js = files_js();
$form = pieform(ArtefactTypeFileBase::files_form(null, $institution));
$js = ArtefactTypeFileBase::files_js();
$smarty = smarty();
......
......@@ -504,60 +504,62 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
return $filedata;
}
public static function files_form($group=null, $institution=null, $folder=null, $highlight=null, $edit=null) {
$folder = param_integer('folder', 0);
$edit = param_variable('edit', 0);
if (is_array($edit)) {
$edit = array_keys($edit);
$edit = $edit[0];
}
$edit = (int) $edit;
$highlight = null;
if ($file = param_integer('file', 0)) {
$highlight = array($file); // todo convert to file1=1&file2=2 etc
}
$form = array(
'name' => 'files',
'jsform' => true,
'newiframeonsubmit' => true,
'reloadformonreply' => false,
'jssuccesscallback' => 'files_success',
// 'jserrorcallback' => 'files_error',
// 'presubmitcallback' => 'uploader_presubmit',
'renderer' => 'oneline',
'plugintype' => 'artefact',
'pluginname' => 'file',
'configdirs' => array(get_config('libroot') . 'form/', get_config('docroot') . 'artefact/file/form/'),
'elements' => array(
'filebrowser' => array(
'type' => 'filebrowser',
'group' => $group,
'institution' => $institution,
'folder' => $folder,
'highlight' => $highlight,
'edit' => $edit,
'config' => array(
'upload' => true,
'uploadagreement' => true,
'createfolder' => true,
'edit' => true,
'select' => false,
),
),
'group' => array(
'type' => 'hidden',
'value' => $group,
),
'institution' => array(
'type' => 'hidden',
'value' => $institution,
),
),
);
return $form;
}
public static function get_my_files_js($folder_id=null, $highlightfiles=null) {
global $USER;
if ($folder_id) {
$folder_list = array();
$current_folder = artefact_instance_from_id($folder_id);
if ($USER->can_view_artefact($current_folder)) {
if ($current_folder->get('artefacttype') == 'folder') {
$folder_list[] = array(
'id' => $current_folder->get('id'),
'name' => $current_folder->get('title'),
);
}
while ($p = $current_folder->get('parent')) {
$current_folder = artefact_instance_from_id($p);
$folder_list[] = array(
'id' => $current_folder->get('id'),
'name' => $current_folder->get('title'),
);
}
}
$enc_folders = json_encode(array_reverse($folder_list));
}
else {
$enc_folders = json_encode(array());
}
if ($highlightfiles) {
$enc_files = json_encode(array_fill_keys($highlightfiles, 1));
} else {
$enc_files = json_encode(array());
}
$copyright = get_field('site_content', 'content', 'name', 'uploadcopyright');
$copyright = json_encode($copyright);
$wwwroot = get_config('wwwroot');
$javascript = <<<JAVASCRIPT
var copyrightnotice = {$copyright};
var browser = new FileBrowser('filelist', '{$wwwroot}artefact/file/myfiles.json.php', null, null, null, null, {$enc_folders});
var uploader = new FileUploader('uploader', '{$wwwroot}artefact/file/upload.php', {}, null, null, browser.refresh, browser.fileexists);
browser.changedircallback = uploader.updatedestination;
var highlightfiles = {$enc_files};
JAVASCRIPT;
return $javascript;
public static function files_js() {
return "function files_success(form, data) { if (!files_filebrowser.success(form, data)) { formSuccess(form, data); return false; }; };";
}
public static function count_user_files($owner=null, $group=null, $institution=null) {
......@@ -712,6 +714,63 @@ JAVASCRIPT;
}
}
function files_validate(Pieform $form, $values) {
global $SESSION;
if (!empty($values['filebrowser']['action'])) {
$result = pieform_element_filebrowser_validate($values['filebrowser']);
if ($result['error']) {
if (!$form->submitted_by_js()) {
$form->set_error('filebrowser', $result['message']);
}
$form->reply(PIEFORM_ERR, $result);
}
}
}
function files_submit(Pieform $form, $values) {
global $SESSION;
if ($values['group']) {
$redirect = get_config('wwwroot') . 'artefact/file/groupfiles.php';
$params = array('group' => $values['group']);
} else if ($values['institution']) {
if ($values['institution'] == 'mahara') {
$redirect = get_config('wwwroot') . 'admin/site/files.php';
$params = array();
}
else {
$redirect = get_config('wwwroot') . 'artefact/file/institutionfiles.php';
$params = array('institution' => $values['institution']);
}
} else {
$redirect = get_config('wwwroot') . 'artefact/file/index.php';
$params = array();
}
if (!empty($values['filebrowser']['action'])) {
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'];
}
}
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);
}
class ArtefactTypeFile extends ArtefactTypeFileBase {
public function __construct($id = 0, $data = null) {
......@@ -1368,120 +1427,4 @@ class ArtefactTypeProfileIcon extends ArtefactTypeImage {
}
function files_form($group=null, $institution=null, $folder=null, $highlight=null, $edit=null) {
$folder = param_integer('folder', 0);
$edit = param_variable('edit', 0);
if (is_array($edit)) {
$edit = array_keys($edit);
$edit = $edit[0];
}
$edit = (int) $edit;
$highlight = null;
if ($file = param_integer('file', 0)) {
$highlight = array($file); // todo convert to file1=1&file2=2 etc
}
$form = array(
'name' => 'files',
'jsform' => true,
'newiframeonsubmit' => true,
'reloadformonreply' => false,
'dieaftersubmit' => true,
'jssuccesscallback' => 'files_success',
// 'jserrorcallback' => 'files_error',
// 'presubmitcallback' => 'uploader_presubmit',
'renderer' => 'oneline',
'plugintype' => 'artefact',
'pluginname' => 'file',
'configdirs' => array(get_config('libroot') . 'form/', get_config('docroot') . 'artefact/file/form/'),
'elements' => array(
'filebrowser' => array(
'type' => 'filebrowser',
'group' => $group,
'institution' => $institution,
'folder' => $folder,
'highlight' => $highlight,
'edit' => $edit,
'config' => array(
'upload' => true,
'uploadagreement' => true,
'createfolder' => true,
'edit' => true,
'select' => false,
),
),
'group' => array(
'type' => 'hidden',
'value' => $group,
),
'institution' => array(
'type' => 'hidden',
'value' => $institution,
),
),
);
return $form;
}
function files_js() {
return "function files_success(form, data) { if (!files_filebrowser.success(form, data)) { formSuccess(form, data); return false; }; };";
}
function files_validate(Pieform $form, $values) {
global $SESSION;
if (!empty($values['filebrowser']['action'])) {
$result = pieform_element_filebrowser_validate($values['filebrowser']);
if ($result['error']) {
if (!$form->submitted_by_js()) {
$form->set_error('filebrowser', $result['message']);
}
$form->reply(PIEFORM_ERR, $result);
}
}
}
function files_submit(Pieform $form, $values) {
global $SESSION;
if ($values['group']) {
$redirect = get_config('wwwroot') . 'artefact/file/groupfiles.php';
$params = array('group' => $values['group']);
} else if ($values['institution']) {
if ($values['institution'] == 'mahara') {
$redirect = get_config('wwwroot') . 'admin/site/files.php';
$params = array();
}
else {
$redirect = get_config('wwwroot') . 'artefact/file/institutionfiles.php';
$params = array('institution' => $values['institution']);
}
} else {
$redirect = get_config('wwwroot') . 'artefact/file/index.php';
$params = array();
}
if (!empty($values['filebrowser']['action'])) {
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'];
}
}
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);
}
?>
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