You are here

kaltura.module in Kaltura 7.3

Same filename and directory in other branches
  1. 5 kaltura.module
  2. 6.2 kaltura.module
  3. 6 kaltura.module
  4. 7.2 kaltura.module

Kaltura integration module - core functions.

File

kaltura.module
View source
<?php

/**
 * @file
 * Kaltura integration module - core functions.
 */

/**
 * @todo Add docs for all constants.
 */
define('KALTURASETTINGS_SERVER_URL', "http://www.kaltura.com");
define('KALTURASETTINGS_UICONF_ID', 600);
define('KALTURASETTINGS_BASE_WIDGET_ID', 600);
define('KALTURASETTINGS_ANONYMOUS_USER_ID', "Anonymous");
define('KALTURASETTINGS_CW_UICONF_ID', 4396241);
define('KALTURASETTINGS_CW_UICONF_ID_AUDIO', 4396051);
define('KALTURASETTINGS_CW_UICONF_ID_IMAGE', 4396061);
define('KALTURASETTINGS_CW_UICONF_ID_VIDEO', 4396041);
define('KALTURASETTINGS_DEFAULT_VIEWPLAYLIST_UICONF', 1292302);
define('KALTURASETTINGS_DEFAULT_EDITOR', 'Simple_Editor');
define('KALTURASETTINGS_SE_UICONF_ID', 603);
define('KALTURASETTINGS_SE_URL', 'kaltura/simple_editor');
define('KALTURASETTINGS_SE_WIDTH', 890);
define('KALTURASETTINGS_SE_HEIGHT', 546);
define('KALTURASETTINGS_SE_CUSTOM', 1);
define('KALTURASETTINGS_AE_UICONF_ID', 1000865);
define('KALTURASETTINGS_AE_URL', 'kaltura/advanced_editor');
define('KALTURASETTINGS_AE_WIDTH', 830);
define('KALTURASETTINGS_AE_HEIGHT', 690);
define('KALTURASETTINGS_AE_CUSTOM', 2);
define('KALTURASETTINGS_CW_COMMENTS_UICONF_ID', 4396231);
define('KALTURASETTINGS_DRUPAL_STATS_URL', "http://corp.kaltura.com/stats/drupal/");
define('KALTURASETTINGS_DEFAULT_VIDEO_PLAYER_UICONF', 'dark');
define('KALTURASETTINGS_DEFAULT_AUDIO_PLAYER_UICONF', 'dark');
define('KALTURASETTINGS_DEFAULT_VIEWPLAYLIST_PLAYER_UICONF', 'dark');
define('KALTURASETTINGS_DEFAULT_RC_PLAYER_UICONF', 'dark');
define('KALTURASETTINGS_DEFAULT_COMMENT_PLAYER_UICONF', 'dark');

/**
 * Indicates the Video media type.
 */
define('KALTURA_MEDIA_TYPE_VIDEO', 1);

/**
 * Indicates the Image media type.
 */
define('KALTURA_MEDIA_TYPE_IMAGE', 2);

/**
 * Indicates the Audio media type.
 */
define('KALTURA_MEDIA_TYPE_AUDIO', 5);

/**
 * Indicates the Remix media type.
 */
define('KALTURA_MEDIA_TYPE_REMIX', 6);

/**
 * Returns a URL prefix for thumbnails.
 *
 * @return string
 *   URL prefix.
 */
function _kaltura_thumbnail_base_url() {
  if (variable_get('kaltura_partner_id', '') != '') {
    $server_url = variable_get('kaltura_server_url', '') != '' ? variable_get('kaltura_server_url', '') : KALTURASETTINGS_SERVER_URL;
    return $server_url . '/p/' . variable_get('kaltura_partner_id', '') . '/sp/' . variable_get('kaltura_subp_id', '') . '/thumbnail';
  }
}

/**
 * Implements hook_libraries_info().
 */
