Skip to content

Commit

Permalink
MDL-19756 Improved API of popup_form and corrected a few problems (su…
Browse files Browse the repository at this point in the history
…bmit button when no JS etc.)
  • Loading branch information
nicolasconnault committed Aug 8, 2009
1 parent 1a192cf commit 43a228b
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 113 deletions.
11 changes: 6 additions & 5 deletions lib/deprecatedlib.php
Expand Up @@ -3280,23 +3280,24 @@ function popup_form($baseurl, $options, $formid, $selected='', $nothing='choose'

foreach ($options as $var => $val) {
$url = new moodle_url($baseurl . $var);
if (!empty($optionsextra[$var])) {
new moodle_url($baseurl . $var . $optionsextra[$var]);
}
$options[$url->out(false, array(), false)] = $val;
unset($options[$var]);
}

$select = moodle_select::make_popup_form($options, $formid, $submitvalue, $selected);
$select = moodle_select::make_popup_form($options, $formid, $selected, $submitvalue);
$select->disabled = $disabled;

if (!empty($optionsextra)) {
debugging('The $optionsextra (11th) param to popup_form is not supported, please improve your code.', DEBUG_DEVELOPER);
}

if ($nothing == 'choose') {
$select->nothinglabel = '';
} else {
$select->nothinglabel = $nothing;
}

$select->set_label($selectlabel, $select->id);
$select->set_label($selectlabel);
$select->set_help_icon($help, $helptext);

$output = $OUTPUT->select($select);
Expand Down
248 changes: 140 additions & 108 deletions lib/outputlib.php
Expand Up @@ -2512,6 +2512,9 @@ public function button($form) {

$buttonoutput = $this->output_empty_tag('input', $buttonattributes);

// Removing the button so it doesn't get output again
unset($form->button);

return $this->form($form, $buttonoutput);
}

Expand All @@ -2527,11 +2530,27 @@ public function form($form, $contents=null) {
$form = clone($form);
$form->prepare();
$this->prepare_event_handlers($form);
$buttonoutput = null;

if (empty($contents) && !empty($form->button)) {
debugging("You probably want to use \$OUTPUT->button(\$form), please read that function's documentation", DEBUG_DEVELOPER);
} else if (empty($contents)) {
$contents = $this->output_empty_tag('input', array('type' => 'submit', 'value' => get_string('ok')));
} else if (!empty($form->button)) {
$form->button->prepare();
$buttonoutput = $this->output_start_tag('div', array('id' => "noscript$form->id"));
$this->prepare_event_handlers($form->button);

$buttonattributes = array('class' => $form->button->get_classes_string(),
'type' => 'submit',
'value' => $form->button->text,
'disabled' => $form->button->disabled,
'id' => $form->button->id);

$buttonoutput .= $this->output_empty_tag('input', $buttonattributes);
$buttonoutput .= $this->output_end_tag('div');
$this->page->requires->js_function_call('hide_item', array("noscript$form->id"));

}

$hiddenoutput = '';
Expand All @@ -2546,7 +2565,7 @@ public function form($form, $contents=null) {
'id' => $form->id,
'class' => $form->get_classes_string());

$divoutput = $this->output_tag('div', array(), $hiddenoutput . $contents);
$divoutput = $this->output_tag('div', array(), $hiddenoutput . $contents . $buttonoutput);
$formoutput = $this->output_tag('form', $formattributes, $divoutput);
$output = $this->output_tag('div', array('class' => 'singlebutton'), $formoutput);

Expand Down Expand Up @@ -2994,7 +3013,6 @@ public function select($select) {
$html .= $this->checkbox($option, $select->name);
$currentcheckbox++;
}

}

if (!empty($select->form) && $select->form instanceof html_form) {
Expand Down Expand Up @@ -3682,7 +3700,7 @@ public function get_actions() {
* This can be used in two ways:
*
* <pre>
* $component->set_label($elementid, $elementlabel);
* $component->set_label($elementlabel, $elementid);
* // OR
* $label = new html_label();
* $label->for = $elementid;
Expand All @@ -3703,6 +3721,9 @@ public function set_label($text, $for=null) {
} else if (!empty($text)) {
$this->label = new html_label();
$this->label->for = $for;
if (empty($for) && !empty($this->id)) {
$this->label->for = $this->id;
}
$this->label->text = $text;
}
}
Expand Down Expand Up @@ -3821,15 +3842,6 @@ public function prepare() {
$this->nothinglabel = get_string('choosedots');
}

if ($this->rendertype == 'radio' && $this->multiple) {
$this->rendertype = 'checkbox';
}

// If nested is on, or if radio/checkbox rendertype is set, remove the default Choose option
if ($this->nested || $this->rendertype == 'radio' || $this->rendertype == 'checkbox') {
$this->nothinglabel = '';
}

if (!empty($this->label) && !($this->label instanceof html_label)) {
$label = new html_label();
$label->text = $this->label;
Expand All @@ -3839,99 +3851,7 @@ public function prepare() {

$this->add_class('select');

$firstoption = reset($this->options);
if (is_object($firstoption)) {
parent::prepare();
return true;
}

$options = $this->options;

$this->options = array();

if ($this->nested && $this->rendertype != 'menu') {
throw new coding_exception('moodle_select cannot render nested options as radio buttons or checkboxes.');
} else if ($this->nested) {
foreach ($options as $section => $values) {
$optgroup = new html_select_optgroup();
$optgroup->text = $section;

foreach ($values as $value => $display) {
$option = new html_select_option();
$option->value = s($value);
$option->text = $display;
if ($display === '') {
$option->text = $value;
}

if ((string) $value == (string) $this->selectedvalue ||
(is_array($this->selectedvalue) && in_array($value, $this->selectedvalue))) {
$option->selected = 'selected';
}

$optgroup->options[] = $option;
}

$this->options[] = $optgroup;
}
} else {
$inoptgroup = false;
$optgroup = false;

foreach ($options as $value => $display) {
if ($display == '--') { /// we are ending previous optgroup
// $this->options[] = $optgroup;
$inoptgroup = false;
continue;
} else if (substr($display,0,2) == '--') { /// we are starting a new optgroup
if (!empty($optgroup->options)) {
$this->options[] = $optgroup;
}

$optgroup = new html_select_optgroup();
$optgroup->text = substr($display,2); // stripping the --

$inoptgroup = true; /// everything following will be in an optgroup
continue;

} else {
// Add $nothing option if there are not optgroups
if ($this->nothinglabel && empty($this->options[0]) && !$inoptgroup) {
$nothingoption = new html_select_option();
$nothingoption->value = 0;
if (!empty($this->nothingvalue)) {
$nothingoption->value = $this->nothingvalue;
}
$nothingoption->text = $this->nothinglabel;
$this->options = array($nothingoption) + $this->options;
}

$option = new html_select_option();
$option->text = $display;

if ($display === '') {
$option->text = $value;
}

if ((string) $value == (string) $this->selectedvalue ||
(is_array($this->selectedvalue) && in_array($value, $this->selectedvalue))) {
$option->selected = 'selected';
}

$option->value = s($value);

if ($inoptgroup) {
$optgroup->options[] = $option;
} else {
$this->options[] = $option;
}
}
}

if ($optgroup) {
$this->options[] = $optgroup;
}
}
$this->initialise_options();
parent::prepare();
}

Expand Down Expand Up @@ -4045,11 +3965,11 @@ public function make_time_selectors($selectors, $currenttime=0, $step=5) {
* @param string $baseurl The target URL up to the point of the variable that changes
* @param array $options A list of value-label pairs for the popup list
* @param string $formid id for the control. Must be unique on the page. Used in the HTML.
* @param string $submitvalue Optional label for the 'Go' button. Defaults to get_string('go').
* @param string $selected The option that is initially selected
* @param string $submitvalue Optional label for the 'Go' button. Defaults to get_string('go').
* @return moodle_select A menu initialised as a popup form.
*/
public function make_popup_form($options, $formid, $submitvalue='', $selected=null) {
public function make_popup_form($options, $formid, $selected=null, $submitvalue='') {
global $CFG;
$select = self::make($options, 'jump', $selected);
$select->form = new html_form();
Expand Down Expand Up @@ -4097,12 +4017,124 @@ public function set_help_icon($page, $text, $linktext=false) {
if ($page instanceof help_icon) {
$this->helpicon = $page;
} else if (!empty($page)) {
$this->helpicon = new html_label();
$this->helpicon = new help_icon();
$this->helpicon->page = $page;
$this->helpicon->text = $text;
$this->helpicon->linktext = $linktext;
}
}

/**
* Parses the $options array and instantiates html_select_option objects in
* the place of the original value => label pairs. This is useful for when you
* need to setup extra html attributes and actions on individual options before
* the component is sent to the renderer
* @return void;
*/
public function initialise_options() {
// If options are already instantiated objects, stop here
$firstoption = reset($this->options);
if ($firstoption instanceof html_select_option || $firstoption instanceof html_select_optgroup) {
return;
}

if ($this->rendertype == 'radio' && $this->multiple) {
$this->rendertype = 'checkbox';
}

// If nested is on, or if radio/checkbox rendertype is set, remove the default Choose option
if ($this->nested || $this->rendertype == 'radio' || $this->rendertype == 'checkbox') {
$this->nothinglabel = '';
}

$options = $this->options;

$this->options = array();

if ($this->nested && $this->rendertype != 'menu') {
throw new coding_exception('moodle_select cannot render nested options as radio buttons or checkboxes.');
} else if ($this->nested) {
foreach ($options as $section => $values) {
$optgroup = new html_select_optgroup();
$optgroup->text = $section;

foreach ($values as $value => $display) {
$option = new html_select_option();
$option->value = s($value);
$option->text = $display;
if ($display === '') {
$option->text = $value;
}

if ((string) $value == (string) $this->selectedvalue ||
(is_array($this->selectedvalue) && in_array($value, $this->selectedvalue))) {
$option->selected = 'selected';
}

$optgroup->options[] = $option;
}

$this->options[] = $optgroup;
}
} else {
$inoptgroup = false;
$optgroup = false;

foreach ($options as $value => $display) {
if ($display == '--') { /// we are ending previous optgroup
// $this->options[] = $optgroup;
$inoptgroup = false;
continue;
} else if (substr($display,0,2) == '--') { /// we are starting a new optgroup
if (!empty($optgroup->options)) {
$this->options[] = $optgroup;
}

$optgroup = new html_select_optgroup();
$optgroup->text = substr($display,2); // stripping the --

$inoptgroup = true; /// everything following will be in an optgroup
continue;

} else {
// Add $nothing option if there are not optgroups
if ($this->nothinglabel && empty($this->options[0]) && !$inoptgroup) {
$nothingoption = new html_select_option();
$nothingoption->value = 0;
if (!empty($this->nothingvalue)) {
$nothingoption->value = $this->nothingvalue;
}
$nothingoption->text = $this->nothinglabel;
$this->options = array($nothingoption) + $this->options;
}

$option = new html_select_option();
$option->text = $display;

if ($display === '') {
$option->text = $value;
}

if ((string) $value == (string) $this->selectedvalue ||
(is_array($this->selectedvalue) && in_array($value, $this->selectedvalue))) {
$option->selected = 'selected';
}

$option->value = s($value);

if ($inoptgroup) {
$optgroup->options[] = $option;
} else {
$this->options[] = $option;
}
}
}

if ($optgroup) {
$this->options[] = $optgroup;
}
}
}
}

/**
Expand Down

0 comments on commit 43a228b

Please sign in to comment.