You are here

private function EntityPagerOut::calculateNextAllPrevValuesFromView in Entity Pager 7

Calculate the values for the links.

E.g. < prev All next >

Parameters

object $entity: The entity object.

object $view: The view object.

Return value

array|bool An array of links.

1 call to EntityPagerOut::calculateNextAllPrevValuesFromView()
EntityPagerOut::getEntityPager in includes/EntityPagerOut.inc
Entity Pager return links.

File

includes/EntityPagerOut.inc, line 150
Provides the Business Logic for the EntityPager module.

Class

EntityPagerOut
Class EntityPagerOut.

Code

private function calculateNextAllPrevValuesFromView($entity, $view) {

  // Extract the info required from the View.
  // I.e. info from Views > Format > settings.
  $settings = $this
    ->getSettings($view);
  $view_result = $this
    ->getViewResult($view);

  // Field.
  $field_name = $this
    ->getIdFieldName();

  // Entity.
  $entity_type = $this
    ->getEntityInfo('entity_type');
  $entity_field = $this
    ->getEntityInfo('field');
  $entity_url = str_replace('_', '/', $entity_type);

  // Next or Previous.
  $next = FALSE;
  $match = FALSE;

  // Set short, convenient incoming values to process below.
  $n = array(
    'cur' => '',
    'count' => count($view_result),
    'next' => $settings['next_prev']['link_next'],
    'prev' => $settings['next_prev']['link_prev'],
  );
  $this
    ->runAdviceLogic('quantity', 'Consider caching View result');

  // Set defaults for output.
  $links = array(
    'next' => '<span class="inactive">' . $n['next'] . '</span>',
    'prev' => '<span class="inactive">' . $n['prev'] . '</span>',
    'count' => '',
  );
  $cnt = $current_cnt = 0;
  foreach ($view_result as $result) {
    $cnt++;

    // Keep a record of the previous nid in the loop, so when the nid is
    // matched, we can reference the previous nid to create the 'prev' link.
    $n['cur_prev'] = $n['cur'];

    // The current value.
    $n['cur'] = $result->{$field_name};
    if ($next) {
      $links['next'] = l($n['next'], $entity_url . '/' . $result->{$field_name}, array(
        'html' => TRUE,
      ));
      break;
    }
    if ($entity->{$entity_field} == $result->{$field_name}) {

      // Must be beyond the first record to have a previous.
      $match = TRUE;
      $current_cnt = $cnt;
      if ($cnt > 1) {
        $links['prev'] = l($n['prev'], $entity_url . '/' . $n['cur_prev'], array(
          'html' => TRUE,
        ));
      }

      // There is a next value.
      $next = TRUE;
    }
  }
  if (!$match) {
    $this
      ->runAdviceLogic('no-records', 'No results in View');

    // Return nothing.
    return FALSE;
  }

  // Display the Count stats.
  if ($settings['next_prev']['display_count']) {
    $links['count'] = t('@cnt of <span class="total">@count</span>', array(
      '@cnt' => number_format($current_cnt),
      '@count' => number_format($n["count"]),
    ));
  }

  // Create the link for the 'all' link.
  $all_tle = $this
    ->getAllTitle();
  $all_url = $this
    ->getAllUrl();
  if ($all_tle && $all_url) {
    $links['all_link'] = l($all_tle, $all_url, array(
      'html' => TRUE,
    ));
  }
  $links = $this
    ->orderLinks($links);
  return $links;
}