You are here

class brightcove_cck_handler_field_video_date_multiple in Brightcove Video Connect 6

Same name and namespace in other branches
  1. 6.2 brightcove_cck/views/brightcove_cck_handler_field_video_date_multiple.inc \brightcove_cck_handler_field_video_date_multiple

@file An extended subclass for field handling that adds multiple field grouping.

Fields that want multiple value grouping options in addition to basic field and formatter handling can extend this class.

Hierarchy

Expanded class hierarchy of brightcove_cck_handler_field_video_date_multiple

1 string reference to 'brightcove_cck_handler_field_video_date_multiple'
_brightcove_cck_views_data in brightcove_cck/views/brightcove_cck.views.inc
This is NOT implementation of hook_views_data(). This is a helper function for hook_field_settings op = 'views data'.

File

brightcove_cck/views/brightcove_cck_handler_field_video_date_multiple.inc, line 10
An extended subclass for field handling that adds multiple field grouping.

View source
class brightcove_cck_handler_field_video_date_multiple extends brightcove_cck_handler_field_video_date {
  var $defer_query;
  function init(&$view, $options) {
    $field = $this->content_field;
    parent::init($view, $options);
    $this->defer_query = !empty($options['multiple']['group']) && $field['multiple'];
    if ($this->defer_query) {

      // Grouped field: ditch the existing additional_fields (field columns + delta).
      // In the main query we'll only need:
      // - vid, which will be used to retrieve the actual values in pre_render,
      // - node type and nid, which wil be used in the pseudo-node used when
      // rendering.
      $this->additional_fields = array(
        'type' => array(
          'table' => 'node',
          'field' => 'type',
        ),
        'nid' => array(
          'table' => 'node',
          'field' => 'nid',
        ),
      );
      if ($view->base_table == 'node_revisions') {
        $this->additional_fields['vid'] = array(
          'table' => 'node_revisions',
          'field' => 'vid',
        );
      }
      else {
        $this->additional_fields['vid'] = array(
          'table' => 'node',
          'field' => 'vid',
        );
      }
    }
  }
  function options(&$options) {
    parent::options($options);
  }

  /**
   * Provide 'group multiple values' option.
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $field = $this->content_field;
    $options = $this->options;
  }
  function options_validate($form, &$form_state) {
  }

  /**
   * Determine if this field is click sortable.
   */
  function click_sortable() {
    $field = $this->content_field;
    $options = $this->options;

    // Grouped fields are not click-sortable.
    return !empty($this->definition['click sortable']) && !$this->defer_query;
  }
  function query() {

    // If this is not a grouped field, use the generic query().
    if (!$this->defer_query) {
      return parent::query();
    }

    // Grouped field: do NOT call ensure_my_table, only add additional fields.
    $this
      ->add_additional_fields();
    $this->field_alias = $this->aliases[$this->real_field];
  }
  function pre_render($values) {

    // If there are no values to render (displaying a summary, or query returned no results),
    // or if this is not a grouped field, do nothing specific.
    if (isset($this->view->build_info['summary']) || empty($values) || !$this->defer_query) {
      return parent::pre_render($values);
    }
    $field = $this->content_field;
    $db_info = content_database_info($field);
    $options = $this->options;
    $this->field_alias = $this->real_field;

    // Build the list of vids to retrieve.
    // TODO: try fetching from cache_content first ??
    $vids = array();
    $this->field_values = array();
    foreach ($values as $result) {
      $vids[] = $result->node_vid;
    }

    // It may happend that the multiple values field is related to a non
    // required relation for which no node data related to the field being
    // processed here is available.
    if (empty($vids)) {
      return parent::pre_render($values);
    }

    // List columns to retrieve.
    $alias = content_views_tablename($field);

    // Prefix aliases with '_' to avoid clashing with field columns names.
    $query_columns = array(
      'vid AS _vid',
      "delta as _delta",
      // nid is needed to generate the links for 'link to node' option.
      'nid AS _nid',
    );

    // The actual field columns.
    foreach ($db_info['columns'] as $column => $attributes) {
      $query_columns[] = "{$attributes['column']} AS {$column}";
    }
    $query = 'SELECT ' . implode(', ', $query_columns) . ' FROM {' . $db_info['table'] . "}" . " WHERE vid IN (" . implode(',', $vids) . ')' . " ORDER BY _nid ASC, _delta " . ($options['multiple']['multiple_reversed'] ? 'DESC' : 'ASC');
    $result = db_query($query);
    while ($item = db_fetch_array($result)) {

      // Clean up the $item from vid and delta. We keep nid for now.
      $vid = $item['_vid'];
      unset($item['_vid']);
      $delta = !empty($item['_delta']) ? $item['_delta'] : 0;
      $item['#delta'] = $item['_delta'];
      unset($item['_delta']);
      $this->field_values[$vid][$delta] = $item;
    }
  }

