You are here

function _brightcove_upload_form_callback in Brightcove Video Connect 7.7

Same name and namespace in other branches
  1. 7.3 brightcove.module \_brightcove_upload_form_callback()
  2. 7.4 brightcove.module \_brightcove_upload_form_callback()
  3. 7.5 brightcove.module \_brightcove_upload_form_callback()
  4. 7.6 brightcove.module \_brightcove_upload_form_callback()

Upload the submitted video.

_state

Parameters

$form:

Return value

bool|stdClass

2 calls to _brightcove_upload_form_callback()
ajax_brightcove_media_upload_callback in brightcove_media/brightcove_media.module
Ajax callback for upload form
ajax_upload_video_dialog_close_callback in ./brightcove.module

File

./brightcove.module, line 2913
Brightcove module is an integration layer between any modules using Brightcove API. It makes all necessary checks for the API and makes settings available to the user.

Code

function _brightcove_upload_form_callback(&$form, $form_state) {
  $validators = [
    'file_validate_extensions' => [
      '3gp 3g2 aac ac3 asf avchd avi avs bdav dv dxa ea eac3 f4v flac flv h261 h263 h264 m2p m2ts m4a m4v mjpeg mka mks mkv mov mp3 mp4 mpeg mpegts mpg mt2s mts ogg ps qt rtsp thd ts vc1 wav webm wma wmv',
    ],
  ];
  $image_validators = [
    'file_validate_extensions' => [
      'png gif jpg jpeg',
    ],
  ];
  $caption_validators = [
    'file_validate_extensions' => [
      'vtt',
    ],
  ];
  $file = file_save_upload('file_upload', $validators, file_default_scheme() . '://');
  if (!$file) {
    drupal_set_message(t('Only Video files are allowed here.'), 'error');
    return $form;
  }
  if ($file->filesize <= 0) {

    // Some uploaded files had zero size, that's an error.
    drupal_set_message(t('Uploaded file not found. Are you sure that you uploaded an existing file?'), 'error');
    return $form;
  }
  $poster = file_save_upload('poster', $image_validators, file_default_scheme() . '://');
  $thumbnail = file_save_upload('thumbnail', $image_validators, file_default_scheme() . '://');
  $captions = [];
  for ($i = 0; $i < $form_state['caption_num']; $i++) {
    $caption = file_save_upload("caption_{$i}_file", $caption_validators, file_default_scheme() . '://');
    $captions[] = [
      'file' => $caption,
      'label' => $form_state['values']["caption_{$i}_label"],
      'srclang' => $form_state['values']["caption_{$i}_srclang"],
      'kind' => $form_state['values']["caption_{$i}_kind"],
      'default' => (bool) $form_state['values']["caption_{$i}_default"],
    ];
  }
  if (form_get_errors()) {
    return $form;
  }
  brightcove_load_lib();
  $video_entity = $form['#entity'];
  $video_id = FALSE;
  brightcove_try(function () use (&$video_id, $file, $form, &$form_state, $video_entity, $poster, $thumbnail, $captions) {
    $client = $video_entity->client;
    $hash = hash('sha512', mt_rand());
    $parent_entity_type = isset($form['#parent_entity_type']) ? $form['#parent_entity_type'] : '';
    $parent_bundle_name = isset($form['#parent_bundle_name']) ? $form['#parent_bundle_name'] : '';
    $parent_field_name = isset($form['#parent_field_name']) ? $form['#parent_field_name'] : '';
    $video = brightcove_upload_and_ingest($client, file_create_url($file->uri), $form_state['values']['profile'], $hash, $parent_entity_type, $parent_bundle_name, $parent_field_name, function ($video, $cms) use ($form_state, $poster, $thumbnail, $captions) {

      /** @var \Brightcove\Object\Video\Video $video */

      /** @var \Brightcove\API\CMS $cms */

      // Generate a reference id if the user left it empty.
      $reference_id = !empty($form_state['values']['reference_id']) ? $form_state['values']['reference_id'] : brightcove_generate_reference();
      $video
        ->setName($form_state['values']['title'])
        ->setDescription($form_state['values']['short'])
        ->setLongDescription($form_state['values']['long'])
        ->setLink((new \Brightcove\Object\Video\Link())
        ->setText($form_state['values']['linktext'])
        ->setUrl($form_state['values']['linkurl']))
        ->setState($form_state['values']['state'] ? 'ACTIVE' : 'INACTIVE')
        ->setEconomics($form_state['values']['economics'])
        ->setReferenceId($reference_id);

      // Set start/end availability dates if one of them was given.
      if ($form_state['values']['start_date'] == 'date_set' || $form_state['values']['end_date'] == 'date_set') {
        $video_schedule = new \Brightcove\Object\Video\Schedule();
        if ($form_state['values']['start_date'] == 'date_set') {
          $datetime = new DateTime($form_state['values']['start_availability_date']);
          $video_schedule
            ->setStartsAt($datetime
            ->format(DateTime::ISO8601));
        }
        if ($form_state['values']['end_date'] == 'date_set') {
          $datetime = new DateTime($form_state['values']['end_availability_date']);
          $video_schedule
            ->setEndsAt($datetime
            ->format(DateTime::ISO8601));
        }
        $video
          ->setSchedule($video_schedule);
      }

      /** @var \Brightcove\Object\CustomFields $fields */
      $fields = $cms
        ->getVideoFields();
      $ids = array_map(function ($field) {

        /** @var \Brightcove\Object\CustomField $field */
        return $field
          ->getId();
      }, $fields
        ->getCustomFields());
      $custom_fields = [];
      foreach ($ids as $id) {
        if (!empty($form_state['values']["custom_field_{$id}"])) {
          $custom_fields[$id] = $form_state['values']["custom_field_{$id}"];
        }
      }
      if ($custom_fields) {
        $video
          ->setCustomFields($custom_fields);
      }
      if (!empty($form_state['values']['tags'])) {
        $tags = explode(',', $form_state['values']['tags']);
        $tags = array_map('trim', $tags);
        $video
          ->setTags($tags);
      }
      return function (\Brightcove\API\Request\IngestRequest $ingest) use ($poster, $thumbnail, $captions) {
        if ($poster) {
          $poster_info = image_get_info($poster->uri);
          $ingest
            ->setPoster((new \Brightcove\API\Request\IngestImage())
            ->setUrl(file_create_url($poster->uri))
            ->setWidth($poster_info['width'])
            ->setHeight($poster_info['height']));
        }
        if ($thumbnail) {
          $thumbnail_info = image_get_info($thumbnail->uri);
          $ingest
            ->setThumbnail((new \Brightcove\API\Request\IngestImage())
            ->setUrl(file_create_url($thumbnail->uri))
            ->setWidth($thumbnail_info['width'])
            ->setHeight($thumbnail_info['height']));
        }
        if ($poster || $thumbnail) {
          $ingest
            ->setCaptureImages(FALSE);
        }
        $text_tracks = [];
        foreach ($captions as $caption) {
          $text_tracks[] = (new \Brightcove\API\Request\IngestTextTrack())
            ->setLabel($caption['label'])
            ->setKind($caption['kind'])
            ->setSrclang($caption['srclang'])
            ->setDefault($caption['default'])
            ->setUrl(file_create_url($caption['file']->uri));
        }
        if ($text_tracks) {
          $ingest
            ->setTextTracks($text_tracks);
        }
      };
    }, $form_state);
    $video_entity->video = $video;
    $video_entity->videoSaved = TRUE;

    // Save additional fields
    field_attach_submit('brightcove_video', $video_entity, $form['additional_fields'], $form_state);
    $video_entity
      ->save();
    $video_id = $video
      ->getId();
  }, NULL, 'brightcove_upload');
  return $video_id;
}