You are here

function kaltura_import_submit in Kaltura 7.3

Form submission handler for kaltura_import().

File

includes/kaltura.admin.inc, line 687
Contains functions for administration use of the kaltura core module.

Code

function kaltura_import_submit($form, &$form_state) {
  try {
    $last_updated = variable_get('kaltura_last_imported');
    $helpers = new KalturaHelpers();
    $client = $helpers
      ->getKalturaClient(TRUE);

    // Use order by updated date (DESC) because if some entries will be
    // updated at the same time while we fetching them then those entries will
    // be fetched on the next cron run.
    $filter = new KalturaMediaEntryFilter();
    $filter->orderBy = KalturaMediaEntryOrderBy::UPDATED_AT_DESC;
    $filter->updatedAtGreaterThanOrEqual = $last_updated ? $last_updated + 1 : NULL;
    $pager = new KalturaFilterPager();

    // @todo Make this value configurable.
    $pager->pageSize = 100;
    $max_time = NULL;
    $entry_ids = $last_updated ? $helpers
      ->filterOutUpToDateEntries($last_updated) : array();
    $queue = DrupalQueue::get('kaltura_import_entries');

    // Cron won't do the same.
    $queue
      ->deleteQueue();
    while (TRUE) {
      ++$pager->pageIndex;
      $result = $client->media
        ->listAction($filter, $pager);
      if (empty($result->objects)) {
        break;
      }
      foreach ($result->objects as $entry) {
        $entry_ids[] = $entry->id;
      }

      // Store the maximum of 'updatedAt' property.
      if (!isset($max_time)) {
        $max_time = reset($result->objects)->updatedAt;
      }
    }
    $batch = array(
      'operations' => array(
        array(
          'kaltura_batch_import',
          array(
            $entry_ids,
            $max_time,
          ),
        ),
      ),
      'title' => t('Importing Kaltura Media Entries'),
      'init_message' => t('Importing...'),
      'progress_message' => t('@estimate remained.'),
      'finished' => 'kaltura_batch_import_finished',
      'file' => drupal_get_path('module', 'kaltura') . '/includes/kaltura.admin.inc',
    );
    batch_set($batch);
  } catch (Exception $e) {
    watchdog_exception('kaltura', $e);
    if (module_exists('dblog')) {
      $message = t('The website encountered an unexpected error. More info in <a href="!url">logs</a>.', array(
        '!url' => url('admin/reports/dblog'),
      ));
    }
    else {
      $message = t('The website encountered an unexpected error. More info in logs.');
    }
    drupal_set_message($message, 'error');
  }
}