You are here

field_kaltura.module in Kaltura 5

kaltura integration module - functions to provide kaltura as CCK field

File

plugins/field_kaltura/field_kaltura.module
View source
<?php

/**
 * @file
 * kaltura integration module - functions to provide kaltura as CCK field 
 * 
 */

/**
 * Implementation of hook_theme().
 */
function field_kaltura_theme() {
  return array(
    'field_kaltura_textfield' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'field_kaltura_remix_textfield' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'field_kaltura_formatter_default' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'field_kaltura_formatter_full' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'field_kaltura_formatter_teaser' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'field_kaltura_formatter_thumbnail' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_field_info().
 */
function field_kaltura_field_info() {
  $fields = array();
  if (module_exists('node_kaltura_entry')) {
    $fields['Kaltura_Media'] = array(
      'label' => t('Kaltura Media'),
      'description' => t('Store text in the database.'),
    );
  }
  if (module_exists('node_kaltura_mix')) {
    $fields['Kaltura_Media_Remix'] = array(
      'label' => t('Kaltura Media Remix'),
      'description' => t('Store text in the database.'),
    );
  }
  return $fields;
}

/**
 * Implementation of hook_field_settings().
 */
function field_kaltura_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();

      // specific field setting can come here (taxonomy integration/player selection/etc...)
      $form['thumbsize'] = array(
        '#type' => 'fieldset',
        '#title' => t('Thumbnail Dimensions'),
        '#description' => t('Note - these settings will only apply when choosing "Thumbnail" display'),
      );
      $form['thumbsize']['thumbsize_width'] = array(
        '#type' => 'textfield',
        '#title' => t('Thumbnail Width'),
        '#size' => 5,
        '#default_value' => !empty($field['thumbsize_width']) ? $field['thumbsize_width'] : '',
      );
      $form['thumbsize']['thumbsize_height'] = array(
        '#type' => 'textfield',
        '#title' => t('Thumbnail Height'),
        '#size' => 5,
        '#default_value' => !empty($field['thumbsize_height']) ? $field['thumbsize_height'] : '',
      );
      return $form;
    case 'save':
      return array(
        'allowed_values',
        'allowed_values_php',
        'thumbsize_height',
        'thumbsize_width',
      );
    case 'database columns':
      $columns['value'] = array(
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
        'sortable' => TRUE,
      );
      return $columns;
    case 'filters':
      return array(
        'like' => array(
          'operator' => 'views_handler_operator_like',
          'handler' => 'views_handler_filter_like',
        ),
      );
      break;
  }
}

/*
 * Implementation of hook_form_alter().
 */
function field_kaltura_form_alter($form_id, &$form) {
  if ($form['field_type']['#value'] == 'Kaltura_Media') {
    $form['field']['multiple']['#type'] = 'hidden';
    $form['field']['multiple']['#default_value'] = 0;
  }
  unset($form['widget']['default_value_fieldset']);
}

/**
 * Implementation of hook_field().
 */
function field_kaltura_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      break;
  }
}
function kaltura_get_default_display($item, $teaser) {
  $entries = explode(',', rtrim($item['value'], ','));
  $all_field = '';
  if (is_array($entries) && count($entries)) {
    foreach ($entries as $entry_id) {
      if (!$entry_id) {
        continue;
      }
      $mynode = node_load(kaltura_get_node_for_mix($entry_id));
      if ($mynode) {
        if ($mynode->type == 'kaltura_entry') {
          $all_field .= '<div class="field-entry-' . $entry_id . '">' . theme('node_kaltura_entry_entryId', $mynode, 0) . '</div>';
        }
        else {
          $all_field .= '<div class="field-entry-' . $entry_id . '">' . theme('node_kaltura_mix_entryId', $mynode, 0) . '</div>';
        }
      }
    }
  }
  $text = $all_field;
  return $text;
}
function kaltura_get_full_display($item, $teaser) {
  $entries = explode(',', rtrim($item['value'], ','));
  $all_field = '';
  if (is_array($entries) && count($entries)) {
    foreach ($entries as $entry_id) {
      if (!$entry_id) {
        continue;
      }
      $mynode = node_load(kaltura_get_node_for_mix($entry_id));
      if ($mynode) {
        $all_field .= node_view($mynode);
      }
    }
  }
  $text = $all_field;
  return $text;
}
function kaltura_get_teaser_display($item, $teaser) {
  $entries = explode(',', rtrim($item['value'], ','));
  $all_field = '';
  if (is_array($entries) && count($entries)) {
    foreach ($entries as $entry_id) {
      if (!$entry_id) {
        continue;
      }
      $mynode = node_load(kaltura_get_node_for_mix($entry_id));
      if ($mynode) {
        $all_field .= node_view($mynode, TRUE);
      }
    }
  }
  $text = $all_field;
  return $text;
}
function kaltura_get_thumbnail_display($item, $teaser, $field) {
  $size = array(
    'width' => !empty($field['thumbsize_width']) ? $field['thumbsize_width'] : NULL,
    'height' => !empty($field['thumbsize_height']) ? $field['thumbsize_height'] : NULL,
  );
  if (!$size['width'] && !$size['height']) {
    unset($size);
  }
  $entries = explode(',', rtrim($item['value'], ','));
  $all_field = '';
  if (is_array($entries) && count($entries)) {
    foreach ($entries as $entry_id) {
      if (!$entry_id) {
        continue;
      }
      $mynode = node_load(kaltura_get_node_for_mix($entry_id));
      if ($mynode) {
        if ($mynode->type == 'kaltura_entry') {
          $all_field .= '<div class="field-entry-' . $entry_id . '">' . theme('node_kaltura_entry_thumbnail_url', $mynode, $teaser, FALSE, $size) . '</div>';
        }
        else {
          $all_field .= '<div class="field-entry-' . $entry_id . '">' . theme('node_kaltura_mix_thumbnail_url', $mynode, $teaser, FALSE, $size) . '</div>';
        }
      }
    }
  }
  $text = $all_field;
  return $text;
}

