[ Index ]

MailPress 544

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


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1