1 | <?php |
||
11 | class GravityView_Field_Time extends GravityView_Field { |
||
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 | /** |
||
25 | * @internal Do not define. This is overridden by the class using a filter. |
||
26 | * @todo Fix using variable for time field |
||
27 | */ |
||
28 | var $is_numeric; |
||
29 | |||
30 | /** |
||
31 | * @var string The part of the Gravity Forms query that's modified to enable sorting by time. `value` gets replaced. |
||
32 | * @since 1.14 |
||
33 | */ |
||
34 | const GF_SORTING_SQL = 'SELECT 0 as query, lead_id as id, value'; |
||
35 | |||
36 | /** |
||
37 | * @var string Used to implode and explode the custom sort key for query modification. |
||
38 | * @since 1.14 |
||
39 | */ |
||
40 | private $_sort_divider = '|:time:|'; |
||
41 | |||
42 | /** |
||
43 | * @var string Used to store the time format for the field ("12" or "24") so it can be used in the query filter |
||
44 | * @since 1.14 |
||
45 | */ |
||
46 | private $_time_format = null; |
||
47 | |||
48 | /** |
||
49 | * @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 |
||
50 | * @since 1.14 |
||
51 | */ |
||
52 | private $_date_format = null; |
||
53 | |||
54 | /** |
||
55 | * GravityView_Field_Time constructor. |
||
56 | */ |
||
57 | public function __construct() { |
||
67 | |||
68 | /** |
||
69 | * Modify the sort key for the time field so it can be parsed by the query filter |
||
70 | * |
||
71 | * @see _modify_query_sort_by_time_hack |
||
72 | * |
||
73 | * @since 1.14 |
||
74 | * @param string $sort_field_id Existing sort field ID (like "5") |
||
75 | * @param int $form_id Gravity Forms Form ID being sorted |
||
76 | * |
||
77 | * @return string Modified sort key imploded with $_sort_divider, like `5|:time:|12|:time:|h:i A` |
||
78 | */ |
||
79 | public function modify_sort_id( $sort_field_id, $form_id ) { |
||
90 | |||
91 | /** |
||
92 | * If the sorting key matches the key set in modify_sort_id(), then modify the Gravity Forms query SQL |
||
93 | * |
||
94 | * @since 1.14 |
||
95 | * @see modify_sort_id() |
||
96 | * |
||
97 | * @param array $criteria Search criteria used by GravityView |
||
98 | * @param array $form_ids Forms to search |
||
99 | * @param int $view_id ID of the view being used to search |
||
100 | * |
||
101 | * @return $criteria If a match, the sorting will be updated to set `is_numeric` to true and make sure the field ID is an int |
||
102 | */ |
||
103 | 10 | public function _maybe_filter_gravity_forms_query( $criteria, $form_ids, $view_id ) { |
|
104 | |||
105 | // If the search is being sorted |
||
106 | 10 | if( ! empty( $criteria['sorting']['key'] ) ) { |
|
107 | |||
108 | 2 | $pieces = explode( $this->_sort_divider, $criteria['sorting']['key'] ); |
|
109 | |||
110 | /** |
||
111 | * And the sort key matches the key set in modify_sort_id(), then modify the Gravity Forms query SQL |
||
112 | * @see modify_sort_id() |
||
113 | */ |
||
114 | 2 | if( ! empty( $pieces[1] ) ) { |
|
115 | |||
116 | // Pass these to the _modify_query_sort_by_time_hack() method |
||
117 | $this->_time_format = $pieces[1]; |
||
118 | $this->_date_format = $pieces[2]; |
||
119 | |||
120 | // Remove fake input IDs (5.1 doesn't exist. Use 5) |
||
121 | $criteria['sorting']['key'] = floor( $pieces[0] ); |
||
122 | |||
123 | /** |
||
124 | * Make sure sorting is numeric (# of seconds). IMPORTANT. |
||
125 | * @see GVCommon::is_field_numeric() is_numeric should also be set here |
||
126 | */ |
||
127 | $criteria['sorting']['is_numeric'] = true; |
||
128 | |||
129 | // Modify the Gravity Forms WP Query |
||
130 | add_filter('query', array( $this, '_modify_query_sort_by_time_hack' ) ); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | 10 | return $criteria; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Modify Gravity Forms query SQL to convert times to numbers |
||
139 | * Gravity Forms couldn't sort by time...until NOW |
||
140 | * |
||
141 | * @since 1.14 |
||
142 | * @param string $query MySQL query |
||
143 | * |
||
144 | * @return string Modified query, if the query matches the expected Gravity Forms SQL string used for sorting time fields. Otherwise, original query. |
||
145 | */ |
||
146 | function _modify_query_sort_by_time_hack( $query ) { |
||
194 | |||
195 | |||
196 | function field_options( $field_options, $template_id = '', $field_id = '', $context = '', $input_type = '' ) { |
||
216 | |||
217 | /** |
||
218 | * Return the field's time format by fetching the form ID and checking the field settings |
||
219 | * |
||
220 | * @since 1.14 |
||
221 | * |
||
222 | * @return string Either "12" or "24". "12" is default. |
||
223 | */ |
||
224 | private function _get_time_format() { |
||
231 | |||
232 | /** |
||
233 | * Return the field's time format by fetching the form ID and checking the field settings |
||
234 | * |
||
235 | * @since 1.14 |
||
236 | * |
||
237 | * @param string $field_id ID for Gravity Forms time field |
||
238 | * @param int $form_id ID for Gravity Forms form |
||
239 | * @return string Either "12" or "24". "12" is default. |
||
240 | */ |
||
241 | static public function _get_time_format_for_field( $field_id, $form_id = 0 ) { |
||
260 | |||
261 | /** |
||
262 | * Modify the default PHP date formats used by the time field based on the field IDs and the field settings |
||
263 | * |
||
264 | * @since 1.14 |
||
265 | * |
||
266 | * @return string PHP date() format text to to display the correctly formatted time value for the newly created field |
||
267 | */ |
||
268 | public function _filter_date_display_date_format() { |
||
275 | |||
276 | /** |
||
277 | * Get the default date format for a field based on the field ID and the time format setting |
||
278 | * |
||
279 | * @since 1.14 |
||
280 | |||
281 | * @param string $time_format The time format ("12" or "24"). Default: "12" {@since 1.14} |
||
282 | * @param int $field_id The ID of the field. Used to figure out full time/hours/minutes/am/pm {@since 1.14} |
||
283 | * |
||
284 | * @return string PHP date format for the time |
||
285 | */ |
||
286 | 1 | static public function date_format( $time_format = '12', $field_id = 0 ) { |
|
314 | |||
315 | } |
||
316 | |||
318 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.