[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  class MP_Form_field_type_time extends MP_form_field_type_
   3  {
   4      var $file            = __FILE__;
   5  
   6      var $id            = 'time';
   7      var $field_not_input     = true;
   8  
   9      var $category         = 'composite';
  10      var $order            = 71;
  11  
  12  	function get_name( $field )
  13      {
  14          $this->field = $field;
  15  
  16          return $this->prefix.'['.$this->field->form_id . ']['. $this->field->id . ']' .  ( ( isset( $this->field->settings['options']['is'] ) ) ? ( '[' . ( ( $this->field->settings['options']['is'] == 'am_pm' ) ? $this->prefix . $this->field->settings['options']['is'] : $this->field->settings['options']['is'] ) . ']' ) : '' ) ;
  17      }
  18  
  19  	function get_id( $field )
  20      {
  21          $this->field = $field;
  22  
  23          return $this->prefix  .  $this->field->form_id . '_' . $this->field->id .        ( ( isset( $this->field->settings['options']['is'] ) ) ? ( '_' . ( ( $this->field->settings['options']['is'] == 'am_pm' ) ? $this->field->settings['attributes']['value']           : $this->field->settings['options']['is'] )       ) : '' ) ;
  24      }
  25  
  26  	public static function valid_date( $y, $m, $d )
  27      {
  28          $maxd = array( 31,( !( $y%4 ) && ( $y%100 || !( $y%400 ) ) ) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
  29          if ( $d > $maxd[$m - 1] ) return false;
  30          return true;
  31      }
  32  
  33  	function submitted( $field )
  34      {
  35          $this->field = $field;
  36  
  37          $value = $this->get_value();
  38  
  39          $required     = ( isset( $this->field->settings['controls']['required'] ) && $this->field->settings['controls']['required'] );
  40          $empty     = ( empty( $value['h'] ) || empty( $value['mn'] ) );
  41  
  42          if ( $required && $empty )
  43          {
  44              $this->field->submitted['on_error'] = 1;
  45              return $this->field;
  46          }
  47  
  48          $format = $this->field->settings['options']['mail_time_format'];
  49          if ( empty( $format ) ) $format = get_option( 'time_format' );
  50  
  51          $this->field->submitted['value'] = $value;
  52          $this->field->submitted['text']  = date( $format, mktime( $value['h'], $value['mn'] ) );
  53          if ( isset( $value[$this->prefix . 'am_pm'] ) ) $this->field->submitted['text'] .= ' '  . $value[$this->prefix . 'am_pm'];
  54          if ( isset( $value['tz'] ) )                 $this->field->submitted['text'] .= ' ( ' . $value['tz'] . ' )'; 
  55  
  56          return $this->field;
  57      }
  58  
  59  	function attributes_filter( $no_reset )
  60      {
  61          if ( !isset( $this->field->settings['options']['form_time_init_value'] ) )
  62              $this->field->settings['options']['form_time_init_value'] = '0';
  63  
  64          if ( '1' == $this->field->settings['options']['form_time_init_value'] )
  65          {
  66                  $this->field->settings['options']['form_time_init_value'] = '0';
  67  
  68              if ( $x = MP_Ip::get_latlng( filter_input( INPUT_SERVER, 'REMOTE_ADDR' ) ) )
  69              {
  70                  $ip_url = 'http://ws.geonames.org/timezone?lat=' . $x['lat'] . '&lng=' .  $x['lng'] ;
  71                  if ( $content = @file_get_contents( $ip_url ) )
  72                  {
  73                      if ( $xml = simplexml_load_string( $content ) )
  74                      {
  75                          $timestamp = strtotime( ( string ) $xml->timezone->time );
  76                          $tzstring  = ( string ) $xml->timezone->timezoneId;
  77  
  78                          if ( ( $timestamp > 0 ) && $tzstring )
  79                          {
  80                              $this->field->settings['options']['form_time_init_value'] = '1';
  81                          }
  82                      }
  83                  }
  84              }
  85          }
  86  
  87          if ( '0' == $this->field->settings['options']['form_time_init_value'] )
  88          {
  89              $timestamp = strtotime( current_time( 'mysql' ) );
  90              if ( isset( $this->field->settings['options']['form_timezones'] ) ) 
  91              {
  92                  $current_offset = get_option( 'gmt_offset' );
  93                  $tzstring = get_option( 'timezone_string' );
  94  
  95                  $check_zone_info = true;
  96  
  97  // Remove old Etc mappings.  Fallback to gmt_offset.
  98                  if ( false !== strpos( $tzstring,'Etc/GMT' ) )
  99                      $tzstring = '';
 100  
 101  // set the Etc zone if no timezone string exists
 102                  if ( empty( $tzstring ) )
 103                  {
 104                      $check_zone_info = false;
 105                      if ( 0 == $current_offset )
 106                          $tzstring = 'UTC+0';
 107                      elseif ( $current_offset < 0 )
 108                          $tzstring = 'UTC' . $current_offset;
 109                      else
 110                          $tzstring = 'UTC+' . $current_offset;
 111                  }
 112              }
 113          }
 114          $this->field->settings['options']['timestamp'] = $timestamp;
 115  // hours
 116          $list     = array();
 117          $start    = -1;
 118          $max        = ( '0' == $this->field->settings['options']['form_time_format'] ) ? 23 : 12;
 119          do { $start++; $k = $start; if ( $k < 10 ) $k = '0' . $k; $v = $k; $list[$k] = $v; } while ( $start < $max );
 120  
 121             $selectedh    = ( '0' == $this->field->settings['options']['form_time_format'] ) ? date( 'H', $timestamp ) : date( 'h', $timestamp );
 122          $this->field->settings['options']['tag_content_h'] = MP_::select_option( $list, $selectedh, false );
 123  // minutes
 124          $list     = array();
 125          $start    = -1;
 126          $max        = 59;
 127          do { $start++; $k = $start; if ( $k < 10 ) $k = '0' . $k; $v = $k; $list[$k] = $v; } while ( $start < $max );
 128  
 129             $selectedmn    = date( 'i' );
 130          $this->field->settings['options']['tag_content_mn'] = MP_::select_option( $list, $selectedmn, false );
 131  // timezones
 132          if ( isset( $this->field->settings['options']['form_timezones'] ) ) 
 133          {
 134              $this->field->settings['options']['tag_content_tz'] = wp_timezone_choice( $tzstring );
 135          }
 136  
 137          if ( !$no_reset ) return;
 138          
 139          $this->field->settings['options']['value'] = $this->get_value();
 140  
 141          $html = MP_Form_field_type_select::no_reset( $this->field->settings['options']['tag_content_h'], $this->field->settings['options']['value']['h'] );
 142          $this->field->settings['options']['tag_content_h'] = ( $html ) ? $html : '<!-- ' . htmlspecialchars( __( 'invalid select options', 'MailPress' ) ) . ' -->';
 143          $html = MP_Form_field_type_select::no_reset( $this->field->settings['options']['tag_content_mn'], $this->field->settings['options']['value']['mn'] );
 144          $this->field->settings['options']['tag_content_mn'] = ( $html ) ? $html : '<!-- ' . htmlspecialchars( __( 'invalid select options', 'MailPress' ) ) . ' -->';
 145          if ( isset( $this->field->settings['options']['form_timezones'] ) )
 146          {    
 147              $html = MP_Form_field_type_select::no_reset( $this->field->settings['options']['tag_content_tz'], $this->field->settings['options']['value']['tz'] );
 148              $this->field->settings['options']['tag_content_tz'] = ( $html ) ? $html : '<!-- ' . htmlspecialchars( __( 'invalid select options', 'MailPress' ) ) . ' -->';
 149          }
 150  
 151          $this->attributes_filter_css();
 152      }
 153  
 154  	function build_tag()
 155      {
 156          $timestamp = $this->field->settings['options']['timestamp'];
 157  
 158          $this->field->type = 'select';
 159  // hours
 160          $this->field->settings['attributes']['tag_content'] = $this->field->settings['options']['tag_content_h'];
 161          $this->field->settings['options']['is'] = 'h';
 162          $id_h  = $this->get_id( $this->field );
 163          $tag_h = parent::build_tag();
 164  // minutes
 165          $this->field->settings['attributes']['tag_content'] = $this->field->settings['options']['tag_content_mn'];
 166          $this->field->settings['options']['is'] = 'mn';
 167          $id_mn  = $this->get_id( $this->field );
 168          $tag_mn = parent::build_tag();
 169  
 170  // timezones
 171          $id_tz  = $tag_tz = '';
 172          if ( isset( $this->field->settings['options']['form_timezones'] ) )
 173          {
 174              $this->field->settings['attributes']['tag_content'] = $this->field->settings['options']['tag_content_tz'];
 175              $this->field->settings['options']['is'] = 'tz';
 176              $id_tz  = $this->get_id( $this->field );
 177              $tag_tz = parent::build_tag();
 178          }
 179  
 180  // am pm
 181          $tag_am = $id_am  = $text_am = $tag_pm = $id_pm  = $text_pm = '';
 182          if ( '0' != $this->field->settings['options']['form_time_format'] )
 183          {
 184              unset( $this->field_not_input );
 185  
 186              $this->field->type = 'radio';
 187              $this->field->settings['attributes']['type']  = 'radio';
 188              $this->field->settings['attributes']['name']  = 'am_pm';
 189              $this->field->settings['options']['is'] = 'am_pm';
 190  
 191              $this->field->settings['attributes']['value'] = 'am';
 192              if ( date( 'G', $timestamp ) < 12 ) $this->field->settings['attributes']['checked'] = 'checked';
 193              $tag_am = parent::build_tag();
 194              $id_am  = $this->get_id( $this->field );
 195              $text_am= __( 'am', 'MailPress' );
 196  
 197              $this->field->settings['attributes']['value'] = 'pm';
 198              if ( date( 'G', $timestamp ) >= 12 ) $this->field->settings['attributes']['checked'] = 'checked';
 199              $tag_pm = parent::build_tag();
 200              $id_pm  = $this->get_id( $this->field );
 201              $text_pm= __( 'pm', 'MailPress' );
 202  
 203              $this->field_not_input = true;
 204          }
 205  
 206          $this->field->type = $this->id;
 207  
 208          $sf  = '';
 209          $sf  = ( '0' != $this->field->settings['options']['form_time_format'] ) ? 'ampm' : '';
 210          $sf .= ( isset( $this->field->settings['options']['form_timezones'] ) )   ? ( ( empty( $sf ) ) ? 'tz' : '_tz' ) : '';
 211          if ( empty( $sf ) ) $sf = 'alone';
 212  
 213          $form_formats['alone']         = '{{h}}&#160;:&#160;{{mn}}';
 214          $form_formats['ampm']         = '{{h}}&#160;:&#160;{{mn}}&#160;{{am}}&#160;<label id="{{id_am}}_label" for="{{id_am}}">{{text_am}}</label>&#160;{{pm}}&#160;<label id="{{id_pm}}_label" for="{{id_pm}}">{{text_pm}}</label>';
 215          $form_formats['tz']         = '{{h}}&#160;:&#160;{{mn}}&#160;{{tz}}';
 216          $form_formats['ampm_tz']     = '{{h}}&#160;:&#160;{{mn}}&#160;{{am}}&#160;<label id="{{id_am}}_label" for="{{id_am}}">{{text_am}}</label>&#160;{{pm}}&#160;<label id="{{id_pm}}_label" for="{{id_pm}}">{{text_pm}}</label>&#160;{{tz}}';
 217  
 218          $form_formats = $this->get_formats( $form_formats );
 219  
 220          $search[] = '{{h}}';        $replace[] = '%1$s';
 221          $search[] = '{{id_h}}';     $replace[] = '%2$s';
 222          $search[] = '{{mn}}';         $replace[] = '%3$s';
 223          $search[] = '{{id_mn}}';    $replace[] = '%4$s';
 224  
 225          $search[] = '{{am}}';        $replace[] = '%5$s';
 226          $search[] = '{{id_am}}';    $replace[] = '%6$s';
 227          $search[] = '{{text_am}}';    $replace[] = '%7$s';
 228  
 229          $search[] = '{{pm}}';        $replace[] = '%8$s';
 230          $search[] = '{{id_pm}}';    $replace[] = '%9$s';
 231          $search[] = '{{text_pm}}';    $replace[] = '%10$s';
 232  
 233          $search[] = '{{tz}}';        $replace[] = '%11$s';
 234          $search[] = '{{id_tz}}';    $replace[] = '%12$s';
 235  
 236          $html = str_replace( $search, $replace, $form_formats[$sf] );
 237          return sprintf( $html, $tag_h, $id_h, $tag_mn, $id_mn, $tag_am, $id_am, $text_am, $tag_pm, $id_pm, $text_pm, $tag_tz, $id_tz );
 238      }
 239  }
 240  new MP_Form_field_type_time( __( 'Time', 'MailPress' ) );


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