Commit 2da93496 authored by Nigel McNie's avatar Nigel McNie
Browse files

Made blocks that have configuration be dropped in configure mode. Refactored...

Made blocks that have configuration be dropped in configure mode. Refactored some duplicate code around the creation of the configuration form for blocks too
parent 106256f6
......@@ -119,7 +119,7 @@ class PluginBlocktypeExternalvideo extends SystemBlocktype {
private static function make_video_url($url) {
static $embedsources = array(
array(
'match' => '#.*youtube\.com.*v=([a-zA-Z0-9]+).*#',
'match' => '#.*youtube\.com.*v=([a-zA-Z0-9-]+).*#',
'url' => 'http://www.youtube.com/v/$1'
),
array(
......
......@@ -243,44 +243,14 @@ class BlockInstance {
*
* @param bool $configure Whether to render the block instance in configure
* mode
* @return array Array with two keys: 'html' for raw html, 'js' for
* javascript to run
*/
public function render_editing($configure=false) {
safe_require('blocktype', $this->get('blocktype'));
$js = '';
if ($configure) {
$elements = call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'instance_config_form', $this);
// Add submit/cancel buttons and helper hidden variable
$elements['action_configureblockinstance_id_' . $this->get('id')] = array(
'type' => 'submitcancel',
'value' => array('Save', 'Cancel'),
);
$form = array(
'name' => 'cb_' . $this->get('id'),
'validatecallback' => array(generate_class_name('blocktype', $this->get('blocktype')), 'instance_config_validate'),
'successcallback' => array($this, 'instance_config_store'),
'elements' => $elements
);
require_once('pieforms/pieform.php');
$pieform = new Pieform($form);
if ($pieform->is_submitted()) {
global $SESSION;
$SESSION->add_error_msg(get_string('errorprocessingform'));
}
else {
// This is a bit hacky. Because pieforms will take values from
// $_POST before 'defaultvalue's of form elements, we need to nuke
// all of the post values for the form. The situation where this
// becomes relevant is when someone clicks the configure button for
// one block, then immediately configures another block
foreach (array_keys($elements) as $name) {
unset($_POST[$name]);
}
}
$content = $pieform->build(false);
list($content, $js) = array_values($this->build_configure_form());
}
else {
$content = call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'render_instance', $this);
......@@ -337,7 +307,7 @@ class BlockInstance {
$smarty->assign('content', $content);
$smarty->assign('javascript', defined('JSON'));
return $smarty->fetch('view/blocktypecontainerediting.tpl');
return array('html' => $smarty->fetch('view/blocktypecontainerediting.tpl'), 'js' => $js);
}
public function render_viewing() {
......@@ -354,6 +324,68 @@ class BlockInstance {
return $smarty->fetch('view/blocktypecontainerviewing.tpl');
}
/**
* Builds the configuration pieform for this blockinstance
*
* @return array Array with two keys: 'html' for raw html, 'js' for
* javascript to run
*/
public function build_configure_form() {
safe_require('blocktype', $this->get('blocktype'));
$elements = call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'instance_config_form', $this);
// Add submit/cancel buttons
$elements['action_configureblockinstance_id_' . $this->get('id')] = array(
'type' => 'submitcancel',
'value' => array(get_string('save'), get_string('cancel')),
);
$form = array(
'name' => 'cb_' . $this->get('id'),
'validatecallback' => array(generate_class_name('blocktype', $this->get('blocktype')), 'instance_config_validate'),
'successcallback' => array($this, 'instance_config_store'),
'elements' => $elements
);
require_once('pieforms/pieform.php');
$pieform = new Pieform($form);
// We need to load any javascript required for the pieform. We do this
// by inspecting the form array and seeing what elements there are,
// getting their headdata and making sure that this js is made
// available to be used on the client side
$js = '';
foreach ($elements as $key => $element) {
$function = 'pieform_element_' . $element['type'] . '_get_headdata';
if (is_callable($function)) {
$headers = call_user_func($function);
if (in_array('tinytinymce', $headers)) {
$js = <<<EOF
tinyMCE.execCommand("mceAddControl", true, "cb_{$this->get('id')}_{$key}");
EOF;
}
}
}
if ($pieform->is_submitted()) {
global $SESSION;
$SESSION->add_error_msg(get_string('errorprocessingform'));
}
else {
// This is a bit hacky. Because pieforms will take values from
// $_POST before 'defaultvalue's of form elements, we need to nuke
// all of the post values for the form. The situation where this
// becomes relevant is when someone clicks the configure button for
// one block, then immediately configures another block
foreach (array_keys($elements) as $name) {
unset($_POST[$name]);
}
}
return array('html' => $pieform->build(false), 'js' => $js);
}
public function commit() {
if (empty($this->dirty)) {
return;
......
......@@ -815,13 +815,21 @@ function ViewManager() {
pd['action_addblocktype_column_' + whereTo['column'] + '_order_' + whereTo['order']] = true;
sendjsonrequest(config['wwwroot'] + 'view/blocks.json.php', pd, 'POST', function(data) {
var div = DIV();
div.innerHTML = data.data;
div.innerHTML = data.data.html;
var blockinstance = getFirstElementByTagAndClassName('div', 'blockinstance', div);
self.rewriteConfigureButton(getFirstElementByTagAndClassName('input', 'configurebutton', blockinstance));
// Make configure button clickable, but disabled as blocks are rendered in configure mode by default
var configureButton = getFirstElementByTagAndClassName('input', 'configurebutton', blockinstance);
if (configureButton) {
self.rewriteConfigureButton(configureButton);
setNodeAttribute(configureButton, 'disabled', 'disabled');
}
self.rewriteDeleteButton(getFirstElementByTagAndClassName('input', 'deletebutton', blockinstance));
self.makeBlockinstanceDraggable(blockinstance);
insertSiblingNodesAfter(self.blockPlaceholder, blockinstance);
removeElement(self.blockPlaceholder);
eval(data.data.js);
});
......
......@@ -760,7 +760,16 @@ class View {
$blockcontent = '';
foreach($data['blockinstances'] as $blockinstance) {
$blockcontent .= $blockinstance->$renderfunction($blockinstance->get('id') == $this->blockinstance_currently_being_configured);
$result = $blockinstance->$renderfunction($blockinstance->get('id') == $this->blockinstance_currently_being_configured);
if ($editing) {
$blockcontent .= $result['html'];
// NOTE: build_column is always called in the context of column
// operations, so the javascript returned, which is currently
// for configuring block instances only, is not necessary
}
else {
$blockcontent .= $result;
}
}
// Widths don't appear to apply to divs unless they have at least
......@@ -818,7 +827,8 @@ class View {
$this->dirtycolumns[$values['column']] = 1;
if ($values['returndata']) {
return $bi->render_editing();
// Make sure it's in configure mode if it has configuration
return $bi->render_editing(call_static_method(generate_class_name('blocktype', $values['blocktype']), 'has_instance_config'));
}
}
......@@ -928,53 +938,7 @@ class View {
public function configureblockinstance($values) {
require_once(get_config('docroot') . 'blocktype/lib.php');
$bi = new BlockInstance($values['id']);
safe_require('blocktype', $bi->get('blocktype'));
$elements = call_static_method(generate_class_name('blocktype', $bi->get('blocktype')), 'instance_config_form', $bi);
// Add submit/cancel buttons and helper hidden variable
$elements['action_configureblockinstance_id_' . $bi->get('id')] = array(
'type' => 'submitcancel',
'value' => array(get_string('save'), get_string('cancel')),
);
$form = array(
'name' => 'cb_' . $bi->get('id'),
'validatecallback' => array(generate_class_name('blocktype', $bi->get('blocktype')), 'instance_config_validate'),
'successcallback' => array($bi, 'instance_config_store'),
'elements' => $elements
);
require_once('pieforms/pieform.php');
$pieform = new Pieform($form);
// We need to load any javascript required for the pieform. We do this
// by inspecting the form array and seeing what elements there are,
// getting their headdata and making sure that this js is made
// available to be used on the client side
$js = '';
foreach ($elements as $key => $element) {
$function = 'pieform_element_' . $element['type'] . '_get_headdata';
if (is_callable($function)) {
$headers = call_user_func($function);
if (in_array('tinytinymce', $headers)) {
$js = <<<EOF
tinyMCE.execCommand("mceAddControl", true, "cb_{$values['id']}_{$key}");
EOF;
}
}
}
// This is a bit hacky. Because pieforms will take values from
// $_POST before 'defaultvalue's of form elements, we need to nuke
// all of the post values for the form. The situation where this
// becomes relevant is when someone clicks the configure button for
// one block, then immediately configures another block
foreach (array_keys($elements) as $name) {
unset($_POST[$name]);
}
return array('html' => $pieform->build(false), 'js' => $js);
return $bi->build_configure_form();
}
/**
......
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