You are here

function views_refresh_ajax_handler in Views Refresh 7

Menu callback to reload a view via AJAX - modified copy of views_ajax().

1 string reference to 'views_refresh_ajax_handler'
views_refresh_menu in ./views_refresh.module
Implements hook_menu().

File

./views_refresh.module, line 86
Views Refresh module.

Code

function views_refresh_ajax_handler() {
  if (isset($_REQUEST['view_name']) && isset($_REQUEST['view_display_id'])) {
    $name = $_REQUEST['view_name'];
    $display_id = $_REQUEST['view_display_id'];
    $args = isset($_REQUEST['view_args']) && $_REQUEST['view_args'] !== '' ? explode('/', htmlspecialchars_decode($_REQUEST['view_args'], ENT_QUOTES)) : array();
    $path = isset($_REQUEST['view_path']) ? rawurldecode($_REQUEST['view_path']) : NULL;
    $dom_id = isset($_REQUEST['view_dom_id']) ? preg_replace('/[^a-zA-Z0-9_-]+/', '-', $_REQUEST['view_dom_id']) : NULL;
    $pager_element = isset($_REQUEST['pager_element']) ? intval($_REQUEST['pager_element']) : NULL;
    $views_refresh_noscroll = isset($_REQUEST['views_refresh_noscroll']) ? intval($_REQUEST['views_refresh_noscroll']) : 0;
    $commands = array();
    $params_to_remove = array(
      'view_name',
      'view_display_id',
      'view_args',
      'view_path',
      'view_dom_id',
      'pager_element',
      'view_base_path',
      'ajax_html_ids',
      'ajax_page_state',
      'views_refresh_noscroll',
    );
    foreach ($params_to_remove as $key) {
      if (isset($_GET[$key])) {
        unset($_GET[$key]);
      }
      if (isset($_REQUEST[$key])) {
        unset($_REQUEST[$key]);
      }
      if (isset($_POST[$key])) {
        unset($_POST[$key]);
      }
    }

    // Allow to set page via POST only.
    if (isset($_GET['page'])) {
      unset($_GET['page']);
    }

    // Load the view.

    /** @var view $view */
    $view = views_get_view($name);
    if ($view && $view
      ->access($display_id) && $view
      ->set_display($display_id) && $view->display_handler
      ->get_option('use_ajax')) {

      // Fix 'q' for paging.
      if (!empty($path)) {
        $_GET['q'] = $path;
      }

      // Add all $_POST data, because AJAX is always a post and many things,
      // such as tablesorts, exposed filters and paging assume $_GET.
      $_GET = $_POST + $_GET;

      // Overwrite the destination.
      // @see drupal_get_destination()
      $origin_destination = $path;
      $query = drupal_http_build_query(drupal_get_query_parameters());
      if ($query != '') {
        $origin_destination .= '?' . $query;
      }
      $destination =& drupal_static('drupal_get_destination');
      $destination = array(
        'destination' => $origin_destination,
      );

      // Override the display's pager_element with the one actually used.
      if (isset($pager_element)) {
        if (!$views_refresh_noscroll) {
          $commands[] = array(
            'command' => 'viewsScrollTop',
            'selector' => '.view-dom-id-' . $dom_id,
          );
        }
        $view->display[$display_id]->handler
          ->set_option('pager_element', $pager_element);
      }

      // Reuse the same DOM id so it matches that in Drupal.settings.
      $view->dom_id = $dom_id;
      $output = $view
        ->preview($display_id, $args);
      if ($view
        ->use_pager() && empty($view->result)) {

        // Shift the current page if the current page result is empty
        // (e.g. the recent action was last row entity deletion).
        $output = views_refresh_shift_page_and_render($view, $name, $output, $display_id, $args, $pager_element, $dom_id);
      }
      $commands[] = ajax_command_replace('.view-dom-id-' . $dom_id, $output);
      $commands[] = ajax_command_invoke('.view-dom-id-' . $dom_id, 'trigger', array(
        'view_refreshed',
      ));
      drupal_alter('views_ajax_data', $commands, $view);

      // Allow to modify the commands for views refresh operations.
      drupal_alter('views_refresh_ajax_data', $commands, $view);
    }
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  return NULL;
}