| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\Dropbox; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\ApiResponse; |
| 6 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 7 |
use WP_Error; |
| 8 |
|
| 9 |
class RecordApiHelper |
| 10 |
{ |
| 11 |
protected $token; |
| 12 |
protected $formId; |
| 13 |
protected $entryId; |
| 14 |
protected $apiBaseUri = 'https://api.dropboxapi.com'; |
| 15 |
protected $contentBaseUri = 'https://content.dropboxapi.com'; |
| 16 |
protected $errorApiResponse = []; |
| 17 |
protected $successApiResponse = []; |
| 18 |
|
| 19 |
private $logResponse; |
| 20 |
|
| 21 |
public function __construct($token, $formId, $entryId) |
| 22 |
{ |
| 23 |
$this->token = $token; |
| 24 |
$this->formId = $formId; |
| 25 |
$this->entryId = $entryId; |
| 26 |
$this->logResponse = new ApiResponse(); |
| 27 |
} |
| 28 |
|
| 29 |
public function uploadFile($folder, $filePath) |
| 30 |
{ |
| 31 |
if ('' === $filePath) { |
| 32 |
return; |
| 33 |
} |
| 34 |
$filePath = $this->makeFilePath($filePath); |
| 35 |
$filesize = filesize($filePath); |
| 36 |
$fp = fopen($filePath, 'rb'); |
| 37 |
$body = fread($fp, $filesize); |
| 38 |
if (!$body) { |
| 39 |
return new WP_Error(423, 'Can\'t open file!'); |
| 40 |
} |
| 41 |
|
| 42 |
$apiEndPoint = $this->contentBaseUri . '/2/files/upload'; |
| 43 |
$headers = [ |
| 44 |
'Authorization' => 'Bearer ' . $this->token, |
| 45 |
'Content-Type' => 'application/octet-stream', |
| 46 |
'Dropbox-API-Arg' => json_encode([ |
| 47 |
'path' => $folder . '/' . $this->fileName($filePath), |
| 48 |
'mode' => 'add', |
| 49 |
'autorename' => true, |
| 50 |
'mute' => true, |
| 51 |
'strict_conflict' => false |
| 52 |
]), |
| 53 |
]; |
| 54 |
return HttpHelper::post($apiEndPoint, $body, $headers); |
| 55 |
} |
| 56 |
|
| 57 |
public function fileName($filePath) |
| 58 |
{ |
| 59 |
$filePathArray = explode('/', $filePath); |
| 60 |
$key = count($filePathArray) - 1; |
| 61 |
$fileName = null; |
| 62 |
if (key_exists($key, $filePathArray)) { |
| 63 |
$fileName = $filePathArray[$key]; |
| 64 |
} |
| 65 |
return $fileName; |
| 66 |
} |
| 67 |
|
| 68 |
public function handleAllFiles($folderWithFile, $actions) |
| 69 |
{ |
| 70 |
foreach ($folderWithFile as $folder => $filePath) { |
| 71 |
if ('' === $filePath) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
if (is_array($filePath)) { |
| 75 |
foreach ($filePath as $singleFilePath) { |
| 76 |
if ('' === $singleFilePath) { |
| 77 |
continue; |
| 78 |
} |
| 79 |
$response = $this->uploadFile($folder, $singleFilePath); |
| 80 |
$this->storeInState($response); |
| 81 |
$this->deleteFile($singleFilePath, $actions); |
| 82 |
} |
| 83 |
} else { |
| 84 |
$response = $this->uploadFile($folder, $filePath); |
| 85 |
$this->storeInState($response); |
| 86 |
$this->deleteFile($filePath, $actions); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
protected function storeInState($response) |
| 92 |
{ |
| 93 |
if (isset($response->id)) { |
| 94 |
$this->successApiResponse[] = $response; |
| 95 |
} else { |
| 96 |
$this->errorApiResponse[] = $response; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public function deleteFile($filePath, $actions) |
| 101 |
{ |
| 102 |
if (isset($actions->delete_from_wp) && $actions->delete_from_wp) { |
| 103 |
$filePath = $this->makeFilePath($filePath); |
| 104 |
if (file_exists($filePath)) { |
| 105 |
unlink($filePath); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public function makeFilePath($filePath) |
| 111 |
{ |
| 112 |
$upDir = wp_upload_dir(); |
| 113 |
return $upDir['basedir'] . '/bitforms/uploads/' . $this->formId . '/' . $this->entryId . '/' . $filePath; |
| 114 |
} |
| 115 |
|
| 116 |
public function executeRecordApi($integrationId, $logID, $fieldValues, $fieldMap, $actions) |
| 117 |
{ |
| 118 |
$folderWithFile = []; |
| 119 |
foreach ($fieldMap as $value) { |
| 120 |
if (!is_null($fieldValues[$value->formField])) { |
| 121 |
$folderWithFile[$value->dropboxFormField] = $fieldValues[$value->formField]; |
| 122 |
} |
| 123 |
} |
| 124 |
$this->handleAllFiles($folderWithFile, $actions); |
| 125 |
|
| 126 |
if (count($this->errorApiResponse) > 0) { |
| 127 |
$this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Some Files Can\'t Upload For Some Reason. ' . json_encode($this->errorApiResponse)); |
| 128 |
} |
| 129 |
if (count($this->successApiResponse) > 0) { |
| 130 |
$this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'success', 'All Files Uploaded. ' . json_encode($this->successApiResponse)); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|