function kaltura_libraries_info() {

  // Kaltura client (PHP5) library.
  // Expected to be extracted into 'sites/all/libraries/KalturaClient'.
  $libraries['KalturaClient'] = array(
    'name' => 'Kaltura Client',
    'vendor url' => 'http://www.kaltura.com/',
    'download url' => 'http://www.kaltura.com/api_v3/testme/client-libs.php',
    'version arguments' => array(
      'file' => 'KalturaClient.php',
      'pattern' => '/\\$.*ApiVersion.*\\(?\'(.*)\'\\)?;/i',
      'lines' => -1,
    ),
    'files' => array(
      'php' => array(
        'KalturaClient.php',
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_permission().
 */
function kaltura_permission() {
  return array(
    'administer kaltura' => array(
      'title' => t('Administer Kaltura'),
    ),
    'access kaltura widgets' => array(
      'title' => t('Access Kaltura widgets'),
    ),
    'view Kaltura embed code' => array(
      'title' => t('View Kaltura embed code'),
    ),
    'edit kaltura metadata' => array(
      'title' => t('Edit Kaltura metadata'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function kaltura_menu() {
  $items['admin/config/media/kaltura'] = array(
    'title' => 'Kaltura module settings',
    'description' => 'Check on the server integration status with Kaltura, import entries from Kaltura, see existing media and edit metadata and additional configuration options.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'kaltura_registration_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'includes/kaltura.admin.inc',
    'type' => MENU_NORMAL_ITEM,
    'weight' => -10,
  );
  $items['admin/config/media/kaltura/general'] = array(
    'title' => 'General',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/config/media/kaltura/entries/import'] = array(
    'title' => 'Import Media Entries from Kaltura',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'kaltura_import',
    ),
    'access arguments' => array(
      'administer kaltura',
    ),
    'file' => 'includes/kaltura.admin.inc',
    'type' => MENU_LOCAL_ACTION,
  );
  $items['admin/config/media/kaltura/entries'] = array(
    'title' => 'List of Kaltura Items',
    'page callback' => 'kaltura_list_entries',
    'access arguments' => array(
      'administer kaltura',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -7,
  );
  $items['admin/config/media/kaltura/fields-mapping'] = array(
    'title' => 'Fields mapping',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'kaltura_manage_fields_mapping',
    ),
    'access arguments' => array(
      'administer kaltura',
    ),
    'file' => 'includes/kaltura.admin.inc',
    'type' => MENU_LOCAL_TASK,
    // Place this tab after field_ui's ones.
    'weight' => 3,
  );
  $items['kaltura/notification_handler'] = array(
    'title' => 'Drupal Notification Handler',
    'file' => 'includes/kaltura.notification.inc',
    'page callback' => 'kaltura_notification_handler',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['kaltura/%ctools_js/metadata_editor/%'] = array(
    'title' => 'Metadata Editor',
    'page callback' => 'kaltura_metadata_editor',
    'page arguments' => array(
      1,
      3,
    ),
    'access arguments' => array(
      'edit kaltura metadata',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['kaltura/%ctools_js/kcw'] = array(
    'title' => 'Add Kaltura Media',
    'page callback' => 'kaltura_uploader',
    'page arguments' => array(
      1,
    ),
    'access arguments' => array(
      'access kaltura widgets',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['kaltura/%ctools_js/existing/%'] = array(
    'title' => 'Add from existing entries',
    'page callback' => 'kaltura_existing',
    'page arguments' => array(
      1,
      3,
      4,
      5,
    ),
    'access arguments' => array(
      'access kaltura widgets',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function kaltura_theme() {
  $themes = array(
    'kaltura_maintenance_page' => array(
      'variables' => array(
        'text' => '',
      ),
      'file' => 'includes/kaltura.theme.inc',
    ),
  );
  return $themes;
}

/**
 * Implements module_invoke().
 *
 * Used to call hooks exposed by kaltura which other module implements.
 */
function kaltura_invoke($hook, $data = NULL) {
  $hook = 'kaltura_' . $hook;
  $return = module_invoke_all($hook, $data);
  return $return;
}

/**
 * Implements hook_node_update_index().
 */
function kaltura_node_update_index($node) {

  // Add kaltura metadata to node search index.
  $text = '';
  $lang = $node->language;

  // Get all fields of field_kaltura_entryid type.
  $fields = field_read_fields(array(
    'type' => 'field_kaltura_entryid',
  ));
  foreach (array_keys($fields) as $field) {
    if (!empty($node->{$field}[$lang])) {
      foreach ($node->{$field}[$lang] as $item) {
        $entity = kaltura_entry_load($item['entryid']);
        $text .= ($text ? ',' : '') . $entity->kaltura_tags . ',' . $entity->kaltura_title . ',' . $entity->kaltura_description;
      }
    }
  }
  return $text;
}

/**
 * Kaltura uploader.
 *
 * CTools ajax callback function for the KCW.
 *
 * @param mixed $js
 *   CTools var to see if js is enabled or not.
 */
function kaltura_uploader($js) {
  ctools_include('modal');
  ctools_include('ajax');
  if (!variable_get('kaltura_partner_id')) {
    $error_message = t('You must configure partner\'s details first.') . ' ' . l(t('Click here'), 'admin/config/media/kaltura');
    ctools_modal_render(t('Error'), $error_message);
    exit;
  }
  $form_state = array(
    'ajax' => TRUE,
  );
  $output = ctools_modal_form_wrapper('kaltura_uploader_form', $form_state);
  if ($form_state['executed'] && $js) {
    $commands[] = array(
      'command' => 'kalturaCallFunction',
      'function' => 'kalturaUpdateMediaField',
      'arguments' => array(
        $form_state['eid'],
        $form_state['media_type'],
      ),
    );
    $commands[] = ctools_modal_command_dismiss();
    $output = $commands;
  }
  print ajax_render($output);
}

/**
 * CTools callback: Adds existing media that is already in the drupal db.
 *
 * @param mixed $js
 *   Is js enabled.
 * @param mixed $field_name
 *   The name of the field that will be populated.
 * @param mixed $widget
 *   The widget type so we can filter the media type.
 * @param mixed $cw_vars
 *
 * @return string|null
 */
function kaltura_existing($js, $field_name, $widget, $cw_vars) {
  if (!$js) {
    return '<div>' . t('We really need javascript for this one') . '</div>';
  }
  if (!module_exists('kaltura_views')) {
    return '<div>' . t('We really need the Views module for this one') . '</div>';
  }
  drupal_add_css(drupal_get_path('module', 'kaltura') . '/style/kaltura_entries.css');
  ctools_include('modal');
  ctools_include('ajax');
  $mtype = kaltura_widget_to_media_type($widget);
  $args[] = $mtype ? $mtype : NULL;
  $widget_vars = drupal_json_decode($cw_vars);
  $out = '';
  if ($widget_vars['add_existing'] == 'both') {
    $out = '<div class="kcw-button">' . ctools_modal_text_button(t('Add New'), 'kaltura/nojs/kcw', t('Add New Media'), 'ctools-modal-kcw-modal-style') . '</div>';
  }
  if (!empty($args[0])) {
    $view = views_get_view('kaltura_existing_one_type');
    $view
      ->set_arguments($args);
  }
  else {
    $view = views_get_view('kaltura_existing');
  }
  $out .= $view
    ->preview('default');
  $b_text = rawurldecode($widget_vars['button_text']);
  ctools_modal_render(t($b_text), $out);
}

/**
 * Implements hook_views_post_render().
 */
function kaltura_views_post_render(&$view, &$output, &$cache) {

  // TODO: Figure out why does he use so strange approach to check the view
  // name.
  if (strpos($view->name, 'kaltura_existing') === 0) {
    $parms = explode('/', $_GET['q']);
    $fname = check_plain($parms[3]);
    $output = str_replace('FnAmE', $fname, $output);
  }
}

/**
 * Implements hook_pre_render().
 */
function kaltura_views_pre_render($view) {

  // TODO: Figure out why does he use so strange approach to check the view
  // name.
  if (strpos($view->name, 'kaltura_existing') === 0) {
    $parms = explode('/', $_GET['q']);
    $widget = check_plain($parms[4]);
    $mtype = kaltura_widget_to_media_type($widget);
    $args[] = $mtype ? $mtype : NULL;
    if ($mtype != 0) {
      unset($view->display_handler->options['filters']['kaltura_media_type']);
      $view->args = $args;
    }
    else {
      unset($view->display['default']->display_options['arguments']['kaltura_media_type']);
    }
  }
}

/**
 * Helper function that converts the filed widget to kaltura media type.
 *
 * @param mixed $widget
 *   The field widget.
 */
function kaltura_widget_to_media_type($widget) {
  $filters = array(
    'field_kaltura_video' => KALTURA_MEDIA_TYPE_VIDEO,
    'field_kaltura_audio' => KALTURA_MEDIA_TYPE_AUDIO,
    'field_kaltura_image' => KALTURA_MEDIA_TYPE_IMAGE,
    'field_kaltura_web_cam' => 0,
    'field_kaltura_all_media' => 0,
  );
  return $filters[$widget];
}

/**
 * CTools callback function for metadata editor modal.
 *
 * @param mixed $js
 *   Is js enabled.
 * @param mixed $entry_id
 *   The entry id of the entry metadata.
 *
 * @return array|null
 */
function kaltura_metadata_editor($js, $entry_id) {
  if (!$js) {
    return drupal_get_form('kaltura_metadata_editor_form');
  }
  ctools_include('modal');
  ctools_include('ajax');
  drupal_add_css(drupal_get_path('module', 'kaltura') . '/style/kaltura_modal.css');
  $form_state = array(
    'title' => t('Kaltura metadata edit'),
    'ajax' => TRUE,
    'eid' => $entry_id,
  );
  $output = ctools_modal_form_wrapper('kaltura_metadata_editor_form', $form_state);
  if ($form_state['executed'] && $js) {
    $commands = array();
    $commands[] = ctools_modal_command_dismiss();
    print ajax_render($commands);
    exit;
  }
  if (isset($form_state['clicked_button']) && $form_state['clicked_button']['#value'] == 'cancel' && $js) {
    $commands = array();
    $commands[] = ctools_modal_command_dismiss();
    print ajax_render($commands);
    exit;
  }
  print ajax_render($output);
  exit;
}

/**
 * Implements hook_form().
 */
function kaltura_metadata_editor_form($form, $form_state) {

  // Get stuff from db.
  if (!isset($form_state['eid'])) {
    $form_state['eid'] = arg(3);
  }
  $entity = kaltura_entry_load($form_state['eid']);
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => isset($entity->kaltura_title) ? $entity->kaltura_title : '',
  );
  $form['desc'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => isset($entity->kaltura_description) ? $entity->kaltura_description : '',
    '#cols' => 20,
  );
  $form['tags'] = array(
    '#type' => 'textfield',
    '#title' => t('Tags'),
    '#size' => 60,
    '#default_value' => isset($entity->kaltura_tags) ? $entity->kaltura_tags : '',
  );
  $form['actions'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'buttons',
      ),
    ),
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('submit'),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'button',
    '#value' => t('cancel'),
  );
  return $form;
}

/**
 * Submit function for the metadata editing form.
 *
 * This function updates the kaltura metadata at the kaltura server and in the
 * drupal db.
 *
 * @param mixed $form
 * @param mixed $form_state
 *
 * @see kaltura_metadata_editor_form
 */
function kaltura_metadata_editor_form_submit($form, $form_state) {
  try {
    $k_helpers = new KalturaHelpers();
    $kaltura_client = $k_helpers
      ->getKalturaClient(TRUE);
    $k_helpers
      ->getSessionUser();
    libraries_load('KalturaClient');
    $kme = new KalturaBaseEntry();
    $kme->name = $form_state['values']['name'];
    $kme->description = $form_state['values']['desc'];
    $kme->tags = $form_state['values']['tags'];

    // TODO: add error handling. do not update local db if the remote is not ok.
    $res = $kaltura_client->baseEntry
      ->update($form_state['eid'], $kme);
    $entity = kaltura_entry_load($form_state['eid']);
    $entity->kaltura_tags = $form_state['values']['tags'];
    $entity->kaltura_title = $form_state['values']['name'];
    $entity->kaltura_description = $form_state['values']['desc'];
    kaltura_entry_save($entity);
    drupal_set_message(t('Metadata updated successfully'));
  } catch (Exception $e) {
    watchdog_exception('kaltura', $e);
  }
}

/**
 * Callback function for the kaltura/entries URL.
 *
 * @return string
 *   Rendered view or FALSE otherwise.
 */
function kaltura_list_entries() {
  if (!module_exists('kaltura_views')) {
    return '<div>' . t('We really need the Kaltura Views module for this one') . '</div>';
  }
  drupal_add_css(drupal_get_path('module', 'kaltura') . '/style/kaltura_entries.css');
  return views_embed_view('kaltura_list_entries', 'default');
}

/**
 * Implements hook_cron_queue_info().
 */
function kaltura_cron_queue_info() {
  $queues['kaltura_import_entries'] = array(
    'worker callback' => 'kaltura_import_entries',
    'time' => 60,
  );
  return $queues;
}

/**
 * Implements hook_cron().
 */
function kaltura_cron() {
  try {
    $helpers = new KalturaHelpers();
    $client = $helpers
      ->getKalturaClient(TRUE);
    $queue = DrupalQueue::get('kaltura_import_entries');

    // Don't fetch remote entries while there are unprocessed ones in the queue.
    if (!$queue
      ->numberOfItems()) {

      // 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 = variable_get('kaltura_last_imported');
      $pager = new KalturaFilterPager();

      // @todo Make this value configurable.
      $pager->pageSize = 100;
      $max_time = NULL;
      while (TRUE) {
        ++$pager->pageIndex;
        $result = $client->media
          ->listAction($filter, $pager);
        if (empty($result->objects)) {
          break;
        }
        $entry_ids = array();
        foreach ($result->objects as $entry) {
          $entry_ids[] = $entry->id;
        }
        $queue
          ->createItem($entry_ids);

        // Store the maximum of 'updatedAt' property.
        if (!isset($max_time)) {
          $max_time = reset($result->objects)->updatedAt;
        }
      }
      if (isset($max_time)) {
        variable_set('kaltura_last_imported', $max_time);
      }
    }
  } catch (Exception $e) {
    watchdog_exception('kaltura', $e);
  }
}

/**
 * Cron worker callback: Imports remote Kaltura Media entries into local system.
 *
 * @param string[] $entry_ids
 *   Entry IDs to be imported.
 *
 * @see kaltura_cron()
 */
function kaltura_import_entries(array $entry_ids) {
  try {
    $helpers = new KalturaHelpers();
    $client = $helpers
      ->getKalturaClient(TRUE);
    $filter = new KalturaMediaEntryFilter();
    $filter->idIn = implode(',', $entry_ids);
    $pager = new KalturaFilterPager();
    $pager->pageSize = count($entry_ids);

    // Fetch fresh entry data.
    $result = $client->media
      ->listAction($filter, $pager);
    if (!empty($result->objects)) {
      foreach ($result->objects as $entry) {

        // Load existing or create new entity.
        if ($entity = kaltura_entry_load($entry->id)) {

          // Check if the entry needs to be updated.
          // @todo This check prevents the fields newly added to the mapping to
          // be fetched. Remove it temporarily and think of a proper method to
          // update custom fields of existing entities.

          /*if ($entity->kaltura_updated_date == $entry->updatedAt) {
              continue;
            }*/
        }
        else {
          $entity = kaltura_entry_create(array(
            'kaltura_entryid' => $entry->id,
          ));
        }

        // Create or update entity properties.
        kaltura_map_properties($entry, $entity);

        // Create or update entity fields.
        kaltura_save_entry_metadata($entity);
        kaltura_entry_save($entity);
      }
    }
  } catch (Exception $e) {
    watchdog_exception('kaltura', $e);
  }
}

/**
 * Maps entry's properties to the entity ones.
 *
 * @param KalturaMediaEntry $entry
 *   Kaltura Media Entry object as returned from the service.
 * @param Entity $entity
 *   Kaltura Media Entry entity.
 */
function kaltura_map_properties(KalturaMediaEntry $entry, Entity $entity) {
  $entity->kaltura_tags = $entry->tags;
  $entity->kaltura_admin_tags = $entry->adminTags;
  $entity->kstatus = $entry->status;
  $entity->kaltura_media_type = $entry->mediaType;
  $entity->kaltura_duration = $entry->duration;
  $entity->kaltura_thumbnail_url = $entry->thumbnailUrl;
  $entity->kaltura_partner_data = $entry->partnerData;
  $entity->kaltura_source = $entry->sourceType;
  $entity->kaltura_width = $entry->width;
  $entity->kaltura_height = $entry->height;
  $entity->kaltura_download_url = $entry->downloadUrl;
  $entity->kaltura_media_date = $entry->mediaDate;
  $entity->kaltura_views = $entry->views;
  $entity->kaltura_plays = $entry->plays;
  $entity->kaltura_votes = $entry->votes;
  $entity->kaltura_rank = $entry->rank;
  $entity->kaltura_total_rank = $entry->totalRank;
  $entity->kaltura_created_date = $entry->createdAt;
  $entity->kaltura_updated_date = $entry->updatedAt;
  $entity->kaltura_title = $entry->name;
  $entity->kaltura_description = $entry->description;
  $entity->categories = $entry->categories;
}

/**
 * Helper function to get the description field from kaltura after notification.
 *
 * @param $type
 * @param $entry_id
 */
function get_entry_description($type, $entry_id) {
  $k_helpers = new KalturaHelpers();
  try {
    $kaltura_client = $k_helpers
      ->getKalturaClient(TRUE);
    if ($type == 6) {
      $result = $kaltura_client->mixing
        ->get($entry_id);
      return $result->description;
    }
    else {
      $result = $kaltura_client->baseEntry
        ->get($entry_id);
      return $result->description;
    }
  } catch (Exception $e) {
    watchdog_exception('kaltura', $e
      ->getMessage());
  }
}

/**
 * Implements hook_help().
 *
 * TODO: all help stuff throughout the module requires a complete overhaul, for
 * now it is completely useless.
 */
function kaltura_help($path, $arg) {
  switch ($path) {
    case 'admin/help#kaltura':
      return kaltura_get_help_html('general');
    case 'admin/config/media/kaltura/fields-mapping':
      return '<p>' . t('This page provides a form for mapping Kaltura Media Entry fields to custom metadata fields created in KMC. So when the entries are imported from remote then the corresponding entities will have their field values filled in locally.') . '</p>';
    case 'admin/config/media/kaltura/entries/import':
      return '<p>' . t('Kaltura media entries are imported automatically on cron. This page lets you explicitly import all entries without waiting till cron will run.') . '</p>';
  }
}

/**
 * Helper function to load HTML content for Drupal's help pages.
 *
 * TODO: completely useless function, requires a complete overhaul.
 */
function kaltura_get_help_html($page) {
  switch ($page) {
    case 'general':
      $filename = 'kaltura_general_help';
      break;
    case 'crossdomain':
      $filename = 'kaltura_cd_help';
      break;
  }

  // $help_html = file_get_contents(drupal_get_path('module', 'kaltura') . '/kaltura_help/' . $filename . '.html');
  $help_html = '';
  return $help_html;
}

/**
 * Helper function that gets a widgetId, calls Kaltura's getWidget service and
 * returns the embed code of the widget.
 *
 * @param int $widget_id
 */
function katura_fetch_widget_html($widget_id) {
  try {
    $k_helpers = new KalturaHelpers();
    $session_user = $k_helpers
      ->getSessionUser();
    $kaltura_client = $k_helpers
      ->getKalturaClient();
    $result = $kaltura_client
      ->getWidget($session_user, $widget_id, 1);
    $embbed = $result['result']['widget']['widgetHtml'];
    return $embbed;
  } catch (Exception $e) {
    watchdog_exception('kaltura', $e);
  }
}

/**
 * Chooses what URL the user will be redirected to after AddEntry in the CW.
 *
 * This function exposes a hook_cw_destination().
 */
function kaltura_cw_destination() {
  $url = kaltura_invoke('cw_destination');
  return $url ? $url[0] : url('kaltura/entries');
}

/**
 * Helper function to convert array of variables to the correct string.
 *
 * @param array $vars
 *
 * @return string
 */
function kaltura_format_cw_vars($vars = array()) {
  $str = urlencode(serialize($vars));
  return $str;
}

/**
 * Implements hook_entity_info().
 */
function kaltura_entity_info() {
  $return['kaltura_entry'] = array(
    'label' => t('Kaltura Media Entry'),
    'plural label' => t('Kaltura Media Entries'),
    'entity class' => 'Entity',
    'controller class' => 'EntityAPIController',
    'base table' => 'node_kaltura',
    'fieldable' => TRUE,
    'entity keys' => array(
      'id' => 'eid',
      'name' => 'kaltura_entryid',
      'label' => 'kaltura_title',
    ),
    'bundles' => array(
      'kaltura_entry' => array(
        'label' => t('Kaltura Media Entry'),
        'admin' => array(
          'path' => 'admin/config/media/kaltura',
          'access arguments' => array(
            'administer kaltura',
          ),
        ),
      ),
    ),
    'module' => 'kaltura',
  );
  $return['kaltura_notification'] = array(
    'label' => t('Kaltura Notification'),
    'plural label' => t('Kaltura Notifications'),
    'entity class' => 'Entity',
    'controller class' => 'EntityAPIController',
    'base table' => 'kaltura_notifications',
    'entity keys' => array(
      'id' => 'notification_id',
    ),
    'module' => 'kaltura',
  );

  // Support entity cache module.
  if (module_exists('entitycache')) {
    $return['kaltura_entry']['entity cache'] = TRUE;
  }
  return $return;
}

/**
 * Creates a new Kaltura Media Entry object.
 *
 * @param array $values
 *   An array of values to set, keyed by property name. "kaltura_entryid"
 *   element has to be specified.
 *
 * @return Entity
 *   A new instance of the Kaltura Media Entry.
 */
function kaltura_entry_create(array $values) {
  return entity_create('kaltura_entry', $values);
}

/**
 * Loads a Kaltura Media Entry object from the database.
 *
 * @param int|string $id
 *   Entity ID or name (Kaltura Entry ID).
 *
 * @return Entity|false
 *   A fully-populated Entity object, or FALSE if the entry is not found.
 */
function kaltura_entry_load($id) {
  $ids = array(
    $id,
  );
  $conditions = array();
  if (!is_numeric($id)) {
    $ids = FALSE;
    $conditions['kaltura_entryid'] = $id;
  }
  $entities = entity_load('kaltura_entry', $ids, $conditions);
  return reset($entities);
}

/**
 * Saves changes to a Kaltura Media Entry or adds a new entry.
 *
 * @param Entity $entity
 *   The Entity object to be saved.
 *
 * @return int|false
 *   SAVED_NEW or SAVED_UPDATED is returned depending on the operation
 *   performed. If there is no information how to save the entity, FALSE is
 *   returned.
 */
function kaltura_entry_save(Entity $entity) {
  return entity_save('kaltura_entry', $entity);
}

/**
 * Permanently deletes the given Kaltura Media Entry.
 *
 * @param int $id
 *   Entity ID.
 */
function kaltura_entry_delete($id) {
  entity_delete('kaltura_entry', $id);
}

/**
 * Retrieves custom fields of media entry.
 *
 * @param Entity $entity
 *   Kaltura Media Entry object.
 */
function kaltura_save_entry_metadata(Entity $entity) {
  $helpers = new KalturaHelpers();
  $metadata = $helpers
    ->getEntryMetadata($entity->kaltura_entryid);
  foreach ($metadata as $profile_id => $profile_meta) {
    $metadata[$profile_id]['metadata_profile'] = $helpers
      ->getMetadataProfile($profile_id);
  }
  module_invoke_all('kaltura_save_entry_metadata', $entity, $metadata);
}

/**
 * Implements hook_kaltura_save_entry_metadata().
 */
function kaltura_kaltura_save_entry_metadata(Entity $entity, array $metadata) {

  // This hook implementation works exclusively with fields mapped at Kaltura
  // 'Fields mapping' configuration page.
  $map = array_filter(variable_get('kaltura_fields_map', array()));
  foreach (array_keys($map) as $field_name) {
    list($profile_id, $remote_field_name) = explode('/', $map[$field_name], 2);
    $entity->{$field_name} = array();
    if (!empty($metadata[$profile_id]['fields'][$remote_field_name])) {
      foreach ($metadata[$profile_id]['fields'][$remote_field_name] as $item) {
        $entity->{$field_name}[LANGUAGE_NONE][]['value'] = $item;
      }
    }
  }
}

Functions

Namesort descending Description
get_entry_description Helper function to get the description field from kaltura after notification.
kaltura_cron Implements hook_cron().
kaltura_cron_queue_info Implements hook_cron_queue_info().
kaltura_cw_destination Chooses what URL the user will be redirected to after AddEntry in the CW.
kaltura_entity_info Implements hook_entity_info().
kaltura_entry_create Creates a new Kaltura Media Entry object.
kaltura_entry_delete Permanently deletes the given Kaltura Media Entry.
kaltura_entry_load Loads a Kaltura Media Entry object from the database.
kaltura_entry_save Saves changes to a Kaltura Media Entry or adds a new entry.
kaltura_existing CTools callback: Adds existing media that is already in the drupal db.
kaltura_format_cw_vars Helper function to convert array of variables to the correct string.
kaltura_get_help_html Helper function to load HTML content for Drupal's help pages.
kaltura_help Implements hook_help().
kaltura_import_entries Cron worker callback: Imports remote Kaltura Media entries into local system.
kaltura_invoke Implements module_invoke().
kaltura_kaltura_save_entry_metadata Implements hook_kaltura_save_entry_metadata().
kaltura_libraries_info Implements hook_libraries_info().
kaltura_list_entries Callback function for the kaltura/entries URL.
kaltura_map_properties Maps entry's properties to the entity ones.
kaltura_menu Implements hook_menu().
kaltura_metadata_editor CTools callback function for metadata editor modal.
kaltura_metadata_editor_form Implements hook_form().
kaltura_metadata_editor_form_submit Submit function for the metadata editing form.
kaltura_node_update_index Implements hook_node_update_index().
kaltura_permission Implements hook_permission().
kaltura_save_entry_metadata Retrieves custom fields of media entry.
kaltura_theme Implements hook_theme().
kaltura_uploader Kaltura uploader.
kaltura_views_post_render Implements hook_views_post_render().
kaltura_views_pre_render Implements hook_pre_render().
kaltura_widget_to_media_type Helper function that converts the filed widget to kaltura media type.
katura_fetch_widget_html Helper function that gets a widgetId, calls Kaltura's getWidget service and returns the embed code of the widget.
_kaltura_thumbnail_base_url Returns a URL prefix for thumbnails.

Constants