GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-time.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-time.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
8 /**
9  * Add custom options for date fields
10  */
12 
13  var $name = 'time';
14 
15  var $is_searchable = true;
16 
17  var $search_operators = array( 'is', 'isnot', 'greater_than', 'less_than' );
18 
19  /** @see GF_Field_Time */
20  var $_gf_field_class_name = 'GF_Field_Time';
21 
22  var $group = 'advanced';
23 
24  var $icon = 'dashicons-clock';
25 
26  /**
27  * @internal Do not define. This is overridden by the class using a filter.
28  * @todo Fix using variable for time field
29  */
31 
32  /**
33  * @var string The part of the Gravity Forms query that's modified to enable sorting by time. `value` gets replaced.
34  * @since 1.14
35  */
36  const GF_SORTING_SQL = 'SELECT 0 as query, lead_id as id, value';
37 
38  /**
39  * @var string Used to implode and explode the custom sort key for query modification.
40  * @since 1.14
41  */
42  private $_sort_divider = '|:time:|';
43 
44  /**
45  * @var string Used to store the time format for the field ("12" or "24") so it can be used in the query filter
46  * @since 1.14
47  */
48  private $_time_format = null;
49 
50  /**
51  * @var string Used to store the date format for the field, based on the input being displayed, so it can be used in the query filter
52  * @since 1.14
53  */
54  private $_date_format = null;
55 
56  /**
57  * GravityView_Field_Time constructor.
58  */
59  public function __construct() {
60 
61  $this->label = esc_html__( 'Time', 'gk-gravityview' );
62 
63  parent::__construct();
64 
65  add_filter( 'gravityview/sorting/time', array( $this, 'modify_sort_id' ), 10, 2 );
66 
67  add_filter('gravityview_search_criteria', array( $this, '_maybe_filter_gravity_forms_query' ), 10, 4 );
68  }
69 
70  /**
71  * Modify the sort key for the time field so it can be parsed by the query filter
72  *
73  * @see _modify_query_sort_by_time_hack
74  *
75  * @since 1.14
76  * @param string $sort_field_id Existing sort field ID (like "5")
77  * @param int $form_id Gravity Forms Form ID being sorted
78  *
79  * @return string Modified sort key imploded with $_sort_divider, like `5|:time:|12|:time:|h:i A`
80  */
81  public function modify_sort_id( $sort_field_id, $form_id ) {
82 
83  $time_format = self::_get_time_format_for_field( $sort_field_id, $form_id );
84 
85  $date_format = self::date_format( $time_format, $sort_field_id );
86 
87  // Should look something like `5|:time:|12|:time:|h:i A`
88  $new_sort_field_id = implode( $this->_sort_divider, array( $sort_field_id, $time_format, $date_format ) );
89 
90  return $new_sort_field_id;
91  }
92 
93  /**
94  * If the sorting key matches the key set in modify_sort_id(), then modify the Gravity Forms query SQL
95  *
96  * @since 1.14
97  * @see modify_sort_id()
98  *
99  * @param array $criteria Search criteria used by GravityView
100  * @param array $form_ids Forms to search
101  * @param int $view_id ID of the view being used to search
102  *
103  * @return array $criteria If a match, the sorting will be updated to set `is_numeric` to true and make sure the field ID is an int
104  */
105  public function _maybe_filter_gravity_forms_query( $criteria, $form_ids, $view_id ) {
106 
107  // If the search is not being sorted, return early
108  if( empty( $criteria['sorting']['key'] ) ) {
109  return $criteria;
110  }
111 
112  $pieces = explode( $this->_sort_divider, $criteria['sorting']['key'] );
113 
114  /**
115  * If the sort key does not match the key set in modify_sort_id(), do not modify the Gravity Forms query SQL
116  * @see modify_sort_id()
117  */
118  if( empty( $pieces[1] ) ) {
119  return $criteria;
120  }
121 
122  // Pass these to the _modify_query_sort_by_time_hack() method
123  $this->_time_format = $pieces[1];
124  $this->_date_format = $pieces[2];
125 
126  // Remove fake input IDs (5.1 doesn't exist. Use 5)
127  $criteria['sorting']['key'] = floor( $pieces[0] );
128 
129  /**
130  * Make sure sorting is numeric (# of seconds). IMPORTANT.
131  * @see GVCommon::is_field_numeric() is_numeric should also be set here
132  */
133  $criteria['sorting']['is_numeric'] = true;
134 
135  // Modify the Gravity Forms WP Query
136  add_filter('query', array( $this, '_modify_query_sort_by_time_hack' ) );
137 
138  return $criteria;
139  }
140 
141  /**
142  * Modify Gravity Forms query SQL to convert times to numbers
143  * Gravity Forms couldn't sort by time...until NOW
144  *
145  * @since 1.14
146  * @param string $query MySQL query
147  *
148  * @return string Modified query, if the query matches the expected Gravity Forms SQL string used for sorting time fields. Otherwise, original query.
149  */
150  function _modify_query_sort_by_time_hack( $query ) {
151 
152  /**
153  * If this is a Gravity Forms entry selection sorting query, generated by sort_by_field_query(),
154  * then we want to modify the query.
155  * @see GFFormsModel::sort_by_field_query()
156  */
157  if( strpos( $query, self::GF_SORTING_SQL ) > 0 ) {
158 
159  if( $this->_time_format === '24' ) {
160  $sql_str_to_date = "STR_TO_DATE( `value`, '%H:%i' )";
161  } else {
162  $sql_str_to_date = "STR_TO_DATE( `value`, '%h:%i %p' )";
163  }
164 
165  switch ( $this->_date_format ) {
166  case 'h':
167  case 'H':
168  $modification = "TIME_FORMAT( {$sql_str_to_date}, '%H' )";
169  break;
170  case 'i':
171  $modification = "TIME_FORMAT( {$sql_str_to_date}, '%i' )";
172  break;
173  case 'H:i':
174  case 'h:i A':
175  default:
176  $modification = "TIME_TO_SEC( {$sql_str_to_date} )";
177  }
178 
179  /**
180  * Convert the time (12:30 pm) to the MySQL `TIME_TO_SEC()` value for that time (45000)
181  * This way, Gravity Forms is able to sort numerically.
182  */
183  $replacement_query = str_replace( 'value', "{$modification} as value", self::GF_SORTING_SQL );
184 
185  /**
186  * Replace it in the main query
187  */
188  $query = str_replace( self::GF_SORTING_SQL, $replacement_query, $query );
189 
190  /**
191  * REMOVE the Gravity Forms WP Query modifications!
192  */
193  remove_filter( 'query', array( $this, '_modify_query_sort_by_time_hack' ) );
194  }
195 
196  return $query;
197  }
198 
199 
200  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
201 
202  // Set variables
203  parent::field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id );
204 
205  if( 'edit' === $context ) {
206  return $field_options;
207  }
208 
209  /**
210  * Set default date format based on field ID and Form ID
211  */
212  add_filter('gravityview_date_format', array( $this, '_filter_date_display_date_format' ) );
213 
214  $this->add_field_support('date_display', $field_options );
215 
216  remove_filter('gravityview_date_format', array( $this, '_filter_date_display_date_format' ) );
217 
218  return $field_options;
219  }
220 
221  /**
222  * Return the field's time format by fetching the form ID and checking the field settings
223  *
224  * @since 1.14
225  *
226  * @return string Either "12" or "24". "12" is default.
227  */
228  private function _get_time_format() {
229  global $post;
230 
231  $current_form = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : gravityview_get_form_id( $post->ID );
232 
233  return self::_get_time_format_for_field( $this->_field_id, $current_form );
234  }
235 
236  /**
237  * Return the field's time format by fetching the form ID and checking the field settings
238  *
239  * @since 1.14
240  *
241  * @param string $field_id ID for Gravity Forms time field
242  * @param int $form_id ID for Gravity Forms form
243  * @return string Either "12" or "24". "12" is default.
244  */
245  static public function _get_time_format_for_field( $field_id, $form_id = 0 ) {
246 
247  // GF defaults to 12, so should we.
248  $time_format = '12';
249 
250  if( $form_id ) {
251  $form = GFAPI::get_form( $form_id );
252 
253  if ( $form ) {
254  $field = GFFormsModel::get_field( $form, floor( $field_id ) );
255  if ( $field && $field instanceof GF_Field_Time ) {
256  $field->sanitize_settings(); // Make sure time is set
257  $time_format = $field->timeFormat;
258  }
259  }
260  }
261 
262  return $time_format;
263  }
264 
265  /**
266  * Modify the default PHP date formats used by the time field based on the field IDs and the field settings
267  *
268  * @since 1.14
269  *
270  * @return string PHP date() format text to to display the correctly formatted time value for the newly created field
271  */
273 
274  $time_format = $this->_get_time_format();
276 
277  return self::date_format( $time_format, $field_id );
278  }
279 
280  /**
281  * Get the default date format for a field based on the field ID and the time format setting
282  *
283  * @since 1.14
284 
285  * @param string $time_format The time format ("12" or "24"). Default: "12" {@since 1.14}
286  * @param int $field_id The ID of the field. Used to figure out full time/hours/minutes/am/pm {@since 1.14}
287  *
288  * @return string PHP date format for the time
289  */
290  static public function date_format( $time_format = '12', $field_id = 0 ) {
291 
292  $field_input_id = gravityview_get_input_id_from_id( $field_id );
293 
294  $default = 'h:i A';
295 
296  // This doesn't take into account 24-hour
297  switch( $field_input_id ) {
298  // Hours
299  case 1:
300  return ( $time_format === '12' ) ? 'h' : 'H';
301  break;
302  // Minutes
303  case 2:
304  return 'i';
305  break;
306  // AM/PM
307  case 3:
308  return 'A';
309  break;
310  // Full time field
311  case 0:
312  return ( $time_format === '12' ) ? $default : 'H:i';
313  break;
314  }
315 
316  return $default;
317  }
318 
319 }
320 
Modify field settings by extending this class.
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
add_field_support( $key, &$field_options)
_maybe_filter_gravity_forms_query( $criteria, $form_ids, $view_id)
If the sorting key matches the key set in modify_sort_id(), then modify the Gravity Forms query SQL...
global $post
Definition: delete-entry.php:7
if(gravityview() ->plugin->is_GF_25()) $form
gravityview_get_input_id_from_id( $field_id='')
Very commonly needed: get the # of the input based on a full field ID.
$criteria['paging']
Modify the search parameters before the entries are fetched.
_filter_date_display_date_format()
Modify the default PHP date formats used by the time field based on the field IDs and the field setti...
modify_sort_id( $sort_field_id, $form_id)
Modify the sort key for the time field so it can be parsed by the query filter.
static _get_time_format_for_field( $field_id, $form_id=0)
Return the field&#39;s time format by fetching the form ID and checking the field settings.
gravityview_get_form_id( $view_id)
Get the connected form ID from a View ID.
if(empty( $created_by)) $form_id
__construct()
GravityView_Field_Time constructor.
Add custom options for date fields.
_modify_query_sort_by_time_hack( $query)
Modify Gravity Forms query SQL to convert times to numbers Gravity Forms couldn&#39;t sort by time...
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22
_get_time_format()
Return the field&#39;s time format by fetching the form ID and checking the field settings.
$current_form
Definition: data-source.php:13
static date_format( $time_format='12', $field_id=0)
Get the default date format for a field based on the field ID and the time format setting...