From 3cd5305f11e86043bde30ef5a120a8c452c0aa7e Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 3 Jan 2010 10:28:29 +0000 Subject: [PATCH] MDL-21198 towards origianl single_button() syntax + other improvements --- lib/outputcomponents.php | 17 +++++++++-------- lib/outputrenderers.php | 25 +++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index e5c3f66d86..c1f0d07a48 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -1521,9 +1521,10 @@ class html_form extends html_component { * Constructor: sets up the other components in case they are needed * @return void */ - public function __construct() { + public function __construct(array $options = null) { + parent::__construct($options); $this->button = new html_button(); - $this->button->text = get_string('yes'); + $this->button->text = get_string('ok'); } /** @@ -1547,14 +1548,14 @@ class html_form extends html_component { parent::prepare($output, $page, $target); } - public static function make_button($url, array $params=null, $label=null, $method='post') { - if ($label === null) { - $label = get_string('ok'); - } - $form = new html_form(); + public static function make_button($url, array $params=null, $label=null, $method='post', array $formoptions=null) { + $form = new html_form($formoptions); $form->url = new moodle_url($url, $params); - $form->button->text = $label; + if ($label !== null) { + $form->button->text = $label; + } $form->method = $method; + return $form; } } diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 2192839db4..6461ae5468 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -993,13 +993,34 @@ class core_renderer extends renderer_base { return $output; } + /** + * Returns a form with single button. + * + * @param string|moodle_url|html_form $url_or_form + * @param string $label button text + * @param string $method get or post submit method + * @return string HTML fragment + */ + public function single_button($url_or_form, $label=null, $method='get') { + if ($url_or_form instanceof html_form) { + $form = clone($url_or_form); + if (!is_null($label)) { + $form->button->text = $label; + } + } else { + $form = html_form::make_button($url_or_form, null, $label, $method); + } + + return $this->button($form); + } + /** * Given a html_form object, outputs an tag within a form that uses the object's attributes. * * @param html_form $form A html_form object * @return string HTML fragment */ - public function button($form) { + public function button(html_form $form) { if (empty($form->button) or !($form->button instanceof html_button)) { throw new coding_exception('$OUTPUT->button($form) requires $form to have a button (html_button) value'); } @@ -1030,7 +1051,7 @@ class core_renderer extends renderer_base { * @param string $contents HTML fragment to put inside the form. If given, must contain at least the submit button. * @return string HTML fragment */ - public function form($form, $contents=null) { + public function form(html_form $form, $contents=null) { $form = clone($form); $form->prepare($this, $this->page, $this->target); $this->prepare_event_handlers($form); -- 2.39.5