Browse Source

Fetch data from airtable

pull/33/head
parent
commit
c55b6b69b8
Signed by: aayla GPG Key ID: 5D2C38575E4217B7
3 changed files with 160 additions and 4 deletions
  1. +91
    -3
      chapters/index.php
  2. +2
    -1
      docker-compose.yml
  3. +67
    -0
      res/php/stateresolution.php

+ 91
- 3
chapters/index.php View File

@ -16,6 +16,96 @@
// You should have received a copy of the GNU General Public License
// along with website. If not, see <http://www.gnu.org/licenses/>.
?>
<?php
include_once("../res/php/stateresolution.php");
// Load JSON info
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.airtable.com/v0/appKRJXHcCjQU3i6p/Chapters',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . getenv("AIRTABLE_KEY"),
),
));
$response = curl_exec($curl);
curl_close($curl);
// Rearrange data how we need it
class Chapter {
// Object oriented programming in PHP. I should be shot for this.
public string $name;
public string $state;
public string $logo;
// Contact info
public string $email;
public string $twitter;
public string $instagram;
public string $facebook;
public function __construct($value, $state) {
// Ensure the chapter is actually active
if (!isset($value->fields->Status) || $value->fields->Status != "Active") {
throw new ErrorException();
}
// Retrieve name
if (!isset($value->fields->Name)) {
throw new ErrorException();
}
$this->name = $value->fields->Name;
// Retrieve state
$this->state = resolve_state($state);
// Retrieve logo
if (!isset($value->fields->Logo[0]->url)) {
$this->logo = "https://srastatic.nyc3.cdn.digitaloceanspaces.com/web/img/logo/default.png";
} else {
$this->logo = $value->fields->Logo[0]->url;
}
// Retrieve socials
foreach (["Email", "Twitter", "Instagram", "Facebook"] as $key) {
if (!isset($value->fields->$key)) { continue; } else {
$data = $value->fields->$key;
switch ($key) {
case "Email": $this->email = $data; break;
case "Twitter": $this->twitter = $data; break;
case "Instagram": $this->instagram = $data; break;
case "Facebook": $this->facebook = $data; break;
default: break;
}
}
}
}
}
$records = json_decode($response) -> records;
$chapters = [];
foreach ($records as $key => $value) {
if (!isset($value->fields->State)) {
continue;
}
foreach ($value->fields->State as $key=>$state) {
try {
$chapter = new Chapter($value, $state);
} catch (ErrorException $e) {
continue;
}
if (!in_array(resolve_state($state), $chapters)) {
$chapters += [resolve_state($state) => []];
}
array_push($chapters[resolve_state($state)], $chapter);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
@ -149,9 +239,7 @@ h5 {
<div style="text-align: center;"><h2 id="instructions">Click on a state above to view local chapters</h2></div>
<hr>
<div>
<?php
// Load JSON info
$chapters = json_decode(file_get_contents("chapters.json"), true);
// Build state list
foreach ($chapters as $state => $list) {
echo("<div id=\"list-$state\" class=\"chapter-details\" style=\"display: none\">");


+ 2
- 1
docker-compose.yml View File

@ -6,4 +6,5 @@ services:
- 80:80
environment:
- STRIPE_PUBLIC_KEY=${STRIPE_PUBLIC_KEY}
- STRIPE_PRIVATE_KEY=${STRIPE_PRIVATE_KEY}
- STRIPE_PRIVATE_KEY=${STRIPE_PRIVATE_KEY}
- AIRTABLE_KEY=${AIRTABLE_KEY}

+ 67
- 0
res/php/stateresolution.php View File

@ -0,0 +1,67 @@
<?php
// The jquery plugin for the map requires two letter abbreviations instead of full state names
function resolve_state(string $state) {
// Array of state abbreviations
$states = [
"Alabama" => "AL",
"Alaska" => "AK",
"Arizona" => "AZ",
"Arkansas" => "AR",
"California" => "CA",
"Colorado" => "CO",
"Connecticut" => "CT",
"Delaware" => "DE",
"Florida" => "FL",
"Georgia" => "GA",
"Hawaii" => "HI",
"Idaho" => "ID",
"Illinois" => "IL",
"Indiana" => "IN",
"Iowa" => "IA",
"Kansas" => "KS",
"Kentucky" => "KY",
"Louisiana" => "LA",
"Maine" => "ME",
"Maryland" => "MD",
"Massachusetts" => "MA",
"Michigan" => "MI",
"Minnesota" => "MN",
"Mississippi" => "MS",
"Missouri" => "MO",
"Montana" => "MT",
"Nebraska" => "NE",
"Nevada" => "NV",
"New Hampshire" => "NH",
"New Jersey" => "NJ",
"New Mexico" => "NM",
"New York" => "NY",
"North Carolina" => "NC",
"North Dakota" => "ND",
"Ohio" => "OH",
"Oklahoma" => "OK",
"Oregon" => "OR",
"Pennsylvania" => "PA",
"Rhode Island" => "RI",
"South Carolina" => "SC",
"South Dakota" => "SD",
"Tennessee" => "TN",
"Texas" => "TX",
"Utah" => "UT",
"Vermont" => "VT",
"Virginia" => "VA",
"Washington" => "WA",
"West Virginia" => "WV",
"Wisconsin" => "WI",
"Wyoming" => "WY",
"District of Columbia" => "DC",
"Guam" => "GU",
"Marhsall Islands" => "MH",
"Puerto Rico" => "PR",
"Virgin Islands" => "VI"
];
if (!array_key_exists($state, $states)) {
return false;
}
return $states[$state];
}
?>

Loading…
Cancel
Save