You are here

kaltura.admin.inc in Kaltura 6.2

File

includes/kaltura.admin.inc
View source
<?php

/*
 * @file
 * contains functions for administration use of the kaltura core module
 *
 */

/*
 * helper function to retrieve partner's details from kaltura
 * TODO: testing
 * CMAC changes:
 * kaltura client config set up to reflect changes in the API
 * getsecrets now called instead of getPartner
 * return array built using Kaltura Partner object
 *
 */
function kaltura_get_partner_details($partner_id, $cms_email, $cms_password) {

  // $session_user is not actually used below. Remove?
  $session_user = KalturaHelpers::getSessionUser();
  $config = KalturaHelpers::getServiceConfiguration();
  $config->partnerId = $partner_id;
  $kaltura_client = new KalturaClient($config);

  /**
   * here we are trying to retrieve the partner details from the kaltura server
   *
   */

  //$result = $kaltura_client->getPartner($session_user, $cms_email, $cms_password, $partner_id);
  try {
    $result = $kaltura_client->partner
      ->getsecrets($partner_id, $cms_email, $cms_password);
  } catch (Exception $ex) {
    drupal_set_message(t('Kaltura client exception: @message', array(
      '@message' => $ex
        ->getMessage(),
    )), 'error');

    //if ( module_exists('devel') ) {

    //  dpm( $ex->getTrace(), 'Exception stack trace' );

    //}
    $result['error'] = $ex
      ->getMessage();
  }

  // convert result to array
  if ($result->id) {
    return array(
      $result->id,
      $result->id . "00",
      //subpartner id
      $result->notificationUrl,
      $result->secret,
      $result->adminSecret,
    );
  }
  else {
    return array(
      FALSE,
      $result['error'],
    );
  }
}

/*
 * helper function that uses Kaltura's getDefaultWidget service to create some default widgets for
 * the partner, in case he does not have these yet
 * TODO
 * CMAC
 * changes
 * changed client initialisation
 * changed the getDefaultWidget function call to $kaltura_client->widget->get
 * question: what do we do with the widget when we get them?
 */
function kaltura_create_widgets_for_partner($partner_id, $secret) {

  /*$session_user = KalturaHelpers::getSessionUser();
    $config = KalturaHelpers::getServiceConfiguration();
    $config->partnerId = $partner_id;
    $kaltura_client = new KalturaClient();
    $kaltura_client->setConfig($config);*/
  $session_user = KalturaHelpers::getSessionUser();
  $kaltura_client = KalturaHelpers::getKalturaClient(1);
  $widgets = new KalturaSettings();
  foreach ($widgets->kdp_widgets as $type => $uiconfs) {
    foreach ($uiconfs as $skin => $confs) {

      /* oferc: that doesn't work for me yet */

      //      $result = $kaltura_client->widget->get("_1_".$confs['view_uiconf']);
      //      $result = $kaltura_client->widget->get("_1_".$confs['remix_uiconf']);
    }
  }
}

/**
 * determine how the general settings form will look like.
 *
 * returns one of 2 states of forms:
 * 1) registration form (if variables do not exist in the DB) 
 *    - either as SaaS or Kaltura CE
 * 2) notifications settings + configuration test + settings form
 *
 * @TODO remove the registration form component from this now that we
 * have a separate form.
 *
 * @TODO FAPI rewrite.
 */
