|
<?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> |