Completed
Push — develop ( 82fa3b...268ce1 )
by Gennady
14:11
created

GF_Query_Call_TIMESORT   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A timesort_sql() 0 46 1
1
<?php
2
namespace GV\Mocks;
3
4
/**
5
 * Time merge calls.
6
 *
7
 * Might be in GF_Query soon.
8
 */
9
class GF_Query_Call_TIMESORT extends \GF_Query_Call {
10
	public function timesort_sql( $query ) {
11
		global $wpdb;
12
13
		list( $column, $sql ) = $this->parameters;
14
		$meta_table = \GFFormsModel::get_entry_meta_table_name();
15
16
		$alias = $query->_alias( $column->field_id, $column->source, 'm' );
17
18
/*
19
SELECT v,
20
IF(
21
POSITION('pm' IN v) > 0,
22
23
(
24
SUBSTRING_INDEX(v, ':', 1)
25
+ IF(SUBSTRING_INDEX(v, ':', 1) < 12, 12, 0)
26
) * 60,
27
28
SUBSTRING_INDEX(v, ':', 1) * 60
29
) +
30
RIGHT(IF(
31
POSITION('m' IN v) > 0,
32
SUBSTRING_INDEX(v, ' ', 1),
33
v
34
),2) t1
35
36
FROM meta;
37
*/
38
39
		// Detect if 'pm' is in the time field
40
		$pm_exists = "POSITION('pm' IN $alias.`meta_value`)";
41
		
42
		// Transform a pm time into minutes ((hour + (12 if hour > 12 else 0)) * 60)
43
		$minutes_12 = "(SUBSTRING_INDEX($alias.`meta_value`, ':', 1) + IF(SUBSTRING_INDEX($alias.`meta_value`, ':', 1) < 12, 12, 0)) * 60";
44
45
		// Transform a 24-hour time into minutes (hour * 60), maybe compensate 12 am = 0
46
		$minutes_24 = "(SUBSTRING_INDEX($alias.`meta_value`, ':', 1) - IF(POSITION('am' IN $alias.`meta_value`) AND SUBSTRING_INDEX($alias.`meta_value`, ':', 1) = '12', 12, 0)) * 60";
47
48
		// Minutes
49
		$minutes = "RIGHT(IF(POSITION('m' IN $alias.`meta_value`), SUBSTRING_INDEX($alias.`meta_value`, ' ', 1), $alias.`meta_value`), 2)";
50
51
		// Combine the insanity :)
52
		$condition = "IF($pm_exists, $minutes_12, $minutes_24) + $minutes";
53
54
		return "(SELECT $condition)";
55
	}
56
}
57