View source
<?php
function bynder_form($form, &$form_state) {
$form['bynder_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Bynder API'),
'#description' => t('You will first need to apply for an API Developer Key'),
'#collapsible' => true,
);
$form['bynder_settings']['bynder_url'] = array(
'#type' => 'textfield',
'#title' => t('Bynder url'),
'#default_value' => variable_get('bynder_url', ''),
'#size' => 50,
'#description' => t('The Bynder url.'),
'#required' => true,
);
$form['bynder_settings']['bynder_oauth_consumer'] = array(
'#type' => 'textfield',
'#title' => t('OAuth Consumer Token'),
'#default_value' => variable_get('bynder_oauth_consumer', ''),
'#size' => 50,
'#description' => t('The consumer token to be used by the module to communicate with Bynder.'),
'#required' => true,
);
$form['bynder_settings']['bynder_oauth_consumer_secret'] = array(
'#type' => 'textfield',
'#title' => t('OAuth Consumer Secret'),
'#default_value' => variable_get('bynder_oauth_consumer_secret', ''),
'#size' => 50,
'#description' => t('The consumer secret to be used by the module to communicate with Bynder.'),
'#required' => true,
);
$form['bynder_settings']['bynder_oauth_token'] = array(
'#type' => 'textfield',
'#title' => t('OAuth Token'),
'#default_value' => variable_get('bynder_oauth_token', ''),
'#size' => 50,
'#description' => t('The token to be used by the module to communicate with Bynder.'),
'#required' => true,
);
$form['bynder_settings']['bynder_oauth_token_secret'] = array(
'#type' => 'textfield',
'#title' => t('OAuth Token Secret'),
'#default_value' => variable_get('bynder_oauth_token_secret', ''),
'#size' => 50,
'#description' => t('The token secret to be used by the module to communicate with Bynder.'),
'#required' => true,
);
$form['bynder_settings']['bynder_cdn_url'] = array(
'#type' => 'textfield',
'#title' => t('Cloudfront CDN url'),
'#default_value' => variable_get('bynder_cdn_url', ''),
'#size' => 50,
'#description' => t('Optional cloudfront CDN url.'),
'#required' => false,
);
$form['bynder_settings']['bynder_proxy_url'] = array(
'#type' => 'textfield',
'#title' => t('Proxy url details'),
'#default_value' => variable_get('bynder_proxy_url', ''),
'#size' => 50,
'#description' => t('Enter the proxy url with port: https://my-proxy:1234'),
'#required' => false,
);
$form['bynder_settings']['bynder_upload_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Bynder upload settings'),
'#description' => t("Choose if uploads should go directly to the Bynder Asset Bank or through Waiting Room \n first. Note that if the user doesn't have enough permissions it will do the latter by default."),
);
$form['bynder_settings']['bynder_upload_settings']['bynder_upload_method'] = array(
'#type' => 'select',
'#title' => t('Bynder upload method'),
'#options' => array(
'upload' => 'Direct Upload',
'wait' => 'Upload for Approval',
),
'#default_value' => variable_get('bynder_upload_method', 'upload'),
);
$form['bynder_settings']['bynder_upload_settings']['bynder_brand_selected'] = array(
'#type' => 'select',
'#title' => t('Bynder brand'),
'#options' => variable_get('bynder_brands_list', array()),
'#default_value' => variable_get('bynder_brand_selected', 0),
'#description' => t('This is the brand associated to Media Asset uploads'),
);
$form['bynder_settings']['bynder_upload_settings']['bynder_brands_list_refresh'] = array(
'#type' => 'submit',
'#value' => t('Refresh brands list'),
'#submit' => [
'bynder_batch_reload_derivatives',
],
);
$form['bynder_settings']['bynder_derivatives'] = array(
'#type' => 'fieldset',
'#title' => t('Bynder image derivatives'),
'#description' => t('Bynder provides "mini", "webimage" and "thul" image sizes by default. Custom derivatives can be configured to better suit specific use-cases. Reload of derivatives can be triggered if any derivatives are missing from the list.'),
'derivatives_list' => array(
'#theme' => 'item_list',
'#items' => unserialize(variable_get('bynder_derivatives', '')),
),
'check' => array(
'#type' => 'submit',
'#value' => t('Reload derivatives info'),
'#submit' => [
'bynder_batch_reload_derivatives',
],
),
);
variable_del('temp_derivatives_settings');
$form['bynder_settings']['bynder_derivatives']['bynder_custom_derivative'] = array(
'#type' => 'textfield',
'#title' => t('Custom derivative name'),
'#default_value' => variable_get('bynder_custom_derivative', 'drupal_custom'),
'#size' => 50,
'#description' => t('Custom derivative name set up in Bynder config, allows cropping and resizing.'),
'#required' => false,
);
$form['#submit'][] = 'bynder_form_submit';
return system_settings_form($form);
}
function bynder_batch_reload_derivatives() {
variable_set('image_style_preview_image', drupal_get_path('module', 'image') . '/sample.png');
$batch = array(
'operations' => array(
array(
'bynder_get_brands_list',
array(),
),
array(
'bynder_batch_reload_derivatives_process',
array(),
),
array(
'bynder_batch_sync_old_bynder_media',
array(),
),
),
'finished' => 'bynder_batch_reload_derivatives_finished',
'title' => t('Batch to reload Bynder derivatives information'),
'init_message' => t('Bynder derivatives batch is starting...'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Fix has encountered an error.'),
);
batch_set($batch);
}
function bynder_get_brands_list(&$context) {
try {
$brandsList = BynderMediaApi::getBynderApi()
->getMediaBrands();
if ($brandsList) {
$options = array();
foreach ($brandsList as $brand) {
$options[$brand['id']] = $brand['name'];
}
variable_set('bynder_brands_list', $options);
variable_set('bynder_brand_selected', array_keys($options)[0]);
}
else {
$context['finished'] = 1;
}
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
$context['finished'] = 1;
}
}
function bynder_batch_reload_derivatives_process(&$context) {
$context['message'] = "Updating derivatives data.";
variable_set('bynder_derivatives', '');
$thumbnails = [
'mini',
'webimage',
'thul',
];
try {
$derivatives = BynderMediaApi::getBynderApi()
->getDerivatives();
if ($derivatives) {
foreach ($derivatives as $derivative) {
$thumbnails[] = $derivative['prefix'];
}
}
$customDerivative = variable_get('bynder_custom_derivative');
foreach ($thumbnails as $derivative) {
if (!image_style_load('bynder_' . $derivative)) {
$context['message'] = "Creating image style for derivative: {$derivative}";
$style = image_style_save(array(
'name' => 'bynder_' . $derivative,
'label' => t('Bynder derivative: ' . $derivative),
));
if ($derivative == $customDerivative) {
$effect = array(
'name' => 'bynder_custom_resolution_resize',
'data' => array(
'width' => 400,
'height' => 400,
),
'isid' => $style['isid'],
);
image_effect_save($effect);
}
}
}
} catch (Exception $e) {
watchdog('bynder', $e
->getMessage());
drupal_set_message(t("There was an error while saving new image styles."), 'error');
$context['finished'] = 1;
}
variable_set('bynder_derivatives', serialize($thumbnails));
}
function bynder_batch_sync_old_bynder_media(&$context) {
sync_old_bynder_media(true);
}
function bynder_batch_reload_derivatives_finished($success, $results, $operations) {
if ($success) {
drupal_set_message('Finished setting up image styles!');
}
else {
$error_operation = reset($operations);
$message = t('An error occurred while processing %error_operation with arguments: @arguments', array(
'%error_operation' => $error_operation[0],
'@arguments' => print_r($error_operation[1], true),
));
drupal_set_message($message, 'error');
}
}
function bynder_form_validate($form, &$form_state) {
if ($form_state['values']['bynder_url']) {
$form_state['values']['bynder_url'] = trim($form_state['values']['bynder_url']);
if (!filter_var($form_state['values']['bynder_url'], FILTER_VALIDATE_URL)) {
form_set_error('bynder_url', t('Bynder url is not a valid url.'));
}
}
}
function bynder_form_updated_api_settings($new_settings) {
$changed = false;
if (!variable_get('bynder_derivatives', false)) {
variable_set('temp_derivatives_settings', array(
'consumerKey' => variable_get('bynder_oauth_consumer', $new_settings['bynder_oauth_consumer']),
'consumerSecret' => variable_get('bynder_oauth_consumer_secret', $new_settings['bynder_oauth_consumer_secret']),
'token' => variable_get('bynder_oauth_token', $new_settings['bynder_oauth_token']),
'tokenSecret' => variable_get('bynder_oauth_token_secret', $new_settings['bynder_oauth_token_secret']),
'baseUrl' => variable_get('bynder_url', $new_settings['bynder_url']),
));
$changed = true;
}
foreach ($new_settings as $key => $setting) {
switch ($key) {
case 'bynder_url':
case 'bynder_cdn_url':
case 'bynder_oauth_consumer':
case 'bynder_oauth_consumer_secret':
case 'bynder_oauth_token':
case 'bynder_oauth_token_secret':
if ($setting !== variable_get($key, false)) {
variable_set($key, $setting);
$changed = true;
}
}
}
if ($changed) {
unset($_SESSION['bynder_data']);
return true;
}
return false;
}
function bynder_form_submit($form, &$form_state) {
if (bynder_form_updated_api_settings($form_state['values'])) {
bynder_batch_reload_derivatives();
}
cache_clear_all('*', 'cache_bynder', true);
}