-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.php
115 lines (93 loc) · 3.5 KB
/
data.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
//Define variable for use
$ministry = $_GET['ministry'];
$session = $_GET['session'];
$graph = $_GET['graph'];
$title = $ministry . " " . $graph . " Graph (" . $session .")";
$baseURL = 'https://api.churchsuite.co.uk/v1/children/group/';
$x_account = 'thec3';
$x_application = 'C3-DataReporting';
$x_auth = 'keyyzyy5l9uqicjacdtm';
//Setting API Header
$api_headers = array(
'Accept: application/json',
'Content-Type: application/json',
'X-Account: '.$x_account,
'X-Auth: '.$x_auth,
'X-Application: '.$x_application,
);
//If statement to extract the right data and then format it into something useable
//Planning on tidying this up later - for now this is how my brain works
if ($ministry == "Kids Church") {
$WtP_URL = $baseURL . "2/";
$PtR_URL = $baseURL . "11/";
$Y1t2_URL = $baseURL . "6/";
$Y3t4_URL = $baseURL . "10/";
$Y5t6_URL = $baseURL . "8/";
if ($graph == "Attendance") {
$WtP_URL = $WtP_URL . "attendance/";
$PtR_URL = $PtR_URL . "attendance/";
$Y1t2_URL = $Y1t2_URL . "attendance/";
$Y3t4_URL = $Y3t4_URL . "attendance/";
$Y5t6_URL = $Y5t6_URL . "attendance/";
$WtP_Data = extractData($WtP_URL, $api_headers);
$PtR_Data = extractData($PtR_URL, $api_headers);
$Y1t2_Data = extractData($Y1t2_URL, $api_headers);
$Y3t4_Data = extractData($Y3t4_URL, $api_headers);
$Y5t6_Data = extractData($Y5t6_URL, $api_headers);
$WtP_Attendance = extractAttendance($WtP_Data);
$PtR_Attendance = extractAttendance($PtR_Data);
$Y1t2_Attendance = extractAttendance($Y1t2_Data);
$Y3t4_Attendance = extractAttendance($Y3t4_Data);
$Y5t6_Attendance = extractAttendance($Y5t6_Data);
$SOMETHING = array_merge($WtP_Attendance, $PtR_Attendance, $Y1t2_Attendance, $Y3t4_Attendance, $Y5t6_Attendance);
}
} else if ($ministry == "Youth") {
if ($session == "Fridays") {
$YouthFridaysY7t8_URL = $baseURL . "15/";
$YouthFridaysY9t10_URL = $baseURL . "16/";
$YouthFridaysY11t13_URL = $baseURL . "17/";
} else if ($session == "Sundays") {
$YouthSundays_URL = $baseURL . "12/";
}
} else if ($ministry == "YoungAdults") {
$YA_URL = $baseURL . "";
}
//Define function for extracting the data from API based upon the URL formed in the if statement
function extractData($url, $api_headers) {
$ch = curl_init($url);
$curlopts = array();
$curlopts[CURLOPT_RETURNTRANSFER] = true;
$curlopts[CURLOPT_HTTPHEADER] = $api_headers;
curl_setopt_array($ch, $curlopts);
$response = curl_exec($ch);
$error = curl_errno($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$json = json_decode($response, 1);
if (in_array($http_code, array(200, 201))) {
// OK response
//echo "<h1>OK: ".$http_code."</h1>";
//echo "<h2>Array has " . sizeof($json) . " elements</h2>";
//var_dump($json);
return $json;
} else {
// got an error response
echo '<h1>Error: '.$http_code.'</h1>';
echo '<p>ChurchSuite reported the following error: '.$json['error']['message'].'</p>';
}
}
//Define function for extracting only the total number of Kids from a session
function extractAttendance($dataArray){
$dateArray=(array_keys($dataArray));
$attendanceArray = array_column($dataArray, 'total');
//print_r($attendanceArray);
$attendanceData = array_combine($dateArray, $attendanceArray);
//print_r($attendanceData);
return $attendanceData;
}
$data = array();
foreach ($SOMETHING as $row) {
$data[] = $row;
}
echo json_encode($data);
?>