You are here

function asset_widget_views_ajax_data_alter in Asset 7

Implements hook_views_ajax_data_alter().

File

modules/asset_widget/asset_widget.module, line 685
Code for the Asset widget module.

Code

function asset_widget_views_ajax_data_alter(&$commands, &$view) {

  // We react only on our exposed plugin to add commands.
  if (isset($view->display_handler->options['exposed_form']) && $view->display_handler->options['exposed_form']['type'] == 'exposed_form_asset_widget') {
    $action = isset($_POST['ajax_source']) ? $_POST['ajax_source'] : 'filter';

    // Skip default scroll top.
    if (count($commands) > 1 && $commands[0]['command'] == 'viewsScrollTop') {
      array_shift($commands);
    }
    switch ($action) {
      case 'pager':
        $commands[] = array(
          'command' => 'assetWidgetGotoTab',
          'data' => 'results',
        );
        break;
      case 'filter':
        $errors = form_get_errors();

        // If validation failed, reload tab and show errors.
        if (!empty($errors)) {

          // Remove default results insert command.
          $commands = array();
          $commands[] = array(
            'command' => 'assetWidgetGotoTab',
            'data' => 'search',
          );
          $commands[] = array(
            'command' => 'assetWidgetFormErrors',
            'data' => $errors,
          );
          drupal_get_messages(NULL, TRUE);
          form_clear_error();
        }
        else {
          $commands[] = array(
            'command' => 'assetWidgetResetSizeMode',
            'data' => 'results',
          );

          // Set filter params into internal vars.
          $filter_params = drupal_http_build_query(_asset_widget_views_get_exposed_values($view));

          // We can disable this functionality using variable.
          // @todo: Provide settings form for module with this setting.
          if (variable_get('asset_widget_save_last_search', TRUE)) {
            global $base_path;
            setcookie('Drupal_asset_widget_filter_params', $filter_params, time() + 60 * 60 * 24, $base_path);
          }
          $commands[] = array(
            'command' => 'assetWidgetSetFilterParams',
            'data' => $filter_params,
          );

          // Then show search params block.
          if (!empty($view->result)) {
            $commands[] = array(
              'command' => 'assetWidgetShowFiltersBlock',
              'data' => theme('asset_widget_search_filters_block', array(
                'search_params' => _asset_widget_views_filters_extract_labels($view),
                'results' => count($view->result),
              )),
            );
          }
          else {
            $commands[] = array(
              'command' => 'assetWidgetRemoveFiltersBlock',
            );
          }
          $commands[] = array(
            'command' => 'assetWidgetGotoTab',
            'data' => 'results',
          );
        }
        break;
    }
  }
}