Skip to content

Instantly share code, notes, and snippets.

@seripap
Last active March 14, 2017 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seripap/b59f944645006ada0cc5a95aa9eb460b to your computer and use it in GitHub Desktop.
Save seripap/b59f944645006ada0cc5a95aa9eb460b to your computer and use it in GitHub Desktop.
  1. events.php
  • create event on inital load, update event afterwards. ?eventId=EVENT_ID to modify specific event.
  1. people.php
  • create person on initial load, update or delete person afterwards. ?personId=PERSON_ID to modify specific person.
<?php
require('client/Client.php');
require('client/GrantType/IGrantType.php');
require('client/GrantType/AuthorizationCode.php');
$message = '';
$eventId = isset($_GET['eventId']) ? $_GET['eventId'] : false;
class Event {
public $info;
public function __construct($eventId = -1) {
$this->eventId = $eventId;
$this->clientId = '';
$this->clientSecret = '';
$this->accessToken = '';
$this->client = new OAuth2\Client($this->clientId, $this->clientSecret);
$this->baseApiUrl = 'https://vpgndp.nationbuilder.com';
$this->info = $this->client->fetch($this->baseApiUrl . '/api/v1/sites/vpgndp/pages/events/' . $eventId . '?access_token=' . $this->accessToken);
if ($this->info['code'] === 404) {
$this->info = null;
} else {
$this->info = $this->info['result'];
}
}
public function create($data) {
$newEvent = array(
'event' => array(
'name' => $data['name'],
'site_slug' => 'vpgndp',
'title' => $data['title'],
'headline' => $data['headline'],
'start_time' => date(DateTime::ISO8601),
'end_time' => date(DateTime::ISO8601, strtotime('+7 days')),
'status' => 'unlisted')
);
$response = $this->client->fetch($this->baseApiUrl . '/api/v1/sites/vpgndp/pages/events' . '?access_token=' . $this->accessToken, json_encode($newEvent), 'POST', array('Content-type' => 'application/json', 'Accept' => 'application/json'));
if ($response["code"] !== 200 && $response["code"] !== 201) {
print_r($response);
die('Could not create event');
}
return $response[result];
}
public function edit($data) {
$newEvent = array(
'event' => array(
'name' => $data['name'],
'site_slug' => 'vpgndp',
'title' => $data['title'],
'headline' => $data['headline'],
'start_time' => date(DateTime::ISO8601),
'end_time' => date(DateTime::ISO8601, strtotime('+7 days')),
'status' => 'unlisted')
);
$response = $this->client->fetch($this->baseApiUrl . '/api/v1/sites/vpgndp/pages/events/' . $this->eventId . '?access_token=' . $this->accessToken, json_encode($newEvent), 'PUT', array('Content-type' => 'application/json', 'Accept' => 'application/json'));
if ($response["code"] !== 200 && $response["code"] !== 201) {
print_r($response);
die('Could not edit event');
}
return $response[result];
}
}
$postData = isset($_POST) ? $_POST : false;
if ($postData) {
if (isset($postData['create'])) {
$newEvent = new Event();
$response = $newEvent->create($postData);
if (isset($response[event][id])) {
$eventId = $response[event][id];
$message = 'Event created';
}
} else if (isset($postData['update'])) {
$eventId = $postData[id];
$event = new Event($postData[id]);
$response = $event->edit($postData);
$message = 'Event updated';
}
}
print_r('<pre>');
if ($eventId) {
$event = new Event($eventId);
if (!$event->info) {
die("Could not find event id $eventId to edit.");
}
}
?>
<div style="border: 1px solid black;"><?= $message; ?></div>
<?php if ($event): ?>
<h1>Edit Event</h1>
<?php $mode = 'update'; ?>
<?php else: ?>
<h1>Create New Event</h1>
<?php $mode = 'create'; ?>
<?php endif ?>
<form action="events.php" method="post">
<input type="hidden" name="id" value="<?= $eventId ?>" />
Event Name: <input type="text" name="name" value="<?= $event->info["event"]["name"]; ?>" />
Title: <input type="text" name="title" value="<?= $event->info["event"]["title"]; ?>" />
Headline: <input type="text" name="headline" value="<?= $event->info["event"]["headline"]; ?>" />
<input type="submit" name="<?= $mode; ?>" value="<?= $mode ?>" />
</form>
<?php
require('client/Client.php');
require('client/GrantType/IGrantType.php');
require('client/GrantType/AuthorizationCode.php');
$message = '';
$personId = isset($_GET['personId']) ? $_GET['personId'] : false;
class Person {
public $info;
public function __construct($personId = -1) {
$this->personId = $personId;
$this->clientId = '';
$this->clientSecret = '';
$this->accessToken = '';
$this->client = new OAuth2\Client($this->clientId, $this->clientSecret);
$this->baseApiUrl = 'https://vpgndp.nationbuilder.com';
$this->info = $this->client->fetch($this->baseApiUrl . '/api/v1/people/' . $personId . '?access_token=' . $this->accessToken);
if ($this->info['code'] === 404) {
$this->info = null;
} else {
$this->info = $this->info['result'];
}
}
public function create($data) {
$newPerson = array(
'person' => array(
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'])
);
$response = $this->client->fetch($this->baseApiUrl . '/api/v1/people' . '?access_token=' . $this->accessToken, json_encode($newPerson), 'POST', array('Content-type' => 'application/json', 'Accept' => 'application/json'));
if ($response["code"] !== 200 && $response["code"] !== 201) {
print_r($response);
die('Could not create person');
}
return $response[result];
}
public function edit($data) {
$newPerson = array(
'person' => array(
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'])
);
$response = $this->client->fetch($this->baseApiUrl . '/api/v1/people/' . $this->personId . '?access_token=' . $this->accessToken, json_encode($newPerson), 'PUT', array('Content-type' => 'application/json', 'Accept' => 'application/json'));
if ($response["code"] !== 200 && $response["code"] !== 201) {
print_r($response);
die('Could not edit person');
}
return $response[result];
}
public function delete() {
$response = $this->client->fetch($this->baseApiUrl . '/api/v1/people/' . $this->personId . '?access_token=' . $this->accessToken, null, 'DELETE', array('Content-type' => 'application/json', 'Accept' => 'application/json'));
if ($response["code"] !== 204) {
print_r($response);
die('Could not delete person');
}
return $response[result];
}
}
$postData = isset($_POST) ? $_POST : false;
if ($postData) {
if (isset($postData['create'])) {
$newPerson = new Person();
$response = $newPerson->create($postData);
if (isset($response[person][id])) {
$personId = $response[person][id];
$message = 'Person created';
}
} else if (isset($postData['update'])) {
$personId = $postData[id];
$person = new Person($personId);
$response = $person->edit($postData);
$message = 'Person updated';
} else if (isset($postData['delete'])) {
$person = new Person ($postData[id]);
$resposne = $person->delete();
$message = 'Person deleted';
$personId = false;
$person = false;
}
}
print_r('<pre>');
if ($personId) {
$person = new Person($personId);
if (!$person->info) {
die("Could not find person id $personId to edit.");
}
}
?>
<div style="border: 1px solid black;"><?= $message; ?></div>
<?php if ($person): ?>
<h1>Edit Person</h1>
<?php $mode = 'update'; ?>
<?php else: ?>
<h1>Create New Person</h1>
<?php $mode = 'create'; ?>
<?php endif ?>
<form action="people.php" method="post">
<input type="hidden" name="id" value="<?= $personId ?>" />
First Name: <input type="text" name="first_name" value="<?= $person->info["person"]["first_name"]; ?>" />
Last Name: <input type="text" name="last_name" value="<?= $person->info["person"]["last_name"]; ?>" />
Email: <input type="text" name="email" value="<?= $person->info["person"]["email"]; ?>" />
<input type="submit" name="<?= $mode; ?>" value="<?= $mode ?>" />
<?php if ($mode === 'update'): ?>
<input type="submit" name="delete" value="delete person" />
<?php endif; ?>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment