From c258605a8d9a2949c1f0e7a89debba1599c827f3 Mon Sep 17 00:00:00 2001 From: Paul Nicholls Date: Wed, 8 Aug 2012 13:50:47 +1200 Subject: [PATCH 1/2] MDL-33640 - Add ability to use custom filepicker templates Allow repository plugins to register a template by defining a get_template() method, coupled with the ability to request the template be used by create_upload_form() instead of the standard 'uploadform' template. The template is automatically registered using the plugin's name, and core templates will override any which clash; this also means that a theme can override these templates in a custom renderer if it wants to. --- repository/filepicker.js | 3 ++- repository/lib.php | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/repository/filepicker.js b/repository/filepicker.js index 7c4e1f4ba94..54b6bc171c8 100644 --- a/repository/filepicker.js +++ b/repository/filepicker.js @@ -1618,7 +1618,8 @@ M.core_filepicker.init = function(Y, options) { var client_id = this.options.client_id; var id = data.upload.id+'_'+client_id; var content = this.fpnode.one('.fp-content'); - content.setContent(M.core_filepicker.templates.uploadform); + var template = data.template || 'uploadform'; + content.setContent(M.core_filepicker.templates[template]); content.all('.fp-file,.fp-saveas,.fp-setauthor,.fp-setlicense').each(function (node) { node.all('label').set('for', node.one('input,select').generateID()); diff --git a/repository/lib.php b/repository/lib.php index 44e4fe47c9d..7828ab7427c 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -2684,16 +2684,21 @@ function initialise_filepicker($args) { // provided by form element $return->accepted_types = file_get_typegroup('extension', $args->accepted_types); $return->return_types = $args->return_types; + $templates = array(); foreach ($repositories as $repository) { $meta = $repository->get_meta(); // Please note that the array keys for repositories are used within // JavaScript a lot, the key NEEDS to be the repository id. $return->repositories[$repository->id] = $meta; + // Register custom repository template if it has one + if(method_exists($repository, 'get_template')) { + $templates[$meta->type] = $repository->get_template(); + } } if (!$templatesinitialized) { // we need to send filepicker templates to the browser just once $fprenderer = $PAGE->get_renderer('core', 'files'); - $templates = $fprenderer->filepicker_js_templates(); + $templates = array_merge($templates, $fprenderer->filepicker_js_templates()); $PAGE->requires->js_init_call('M.core_filepicker.set_templates', array($templates), true); $templatesinitialized = true; } From 55c91c8760147a8af65afa0ddeae68c1cc106a73 Mon Sep 17 00:00:00 2001 From: Paul Nicholls Date: Thu, 9 Aug 2012 09:43:53 +1200 Subject: [PATCH 2/2] MDL-33640 - change $templatesinitialized to an array; improve naming and automate use of template * $templatesinitialized is now an array, so that subsequent calls to initialise_filepicker which request different repositories will include those (and only those) templates which it requires but have not yet been included * The get_template method has also been renamed to get_upload_template (and the template to "uploadform_" followed by the repository type), since it only applies to upload forms * If a plugin provides a get_upload_template method, the template it returns will now automatically be used instead of the standard uploadform template when generating an upload form --- repository/filepicker.js | 5 +++-- repository/lib.php | 15 +++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/repository/filepicker.js b/repository/filepicker.js index 54b6bc171c8..d277574f18d 100644 --- a/repository/filepicker.js +++ b/repository/filepicker.js @@ -1618,8 +1618,9 @@ M.core_filepicker.init = function(Y, options) { var client_id = this.options.client_id; var id = data.upload.id+'_'+client_id; var content = this.fpnode.one('.fp-content'); - var template = data.template || 'uploadform'; - content.setContent(M.core_filepicker.templates[template]); + var template_name = 'uploadform_'+this.options.repositories[data.repo_id].type; + var template = M.core_filepicker.templates[template_name] || M.core_filepicker.templates['uploadform']; + content.setContent(template); content.all('.fp-file,.fp-saveas,.fp-setauthor,.fp-setlicense').each(function (node) { node.all('label').set('for', node.one('input,select').generateID()); diff --git a/repository/lib.php b/repository/lib.php index 7828ab7427c..3818cc92a06 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -2614,7 +2614,7 @@ final class repository_type_form extends moodleform { */ function initialise_filepicker($args) { global $CFG, $USER, $PAGE, $OUTPUT; - static $templatesinitialized; + static $templatesinitialized = array(); require_once($CFG->libdir . '/licenselib.php'); $return = new stdClass(); @@ -2691,16 +2691,19 @@ function initialise_filepicker($args) { // JavaScript a lot, the key NEEDS to be the repository id. $return->repositories[$repository->id] = $meta; // Register custom repository template if it has one - if(method_exists($repository, 'get_template')) { - $templates[$meta->type] = $repository->get_template(); + if(method_exists($repository, 'get_upload_template') && !array_key_exists('uploadform_' . $meta->type, $templatesinitialized)) { + $templates['uploadform_' . $meta->type] = $repository->get_upload_template(); + $templatesinitialized['uploadform_' . $meta->type] = true; } } - if (!$templatesinitialized) { - // we need to send filepicker templates to the browser just once + if (!array_key_exists('core', $templatesinitialized)) { + // we need to send each filepicker template to the browser just once $fprenderer = $PAGE->get_renderer('core', 'files'); $templates = array_merge($templates, $fprenderer->filepicker_js_templates()); + $templatesinitialized['core'] = true; + } + if (sizeof($templates)) { $PAGE->requires->js_init_call('M.core_filepicker.set_templates', array($templates), true); - $templatesinitialized = true; } return $return; }