[ Index ]

MailPress 544

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


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