[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  class MP_Form_field_type_date extends MP_form_field_type_
   3  {
   4      var $file            = __FILE__;
   5  
   6      var $id            = 'date';
   7  
   8      var $field_not_input     = true;
   9  
  10      var $category         = 'composite';
  11  
  12      var $order            = 70;
  13  
  14  	function get_name( $field )
  15      {
  16          $this->field = $field;
  17  
  18          return $this->prefix.'['.$this->field->form_id . ']['. $this->field->id . ']' . ( ( isset( $this->field->settings['options']['is'] ) ) ? '[' . $this->field->settings['options']['is'] . ']' : '' );
  19      }
  20  
  21  	function get_id( $field )
  22      {
  23          $this->field = $field;
  24  
  25          return $this->prefix  .  $this->field->form_id . '_' . $this->field->id .       ( ( isset( $this->field->settings['options']['is'] ) ) ? '_' . $this->field->settings['options']['is'] : '' );
  26      }
  27  
  28  	public static function valid_date( $y, $m, $d )
  29      {
  30          $maxd = array( 31, ( !( $y%4 ) && ( $y%100 || !( $y%400 ) ) ) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );  if ( $d > $maxd[$m - 1] ) return false;
  31          return true;
  32      }
  33  
  34  	function submitted( $field )
  35      {
  36          $this->field = $field;
  37  
  38          $value = $this->get_value();
  39  
  40          $required     = ( isset( $this->field->settings['controls']['required'] ) && $this->field->settings['controls']['required'] );
  41          $empty     = ( empty( $value['y'] ) || empty( $value['m'] ) || empty( $value['d'] ) );
  42  
  43          if ( $required && $empty )
  44          {
  45              $this->field->submitted['on_error'] = 1;
  46              return $this->field;
  47          }
  48          if ( !$empty )
  49          {
  50              if ( !self::valid_date( $value['y'], $value['m'], $value['d'] ) )
  51              {
  52                  $this->field->submitted['on_error'] = 1;
  53                  return $this->field;
  54              }
  55          }
  56  
  57          $format = $this->field->settings['options']['mail_date_format'];
  58          if ( empty( $format ) ) $format = get_option( 'date_format' );
  59  
  60          $this->field->submitted['value'] = $value;
  61          $this->field->submitted['text']  =  mysql2date( $format, mktime( 0, 0, 0, $value['m'], $value['d'], $value['y'] ) );
  62  
  63          return $this->field;
  64      }
  65  
  66  	function attributes_filter( $no_reset )
  67      {
  68          $this->field->settings['options']['is'] = 'y';
  69          $id_y = $this->get_id( $this->field );
  70  
  71          $this->field->settings['options']['is'] = 'm';
  72          $id_m = $this->get_id( $this->field );
  73  
  74          $this->field->settings['options']['is'] = 'd';
  75          $id_d = $this->get_id( $this->field );
  76  
  77          $onchange = "var y=document.getElementById( '" . $id_y . "' ).value;var feb=( ( ( y%4==0 )&&( ( !( y%100==0 ) )||( y%400==0 ) ) )?29:28 );var maxd=new Array( 31,feb,31,30,31,30,31,31,30,31,30,31 );var m=document.getElementById( '" . $id_m . "' ).value;var d=document.getElementById( '" . $id_d . "' );for( var j=31;j>28;j-- ){var x=document.getElementById( '" . $id_d . "_'+j );if( x ){if( j>maxd[m-1] ){x.style.display='none';if( d.value==j )d.selectedIndex=maxd[m-1]-1}else{x.style.display='block'}}}";
  78          $onchange = "onchange=\"$onchange\"";
  79          $this->field->settings['attributes']['misc'] = ( isset( $this->field->settings['attributes']['misc'] ) ) ? $this->field->settings['attributes']['misc'] . " $onchange" : $onchange;
  80  
  81  // years
  82          $start    = ( isset( $this->field->settings['options']['year_start_c'] ) ) ? date( 'Y' ) : $this->field->settings['options']['year_start'];
  83          $max        = ( isset( $this->field->settings['options']['year_end_c'] ) )   ? date( 'Y' ) : $this->field->settings['options']['year_end'];
  84             $selected_y    = ( isset( $this->field->settings['options']['value']['y'] ) )   ? $this->field->settings['options']['value']['y'] : date( 'Y' );
  85          $this->field->settings['options']['tag_content_y'] = MP_::select_number( $start, $max, $selected_y, 1, false );
  86  // months
  87          $start    = 0;
  88          $month_f    = $this->field->settings['options']['form_month_format'];
  89             $selected_m     = ( isset( $this->field->settings['options']['value']['m'] ) )   ? $this->field->settings['options']['value']['m'] : date( 'm' );
  90          if ( $month_f != 'n' ) $wpl = new WP_Locale();
  91          do { $start++; $k = $start; if ( $k < 10 ) $k = '0' . $k; $v = $k; if ( 's' == $month_f ) $v = $wpl->get_month_abbrev( $wpl->get_month( $start ) ); if ( 'f' == $month_f ) $v = $wpl->get_month( $start ); $list[$k] = $v; } while ( $start < 12 );
  92          $this->field->settings['options']['tag_content_m'] = MP_::select_option( $list, $selected_m, false );
  93  // days
  94          $start = 0; $days = '';
  95          $selected_d = ( isset( $this->field->settings['options']['value']['d'] ) ) ? $this->field->settings['options']['value']['d'] : date( 'd' );
  96          $maxd = array( 31,( !( $selected_y%4 ) && ( $selected_y%100 || !( $selected_y%400 ) ) ) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); if ( $selected_d > $maxd[$selected_m - 1] ) $selected_d = $maxd[$selected_m - 1] - 1;
  97          do { $start++; $k = $start; if ( $k < 10 ) $k = '0' . $k; $v = $k; $style = ( $start > $maxd[$selected_m - 1] ) ? ' style="display:none;"' : ''; $days .='<option id="' . $id_d . '_' . $k . '" value="' . $k . '"' . MP_::selected( $selected_d, $k, false )  . $style . '>' . $k . '</option>'; } while ( $start < 31 ); $days = "\n$days\n";
  98          $this->field->settings['options']['tag_content_d'] = $days;
  99  
 100          if ( !$no_reset ) return;
 101          
 102          $this->field->settings['options']['value'] = $this->get_value();
 103  
 104          $html = MP_Form_field_type_select::no_reset( $this->field->settings['options']['tag_content_y'], $this->field->settings['options']['value']['y'] );
 105          $this->field->settings['options']['tag_content_y'] = ( $html ) ? $html : '<!-- ' . htmlspecialchars( __( 'invalid select options', 'MailPress' ) ) . ' -->';
 106          $html = MP_Form_field_type_select::no_reset( $this->field->settings['options']['tag_content_m'], $this->field->settings['options']['value']['m'] );
 107          $this->field->settings['options']['tag_content_m'] = ( $html ) ? $html : '<!-- ' . htmlspecialchars( __( 'invalid select options', 'MailPress' ) ) . ' -->';
 108          $html = MP_Form_field_type_select::no_reset( $this->field->settings['options']['tag_content_d'], $this->field->settings['options']['value']['d'] );
 109          $this->field->settings['options']['tag_content_d'] = ( $html ) ? $html : '<!-- ' . htmlspecialchars( __( 'invalid select options', 'MailPress' ) ) . ' -->';
 110  
 111          $this->attributes_filter_css();
 112      }
 113  
 114  	function build_tag()
 115      {
 116  
 117          $this->field->type = 'select';
 118  // years
 119          $this->field->settings['attributes']['tag_content'] = $this->field->settings['options']['tag_content_y'];
 120          $this->field->settings['options']['is'] = 'y';
 121          $id_y = $this->get_id( $this->field );
 122          $tag_y = parent::build_tag();
 123  // months
 124          $this->field->settings['attributes']['tag_content'] = $this->field->settings['options']['tag_content_m'];
 125          $this->field->settings['options']['is'] = 'm';
 126          $id_m = $this->get_id( $this->field );
 127          $tag_m = parent::build_tag();
 128  // days
 129          $this->field->settings['attributes']['tag_content'] = $this->field->settings['options']['tag_content_d'];
 130          $this->field->settings['options']['is'] = 'd';
 131          $id_d = $this->get_id( $this->field );
 132          $tag_d = parent::build_tag();
 133  
 134          $this->field->type = $this->id;
 135  
 136          $form_formats['ymd'] = '{{y}}&#160;{{m}}&#160;{{d}}';
 137          $form_formats['dmy'] = '{{d}}&#160;{{m}}&#160;{{y}}';
 138          $form_formats['mdy'] = '{{m}}&#160;{{d}}&#160;{{y}}';
 139  
 140          $form_formats = $this->get_formats( $form_formats );
 141  
 142          $search[] = '{{y}}';    $replace[] = '%1$s';
 143          $search[] = '{{id_y}}';    $replace[] = '%2$s';
 144          $search[] = '{{m}}';     $replace[] = '%3$s';
 145          $search[] = '{{id_m}}';    $replace[] = '%4$s';
 146          $search[] = '{{d}}';     $replace[] = '%5$s';
 147          $search[] = '{{id_d}}';    $replace[] = '%6$s';
 148  
 149          $html = str_replace( $search, $replace, $form_formats[$this->field->settings['options']['form_date_format']] );
 150          return sprintf( $html, $tag_y, $id_y, $tag_m, $id_m, $tag_d, $id_d );
 151      }
 152  }
 153  new MP_Form_field_type_date( __( 'Date', 'MailPress' ) );


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