-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdig-nav-list.php
More file actions
154 lines (136 loc) · 5.13 KB
/
dig-nav-list.php
File metadata and controls
154 lines (136 loc) · 5.13 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
#By Zachary Spalding @Southeastern New York Library Resources Council
#https://airtable.com/<YOUR BASE ID>/api/docs
#https://codepen.io/airtable/full/MeXqOg
// Airtable API key and URL
$apiKey = '<ADD YOUR API KEY>';
$baseId = '<YOUR BASE ID>';
$tableId = '<YOUR TABLE ID>';
$apiUrl = "https://api.airtable.com/v0/{$baseId}/{$tableId}";
// --- Helpers ---
function fetchData($url, $apiKey, $params = []) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $apiKey]);
$response = curl_exec($ch);
if (curl_errno($ch)) { echo 'Error: ' . curl_error($ch); exit; }
curl_close($ch);
return json_decode($response, true);
}
// Normalize field that may be scalar or array (take first if array)
function field_first($fields, $name) {
if (!isset($fields[$name])) return '';
$v = $fields[$name];
if (is_array($v)) return isset($v[0]) ? trim((string)$v[0]) : '';
return trim((string)$v);
}
$params = [
'fields' => [
'Organization','URL','Street','City','State','Zip Code','County',
'Phone Number','First Name','Last Name','Status'
],
// ✅ Your updated filter
'filterByFormula' => 'OR(FIND("Active", {Status}), FIND("Cohort ONE", {Status}), FIND("Cohort ONE Audit", {Status}))',
'pageSize' => 100,
// (Don’t rely on API sort; we’ll sort locally.)
];
// Fetch all pages
$all = [];
do {
$data = fetchData($apiUrl, $apiKey, $params);
if (isset($data['records'])) {
foreach ($data['records'] as $r) {
$f = $r['fields'];
$row = [
'county' => field_first($f, 'County'),
'org' => field_first($f, 'Organization'),
'url' => field_first($f, 'URL'),
'street' => field_first($f, 'Street'),
'city' => field_first($f, 'City'),
'state' => field_first($f, 'State'),
'zip' => field_first($f, 'Zip Code'),
'phone' => field_first($f, 'Phone Number'),
'first' => field_first($f, 'First Name'),
'last' => field_first($f, 'Last Name'),
];
if ($row['county'] !== '' && $row['org'] !== '') {
$all[] = $row;
}
}
}
$params['offset'] = $data['offset'] ?? null;
} while (isset($data['offset']));
if (empty($all)) { echo 'No records found.'; exit; }
// Group by County → Organization, aggregate people
$byCountyOrg = [];
foreach ($all as $row) {
$county = $row['county'];
$org = $row['org'];
if (!isset($byCountyOrg[$county])) $byCountyOrg[$county] = [];
if (!isset($byCountyOrg[$county][$org])) {
$byCountyOrg[$county][$org] = [
'org' => $org,
'url' => $row['url'],
'street' => $row['street'],
'city' => $row['city'],
'state' => $row['state'],
'zip' => $row['zip'],
'people' => [],
];
}
$byCountyOrg[$county][$org]['people'][] = [
'first' => $row['first'],
'last' => $row['last'],
'phone' => $row['phone'],
];
}
// Sort counties and orgs alphabetically (natural, case-insensitive)
uksort($byCountyOrg, fn($a, $b) => strnatcasecmp($a, $b));
foreach ($byCountyOrg as $county => &$orgs) {
uksort($orgs, fn($a, $b) => strnatcasecmp($a, $b));
}
unset($orgs);
// --- Render ---
echo "<div class='SenyMemberDirTable'>";
foreach ($byCountyOrg as $countyName => $orgs) {
// County header row
echo "<div class='SenyMemberDirTableRow'>";
echo "<hr>";
echo "<div class='HVconnCountyTableCell'><h2>" . htmlspecialchars($countyName) . " County</h2></div>";
echo "<br><hr>";
echo "</div>";
// Two-up grid for orgs
$i = 0;
$rowOpen = false;
foreach ($orgs as $orgName => $info) {
if ($i % 2 === 0) {
if ($rowOpen) echo "</div>"; // close previous org row
echo "<div class='SenyMemberDirTableRow'>";
$rowOpen = true;
}
echo "<div class='SenyMemberDirTableCell'>";
$orgText = htmlspecialchars($orgName);
$orgLink = $info['url'] ? "<a href='" . htmlspecialchars($info['url']) . "'>{$orgText}</a>" : $orgText;
echo "<h2>{$orgLink}</h2>";
$addressParts = array_filter([
$info['street'],
$info['city'],
$info['state'],
$info['zip']
], fn($v) => $v !== '');
if (!empty($addressParts)) {
echo "<h3>" . htmlspecialchars(implode(', ', $addressParts)) . "</h3>";
}
echo "<h3>Digital Navigator(s):</h3><br>";
echo "<div style='font-size:22px'>";
foreach ($info['people'] as $p) {
$name = trim($p['first'] . ' ' . $p['last']);
echo "<p>" . htmlspecialchars($name) . ( $p['phone'] ? " " . htmlspecialchars($p['phone']) : "" ) . "</p>";
}
echo "</div><br>";
echo "</div>"; // cell
$i++;
}
if ($rowOpen) echo "</div>"; // close last open org row in this county
}
echo "</div>"; // SenyMemberDirTable