You are here

apachesolr_sort.module in Apachesolr Sort 6

Same filename and directory in other branches
  1. 6.3 apachesolr_sort.module
  2. 7 apachesolr_sort.module

File

apachesolr_sort.module
View source
<?php

/**
 * implementation of hook_menu
 * To enable sorting and removal of the sort by fields
 * 
 */
function apachesolr_sort_menu() {
  $items['admin/settings/apachesolr/sort'] = array(
    'title' => 'Sorting',
    'description' => 'Sorting settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'apachesolr_sort_page_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'weight' => 55,
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/*
 * The submit handler of the form that saves the input content into the DB.
 * */
function apachesolr_sort_page_form_submit($form, &$form_state) {
  foreach ($form['#post'] as $key => $field) {
    if (is_array($field)) {
      $keys = array_keys($field);
      if (isset($field['apachesolr_sort_sort_' . $key])) {
        variable_set('apachesolr_sort_sort_' . $key, TRUE);
      }
      else {
        variable_set('apachesolr_sort_sort_' . $key, FALSE);
      }
      variable_set('apachesolr_sort_sort_weight_' . $key, $field['apachesolr_sort_sort_weight_' . $key]);
    }
  }
  $bid = 'apachesolr-sort';
  $delta = "sort";
}

/*
 * The form where you set the variables.
 * */
function apachesolr_sort_page_form() {
  $default_search_term = variable_get('apachesolr_sort_term', "test");
  $form['apachesolr_sort_term'] = array(
    '#type' => 'textfield',
    '#title' => t('A search term which returns multiple search results. '),
    '#default_value' => $default_search_term,
    '#description' => t('A search term which returns multiple search results. '),
    '#size' => 60,
  );
  $query = apachesolr_drupal_query($keys = $default_search_term);
  $sorts = $query
    ->get_available_sorts();
  $form['apachesolr_sort_enable'] = array(
    '#type' => 'fieldset',
    '#title' => t('enable/disable sort fields'),
    '#tree' => FALSE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['apachesolr_sort_weight'] = array(
    '#type' => 'fieldset',
    '#title' => t('adapt the weight on sort fields'),
    '#tree' => FALSE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  foreach ($sorts as $key => $sort) {
    $variable = 'apachesolr_sort_sort_' . $key;
    $wvariable = 'apachesolr_sort_sort_weight_' . $key;
    $form['apachesolr_sort_enable'][$variable] = array(
      '#type' => 'checkbox',
      '#title' => $sort['title'],
      '#default_value' => variable_get($variable, TRUE),
      '#description' => t('enable this sort. '),
    );
    $form['apachesolr_sort_weight'][$wvariable] = array(
      '#type' => 'textfield',
      '#title' => t('The weight of ' . $sort['title']),
      '#default_value' => variable_get($wvariable, 0),
      '#description' => t('Change the order of the facest by altering this weight. '),
      '#size' => 5,
    );
  }
  return system_settings_form($form);
}
function apachesolr_sort_apachesolr_sort_links_alter($sort_links) {
  foreach ($sort_links as $key => $sort_link) {
    if (!variable_get('apachesolr_sort_sort_' . $key, TRUE)) {
      unset($sort_links[$key]);
    }
    else {
      $sort_links[$key]['weight'] = variable_get('apachesolr_sort_sort_weight_' . $key, '0');
    }
  }
  uasort($sort_links, "apachesolr_sort_weight_sort");
  return $sort_links;
}
function apachesolr_sort_weight_sort($a, $b) {
  return strcmp($a['weight'], $b['weight']);
}

Functions