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

Add filebrowser to image block config with artefacttype filtering

parent 7c98a639
......@@ -88,8 +88,11 @@ class PluginBlocktypeImage extends PluginBlocktype {
public static function instance_config_form($instance, $istemplate) {
$configdata = $instance->get('configdata');
safe_require('artefact', 'file');
$instance->set('artefactplugin', 'file');
return array(
self::artefactchooser_element((isset($configdata['artefactid'])) ? $configdata['artefactid'] : null, $istemplate),
// self::artefactchooser_element((isset($configdata['artefactid'])) ? $configdata['artefactid'] : null, $istemplate),
'filebrowser' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? $configdata['artefactid'] : null, $istemplate),
'showdescription' => array(
'type' => 'checkbox',
'title' => get_string('showdescription', 'blocktype.file/image'),
......@@ -109,6 +112,14 @@ 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 artefactchooser_element($default=null, $istemplate=false) {
$element = array(
'name' => 'artefactid',
......@@ -129,6 +140,41 @@ class PluginBlocktypeImage extends PluginBlocktype {
return $element;
}
public static function filebrowser_element(&$instance, $default=null, $istemplate=false) {
$element = array(
'name' => 'filebrowser',
'type' => 'filebrowser',
'title' => get_string('image'),
'folder' => param_integer('folder', 0),
'highlight' => null,
'browse' => true,
'page' => View::make_base_url(),
'config' => array(
'upload' => true,
'uploadagreement' => get_config_plugin('artefact', 'file', 'uploadagreement'),
'createfolder' => false,
'edit' => false,
'select' => true,
'selectone' => true,
'alwaysopen' => true,
),
'filters' => array(
'artefacttype' => array('image'),
),
'selectlistcallback' => array(
'name' => 'artefact_get_records_by_id',
'args' => array(empty($default) ? array() : array($default)),
),
);
if (!$istemplate) {
// You don't have to choose a file if this view is a template
$element['rules'] = array(
'required' => true,
);
}
return $element;
}
/**
* Optional method. If specified, allows the blocktype class to munge the
* artefactchooser element data before it's templated
......
......@@ -80,7 +80,8 @@ function pieform_element_filebrowser(Pieform $form, $element) {
}
$smarty->assign('config', $config);
$filedata = ArtefactTypeFileBase::get_my_files_data($folder, $userid, $group, $institution);
$filters = isset($element['filters']) ? $element['filters'] : null;
$filedata = ArtefactTypeFileBase::get_my_files_data($folder, $userid, $group, $institution, $filters);
$smarty->assign('filelist', $filedata);
$configstr = json_encode($config);
......@@ -103,7 +104,6 @@ function pieform_element_filebrowser(Pieform $form, $element) {
$initjs .= "addLoadEvent({$prefix}.init);";
$smarty->assign('initjs', $initjs);
$smarty->assign('querybase', $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&'));
return $smarty->fetch('artefact:file:form/filebrowser.tpl');
......@@ -169,7 +169,8 @@ function pieform_element_filebrowser_build_filelist($form, $element, $folder, $h
$querybase = $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&');
$prefix = $form->get_name() . '_' . $element['name'];
$filedata = ArtefactTypeFileBase::get_my_files_data($folder, $userid, $group, $institution);
$filters = isset($element['filters']) ? $element['filters'] : null;
$filedata = ArtefactTypeFileBase::get_my_files_data($folder, $userid, $group, $institution, $filters);
$smarty = smarty_core();
$smarty->assign('edit', -1);
......
......@@ -411,7 +411,7 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
}
public static function get_my_files_data($parentfolderid, $userid, $group=null, $institution=null) {
public static function get_my_files_data($parentfolderid, $userid, $group=null, $institution=null, $filters=null) {
$select = '
SELECT
a.id, a.artefacttype, a.mtime, f.size, a.title, a.description,
......@@ -421,8 +421,15 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
LEFT OUTER JOIN {artefact_file_files} f ON f.artefact = a.id
LEFT OUTER JOIN {artefact} c ON c.parent = a.id
LEFT OUTER JOIN {artefact_attachment} aa ON aa.attachment = a.id';
if ($filters && $filters['artefacttype']) {
$artefacttypes = $filters['artefacttype'];
$artefacttypes[] = 'folder';
}
else {
$artefacttypes = array_diff(PluginArtefactFile::get_artefact_types(), array('profileicon'));
}
$where = "
WHERE a.artefacttype IN ('" . join("','", array_diff(PluginArtefactFile::get_artefact_types(), array('profileicon'))) . "')";
WHERE a.artefacttype IN (" . join(',', array_map('db_quote', $artefacttypes)). ")";
$groupby = '
GROUP BY
a.id, a.artefacttype, a.mtime, f.size, a.title, a.description';
......
Supports Markdown
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