[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/htmlform/ -> HTMLMultiSelectField.php (source)

   1  <?php
   2  
   3  /**
   4   * Multi-select field
   5   */
   6  class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
   7  	function validate( $value, $alldata ) {
   8          $p = parent::validate( $value, $alldata );
   9  
  10          if ( $p !== true ) {
  11              return $p;
  12          }
  13  
  14          if ( !is_array( $value ) ) {
  15              return false;
  16          }
  17  
  18          # If all options are valid, array_intersect of the valid options
  19          # and the provided options will return the provided options.
  20          $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
  21  
  22          $validValues = array_intersect( $value, $validOptions );
  23          if ( count( $validValues ) == count( $value ) ) {
  24              return true;
  25          } else {
  26              return $this->msg( 'htmlform-select-badoption' )->parse();
  27          }
  28      }
  29  
  30  	function getInputHTML( $value ) {
  31          $value = HTMLFormField::forceToStringRecursive( $value );
  32          $html = $this->formatOptions( $this->getOptions(), $value );
  33  
  34          return $html;
  35      }
  36  
  37  	function formatOptions( $options, $value ) {
  38          $html = '';
  39  
  40          $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
  41          $elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
  42  
  43          foreach ( $options as $label => $info ) {
  44              if ( is_array( $info ) ) {
  45                  $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
  46                  $html .= $this->formatOptions( $info, $value );
  47              } else {
  48                  $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
  49  
  50                  // @todo: Make this use checkLabel for consistency purposes
  51                  $checkbox = Xml::check(
  52                      $this->mName . '[]',
  53                      in_array( $info, $value, true ),
  54                      $attribs + $thisAttribs
  55                  );
  56                  $checkbox .= '&#160;' . call_user_func( $elementFunc,
  57                      'label',
  58                      array( 'for' => "{$this->mID}-$info" ),
  59                      $label
  60                  );
  61  
  62                  $html .= ' ' . Html::rawElement(
  63                      'div',
  64                      array( 'class' => 'mw-htmlform-flatlist-item' ),
  65                      $checkbox
  66                  );
  67              }
  68          }
  69  
  70          return $html;
  71      }
  72  
  73      /**
  74       * @param WebRequest $request
  75       *
  76       * @return string
  77       */
  78  	function loadDataFromRequest( $request ) {
  79          if ( $this->mParent->getMethod() == 'post' ) {
  80              if ( $request->wasPosted() ) {
  81                  # Checkboxes are just not added to the request arrays if they're not checked,
  82                  # so it's perfectly possible for there not to be an entry at all
  83                  return $request->getArray( $this->mName, array() );
  84              } else {
  85                  # That's ok, the user has not yet submitted the form, so show the defaults
  86                  return $this->getDefault();
  87              }
  88          } else {
  89              # This is the impossible case: if we look at $_GET and see no data for our
  90              # field, is it because the user has not yet submitted the form, or that they
  91              # have submitted it with all the options unchecked? We will have to assume the
  92              # latter, which basically means that you can't specify 'positive' defaults
  93              # for GET forms.
  94              # @todo FIXME...
  95              return $request->getArray( $this->mName, array() );
  96          }
  97      }
  98  
  99  	function getDefault() {
 100          if ( isset( $this->mDefault ) ) {
 101              return $this->mDefault;
 102          } else {
 103              return array();
 104          }
 105      }
 106  
 107  	function filterDataForSubmit( $data ) {
 108          $data = HTMLFormField::forceToStringRecursive( $data );
 109          $options = HTMLFormField::flattenOptions( $this->getOptions() );
 110  
 111          $res = array();
 112          foreach ( $options as $opt ) {
 113              $res["$opt"] = in_array( $opt, $data, true );
 114          }
 115  
 116          return $res;
 117      }
 118  
 119  	protected function needsLabel() {
 120          return false;
 121      }
 122  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1