-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_api.php
More file actions
180 lines (154 loc) · 5.96 KB
/
class_api.php
File metadata and controls
180 lines (154 loc) · 5.96 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
class Procore_API{
public function get_access_token(){
$access_token = get_option( 'procore-token');
if($access_token){
return $access_token;
}
return false;
}
public function refresh_token($token){
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://app.procore.com/oauth/token?grant_type=refresh_token&client_id=".CLIENT_ID."&client_secret=".CLIENT_SECRET."&refresh_token=".$token);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$json = json_decode($output);
if($json->access_token){
update_option('procore-token', $json->refresh_token);
return true;
}
return false;
}
public function get_companies($store = false){
if($this->get_access_token()){
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://app.procore.com/vapid/companies");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$this->get_access_token()
));
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
// $output contains the output string
$output = curl_exec($ch);
$json = json_decode($output, true);
if(count($json) > 0 && isset($json[0]['id'])){
if($store){
update_option('procore_companies', $output);
}
return $json;
}
// close curl resource to free up system resources
curl_close($ch);
}
return false;
}
public function get_projects($company_id, $store = false){
if($this->get_access_token()){
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://app.procore.com/vapid/projects?company_id=".$company_id);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$this->get_access_token()
));
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
// $output contains the output string
$output = curl_exec($ch);
$json = json_decode($output, true);
if(count($json) > 0 && isset($json[0]['id'])){
if($store){
update_option('procore_projects', $output);
}
return $json;
}
// close curl resource to free up system resources
curl_close($ch);
}
return false;
}
public function get_project($project_id, $store = false){
if($this->get_access_token()){
$companies = $this->get_companies();
if(!$companies)
return false;
$comp = array();
foreach($companies as $company){
$projects = $this->get_projects($company['id']);
if($projects){
foreach($projects as $project){
if($project['id'] == $project_id){
$comp = $company;
break 2;
}
}
}
}
if(!$comp)
return false;
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://app.procore.com/vapid/projects/".$project_id."/?company_id=".$comp['id']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$this->get_access_token()
));
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
// $output contains the output string
$output = curl_exec($ch);
$json = json_decode($output, true);
if(isset($json['id'])){
if($store){
update_option('procore_projects', $output);
}
return $json;
}
// close curl resource to free up system resources
curl_close($ch);
}
return false;
}
public function get_logs($log, $project_id, $date = '', $store = false){
if($this->get_access_token()){
$ch = curl_init();
$query_date = '';
if($date){
if($log == "weather_logs"){
$query_date = '?start_date='.$date.'&end_date='.$date;
}
else{
$query_date = '?log_date='.$date;
}
}
// set url
curl_setopt($ch, CURLOPT_URL, "https://app.procore.com/vapid/projects/".$project_id."/".$log.$query_date);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$this->get_access_token()
));
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
// $output contains the output string
$output = curl_exec($ch);
$json = json_decode($output, true);
if(count($json) > 0 && isset($json[0]['id'])){
if($store){
update_option('procore_'.$log, $output);
}
return $json;
}
// close curl resource to free up system resources
curl_close($ch);
}
return false;
}
}