| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 3 | //
 | 
        
           |  |  | 4 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 5 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 6 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 7 | // (at your option) any later version.
 | 
        
           |  |  | 8 | //
 | 
        
           |  |  | 9 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 12 | // GNU General Public License for more details.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 14 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 15 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 | /**
 | 
        
           |  |  | 19 |  * Button form element
 | 
        
           |  |  | 20 |  *
 | 
        
           |  |  | 21 |  * Contains HTML class for a button type element
 | 
        
           |  |  | 22 |  *
 | 
        
           |  |  | 23 |  * @package   core_form
 | 
        
           |  |  | 24 |  * @copyright 2007 Jamie Pratt <me@jamiep.org>
 | 
        
           |  |  | 25 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 26 |  */
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | require_once("HTML/QuickForm/button.php");
 | 
        
           |  |  | 29 | require_once('templatable_form_element.php');
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | /**
 | 
        
           |  |  | 32 |  * HTML class for a button type element
 | 
        
           |  |  | 33 |  *
 | 
        
           |  |  | 34 |  * Overloaded {@link HTML_QuickForm_button} to add help button
 | 
        
           |  |  | 35 |  *
 | 
        
           |  |  | 36 |  * @package   core_form
 | 
        
           |  |  | 37 |  * @category  form
 | 
        
           |  |  | 38 |  * @copyright 2007 Jamie Pratt <me@jamiep.org>
 | 
        
           |  |  | 39 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 40 |  */
 | 
        
           |  |  | 41 | class MoodleQuickForm_button extends HTML_QuickForm_button implements templatable
 | 
        
           |  |  | 42 | {
 | 
        
           |  |  | 43 |     use templatable_form_element {
 | 
        
           |  |  | 44 |         export_for_template as export_for_template_base;
 | 
        
           |  |  | 45 |     }
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |     /** @var string html for help button, if empty then no help */
 | 
        
           |  |  | 48 |     var $_helpbutton='';
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |     /** @var bool if true label will be hidden. */
 | 
        
           |  |  | 51 |     protected $_hiddenLabel = false;
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |     /**
 | 
        
           |  |  | 54 |      * Any class apart from 'btn' would be overridden with this content.
 | 
        
           |  |  | 55 |      *
 | 
        
           |  |  | 56 |      * By default, buttons will utilize the btn-secondary class. However, there are cases where we
 | 
        
           |  |  | 57 |      * require a button with different stylings (e.g. btn-primary). In these cases, $customclassoverride will override
 | 
        
           |  |  | 58 |      * the defaults mentioned previously and utilize the provided class(es).
 | 
        
           |  |  | 59 |      *
 | 
        
           |  |  | 60 |      * @var null|string $customclassoverride Custom class override for the input element
 | 
        
           |  |  | 61 |      */
 | 
        
           |  |  | 62 |     protected $customclassoverride;
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |     /**
 | 
        
           |  |  | 65 |      * constructor
 | 
        
           |  |  | 66 |      *
 | 
        
           |  |  | 67 |      * @param string $elementName (optional) name for the button
 | 
        
           |  |  | 68 |      * @param string $value (optional) value for the button
 | 
        
           |  |  | 69 |      * @param mixed $attributes (optional) Either a typical HTML attribute string
 | 
        
           |  |  | 70 |      *              or an associative array
 | 
        
           |  |  | 71 |      * @param array $options Options to further customise the button. Currently accepted options are:
 | 
        
           |  |  | 72 |      *                  customclassoverride String The CSS class to use for the button instead of the standard
 | 
        
           |  |  | 73 |      *                                             btn-primary and btn-secondary classes.
 | 
        
           |  |  | 74 |      */
 | 
        
           |  |  | 75 |     public function __construct($elementName=null, $value=null, $attributes=null, $options = []) {
 | 
        
           |  |  | 76 |         parent::__construct($elementName, $value, $attributes);
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         $this->customclassoverride = $options['customclassoverride'] ?? null;
 | 
        
           |  |  | 79 |     }
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |     /**
 | 
        
           |  |  | 82 |      * Old syntax of class constructor. Deprecated in PHP7.
 | 
        
           |  |  | 83 |      *
 | 
        
           |  |  | 84 |      * @deprecated since Moodle 3.1
 | 
        
           |  |  | 85 |      */
 | 
        
           |  |  | 86 |     public function MoodleQuickForm_button($elementName=null, $value=null, $attributes=null) {
 | 
        
           |  |  | 87 |         debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
 | 
        
           |  |  | 88 |         self::__construct($elementName, $value, $attributes);
 | 
        
           |  |  | 89 |     }
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |     /**
 | 
        
           |  |  | 92 |      * get html for help button
 | 
        
           |  |  | 93 |      *
 | 
        
           |  |  | 94 |      * @return string html for help button
 | 
        
           |  |  | 95 |      */
 | 
        
           |  |  | 96 |     function getHelpButton(){
 | 
        
           |  |  | 97 |         return $this->_helpbutton;
 | 
        
           |  |  | 98 |     }
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |     /**
 | 
        
           |  |  | 101 |      * Slightly different container template when frozen.
 | 
        
           |  |  | 102 |      *
 | 
        
           |  |  | 103 |      * @return string
 | 
        
           |  |  | 104 |      */
 | 
        
           |  |  | 105 |     function getElementTemplateType(){
 | 
        
           |  |  | 106 |         if ($this->_flagFrozen){
 | 
        
           |  |  | 107 |             return 'nodisplay';
 | 
        
           |  |  | 108 |         } else {
 | 
        
           |  |  | 109 |             return 'default';
 | 
        
           |  |  | 110 |         }
 | 
        
           |  |  | 111 |     }
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 |     /**
 | 
        
           |  |  | 114 |      * Sets label to be hidden
 | 
        
           |  |  | 115 |      *
 | 
        
           |  |  | 116 |      * @param bool $hiddenLabel sets if label should be hidden
 | 
        
           |  |  | 117 |      */
 | 
        
           |  |  | 118 |     public function setHiddenLabel($hiddenLabel) {
 | 
        
           |  |  | 119 |         $this->_hiddenLabel = $hiddenLabel;
 | 
        
           |  |  | 120 |     }
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |     public function export_for_template(renderer_base $output) {
 | 
        
           |  |  | 123 |         $context = $this->export_for_template_base($output);
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 |         if ($this->customclassoverride) {
 | 
        
           |  |  | 126 |             $context['customclassoverride'] = $this->customclassoverride;
 | 
        
           |  |  | 127 |         }
 | 
        
           |  |  | 128 |         return $context;
 | 
        
           |  |  | 129 |     }
 | 
        
           |  |  | 130 | }
 |