You are here

function draggableviews_views_pre_render in DraggableViews 7

Same name and namespace in other branches
  1. 6.3 draggableviews.module \draggableviews_views_pre_render()

Implements hook_views_pre_render

We distinguish between two cases A and B: A) Click sort was used:

  • Renumber the results as they are returned from the view. Simply use ascending numbers. (and re-execute the view).

B) The view is just going to be rendered.

  • Check the structure. If it's broken:
  • - Repair the structure (and re-execute the view).

In both cases extend the visible window if paging is beeing used.

File

./draggableviews.module, line 332
Draggableviews module provides a style plugin for views. With this plugin rows become draggable and can be organized in complex structures.

Code

function draggableviews_views_pre_render(&$view) {
  if (!isset($view->draggableviews_info)) {

    // This view doesn't use draggable_table style plugin. Nothing to do.
    return;
  }

  // Initialize info array with the results of the executed view object.
  $view->draggableviews_info = _draggableviews_info($view, $view->draggableviews_info);
  $info =& $view->draggableviews_info;
  if (isset($info['pager']->options['items_per_page']) && empty($info['hierarchy']) && isset($view->total_rows)) {

    // The current page of hierarchies cannot be checked here because $views->total_rows will not be
    // calculated when the entire view gets loaded.
    if ($info['pager']->current_page * $info['pager']->options['items_per_page'] >= $view->total_rows + $view->query->pager->options['offset']) {

      // The current page is out of range.
      return;
    }
  }
  if (!empty($_GET['order'])) {

    // CASE A) Click sort was used. Assign order values manually.
    _draggableviews_click_sort($info);

    // The entire view is loaded at the moment. We try to extend the visible window with the suggested
    // values and reload the view. The calculated range will be returned.
    $range = _draggableviews_extend_view_window($info, TRUE);
  }
  else {

    // CASE B) Extend view and check structure.
    if (isset($info['hierarchy'])) {

      // Shrink views window in case of paging and reload view.
      $range = _draggableviews_extend_view_window($info, TRUE);
    }
    else {

      // Extend views window in case of paging, but don't reload view. (Only mark extension nodes)
      $range = _draggableviews_extend_view_window($info, FALSE);
    }
    if ($info['repair_if_broken'] && !_draggableviews_quick_check_structure($info)) {

      // The structure is broken and has to be repaired. We restore the original page settings now because
      // the current settings are based on a broken structure.
      $info['view']->query->pager = $info['pager'];

      // Rebuild the view.
      _draggableviews_rebuild_hierarchy($info);

      // Try to extend the visible window with the suggested values. The calculated range will be returned.
      $range = _draggableviews_extend_view_window($info, TRUE);
      if (drupal_strlen(variable_get('draggableviews_repaired_msg', 'The structure was broken. It has been repaired.')) > 0) {
        drupal_set_message(filter_xss(variable_get('draggableviews_repaired_msg', 'The structure was broken. It has been repaired.')));
      }
    }
  }
  if ($info['needs_pager_modifications']) {
    global $pager_page_array, $pager_total, $pager_total_items;

    // The global $pager_total variable was calculated with wrong values because we changed the
    // global $_GET['page'] and $view->pager['items_per_page'] variable in hook_pre_execute.
    if (isset($info['view']->total_rows)) {
      $pager_total[$info['pager']->options['id']] = ceil(($info['view']->total_rows + $range['first_index']) / $info['pager']->options['items_per_page']);
    }
    else {
      $pager_total[$info['pager']->options['id']] = 0;
    }

    // Restore the global variable.
    $pager_page_array[$info['pager']->options['id']] = $info['pager']->current_page;

    // Restore the _GET variable.
    $_GET['page'] = $info['globals']['page'];
  }

  // Calculate depth values. These values will be used for theming.
  _draggableviews_calculate_depths($info);

  // Finally we set the selectable options of the order field.
  $info['order']['field']['handler']
    ->set_range($range['first_index'], $range['last_index']);
}