class BynderMediaApi in Bynder 7
Hierarchy
- class \BynderMediaApi
Expanded class hierarchy of BynderMediaApi
File
- includes/
BynderMediaApi.inc, line 11 - Manages the Bynder media browser functionality
View source
class BynderMediaApi {
const BYNDER_INTEGRATION_ID = '67e4cef1-320a-4ec8-b307-0a6614325259';
private $bynderApi;
private $assetBankManager;
private function __construct() {
try {
$tokens = $this
->hasAccessToken();
$settings = array(
'consumerKey' => variable_get('bynder_oauth_consumer', ''),
'consumerSecret' => variable_get('bynder_oauth_consumer_secret', ''),
'token' => !$tokens ? variable_get('bynder_oauth_token', '') : $tokens['oauth_token'],
'tokenSecret' => !$tokens ? variable_get('bynder_oauth_token_secret', '') : $tokens['oauth_token_secret'],
'baseUrl' => variable_get('bynder_url', ''),
);
if (!variable_get('bynder_url')) {
$settings = variable_get('temp_derivatives_settings');
}
if ($proxyUrl = variable_get('bynder_proxy_url')) {
$proxyUrl = str_replace([
'tcp://',
'https://',
], 'http://', $proxyUrl);
$settings['requestOptions'] = [
'proxy' => $proxyUrl,
];
}
$this->bynderApi = BynderApiFactory::create($settings);
$this->assetBankManager = $this->bynderApi
->getAssetBankManager();
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
}
}
public static function getBynderApi() {
return new BynderMediaApi();
}
public function getAssetBankManager() {
if (!isset($this->assetBankManager)) {
$this
->getBynderApi();
}
return $this->assetBankManager;
}
public function getObjectById($id) {
try {
$result = $this
->getAssetBankManager()
->getMediaInfo($id)
->wait();
return $result;
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
return false;
}
}
/**
* Get brands.
*/
public function getMediaBrands() {
try {
$result = $this
->getAssetBankManager()
->getBrands()
->wait();
return $result;
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
drupal_set_message(t('Could not retrieve brand list'), 'error');
return false;
}
}
/**
* Get Media list.
*/
public function getMediaList($query = null) {
$query['page'] = isset($query['page']) ? $query['page'] : 1;
try {
$result = $this
->getAssetBankManager()
->getMediaList($query)
->wait();
return $result;
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
drupal_set_message(t('Could not retrieve search results'), 'error');
return false;
}
}
/**
* Get Metaproperties.
*/
public function getMetaProperties() {
if (!($metaProperties = cache_get('bynder_media_metaproperties', 'cache_bynder'))) {
try {
$result = $this
->getAssetBankManager()
->getMetaProperties()
->wait();
cache_set('bynder_media_metaproperties', $result, 'cache_bynder', CACHE_TEMPORARY);
return $result;
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
drupal_set_message(t('Could not retrieve metaproperties:'), 'error');
return false;
}
}
else {
return $metaProperties->data;
}
}
public function initiateOAuthTokenRetrieval() {
$settings = array(
'consumerKey' => variable_get('bynder_oauth_consumer', ''),
'consumerSecret' => variable_get('bynder_oauth_consumer_secret', ''),
'baseUrl' => variable_get('bynder_url', ''),
);
$this->bynderApi = BynderApiFactory::create($settings);
try {
$requestToken = $this->bynderApi
->getRequestToken()
->wait();
parse_str($requestToken, $tokenArray);
$token = $tokenArray['oauth_token'];
$tokenSecret = $tokenArray['oauth_token_secret'];
$_SESSION['bynder_data'] = array(
'oauth_token' => $token,
'oauth_token_secret' => $tokenSecret,
'isRequestToken' => true,
);
$query = array(
'oauth_token' => $token,
// Would be the url pointing to this script for example.
'callback' => url('bynder-oauth', array(
'absolute' => true,
)),
);
return url(variable_get('bynder_url') . '/api/v4/oauth/authorise', [
'query' => $query,
]);
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
drupal_set_message(t('An error occurred while trying to login'), 'error');
return false;
}
}
public static function hasSettings() {
$settings = array(
'consumerKey' => variable_get('bynder_oauth_consumer', false),
'consumerSecret' => variable_get('bynder_oauth_consumer_secret', false),
'baseUrl' => variable_get('bynder_url', false),
);
foreach ($settings as $setting) {
if (!$setting) {
return false;
}
}
return true;
}
public static function hasAccessToken() {
$tokens = isset($_SESSION['bynder_data']) ? $_SESSION['bynder_data'] : array();
// Required tokens need to be stored in the session.
if (!isset($tokens['oauth_token']) || !isset($tokens['oauth_token_secret'])) {
return false;
}
return $tokens;
}
public function hasUploadPermissions() {
$user = $this->bynderApi
->getCurrentUser()
->wait();
if (isset($user)) {
$profileId = $user['profileId'];
$userProfile = $this->bynderApi
->getSecurityProfile($profileId)
->wait();
foreach ($userProfile['roles'] as $role) {
if ($role == 'MEDIAUPLOAD' || $role == 'MEDIAUPLOADFORAPPROVAL') {
return true;
}
}
}
return false;
}
public function finishOAuthTokenRetrieval() {
$tokens = $this
->hasAccessToken();
if ($tokens && isset($tokens['isRequestToken'])) {
unset($tokens['isRequestToken']);
$settings = array(
'consumerKey' => variable_get('bynder_oauth_consumer', ''),
'consumerSecret' => variable_get('bynder_oauth_consumer_secret', ''),
'token' => $tokens['oauth_token'],
'tokenSecret' => $tokens['oauth_token_secret'],
'baseUrl' => variable_get('bynder_url', ''),
);
$bynder_api = BynderApiFactory::create($settings);
foreach ($bynder_api
->getAccessToken()
->wait() as $key => $token) {
$tokens[$key] = $token;
}
$_SESSION['bynder_data'] = $tokens;
}
else {
drupal_set_message(t('Could not finish login, please try again'), 'error');
}
}
public function uploadFile($fileInfo) {
try {
$result = $this->assetBankManager
->uploadFileAsync($fileInfo)
->wait();
return $result;
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
}
return false;
}
public function genericRequest($url, $options = null) {
$client = new Client([
'allow_redirects' => false,
]);
return $client
->requestAsync('GET', $url)
->wait();
}
public function getDerivatives() {
try {
$result = $this->assetBankManager
->getDerivatives()
->wait();
return $result;
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
drupal_set_message(t('Could not retrieve account derivatives information'), 'error');
}
return false;
}
public function addAssetUsage($assetID, $usageUrl, $usageAdditional = null, $timestamp = null) {
try {
$usageProperties = [
'integration_id' => self::BYNDER_INTEGRATION_ID,
'asset_id' => $assetID,
'timestamp' => isset($timestamp) ? $timestamp : date(DateTime::ISO8601),
'uri' => $usageUrl,
'additional' => $usageAdditional,
];
return $this->assetBankManager
->createUsage($usageProperties)
->wait();
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
drupal_set_message(t('Could not add asset usage information'), 'error');
return false;
}
}
public function removeAssetUsage($assetID, $usageUrl = null) {
try {
$usageProperties = [
'integration_id' => self::BYNDER_INTEGRATION_ID,
'asset_id' => $assetID,
'uri' => $usageUrl,
];
return $this->assetBankManager
->deleteUSage($usageProperties)
->wait();
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
drupal_set_message(t('Could not add asset usage information'), 'error');
return false;
}
}
public function getAssetUsages($assetID) {
try {
return $this->assetBankManager
->getUsage([
'asset_id' => $assetID,
])
->wait();
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
drupal_set_message(t('Could not add asset usage information'), 'error');
return false;
}
}
public static function getIntegrationId() {
return self::BYNDER_INTEGRATION_ID;
}
}