-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclass-trackserver-location.php
115 lines (101 loc) · 2.75 KB
/
class-trackserver-location.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
if ( ! defined( 'TRACKSERVER_PLUGIN_DIR' ) ) {
die( 'No, sorry.' );
}
class Trackserver_Location {
// Database fields
public $id = null;
public $track_id = null;
public $latitude = null;
public $longitude = null;
public $altitude = 0;
public $speed = 0;
public $heading = 0;
public $created = null;
public $occurred = null;
public $comment = '';
public $hidden = 0;
private $trackserver;
private $user_id;
private $tbl_tracks;
private $tbl_locations;
/**
* Initialize the instance.
*
* @since 5.0
*/
public function __construct( $trackserver, $track_id = null, $user_id = null ) {
$this->trackserver = $trackserver;
$this->track_id = $track_id;
$this->user_id = $user_id; // Needed for geofencing
$this->tbl_tracks = $this->trackserver->tbl_tracks;
$this->tbl_locations = $this->trackserver->tbl_locations;
}
/**
* Function to check whether this location is geofenced.
*
* @since 3.1
* @since 5.0 Delegate to the function in Trackserver main class
*/
private function is_geofenced() {
if ( is_null( $this->latitude ) || is_null( $this->longitude ) || is_null( $this->user_id ) ) {
return false;
}
$data = array(
'latitude' => $this->latitude,
'longitude' => $this->longitude,
);
return $this->trackserver->is_geofenced( $this->user_id, $data );
}
/**
* Save the location to the database.
*
* If the instance doesn't have an ID, insert a new location, otherwise update
* the existing one. Returns true on success, or false on failure.
*
* @since 5.0
*/
public function save() {
global $wpdb;
$fenced = $this->is_geofenced();
if ( $fenced === 'discard' ) {
return true;
}
$data = array(
'trip_id' => $this->track_id,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'altitude' => $this->altitude,
'speed' => $this->speed,
'heading' => $this->heading,
'created' => $this->created,
'occurred' => $this->occurred,
'comment' => $this->comment,
'hidden' => ( $fenced === 'hide' ? 1 : 0 ),
);
$format = array( '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d' );
if ( is_null( $this->id ) ) {
$this->created = current_time( 'Y-m-d H:i:s' );
$data['created'] = $this->created;
if ( $wpdb->insert( $this->tbl_locations, $data, $format ) ) {
$this->id = $wpdb->insert_id;
return true;
}
} else {
$where = array( 'id' => $this->id );
$where_fmt = array( '%d' );
if ( $wpdb->update( $this->tbl_locations, $data, $where, $format, $where_fmt ) ) {
return true;
}
}
return false;
}
/**
* Set a property on this object to a given value.
*
* @since 5.0.
*/
public function set( $what, $value ) {
$this->$what = $value;
}
} // class