You are here

function draggableviews_views_pre_execute in DraggableViews 6.3

Same name and namespace in other branches
  1. 7 draggableviews.module \draggableviews_views_pre_execute()

Implementing hook_views_pre_execute

We use this hook to extend the view window before it gets executed. In hook_pre_render we afterwards check for the correctness and re-execute the view if needed.

File

./draggableviews.module, line 196
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_execute(&$view) {
  if ($view->style_plugin->definition['handler'] != 'draggableviews_plugin_style_draggabletable') {

    // This view doesn't use draggable_table style plugin. Nothing to do.
    return;
  }
  if (isset($view->draggableviews_info)) {

    // Don't fall in an infinit recursion when the view gets re-executed.
    return;
  }

  // Get info array.
  $info = _draggableviews_info($view);
  if (!isset($info['order'])) {

    // Nothing to do.
    return;
  }

  // Attach the info array to the view object.
  // We need to use a reference because the info array will change.
  $view->draggableviews_info =& $info;

  // We want the view to execute the count query. New nodes which should be added to the very end will use this value.
  // @todo: Deprecated: If paging is not used the count query will not be executed anyway. the $view->total_rows property is not needed anymore. It has been replaced with 999999999999.
  $view->get_total_rows = TRUE;
  $info['pager'] = $info['view']->pager;
  $info['globals'] = array();
  if ($info['pager']['items_per_page'] > 0) {

    // Paging is beeing used. We modify some paging settings before the view gets executed.
    // We let views think that the current page is 0, but we also backup the actual value. We
    // will restore it after finishing all operations (see hook_views_pre_render).
    $info['globals']['page'] = isset($_GET['page']) ? $_GET['page'] : NULL;
    $pager_page_array = isset($_GET['page']) ? explode(',', $_GET['page']) : array();
    if (!empty($pager_page_array[$info['pager']['element']]) && $pager_page_array[$info['pager']['element']] > 0) {
      $info['pager']['current_page'] = intval($pager_page_array[$info['pager']['element']]);
    }
    else {
      $info['pager']['current_page'] = 0;
    }
    $pager_page_array[$info['pager']['element']] = 0;

    // Change the _GET variable.
    $_GET['page'] = implode(',', $pager_page_array);
    if (isset($info['hierarchy'])) {

      // In order to find hidden parents or child nodes we need to load the entire view.
      // To induce that the entire view gets loaded we set to items_per_page to 999999999.
      $info['view']->pager['items_per_page'] = DRAGGABLEVIEWS_DBQUERY_LIMIT;

      // Permissions checks cannot be done right now because the window probably must be extended
      // even if the user doesn't have permissions.
    }
    else {

      // We just extend the visible window.
      $first_index = $info['pager']['current_page'] * $info['pager']['items_per_page'];
      $last_index = $first_index + $info['pager']['items_per_page'] - 1;

      // When we deal with simple lists permission checks must be done right now (differently to hierarchies).
      if (user_access('Allow Reordering') && !$info['locked']) {

        // Add extensions.
        $first_index -= $info['view_window_extensions']['extension_top'];
        $last_index += $info['view_window_extensions']['extension_bottom'];
        if ($first_index < 0) {
          $first_index = 0;
        }
      }
      $info['view']->pager['items_per_page'] = $last_index - $first_index + 1;
      $info['view']->pager['offset'] = $first_index + $info['pager']['offset'];
    }
  }
}