You are here

function bynder_upload_add_to_library in Bynder 7

1 string reference to 'bynder_upload_add_to_library'
bynder_upload_submit in includes/bynder_upload.inc

File

includes/bynder_upload.inc, line 164

Code

function bynder_upload_add_to_library(&$context) {

  // We only try to save the file if it's not going to the waiting room.
  if (variable_get('bynder_upload_method') == 'upload') {
    $uploaded = false;
    for ($retries = 0; $retries < 10; $retries++) {
      $id = $context['results']['uploaded_file']['mediaid'];
      $selected_media = BynderMediaApi::getBynderApi()
        ->getObjectById($id);
      if (isset($selected_media['id'])) {
        $idHash = $selected_media['idHash'];
        $uri = file_stream_wrapper_uri_normalize("bynder://f/{$id}/i/{$idHash}");
        $file = file_uri_to_object($uri, true);
        $file->filemime = 'image/bynder';
        $file->filesize = 0;
        $file->filename = $selected_media['name'];
        $file = file_save($file);
        $file->file = array();
        $file->file[LANGUAGE_NONE] = array();
        $file->file[LANGUAGE_NONE][0] = (array) $file + array(
          'display' => true,
        );
        $file->is_new = true;
        field_attach_insert('media', $file);

        // Save Bynder asset entry.
        $bynderId = $selected_media['id'];
        $name = $selected_media['name'];
        $description = $selected_media['description'];
        $derivatives = $selected_media['thumbnails'];
        try {
          db_insert('bynder_media_entity')
            ->fields(array(
            'bynder_id' => $bynderId,
            'bynder_hash_id' => $idHash,
            'name' => $name,
            'description' => $description,
            'derivatives' => serialize($derivatives),
            'fid' => $file->fid,
          ))
            ->execute();
          $uploaded = true;
          $context['results']['fids_done'][] = $file->fid;
          break;
        } catch (Exception $e) {
          watchdog('bynder', $e
            ->getMessage());
        }
      }
      else {
        sleep(5);
        continue;
      }
    }
    if (!$uploaded) {
      drupal_set_message(t('An error occurred: a file was uploaded to Bynder but it was
            not added to your Drupal Media Library.'), 'warning');
    }
  }
  else {
    drupal_set_message(t("The media file was successfully uploaded to Bynder and sent to the \n            Waiting Room. You will have to add this file to your Drupal media library manually once it has been accepted."), 'warning');
  }
}