/**
 * Implementation of hook_content_is_empty().
 */
function field_kaltura_content_is_empty($item, $field) {
  if (empty($item['value']) && (string) $item['value'] !== '0') {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implementation of hook_field_formatter_info().
 */
function field_kaltura_field_formatter_info() {
  return array(
    'default' => array(
      'label' => t('Default'),
      'field types' => array(
        'Kaltura_Media',
        'Kaltura_Media_Remix',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'full' => array(
      'label' => t('Full'),
      'field types' => array(
        'Kaltura_Media',
        'Kaltura_Media_Remix',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'teaser' => array(
      'label' => t('Teaser'),
      'field types' => array(
        'Kaltura_Media',
        'Kaltura_Media_Remix',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'thumbnail' => array(
      'label' => t('Thumbnail'),
      'field types' => array(
        'Kaltura_Media',
        'Kaltura_Media_Remix',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
  );
}
function field_kaltura_field_formatter($field, $item, $formatter, $node) {
  $function = 'theme_field_kaltura_formatter_' . $formatter;
  return $function($item, $field);
}

/**
 * Theme function for 'default' text field formatter.
 */
function theme_field_kaltura_formatter_default($item) {
  return kaltura_get_default_display($item, 1);
}

/**
 * Theme function for 'full' text field formatter.
 */
function theme_field_kaltura_formatter_full($item) {
  return kaltura_get_default_display($item, 0);
}

/**
 * Theme function for 'teaser' text field formatter.
 */
function theme_field_kaltura_formatter_teaser($item) {
  return kaltura_get_teaser_display($item, 1);
}

/**
 * Theme function for 'thumbnail' text field formatter.
 */
function theme_field_kaltura_formatter_thumbnail($item, $field) {
  return kaltura_get_thumbnail_display($item, 0, $field);
}

/**
 * Implementation of hook_widget_info().
 *
 * Here we indicate that the content module will handle
 * the default value and multiple values for these widgets.
 *
 * Callbacks can be omitted if default handing is used.
 * They're included here just so this module can be used
 * as an example for custom modules that might do things
 * differently.
 */
function field_kaltura_widget_info() {
  return array(
    'field_kaltura_textfield' => array(
      'label' => t('Kaltura Media'),
      'field types' => array(
        'Kaltura_Media',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
    'field_kaltura_remix_textfield' => array(
      'label' => t('Kaltura Media Remix'),
      'field types' => array(
        'Kaltura_Media_Remix',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
  );
}

/**
 * Implementation of FAPI hook_elements().
 *
 * Any FAPI callbacks needed for individual widgets can be declared here,
 * and the element will be passed to those callbacks for processing.
 *
 * Drupal will automatically theme the element using a theme with
 * the same name as the hook_elements key.
 *
 * Autocomplete_path is not used by field_kaltura_widget but other widgets can use it
 * (see nodereference and userreference).
 */
function field_kaltura_elements() {
  return array(
    'field_kaltura_textfield' => array(
      '#input' => TRUE,
      '#columns' => array(
        'value',
      ),
      '#delta' => 0,
      '#process' => array(
        'field_kaltura_textfield_process',
      ),
      '#autocomplete_path' => FALSE,
    ),
    'field_kaltura_remix_textfield' => array(
      '#input' => TRUE,
      '#columns' => array(
        'value',
      ),
      '#delta' => 0,
      '#process' => array(
        'field_kaltura_remix_textfield_process',
      ),
      '#autocomplete_path' => FALSE,
    ),
  );
}

/**
 * Implementation of hook_widget_settings().
 */
function field_kaltura_widget_settings($op, $widget) {
  switch ($op) {
    case 'form':
      $form = array();
      $rows = isset($widget['rows']) && is_numeric($widget['rows']) ? $widget['rows'] : 1;
      $size = isset($widget['size']) && is_numeric($widget['size']) ? $widget['size'] : 60;

      //if ($widget['type'] == 'field_kaltura_textfield' ) {
      $form['rows'] = array(
        '#type' => 'hidden',
        '#value' => $rows,
      );
      $form['size'] = array(
        '#type' => 'hidden',
        '#title' => t('Size of textfield'),
        '#default_value' => $size,
        '#element_validate' => array(
          '_field_kaltura_widget_settings_size_validate',
        ),
        '#required' => TRUE,
      );

      /*}
        else {
          $form['rows'] = array(
            '#type' => 'textfield',
            '#title' => t('Rows'),
            '#default_value' => $rows,
            '#element_validate' => array('_field_kaltura_widget_settings_row_validate'),
            '#required' => TRUE,
          );
          $form['size'] = array('#type' => 'hidden', '#value' => $size);
        }*/
      return $form;
    case 'save':
      return array(
        'rows',
        'size',
      );
  }
}

/*
 * helper function to validate settings form input
 */
function _field_kaltura_widget_settings_row_validate($element, &$form_state) {
  $value = $form_state['values']['rows'];
  if (!is_numeric($value) || intval($value) != $value || $value <= 0) {
    form_error($element, t('"Rows" must be a positive integer.'));
  }
}

/*
 * helper function to validate settings form input
 */
function _field_kaltura_widget_settings_size_validate($element, &$form_state) {
  $value = $form_state['values']['size'];
  if (!is_numeric($value) || intval($value) != $value || $value <= 0) {
    form_error($element, t('"Size" must be a positive integer.'));
  }
}

/**
 * Implementation of hook_widget().
 *
 * Attach a single form element to the form. It will be built out and
 * validated in the callback(s) listed in hook_elements. We build it
 * out in the callbacks rather than here in hook_widget so it can be
 * plugged into any module that can provide it with valid
 * $field information.
 *
 * Content module will set the weight, field name and delta values
 * for each form element. This is a change from earlier CCK versions
 * where the widget managed its own multiple values.
 *
 * If there are multiple values for this field, the content module will
 * call this function as many times as needed.
 *
 * @param $form
 *   the entire form array, $form['#node'] holds node information
 * @param $form_state
 *   the form_state, $form_state['values'][$field['field_name']]
 *   holds the field's form values.
 * @param $field
 *   the field array
 * @param $items
 *   array of default values for this field
 * @param $delta
 *   the order of this item in the array of subelements (0, 1, 2, etc)
 *
 * @return
 *   the form item for a single element for this field
 */
function field_kaltura_widget($op, &$node, $field, &$items) {
  switch ($op) {
    case 'form':
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
      );
      if ($field['multiple']) {
        $form[$field['field_name']]['#type'] = 'fieldset';
        $form[$field['field_name']]['#description'] = content_filter_xss(t($field['widget']['description']));
        $delta = 0;

        // real widgets
        foreach ($items as $data) {
          if ($data['value']) {
            field_kaltura_remix_textfield_process($field, $edit, $form_state, $form, $items, $delta);
            $delta++;
          }
        }

        // adding empty widgets
        foreach (range($delta, $delta + 2) as $delta) {
          field_kaltura_remix_textfield_process($field, $edit, $form_state, $form, $items, $delta, $no_content);
        }
      }
      else {
        if ($field['type'] == 'Kaltura_Media') {
          field_kaltura_textfield_process($field, $edit, $form_state, $form, $items);
        }
        if ($field['type'] == 'Kaltura_Media_Remix') {
          field_kaltura_remix_textfield_process($field, $edit, $form_state, $form, $items);
        }
      }
      return $form;
    case 'process form values':

      // Don't save empty fields except the first value
      foreach ($items as $delta => $item) {
        if ($item['value'] == '' && $delta > 0) {
          unset($items[$delta]);
        }
      }
      break;
  }
}

/**
 * Process an individual element.
 *
 * Build the form element. When creating a form using FAPI #process,
 * note that $element['#value'] is already set.
 *
 * The $fields array is in $form['#field_info'][$element['#field_name']].
 */
function field_kaltura_textfield_process($field, $edit, $form_state, &$form, $items) {
  global $_kaltura_thumbnail_base_url;
  $custom_name = 'edit-' . str_replace('_', '-', $field['field_name']) . '-0-value';
  $cw_vars = kaltura_format_cw_vars(array(
    'kshow_id' => 0,
    'partner_data' => '',
    'type' => 'entry',
    'context' => 'field',
    'field_id' => $custom_name,
  ));
  $action_buttons = '<input type="button" value="Add Kaltura Media" onclick="kalturaInitModalBox(\'' . url('kaltura/contribution_wizard/' . $cw_vars) . '\', {width:680, height:380} );" />';
  $field_thumbs = kaltura_get_field_thumbs($items[0]['value'], $custom_name, 'Media', $field['required']);
  $form['data'] = array(
    $custom_name . '_thumbs' => array(
      '#type' => 'item',
      '#value' => '<div id="' . $custom_name . '_thumbs_div">' . $field_thumbs . '</div>',
    ),
    'action_buttons' => array(
      '#type' => 'item',
      '#value' => $action_buttons,
    ),
  );
  $form[$field['field_name']][0]['value'] = array(
    '#tree' => TRUE,
    '#type' => 'hidden',
    '#default_value' => $items[0]['value'],
    '#size' => 60,
    '#attributes' => array(
      'class' => 'text',
      'readonly' => 'readonly',
    ),
    '#readonly' => 'readonly',
  );
}
function kaltura_get_field_thumbs($field_value, $field_name, $type, $required) {
  global $_kaltura_thumbnail_base_url;
  $thumbs = array();
  $entries = explode(',', $field_value);
  foreach ($entries as $entry) {
    if ($entry) {
      $thumbs[] = '<div class="kaltura_field_thumb"><img src="' . $_kaltura_thumbnail_base_url . '/entry_id/' . $entry . '" /><br />' . '<input type="button" title="remove item" onclick="remove_item_from_field(\'' . $field_name . '\', \'' . $entry . '\', \'' . $_kaltura_thumbnail_base_url . '\');" class="remove_media" /></div>';
    }
  }
  $span_required = $required ? '<span class="form-required" title="This field is required.">*</span>' : '';
  return '<div class="title">Added ' . $type . ': ' . $span_required . '</div>' . implode('', $thumbs) . '<div class="clear-block"></div>';
}

/**
 * Process an individual element.
 *
 * Build the form element. When creating a form using FAPI #process,
 * note that $element['#value'] is already set.
 *
 * The $fields array is in $form['#field_info'][$element['#field_name']].
 */
function field_kaltura_remix_textfield_process($field, $edit, $form_state, &$form, $items, $delta = 0, $no_content = FALSE) {
  global $_kaltura_thumbnail_base_url;
  $custom_name = 'edit-' . str_replace('_', '-', $field['field_name']) . '-' . $delta . '-value';
  $cw_vars = kaltura_format_cw_vars(array(
    'kshow_id' => 0,
    'partner_data' => '',
    'type' => 'entry',
    'context' => 'field_mix',
    'field_id' => $custom_name,
    'add_filter' => 6,
  ));
  $action_buttons = '<input type="button" value="Add Kaltura Remix" onclick="kalturaInitModalBox(\'' . url('kaltura/contribution_wizard/' . $cw_vars) . '/\'+ get_title(), {width:680, height:380});" /><input type="button" value="Clear" onclick="remove_items_from_field(\'' . $custom_name . '\');" />';
  $action_buttons .= '<input type="button" value="Edit Mix" onclick="if(document.getElementById(\'' . $custom_name . '\').value) { kalturaInitModalBox(\'' . url('kaltura/simple_editor/') . '\' + document.getElementById(\'' . $custom_name . '\').value +\'/entry/empty_partner_data/no_refresh\', { width: 890, height: 546 }); }" />';
  $field_thumbs = kaltura_get_field_thumbs($items[$delta]['value'], $custom_name, 'Remix', $field['required']);
  $form['mixdata'][$custom_name . '_thumbs'] = array(
    '#type' => 'item',
    '#value' => '<div id="' . $custom_name . '_thumbs_div">' . $field_thumbs . '</div>',
  );
  $form['mixdata']['add_video_' . $delta] = array(
    '#type' => 'item',
    '#value' => $action_buttons,
  );
  $form[$field['field_name']][$delta]['value'] = array(
    '#type' => 'hidden',
    '#default_value' => !$no_content ? $items[$delta]['value'] : '',
    '#size' => 60,
    '#attributes' => array(
      'class' => 'text',
      'readonly' => 'readonly',
    ),
    '#readonly' => 'readonly',
  );

  // Used so that hook_field('validate') knows where to flag an error.
}

/**
 * FAPI theme for an individual text elements.
 *
 * The textfield or textarea is already rendered by the
 * textfield or textarea themes and the html output
 * lives in $element['#children']. Override this theme to
 * make custom changes to the output.
 *
 * $element['#field_name'] contains the field name
 * $element['#delta]  is the position of this element in the group
 */
function theme_field_kaltura_textfield($element) {
  return $element['#children'];
}

/*
 * theme remix field
 */
function theme_field_kaltura_remix_textfield($element) {
  return $element['#children'];
}

/*
 * helper function to check if a specific field belongs to this module
 */
function kaltura_is_kaltura_field($field_name) {
  $query = 'SELECT type FROM {node_field} WHERE field_name = \'%s\'';
  $result = db_fetch_object(db_query($query, $field_name));
  $my_fields = field_kaltura_field_info();
  if (array_key_exists($result->type, $my_fields)) {
    return $result->type;
  }
  return FALSE;
}

/*
 * Implementation of hook_nodeapi().
 */
function field_kaltura_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op == 'update') {

    // do something only if tags vocabulary applied and not empty on this node
    if ($node->taxonomy['tags']) {

      // get all tags as string
      foreach ($node->taxonomy['tags'] as $tags) {
        $node_tags[] = $tags;
      }
      $node_all_tags = implode(',', $node_tags);

      // get all fields for node which are kaltura fields
      foreach ($node as $obj_key => $values) {
        if (substr($obj_key, 0, 5) != 'field') {
          continue;
        }
        if ($type = kaltura_is_kaltura_field($obj_key)) {
          foreach ($values as $value) {
            if ($value['value']) {
              if ($type == 'Kaltura_Media') {
                $media_entries = explode(',', rtrim($value['value']));
                foreach ($media_entries as $entry) {
                  $kaltura_entries[] = $entry;
                }
              }
              else {
                $kaltura_entries[] = $value['value'];
              }
            }
          }
        }
      }

      // update taxonomy as entries tags
      if (is_array($kaltura_entries) && count($kaltura_entries)) {
        kaltura_update_field_entries_tags($kaltura_entries, $tags, $node->title);
      }
    }
  }
}

/*
 * helper function that calls Kaltura updateEntry service to update the kaltura DB with the metadata
 * of the node which a specific entry belongs to
 */
function kaltura_update_field_entries_tags($entries, $tags, $name) {
  $session_user = KalturaHelpers::getSessionUser();
  $kaltura_client = KalturaHelpers::getKalturaClient();
  $entry = new KalturaEntry();
  $entry->tags = $tags;
  $entry->name = $name;
  foreach ($entries as $entry_id) {
    $result = $kaltura_client
      ->updateEntry($session_user, $entry_id, $entry);
  }
}

Functions

Namesort descending Description
field_kaltura_content_is_empty Implementation of hook_content_is_empty().
field_kaltura_elements Implementation of FAPI hook_elements().
field_kaltura_field Implementation of hook_field().
field_kaltura_field_formatter
field_kaltura_field_formatter_info Implementation of hook_field_formatter_info().
field_kaltura_field_info Implementation of hook_field_info().
field_kaltura_field_settings Implementation of hook_field_settings().
field_kaltura_form_alter
field_kaltura_nodeapi
field_kaltura_remix_textfield_process Process an individual element.
field_kaltura_textfield_process Process an individual element.
field_kaltura_theme Implementation of hook_theme().
field_kaltura_widget Implementation of hook_widget().
field_kaltura_widget_info Implementation of hook_widget_info().
field_kaltura_widget_settings Implementation of hook_widget_settings().
kaltura_get_default_display
kaltura_get_field_thumbs
kaltura_get_full_display
kaltura_get_teaser_display
kaltura_get_thumbnail_display
kaltura_is_kaltura_field
kaltura_update_field_entries_tags
theme_field_kaltura_formatter_default Theme function for 'default' text field formatter.
theme_field_kaltura_formatter_full Theme function for 'full' text field formatter.
theme_field_kaltura_formatter_teaser Theme function for 'teaser' text field formatter.
theme_field_kaltura_formatter_thumbnail Theme function for 'thumbnail' text field formatter.
theme_field_kaltura_remix_textfield
theme_field_kaltura_textfield FAPI theme for an individual text elements.
_field_kaltura_widget_settings_row_validate
_field_kaltura_widget_settings_size_validate