You are here

function brightcove_client_form_validate in Brightcove Video Connect 7.6

Same name and namespace in other branches
  1. 7.7 brightcove.client.inc \brightcove_client_form_validate()

Validation callback for brightcove_client_form().

1 string reference to 'brightcove_client_form_validate'
brightcove_client_form in ./brightcove.client.inc
Form callback: create or edit a brightcove client.

File

./brightcove.client.inc, line 302
Client related code.

Code

function brightcove_client_form_validate($form, &$form_state) {
  $client = $form_state['brightcove_client'];
  $bcid = empty($client->bcid) ? NULL : $client->bcid;
  $client_id = $form_state['values']['client_id'];
  $client_secret = $form_state['values']['client_secret'];
  brightcove_load_lib();
  $client = NULL;
  brightcove_try(function () use (&$form, &$client, $client_id, $client_secret) {
    $client = \Brightcove\API\Client::authorize($client_id, $client_secret);
    if (!$client
      ->isAuthorized()) {
      form_error($form, t('Failed to authorize.'));
    }
  }, function (Exception $ex) use (&$form) {
    form_error($form, t('Failed to authorize.'));
  });

  // Ensure the proposed client id is unique or reused only during client
  // updates.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'brightcove_client')
    ->propertyCondition('client_id', $client_id);
  if (!empty($bcid)) {
    $query
      ->entityCondition('entity_id', $bcid, '!=');
  }
  $result = $query
    ->execute();
  if (!empty($result)) {
    $bcid = key($result['brightcove_client']);
    if (empty($client->bcid) || $bcid != $client->bcid) {
      form_set_error('client_id', t('This client id is <a href="!url">already in use</a> and must be unique. Please supply another value.', [
        '!url' => url('admin/config/media/brightcove/brightcove_client/manage/' . $bcid),
      ]));
    }
  }

  // Trim leading and trailing whitespace from the client_id.
  form_set_value($form['client_id'], trim($client_id), $form_state);
}