[ Index ]

PHP Cross Reference of moodle-2.8

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 <[email protected]>
  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 <[email protected]>
  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      function MoodleQuickForm_selectwithlink($elementName=null, $elementLabel=null, $options=null, $attributes=null, $linkdata=null)
  66      {
  67          if (!empty($linkdata['link']) && !empty($linkdata['label'])) {
  68              $this->_link = $linkdata['link'];
  69              $this->_linklabel = $linkdata['label'];
  70          }
  71  
  72          if (!empty($linkdata['return'])) {
  73              $this->_linkreturn = $linkdata['return'];
  74          }
  75  
  76          parent::HTML_QuickForm_select($elementName, $elementLabel, $options, $attributes);
  77      }
  78  
  79      /**
  80       * Sets label to be hidden
  81       *
  82       * @param bool $hiddenLabel sets if label should be hidden
  83       */
  84      function setHiddenLabel($hiddenLabel){
  85          $this->_hiddenLabel = $hiddenLabel;
  86      }
  87  
  88      /**
  89       * Returns the SELECT in HTML
  90       *
  91       * @return string
  92       */
  93      function toHtml(){
  94          $retval = '';
  95          if ($this->_hiddenLabel){
  96              $this->_generateId();
  97              $retval = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
  98                          $this->getLabel().'</label>'.parent::toHtml();
  99          } else {
 100               $retval = parent::toHtml();
 101          }
 102  
 103          if (!empty($this->_link)) {
 104              if (!empty($this->_linkreturn) && is_array($this->_linkreturn)) {
 105                  $appendchar = '?';
 106                  if (strstr($this->_link, '?')) {
 107                      $appendchar = '&amp;';
 108                  }
 109  
 110                  foreach ($this->_linkreturn as $key => $val) {
 111                      $this->_link .= $appendchar."$key=$val";
 112                      $appendchar = '&amp;';
 113                  }
 114              }
 115  
 116              $retval .= '<a style="margin-left: 5px" href="'.$this->_link.'">'.$this->_linklabel.'</a>';
 117          }
 118  
 119          return $retval;
 120      }
 121  
 122      /**
 123       * get html for help button
 124       *
 125       * @return string html for help button
 126       */
 127      function getHelpButton(){
 128          return $this->_helpbutton;
 129      }
 130  
 131      /**
 132       * Removes an OPTION from the SELECT
 133       *
 134       * @param string $value Value for the OPTION to remove
 135       */
 136      function removeOption($value)
 137      {
 138          $key=array_search($value, $this->_values);
 139          if ($key!==FALSE and $key!==null) {
 140              unset($this->_values[$key]);
 141          }
 142          foreach ($this->_options as $key=>$option){
 143              if ($option['attr']['value']==$value){
 144                  unset($this->_options[$key]);
 145                  return;
 146              }
 147          }
 148      }
 149  
 150      /**
 151       * Removes all OPTIONs from the SELECT
 152       */
 153      function removeOptions()
 154      {
 155          $this->_options = array();
 156      }
 157  
 158      /**
 159       * Slightly different container template when frozen. Don't want to use a label tag
 160       * with a for attribute in that case for the element label but instead use a div.
 161       * Templates are defined in renderer constructor.
 162       *
 163       * @return string
 164       */
 165      function getElementTemplateType(){
 166          if ($this->_flagFrozen){
 167              return 'static';
 168          } else {
 169              return 'default';
 170          }
 171      }
 172  
 173     /**
 174      * We check the options and return only the values that _could_ have been
 175      * selected. We also return a scalar value if select is not "multiple"
 176      *
 177      * @param array $submitValues submitted values
 178      * @param bool $assoc if true the retured value is associated array
 179      * @return mixed
 180      */
 181      function exportValue(&$submitValues, $assoc = false)
 182      {
 183          if (empty($this->_options)) {
 184              return $this->_prepareValue(null, $assoc);
 185          }
 186  
 187          $value = $this->_findValue($submitValues);
 188          if (is_null($value)) {
 189              $value = $this->getValue();
 190          }
 191          $value = (array)$value;
 192  
 193          $cleaned = array();
 194          foreach ($value as $v) {
 195              foreach ($this->_options as $option) {
 196                  if ((string)$option['attr']['value'] === (string)$v) {
 197                      $cleaned[] = (string)$option['attr']['value'];
 198                      break;
 199                  }
 200              }
 201          }
 202  
 203          if (empty($cleaned)) {
 204              return $this->_prepareValue(null, $assoc);
 205          }
 206          if ($this->getMultiple()) {
 207              return $this->_prepareValue($cleaned, $assoc);
 208          } else {
 209              return $this->_prepareValue($cleaned[0], $assoc);
 210          }
 211      }
 212  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1