function kaltura_settings_form() {
  global $base_url;
  if (variable_get('kaltura_partner_id', '') == '') {
    drupal_goto('admin/settings/kaltura/install');
  }
  if (!module_exists('advanced_help')) {
    $form['kaltura_help'] = array(
      '#value' => t('For more detailed help enable the advanced help module.'),
    );
  }

  /* status report */
  $form['kaltura_server_status'] = array(
    '#type' => 'fieldset',
    '#title' => t('Server Status'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  list($admin_session_status, $admin_session_error) = kaltura_check_server_status($admin = 1);
  list($normal_session_status, $normal_session_error) = kaltura_check_server_status($admin = 0);
  $status_class = $admin_session_status && $normal_session_status ? 'ok' : 'bad';
  $session_status = $admin_session_status && $normal_session_status ? 'OK' : 'ERROR';
  $error = '';
  if ($admin_session_error) {
    $error .= '<br />Admin Session: ' . $admin_session_error;
    $error_status = TRUE;
  }
  if ($normal_session_error) {
    $error .= '<br />Normal Session: ' . $normal_session_error;
    $error_status = TRUE;
  }
  $form['kaltura_server_status']['kaltura_Admin_test_server'] = array(
    '#type' => 'item',
    '#title' => t('Drupal to Kaltura Session Test'),
    '#prefix' => '<div class="kaltura-status-' . $status_class . '">',
    '#value' => 'Session status: ' . $session_status . ($error_status ? $error : ''),
    '#suffix' => '</div>',
  );
  if (kaltura_variable_get_real('kaltura_notification_type', 0) == 1) {
    list($status, $text) = kaltura_test_notification_config();
    $prefix = '<div class="kaltura-status-' . ($status ? 'ok' : 'bad') . '">';
    $form['kaltura_server_status']['kaltura_notification_status'] = array(
      '#type' => 'item',
      '#title' => t('Kaltura to Drupal Notification Status'),
      '#prefix' => $prefix,
      '#value' => $text,
      '#suffix' => '</div>',
    );
  }
  list($status, $text) = kaltura_test_cron_job_status();
  $prefix = '<div class="kaltura-status-' . ($status ? 'ok' : 'bad') . '">';
  $form['kaltura_server_status']['kaltura_cron_job_status'] = array(
    '#type' => 'item',
    '#title' => t('Cron-Job Status'),
    '#prefix' => $prefix,
    '#value' => $text,
    '#suffix' => '</div>',
  );
  list($status, $text) = kaltura_test_crossdomain();
  $prefix = '<div class="kaltura-status-' . ($status ? 'ok' : 'bad') . '">';
  $form['kaltura_server_status']['kaltura_crossdomain_status'] = array(
    '#type' => 'item',
    '#title' => t('crossdomain.xml Status'),
    '#prefix' => $prefix,
    '#value' => $text,
    '#suffix' => '</div>',
  );

  /* The normal settings form. */
  if (variable_get('kaltura_partner_id', '') == '') {
    drupal_set_title('All in One Video Module by Kaltura Installation');
    $forgot_link_attributes = array(
      'attributes' => array(
        'target' => '_blank',
      ),
    );
    $information_text = 'Please enter your CMS email & password';
    $information_text .= '<br />' . l('forgot password', 'http://www.kaltura.com/index.php/cms/login', $forgot_link_attributes);
    $form['kaltura_settings_information']['#value'] = $information_text;
  }
  $form['kaltura_partner_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Partner Info'),
    '#collapsible' => $_GET['register'] === 'no' ? FALSE : TRUE,
    '#collapsed' => $_GET['register'] === 'no' ? FALSE : TRUE,
  );

  /*
    $form['kaltura_partner_settings']['kaltura_exitss_switch'] = array(
    '#value' => $not_registered,
    );
  */
  $partner_exist = variable_get('kaltura_partner_id', '');
  $readonly_attr = array();
  if (!empty($partner_exist)) {
    $readonly_attr = array(
      'readonly' => 'readonly',
    );
  }
  $form['kaltura_partner_settings']['kaltura_partner_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Partner ID'),
    '#default_value' => variable_get('kaltura_partner_id', ''),
    '#size' => 20,
    '#maxlength' => 10,
    '#attributes' => $readonly_attr,
  );
  $form['kaltura_partner_settings']['kaltura_partner_cms_email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#default_value' => variable_get('kaltura_partner_cms_email', ''),
    //    '#attributes' => $readonly_attr,
    '#description' => t('This value must match your Kaltura server settings.'),
  );
  $form['kaltura_partner_settings']['kaltura_partner_cms_password'] = array(
    '#type' => 'password',
    '#title' => t('Password'),
    '#default_value' => variable_get('kaltura_partner_cms_password', ''),
    //    '#attributes' => $readonly_attr,
    '#description' => t('This value must match your Kaltura server settings. Leave blank unless changing your password.'),
  );
  if (!empty($partner_exist)) {
    $kmc_url = trim(variable_get('kaltura_server_url', 'http://www.kaltura.com'), '/') . '/kmc';
    $form['kaltura_partner_settings']['kaltura_upd_data'] = array(
      '#type' => 'item',
      '#value' => t('You can update your email and password via Kaltura Management Console here: ') . l($kmc_url, $kmc_url, array(
        'attributes' => array(
          'target' => '_blank',
        ),
      )),
    );
  }
  if (variable_get('kaltura_partner_id', '') == '') {
    $form_state['storage']['submit'] = 'ImportPartner';
    $form['kaltura_registration']['buttons']['import_partner'] = array(
      '#type' => 'button',
      '#title' => 'Complete Installation',
      '#value' => 'Complete Installation',
      '#default_value' => 'Complete Installation',
      '#weight' => 40,
    );
  }

  /* if (variable_get('kaltura_partner_id', '') == '') */
  $form['kaltura_partner_settings']['kaltura_subp_id'] = array(
    '#type' => 'hidden',
    '#title' => t('Kaltura Sub-Partner Id'),
    '#default_value' => variable_get('kaltura_subp_id', ''),
    '#size' => 20,
    '#maxlength' => 10,
  );

  /**
   * This test always returns true, right?
   */
  if (variable_get('kaltura_local_registration', 0) != 1 || 1) {
    $form['kaltura_partner_settings']['kaltura_secret'] = array(
      '#type' => 'hidden',
      '#title' => t('Kaltura Secret'),
      '#default_value' => variable_get('kaltura_secret', ''),
      '#size' => 60,
      '#maxlength' => 64,
    );
    $form['kaltura_partner_settings']['kaltura_admin_secret'] = array(
      '#type' => 'hidden',
      '#title' => t('Kaltura Admin Secret'),
      '#default_value' => variable_get('kaltura_admin_secret', ''),
      '#size' => 60,
      '#maxlength' => 64,
    );
  }

  /* ALWAYS TRUE: if (variable_get('kaltura_local_registration', 0) != 1 || 1) */
  $form['kaltura_partner_settings']['kaltura_server_url'] = array(
    '#type' => 'hidden',
    '#title' => t('Kaltura Server URL'),
    '#default_value' => variable_get('kaltura_server_url', KalturaHelpers::getKalturaServerUrl()),
    '#size' => 100,
    '#maxlength' => 64,
  );

  /* notification settings fieldset */
  $notification_settings = kaltura_notifications_settings_form();
  $form['kaltura_notification_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Kaltura to Drupal Notification Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  foreach ($notification_settings as $name => $form_element) {
    $form['kaltura_notification_settings'][$name] = $form_element;
  }

  /* credits / backlinks */
  $form['kaltura_credits'] = array(
    '#type' => 'fieldset',
    '#title' => 'Credits',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $text = '<b>Open Source Video Links</b>' . PHP_EOL;
  $text .= 'Kaltura invests a great deal of effort in the open source community, and specifically in the open source video movement.';
  $text .= 'The following two links will be included within the video player in order to further promote Kaltura and its efforts in the open video space. ';
  $text .= 'We hope that you will join us by including these links in the player.' . PHP_EOL;
  $text .= l('open source video', 'http://corp.kaltura.com/download') . PHP_EOL . l('video platform', 'http://corp.kaltura.com/technology/');
  $form['kaltura_credits']['kaltura_credits_text'] = array(
    '#type' => 'item',
    '#value' => nl2br($text),
  );
  $form['kaltura_credits']['kaltura_display_credits'] = array(
    '#type' => 'checkbox',
    '#title' => t('Include links in player'),
    '#option' => t('Include links in player'),
    '#default_value' => variable_get('kaltura_display_credits', 1),
  );
  $form['kaltura_help_message'] = array(
    '#type' => 'item',
    '#value' => t('Please feel free to ') . l('contact us', 'http://corp.kaltura.com/support/form/project/16', array(
      'attributes' => array(
        'target' => '_blank',
      ),
    )) . t(' with any questions.'),
  );
  $form['#submit'][] = 'kaltura_settings_form_submit';
  return system_settings_form($form);
}

/**
 * Validation handler for kaltura_settings_form() 
 */
function kaltura_settings_form_validate($form, &$form_state) {
  if (empty($form_state['values']['kaltura_partner_cms_password'])) {
    $form_state['values']['kaltura_partner_cms_password'] = variable_get('kaltura_partner_cms_password', '');
  }
  if (!valid_url($form_state['values']['kaltura_partner_url2'], TRUE)) {
    form_set_error('kaltura_partner_url2', t('Notification URL should be a valid URL, but %partner_url is not.', array(
      '%partner_url' => $form_state['values']['kaltura_partner_url2'],
    )));
  }
  try {

    // test access credentials
    $config = Kalturahelpers::getServiceConfiguration();

    // we're still in validate, so the changed values aren't
    // committed to $form_state['storage'] yet
    $config->partnerId = variable_get('kaltura_partner_id', '');
    $config->serviceUrl = variable_get('kaltura_server_url', '');
    $client = new KalturaClient($config);
    $result = $client->partner
      ->getSecrets($config->partnerId, $form_state['values']['kaltura_partner_cms_email'], $form_state['values']['kaltura_partner_cms_password']);
    drupal_set_message(t('Kaltura credentials verified against !kaltura_url.', array(
      '!kaltura_url' => $config->serviceUrl,
    )));
  } catch (Exception $ex) {
    $targs = array(
      '%message' => $ex
        ->getMessage(),
      '%file' => $ex
        ->getFile(),
      '%line' => $ex
        ->getLine(),
      '%code' => $ex
        ->getCode(),
    );
    form_set_error('', t('Kaltura credentials failed login: %message', $targs));
    watchdog('kaltura', 'Unable to verify Kaltura credentials: %message', $targs, WATCHDOG_ERROR);
  }
}

/**
 * Submit handler for kaltura_settings_form()
 */
function kaltura_settings_form_submit($form, &$form_state) {
  if ($form_state['values']['kaltura_notification_type']) {
  }
  if (isset($form_state['values']['kaltura_notification_type']) && $form_state['values']['kaltura_partner_url2']) {

    // TODO: update partner settings with notification type
    $url2 = url('kaltura/notification_handler', array(
      'absolute' => TRUE,
    ));
    if ($form_state['values']['kaltura_notification_type'] == 1) {
      $not_type = 3;
    }
    else {
      $not_type = 2;
    }
    try {
      $result = kaltura_update_kaltura_partner($not_type, $url2);
      if (!$result->id) {
        if (isset($result['error']['code'])) {
          throw new Exception($result['error']['desc'], $result['error']['code']);
        }
        else {
          throw new Exception('Updating notification settings on Kaltura server failed.');
        }
      }

      /* if notification URL has changed, or notification type is 3 */
      if ($form_state['values']['last_notification_url'] != $form_state['values']['kaltura_partner_url2'] || $not_type == 3) {
        variable_set('kaltura_notification_status', 0);
        list($status, $text) = kaltura_test_notification_config();
        if ($status == FALSE) {
          drupal_set_message(t('Notification test failed. The URL you specified is not valid. Please make sure it is correct and save the settings again.'), 'error');
        }
        else {

          // drupal_set_message(t('Notification test was successful.')); /* Uncomment when [#1025090] is resolved. */
        }
      }
    } catch (Exception $ex) {
      drupal_set_message(t('Error: %message when updating notification settings.', array(
        '%message' => $ex
          ->getMessage(),
      )), 'error');
      watchdog('kaltura', 'Error @message updating notification settings.<br />Backtrace:<pre>!trace</pre>', array(
        '@message' => $ex
          ->getMessage(),
        '!trace' => $ex
          ->getTrace(),
      ));
    }
  }
}

/*
 * Implementation of hook_form_alter().
 */
function kaltura_form_alter(&$form, &$form_state, $form_id) {
  if (variable_get('kaltura_partner_id', '') == '') {
    if ($form_id == 'kaltura_settings_form') {
      unset($form['buttons']['submit']);
      unset($form['buttons']['preview']);
      unset($form['buttons']['reset']);
    }
  }
}

/**
 * callback function for url admin/settings/kaltura/entries/import
 *
 * CMAC
 * changes
 * KalturaEntryFilter changed to KalturaMediaEntryFilter
 *
 * @TODO this may need some reworking
 */
function kaltura_import_entries_page() {

  // initialise variables
  $foundFlag = TRUE;

  // set to false if there are no entries
  $enabledFlag = FALSE;

  // set to true if mix or media node is enabled
  $entries = array();

  // mix entries
  $mediaEntries = array();

  // media entries
  $entries_need_import = array();
  $each_page = 30;
  $total = 0;
  $mediaTotal = 0;
  $finished = false;
  $current_page = 1;
  if ($_POST['op'] == 'import selected') {
    kaltura_import_entries($_POST['entries_to_import']);
    drupal_goto('admin/settings/kaltura/entries/import');
  }
  if (variable_get('kaltura_partner_id', '') == '') {
    return t('You have not configured your partner details yet. Import cannot be performed until you setup your partner details');
  }
  $get_entries = module_exists('node_kaltura_entry');
  $get_mixes = module_exists('node_kaltura_mix');
  if (empty($_GET['p'])) {
    $_GET['p'] = 1;
  }
  else {
    $current_page = $_GET['p'];
  }
  while (!$finished) {
    set_time_limit(30);

    //get the mix entries
    if ($get_mixes) {
      $filter = new KalturaMixEntryFilter();
      list($total, $entries) = kaltura_get_my_entries($filter, KalturaEntryType::MIX, $each_page, $current_page);
      if ($total == 0) {
        $foundFlag = FALSE;
      }
      $enabledFlag = TRUE;
    }

    // get the media entries
    if ($get_entries) {
      $filter = new KalturaBaseEntryFilter();
      list($mediaTotal, $mediaEntries) = kaltura_get_my_entries($filter, KalturaEntryType::MEDIA_CLIP, $each_page, $current_page);
      if ($mediaTotal == 0) {
        $foundFlag = FALSE;
      }
      $enabledFlag = TRUE;
    }
    if (!$enabledFlag) {
      return t('Can\'t import entries, you did not enable the node_kaltura_entries module.');
    }
    if ($entries === NULL) {
      return t('No entries found.');
    }
    if (is_array($mediaEntries)) {
      $entries_need_import = array_merge($entries_need_import, $mediaEntries);
    }
    if (is_array($entries)) {
      $entries_need_import = array_merge($entries_need_import, $entries);
    }
    $entries_need_import = kaltura_remove_existing_entries($entries_need_import);
    if (count($entries_need_import) >= $each_page) {
      $finished = TRUE;
    }
    $shown = $current_page * $each_page;
    if ($mediaTotal < $shown && $total < $shown) {
      $finished = TRUE;
    }
    $current_page++;
  }
  if (!count($entries_need_import)) {
    return t('No entries needed to be imported.');
  }
  if (count($entries_need_import) > $each_page) {
    $multi_dim = array_chunk($entries_need_import, $each_page, true);
    $entries_need_import = $multi_dim[0];
  }

  /*
    if (count($entries_need_import) < $each_page) {
      list($second_count, $second_page) = kaltura_get_my_entries($filter, $each_page, $_GET['p'] + 1);
      $new_entries_need_import = kaltura_remove_existing_entries($second_page);
      if (is_array($new_entries_need_import) && $second_page !== NULL) {
        foreach ($new_entries_need_import as $eid => $entry) {
          $entries_need_import[$eid] = $entry;
          if (count($entries_need_import) == $each_page) {
            break;
          }
        }
      }
    }
  */
  if ($total + $mediaTotal > $each_page) {
    $pages = ceil(($total + $mediaTotal) / $each_page);
    for ($start = 1; $start <= $pages; $start++) {
      if ($start == $_GET['p']) {
        $pager .= '<a>' . $start . '</a>';
      }
      else {
        $pager .= l($start, 'admin/settings/kaltura/entries/import', array(
          'query' => 'p=' . $start,
        ));
      }
      $pager .= '&nbsp;';
    }
  }
  if (is_array($entries_need_import) && count($entries_need_import)) {
    foreach ($entries_need_import as $entry => $details) {
      $entries_options[$details['id']] = $details['id'] . ': ' . $details['name'] . ' (' . ($details['type'] == 2 ? 'mix' : 'entry') . ')';
    }
  }
  else {
    $entries_options = array();
  }
  $output = drupal_get_form('kaltura_create_entries_form', array(
    $entries_options,
  ));
  return $pager . $output . $pager;
}

/**
 * helper function that uses kaltura_get_entries() to retreive a list of specific entries
 * belongs to the partner, and call kaltura_import_entry() for each of the retreived list
 */
function kaltura_import_entries($entries) {
  $entries_list = implode(',', $entries);
  $my_entries = kaltura_get_entries($entries_list, count($entries), true);
  foreach ($my_entries->objects as $entry) {
    kaltura_import_entry($entry);
  }
  drupal_set_message('<b>Please Note: All imported nodes are not yet published (node status 0)</b>');
}

/*
 * helper function that gets an entry retreived from Kaltura
 * builds a node object according to entry, and invokes node_insert to save the node
 */
function kaltura_import_entry($entry_details) {
  global $user;
  $node = new stdClass();
  if ($entry_details->type == 1) {
    $type = 'entry';
  }
  elseif ($entry_details->type == 2) {
    $type = 'mix';
  }
  else {
    $type = '';
  }
  $node->type = 'kaltura_' . $type;
  $node->title = $entry_details->name;
  $node->kaltura_entryId = $entry_details->id;
  $node->uid = $user->uid;
  $node->body = '';
  $node->status = 0;
  $node->created = time();
  $node->comment = FALSE;
  $node->changed = time();
  $node->promote = 0;
  $node->sticky = 0;
  $node->kstatus = $entry_details->status;
  $node->kaltura_entry_data = serialize($entry_details);
  $node->kaltura_tags = $entry_details->tags;
  $node->kaltura_admin_tags = '';
  $node->kaltura_media_type = $entry_details->mediaType;
  $node->kaltura_duration = $entry_details->duration;
  $node->kaltura_thumbnail_url = $entry_details->thumbnailUrl;
  $node->kaltura_partner_data = 'imported ' . $type;

  //$node->kaltura_source = $entry_details['source'];

  //$node->kaltura_source_id = $entry_details['sourceId'];

  //$node->kaltura_source_link = $entry_details['sourceLink'];
  $node->kaltura_width = $entry_details->width;
  $node->kaltura_height = $entry_details->height;
  $node->kaltura_download_url = $entry_details->downloadUrl;

  //$node->kaltura_media_date = $entry_details['mediaDate'];
  $node->kaltura_views = $entry_details->views;
  $node->kaltura_plays = $entry_details->plays;
  $node->kaltura_votes = $entry_details->votes;
  $node->kaltura_rank = $entry_details->rank;
  $node->kaltura_total_rank = $entry_details->totalRank;
  node_save($node);
  module_invoke('node_kaltura_' . $type, 'hook_insert', $node);
  drupal_set_message('Kaltura item imported: ' . l($node->title, 'node/' . $node->nid) . ' (id: ' . $node->kaltura_entryId . ')');
}

/*
 * helper function to provide form fields containing all entries for import
 * each entry as checkbox option
 */
function kaltura_create_entries_form($form) {
  $args = func_get_args();
  $entries_options = $args[1][0];
  $form['entries_to_import'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Choose the entries you want to import'),
    '#options' => $entries_options,
  );
  $form['import_now'] = array(
    '#type' => 'submit',
    '#title' => t('import selected'),
    '#value' => t('import selected'),
  );
  return $form;
}

/*
 * helper functuion to get all entries of partner from Kaltura's DB
 * TODO
 * CMAC
 * changes:
 * now retrieving all media entries - needs to import mix entries as well
 */
function kaltura_get_my_entries($filter, $type, $page_size = 30, $page = 1) {
  $isAdmin = 1;
  $session_user = KalturaHelpers::getSessionUser();
  $kaltura_client = KalturaHelpers::getKalturaClient($isAdmin);

  // this is admin task and should be able to import all entries in the account
  $pager_filter = new KalturaFilterPager();
  $pager_filter->pageIndex = $page;
  $pager_filter->pageSize = $page_size;

  //$result = $kaltura_client->listMyEntries($session_user, $filter, 0, $page_size, $page);
  switch ($type) {
    case KalturaEntryType::MIX:
      $result = $kaltura_client->mixing
        ->listAction($filter, $pager_filter);
      break;
    case KalturaEntryType::MEDIA_CLIP:
      $filter->TypeEqual = KalturaEntryType::MEDIA_CLIP;
      $result = $kaltura_client->baseEntry
        ->listAction($filter, $pager_filter);
      break;
  }
  if ($result->totalCount > 0) {
    $count = $result->totalCount;

    //$each_page = $result['result']['page_size'];
    if (is_array($result->objects) && count($result->objects)) {
      foreach ($result->objects as $entry) {
        $returned_entries[$entry->id] = array(
          'id' => $entry->id,
          'name' => $entry->name,
          'type' => $entry->type,
        );
      }
    }
    return array(
      $count,
      $returned_entries,
    );
  }
  else {
    return array(
      0,
      array(),
    );
  }
}

/*
 * helper function that recieves a list of entries, and returns a new list
 * which does not contain entries that are already represented as nodes
 */
function kaltura_remove_existing_entries($entries) {
  $count = 0;
  $new_entries = array();
  if (is_array($entries) && count($entries)) {
    foreach ($entries as $entry) {
      $sel = 'SELECT COUNT(nid) n FROM {node_kaltura} WHERE kaltura_entryId = \'%s\'';
      $result = db_fetch_object(db_query($sel, $entry['id']));
      if ($result->n == 0) {
        $new_entries[] = $entry;
      }
      else {
        $count++;
      }
    }
  }
  return $new_entries;
}

/*
 * helper function that checks the drupal cron job status
 * cron job is required to collect statictics about kaltura items
 */
function kaltura_test_cron_job_status() {
  $cron_last = variable_get('cron_last', NULL);
  if (!$cron_last) {
    return array(
      FALSE,
      t('cron has not run.') . ' ' . t('If you do not configure cron to run regularly, statistics about kaltura items will not be updated.'),
    );
  }
  else {
    return array(
      TRUE,
      t('cron has run.') . ' ' . t('Verify that you have a cron job configured, so you don\'t need to run it manually.') . '<br />' . t('Statistics about kaltura items will only be updated at cron run.'),
    );
  }
}

/**
 * helper function that tests the notification status of the module
 *
 * this function tries to close a loop with the kaltura server in a
 * "real-time" test.  The notification handler sets a variable in the
 * DB before the this function run ends, therefore we need to use the
 * kaltura_variable_get_real() function to check the variable status
 * in the DB and not in the $_GLOBALS which are set before this
 * function even start
 *
 * @TODO in KalturaClient we need a new method:
 *
 *  function testNotification($kalturaSessionUser)
 *  {
 *    $params = array();
 *    $result = $this->hit("testnotification", $kalturaSessionUser, $params);
 *    return $result;
 *  }
 * 
 * @TODO http://drupal.org/node/1025090
 */
function kaltura_test_notification_config() {
  return array(
    TRUE,
    t('Test call not yet implemented; see !test_call_issue', array(
      '!test_call_issue' => l('[#1025090]', 'http://drupal.org/node/' . 1025090),
    )),
  );

  /*
    if (kaltura_variable_get_real('kaltura_notification_status', 0) == 0) {
      $session_user = KalturaHelpers::getSessionUser();
      $kaltura_client = KalturaHelpers::getKalturaClient();
      $result = $kaltura_client->testNotification($session_user);
      sleep(3);
    }
    if (kaltura_variable_get_real('kaltura_notification_status', 0) === 1) {
      return array(TRUE, 'Drupal server receives notification from Kaltura server.');
    }
    return array(FALSE, 'Drupal server fails to receive notifications from Kaltura. Please fix your configuration or disable server notifications.');
  */
}

/**
 * Replace default Drupal's variable_get() function with a "real-time" one.
 *
 * This function checks a variable status in the DB (not from $_GLOBALS['conf']).
 * 
 * TODO: This smells funny. Presumably it's here because $conf is
 * already set when kaltura_test_notification_config() is called, and
 * we need to allow the Kaltura server to ping us back and
 * variable_set('kaltura_notification_status',1), then read the stored
 * value. Check if it's used elsewhere, and why. Consider a straight
 * SQL check against the variables table to replace this.
 */
function kaltura_variable_get_real($var, $default) {
  $sql = "SELECT * FROM {variable} WHERE name = '%s'";
  $result = db_fetch_object(db_query($sql, $var));
  if (!$result) {
    return $default;
  }
  return unserialize($result->value);
}

/**
 * Used when SimpleXML is not available; just assume everything looks OK and hope for the best.
 */
function kaltura_alternate_xml_verification($filename) {
  return array(
    TRUE,
    t('crossdomain.xml is in place and seems to be OK'),
  );
}

/**
 * helper function that checks if crossdomain.xml is in the right place
 */
function kaltura_test_crossdomain() {
  global $base_url;
  $cd_req = drupal_http_request($base_url . '/crossdomain.xml');
  if ($cd_req->code == '200') {
    return kaltura_parse_crossdomain($cd_req->data);
  }
  else {

    // fall back to fetching and retrieving file contents (should we
    // succeed if HTTP request has failed, though?
    $filename = $_SERVER['DOCUMENT_ROOT'] . '/crossdomain.xml';
    if ($cd_xml = file_get_contents($filename)) {
      return kaltura_parse_crossdomain($cd_xml);
    }
    else {
      return array(
        FALSE,
        t('crossdomain.xml file could not be found in your site\'s root directory') . ' [' . $filename . ']' . t('Please read ') . l('here', 'admin/help/kaltura'),
      );
    }
  }
}

/**
 * helper function that checks if crossdomain.xml says the right things
 */
function kaltura_parse_crossdomain($crossdomain_xml) {
  if (!function_exists('simplexml_load_string')) {
    return kaltura_alternate_xml_verification($filename);
  }
  $cd_xml_content = simplexml_load_string($crossdomain_xml);
  foreach ($cd_xml_content
    ->children() as $child) {
    $atts = $child
      ->attributes();
    if ($atts['domain'] == '*' || $atts['domain'] == '*.kaltura.com') {
      $kaltura_ok = TRUE;
    }
    if ($atts['headers'] == '*' && ($atts['domain'] == '*' || $atts['domain'] == '*.kaltura.com')) {
      $headers_ok = TRUE;
    }
  }
  if ($kaltura_ok && $headers_ok) {
    return array(
      TRUE,
      t('crossdomain.xml is in place and seems to be OK'),
    );
  }
  else {
    return array(
      FALSE,
      t('crossdomain.xml is in place, but it is probably not configured properly') . '<br />' . t('Please read ') . l('here', 'admin/help/kaltura'),
    );
  }
}

/**
 * @TODO: Settings form in URL admin/settings/kaltura/general/notifications
 */
function kaltura_notifications_settings_form() {
  global $base_url;
  $notification_types = array(
    //'1' => 'only server side notifications',
    '2' => 'only client side notifications',
    '3' => 'both server & client notifications',
  );
  $notification_type_description = t('Enabling server notifications allows the Kaltura video platform to update your Drupal server about the status of ' . 'the media content. This is especially useful when uploading content in various formats that needs to be transcoded. ' . 'In order to support server side notifications, your server must be publicly available on the internet.');
  $form['kaltura_notification_type'] = array(
    '#type' => 'checkbox',
    '#options' => 'Enable server side notifications',
    '#description' => $notification_type_description,
    '#title' => t('Enable server side notifications'),
    '#default_value' => variable_get('kaltura_notification_type', 0),
  );
  $form['kaltura_partner_url2'] = array(
    '#type' => 'textfield',
    '#title' => 'Notification URL',
    '#description' => t('Your Drupal site base URL; path %notification_path will be appended.', array(
      '%notification_path' => 'kaltura/notification_handler',
    )),
    '#default_value' => variable_get('kaltura_partner_url2', $base_url),
  );
  $form['last_notification_url'] = array(
    '#type' => 'hidden',
    '#value' => variable_get('kaltura_partner_url2', ''),
  );
  return $form;
  return system_settings_form($form);
}
function kaltura_notifications_settings_form_submit() {
}

/*
 * helper function that tries to open session with the kaltura server
 *
 * used to verify partner details, and display errors if there are
 *
 * CMAC
 * changed
 * added new variable_get statements required for the api calls
 * updated KalturaClient creation process
 * changed API calls
 *
 * TODO check correct format for error checking of result
 */
function kaltura_check_server_status($admin) {

  //get the variables we will need to start the session

  /*
    $partner_id = variable_get('kaltura_partner_id', '');
    $secret = variable_get('kaltura_secret', '');
    $admin_secret = variable_get('kaltura_admin_secret', '');

    $session_user = KalturaHelpers::getSessionUser();
    $config = KalturaHelpers::getServiceConfiguration();
    $config->partnerId = $partner_id;
    $kaltura_client = new KalturaClient();
    $kaltura_client->setConfig($config);

    if ($admin == 1) {
      $result = $kaltura_client->session->start($partner_id, $secret, $session_user->screenName);
    }
    else {
      $result = $kaltura_client->session->start($partner_id, $admin_secret, $session_user->screenName, KalturaSessionType::ADMIN);
    }
  */
  try {
    $result = KalturaHelpers::getKalturaClient($admin);
    $kaltura_status = TRUE;
    $kaltura_error = "";
  } catch (Exception $ex) {
    $kaltura_status = FALSE;
    $kaltura_error = $ex
      ->getMessage();
  }

  /*
    if ($result[]) {
      $kaltura_status = FALSE;
      $kaltura_error = $result['error'][0]["code"] .' '. $result['error'][0]["desc"];
    }
    else {
    }
  */
  return array(
    $kaltura_status,
    $kaltura_error,
  );
}

/*
 * helper function to generate a cms password
 *
 * cms is the "administration panel" for kaltura's partners in kaltura's website
 */
function kaltura_generate_cms_password() {
  $pass = '';
  $pass = md5(time() . $_SERVER['REMOTE_ADDR']);
  $pass = drupal_substr($pass, 0, 8);
  return $pass;
}

Functions

Namesort descending Description
kaltura_alternate_xml_verification Used when SimpleXML is not available; just assume everything looks OK and hope for the best.
kaltura_check_server_status
kaltura_create_entries_form
kaltura_create_widgets_for_partner
kaltura_form_alter
kaltura_generate_cms_password
kaltura_get_my_entries
kaltura_get_partner_details
kaltura_import_entries helper function that uses kaltura_get_entries() to retreive a list of specific entries belongs to the partner, and call kaltura_import_entry() for each of the retreived list
kaltura_import_entries_page callback function for url admin/settings/kaltura/entries/import
kaltura_import_entry
kaltura_notifications_settings_form @TODO: Settings form in URL admin/settings/kaltura/general/notifications
kaltura_notifications_settings_form_submit
kaltura_parse_crossdomain helper function that checks if crossdomain.xml says the right things
kaltura_remove_existing_entries
kaltura_settings_form determine how the general settings form will look like.
kaltura_settings_form_submit Submit handler for kaltura_settings_form()
kaltura_settings_form_validate Validation handler for kaltura_settings_form()
kaltura_test_cron_job_status
kaltura_test_crossdomain helper function that checks if crossdomain.xml is in the right place
kaltura_test_notification_config helper function that tests the notification status of the module
kaltura_variable_get_real Replace default Drupal's variable_get() function with a "real-time" one.