[ Index ]

MailPress 7.1

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/options/form/field_types/select/ -> select.php (source)

   1  <?php
   2  class MP_Form_field_type_select extends MP_form_field_type_
   3  {
   4      var $file            = __FILE__;
   5  
   6      var $id            = 'select';
   7      var $field_not_input     = true;
   8  
   9      var $category         = 'html';
  10      var $order            = 50;
  11  
  12      const sep            = '::';
  13  
  14  	function get_name( $field ) 
  15      {
  16          $this->field = $field;
  17  
  18          return ( isset( $this->field->settings['attributes']['multiple'] ) ) ? parent::get_name( $this->field ) . '[]' : parent::get_name( $this->field );
  19      }
  20  
  21  	function submitted( $field )
  22      {
  23          $this->field = $field;
  24  
  25          $value = $this->get_value();
  26  
  27          $required     = ( isset( $this->field->settings['controls']['required'] ) && $this->field->settings['controls']['required'] );
  28          $empty    = ( !isset( $value ) );
  29  
  30          if ( $required && $empty )
  31          {
  32              $this->field->submitted['on_error'] = 1;
  33              return $this->field;
  34          }    
  35  
  36          if ( $empty )
  37          {
  38              $this->field->submitted['value'] = false;
  39              $this->field->submitted['text']  = __( 'nothing selected', 'MailPress' );
  40              return $this->field;
  41          }
  42  
  43          if ( !is_array( $value ) ) return parent::submitted( $this->field );
  44  
  45          $this->field->submitted['value'] = $value;
  46          $this->field->submitted['text']  = '';
  47          $i = 0;
  48          foreach( $this->field->submitted['value'] as $v )
  49          {
  50              $i++;
  51              if ( $i == 1 )     $this->field->submitted['text'] .= $v . ( ( count( $this->field->submitted['value'] ) > 1 )   ? ', ' : '' );
  52              else            $this->field->submitted['text'] .= $v . ( ( count( $this->field->submitted['value'] ) != $i ) ? ', ' : '' );
  53          }
  54          return $this->field;
  55      }
  56  
  57  	function attributes_filter( $no_reset )
  58      {
  59          $html = $this->get_select_options( base64_decode( $this->field->settings['attributes']['tag_content'] ), $no_reset );
  60          $this->field->settings['attributes']['tag_content'] = ( $html ) ? $html : '<!-- ' . htmlspecialchars( __( 'invalid select options', 'MailPress' ) ) . ' -->';
  61          $this->attributes_filter_css();
  62      }
  63  
  64  	function get_select_options( $options, $no_reset )
  65      {
  66          $is_options = ( ( ( '<option' == substr( $options, 0, 7 ) ) || ( '<optgroup' == substr( $options, 0, 9 ) ) ) && ( ( '</option>' == substr( $options, -9 ) ) || ( '</optgroup>' == substr( $options, -11 ) ) ) );
  67          if ( !$is_options ) $options = self::convert_custom_format( $options );
  68          if ( !$options ) return false;
  69          if ( !$no_reset ) return $options;
  70          return self::no_reset( $options, $this->get_value() );
  71      }
  72  
  73  	public static function convert_custom_format( $options )
  74      {
  75          $datas = explode( "\n", $options );
  76          unset( $options );
  77          $selected = array();
  78          if ( count( $datas ) > 0 )
  79          {
  80              foreach( $datas as $data )
  81              {
  82                  $ys = explode( self::sep, $data );
  83                  if ( count( $ys ) < 2 ) continue;
  84                  $k = esc_attr( trim( $ys[0] ) );
  85                  $options[$k] = trim( $ys[1] );
  86                  if ( isset( $ys[2] ) && ( 'selected' == trim( $ys[2] ) ) ) $selected[] = $k;
  87              }
  88          }
  89          return ( isset( $options ) ) ? MP_::select_option( $options, $selected, false ) : false;
  90      }
  91  
  92  	public static function no_reset( $options, $post_value )
  93      {
  94          $xml = MP_Xml::sanitize( $options, 'select' );
  95          if ( !$xml ) return false;
  96  
  97          $options = new MP_Xml( $xml );
  98          $options->object = self::get_options_selected( $options->object, $post_value );
  99          $new_options = $options->get_xml( $options->object );
 100          $x = substr( $new_options, 30, -10 );
 101          return $x;
 102      }
 103  
 104  	public static function get_options_selected( $options, $post_value )
 105      {
 106          switch ( true )
 107          {
 108              case ( isset( $options->children ) ) :
 109                  $options->children = self::get_options_selected( $options->children, $post_value );
 110              break;
 111              case ( is_array( $options ) ) :
 112                  foreach( $options as $k => $option ) $options[$k] = self::get_options_selected( $option, $post_value );
 113              break;
 114              default :
 115                  $value = ( isset( $options->attributes['value'] ) ) ? $options->attributes['value'] : $options->textValue;
 116  
 117                  unset( $options->attributes['selected'] );
 118                  if ( is_array( $post_value ) && in_array( $value, $post_value ) ) $options->attributes['selected'] = 'selected';
 119                  if ( $value == $post_value ) $options->attributes['selected'] = 'selected';
 120              break;
 121          }
 122          return $options;
 123      }
 124  }
 125  new MP_Form_field_type_select( __( 'Drop-down list', 'MailPress' ) );


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1