  /**
   * Return DIV or SPAN based upon the field's element type.
   *
   * Fields rendered with the 'group multiple' option use <div> markers,
   * and thus shouldn't be wrapped in a <span>.
   */
  function element_type() {
    if (!$this->defer_query) {
      return parent::element_type();
    }
    if (isset($this->definition['element type'])) {
      return $this->definition['element type'];
    }
    return 'div';
  }
  function render($values) {

    // If this is not a grouped field, use content_handler_field::render().
    if (!$this->defer_query) {
      return parent::render($values);
    }
    $options = $this->options;
    $vid = $values->node_vid;
    if (isset($this->field_values[$vid])) {

      // Gather items, respecting the 'Display n values starting from m' settings.
      $count_skipped = 0;
      $items = array();
      foreach ($this->field_values[$vid] as $item) {
        if (empty($options['multiple']['multiple_from']) || $count_skipped >= $options['multiple']['multiple_from']) {
          if (empty($options['multiple']['multiple_number']) || count($items) < $options['multiple']['multiple_number']) {

            // Grab the nid - needed for render_link().
            $video_id = $item['video_id'];
            $items[] = $item;
          }
          else {
            break;
          }
        }
        $count_skipped++;
      }
      foreach ($items as $item) {
        $video = brightcove_video_load($item['video_id']);
        if (!empty($video)) {
          if (isset($video->{$this->bc_field})) {
            $value = floor($video->{$this->bc_field} / 1000);
            $time_diff = $_SERVER['REQUEST_TIME'] - $value;

            // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
            switch ($format) {
              case 'raw time ago':
                $output = format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
              case 'time ago':
                $output = t('%time ago', array(
                  '%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2),
                ));
              case 'raw time span':
                $output = ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
              case 'time span':
                $output = t($time_diff < 0 ? '%time hence' : '%time ago', array(
                  '%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2),
                ));
              case 'custom':
                $output = format_date($value, $format, $custom_format);
              default:
                $output = format_date($value, $format);
            }
            $rendered[] = $this
              ->render_link($output, (object) array(
              'nid' => $item['_nid'],
            ));
          }
        }
      }
      if (count($rendered) > 1) {

        // TODO: could we use generic field display ?
        return theme('content_view_multiple_field', $rendered, $field, $values);
      }
      elseif ($rendered) {
        return $rendered[0];
      }
    }
    return '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
brightcove_cck_handler_field_video_date::$content_field property
brightcove_cck_handler_field_video_date::admin_summary function
brightcove_cck_handler_field_video_date::construct function
brightcove_cck_handler_field_video_date::label function
brightcove_cck_handler_field_video_date_multiple::$defer_query property Overrides content_handler_field_multiple::$defer_query
brightcove_cck_handler_field_video_date_multiple::click_sortable function Determine if this field is click sortable. Overrides content_handler_field_multiple::click_sortable
brightcove_cck_handler_field_video_date_multiple::element_type function Return DIV or SPAN based upon the field's element type. Overrides content_handler_field_multiple::element_type
brightcove_cck_handler_field_video_date_multiple::init function Overrides brightcove_cck_handler_field_video_date::init
brightcove_cck_handler_field_video_date_multiple::options function Overrides brightcove_cck_handler_field_video_date::options
brightcove_cck_handler_field_video_date_multiple::options_form function Provide 'group multiple values' option. Overrides brightcove_cck_handler_field_video_date::options_form
brightcove_cck_handler_field_video_date_multiple::options_validate function Overrides brightcove_cck_handler_field_video_date::options_validate
brightcove_cck_handler_field_video_date_multiple::pre_render function Overrides content_handler_field_multiple::pre_render
brightcove_cck_handler_field_video_date_multiple::query function Overrides content_handler_field_multiple::query
brightcove_cck_handler_field_video_date_multiple::render function Overrides brightcove_cck_handler_field_video_date::render
content_handler_field_multiple::render_link function