[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/form/ -> selectwithlink.php (source)

   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   * select type form element
  20   *
  21   * Contains HTML class for a select type element with options containing link
  22   *
  23   * @package   core_form
  24   * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  require_once('HTML/QuickForm/select.php');
  29  
  30  /**
  31   * select type form element
  32   *
  33   * HTML class for a select type element with options containing link
  34   *
  35   * @package   core_form
  36   * @category  form
  37   * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class MoodleQuickForm_selectwithlink extends HTML_QuickForm_select{
  41      /** @var string html for help button, if empty then no help */
  42      var $_helpbutton='';
  43  
  44      /** @var bool if true label will be hidden */
  45      var $_hiddenLabel=false;
  46  
  47      /** @var string url to which select option will be posted */
  48      var $_link=null;
  49  
  50      /** @var string data which will be posted to link */
  51      var $_linklabel=null;
  52  
  53      /** @var string url return link */
  54      var $_linkreturn=null;
  55  
  56      /**
  57       * constructor
  58       *
  59       * @param string $elementName Select name attribute
  60       * @param mixed $elementLabel Label(s) for the select
  61       * @param array $options Data to be used to populate options
  62       * @param mixed $attributes Either a typical HTML attribute string or an associative array
  63       * @param bool $linkdata data to be posted
  64       */
  65      public function __construct($elementName=null, $elementLabel=null, $options=null, $attributes=null, $linkdata=null) {
  66          if (!empty($linkdata['link']) && !empty($linkdata['label'])) {
  67              $this->_link = $linkdata['link'];
  68              $this->_linklabel = $linkdata['label'];
  69          }
  70  
  71          if (!empty($linkdata['return'])) {
  72              $this->_linkreturn = $linkdata['return'];
  73          }
  74  
  75          parent::__construct($elementName, $elementLabel, $options, $attributes);
  76      }
  77  
  78      /**
  79       * Old syntax of class constructor. Deprecated in PHP7.
  80       *
  81       * @deprecated since Moodle 3.1
  82       */
  83      public function MoodleQuickForm_selectwithlink($elementName=null, $elementLabel=null, $options=null, $attributes=null, $linkdata=null) {
  84          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  85          self::__construct($elementName, $elementLabel, $options, $attributes, $linkdata);
  86      }
  87  
  88      /**
  89       * Sets label to be hidden
  90       *
  91       * @param bool $hiddenLabel sets if label should be hidden
  92       */
  93      function setHiddenLabel($hiddenLabel){
  94          $this->_hiddenLabel = $hiddenLabel;
  95      }
  96  
  97      /**
  98       * Returns the SELECT in HTML
  99       *
 100       * @return string
 101       */
 102      function toHtml(){
 103          $retval = '';
 104          if ($this->_hiddenLabel){
 105              $this->_generateId();
 106              $retval = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
 107                          $this->getLabel().'</label>'.parent::toHtml();
 108          } else {
 109               $retval = parent::toHtml();
 110          }
 111  
 112          if (!empty($this->_link)) {
 113              if (!empty($this->_linkreturn) && is_array($this->_linkreturn)) {
 114                  $appendchar = '?';
 115                  if (strstr($this->_link, '?')) {
 116                      $appendchar = '&amp;';
 117                  }
 118  
 119                  foreach ($this->_linkreturn as $key => $val) {
 120                      $this->_link .= $appendchar."$key=$val";
 121                      $appendchar = '&amp;';
 122                  }
 123              }
 124  
 125              $retval .= '<a style="margin-left: 5px" href="'.$this->_link.'">'.$this->_linklabel.'</a>';
 126          }
 127  
 128          return $retval;
 129      }
 130  
 131      /**
 132       * get html for help button
 133       *
 134       * @return string html for help button
 135       */
 136      function getHelpButton(){
 137          return $this->_helpbutton;
 138      }
 139  
 140      /**
 141       * Removes an OPTION from the SELECT
 142       *
 143       * @param string $value Value for the OPTION to remove
 144       */
 145      function removeOption($value)
 146      {
 147          $key=array_search($value, $this->_values);
 148          if ($key!==FALSE and $key!==null) {
 149              unset($this->_values[$key]);
 150          }
 151          foreach ($this->_options as $key=>$option){
 152              if ($option['attr']['value']==$value){
 153                  unset($this->_options[$key]);
 154                  return;
 155              }
 156          }
 157      }
 158  
 159      /**
 160       * Removes all OPTIONs from the SELECT
 161       */
 162      function removeOptions()
 163      {
 164          $this->_options = array();
 165      }
 166  
 167      /**
 168       * Slightly different container template when frozen. Don't want to use a label tag
 169       * with a for attribute in that case for the element label but instead use a div.
 170       * Templates are defined in renderer constructor.
 171       *
 172       * @return string
 173       */
 174      function getElementTemplateType(){
 175          if ($this->_flagFrozen){
 176              return 'static';
 177          } else {
 178              return 'default';
 179          }
 180      }
 181  
 182     /**
 183      * We check the options and return only the values that _could_ have been
 184      * selected. We also return a scalar value if select is not "multiple"
 185      *
 186      * @param array $submitValues submitted values
 187      * @param bool $assoc if true the retured value is associated array
 188      * @return mixed
 189      */
 190      function exportValue(&$submitValues, $assoc = false)
 191      {
 192          if (empty($this->_options)) {
 193              return $this->_prepareValue(null, $assoc);
 194          }
 195  
 196          $value = $this->_findValue($submitValues);
 197          if (is_null($value)) {
 198              $value = $this->getValue();
 199          }
 200          $value = (array)$value;
 201  
 202          $cleaned = array();
 203          foreach ($value as $v) {
 204              foreach ($this->_options as $option) {
 205                  if ((string)$option['attr']['value'] === (string)$v) {
 206                      $cleaned[] = (string)$option['attr']['value'];
 207                      break;
 208                  }
 209              }
 210          }
 211  
 212          if (empty($cleaned)) {
 213              return $this->_prepareValue(null, $assoc);
 214          }
 215          if ($this->getMultiple()) {
 216              return $this->_prepareValue($cleaned, $assoc);
 217          } else {
 218              return $this->_prepareValue($cleaned[0], $assoc);
 219          }
 220      }
 221  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1