You are here

function facetapi_facet_settings_form in Facet API 6

Returns settings for an individual facet that apply to a realm.

Parameters

&$form_state: A keyed array containing the current state of the form.

$adapter: A FacetapiAdapter object.

$realm: An array containing the full realm definition.

$facet: An array containing the fill realm definition.

Return value

A FAPI array containing the form.

1 string reference to 'facetapi_facet_settings_form'
facetapi_menu in ./facetapi.module
Implementation of hook_menu().

File

./facetapi.admin.inc, line 226
Administrative settings for Facet API modules.

Code

function facetapi_facet_settings_form(&$form_state, FacetapiAdapter $adapter, array $realm, array $facet) {
  $form = array();

  // Captures Identifier for facet specific settings.
  $identifier = "{$adapter->getSearcher()}:{$realm['name']}:{$facet['name']}";
  $form['global'] = array(
    '#type' => 'fieldset',
    '#title' => t('Global settings'),
    '#description' => t('Configuration settings for the %facet facet across <em>all</em> realms that apply to %searcher searches.', array(
      '%facet' => $facet['title'],
      '%searcher' => $adapter
        ->getSearcher(),
    )),
    '#collapsible' => TRUE,
  );
  $operator_var = sprintf('facetapi:operator:%s::%s', $adapter
    ->getSearcher(), $facet['name']);
  $form['global'][$operator_var] = array(
    '#type' => 'radios',
    '#title' => t('Operator'),
    '#default_value' => variable_get($operator_var, FACETAPI_OPERATOR_AND),
    '#options' => array(
      FACETAPI_OPERATOR_AND => t('AND'),
      FACETAPI_OPERATOR_OR => t('OR'),
    ),
    '#description' => t('AND filters are exclusive. OR filters are inclusive. Selecting more AND filters narrows the result set. Selecting more OR filters widens the result set.'),
  );
  $hlimit_var = sprintf('facetapi:hard_limit:%s::%s', $adapter
    ->getSearcher(), $facet['name']);
  $form['global'][$hlimit_var] = array(
    '#type' => 'select',
    '#title' => t('Hard limit'),
    '#default_value' => variable_get($hlimit_var, 20),
    '#options' => drupal_map_assoc(array(
      100,
      75,
      50,
      40,
      30,
      20,
      15,
      10,
      5,
      3,
    )),
    '#description' => t('Display no more than this number of facet items.'),
  );
  $form['realm'] = array(
    '#type' => 'fieldset',
    '#title' => t('@realm realm settings', array(
      '@realm' => $realm['title'],
    )),
    '#description' => t('Configuration settings for the %facet facet in the %realm realm that apply to %searcher searches.', array(
      '%facet' => $facet['title'],
      '%realm' => $realm['title'],
      '%searcher' => $adapter
        ->getSearcher(),
    )),
    '#collapsible' => TRUE,
  );

  // Gets widgets, sorts.
  $widgets = facetapi_widgets_get(array(
    'realm' => $realm,
    'facet' => $facet,
  ));
  uasort($widgets, 'facetapi_sort_weight');

  // Builds select options for widgets.
  $options = array();
  foreach ($widgets as $widget_name => $definition) {
    $options[$widget_name] = $definition['title'];
  }
  $widget_var = "facetapi:widget:{$identifier}";
  $default = facetapi_facet_widget_get($widgets, $adapter
    ->getSearcher(), $realm, $facet);
  $form['realm'][$widget_var] = array(
    '#type' => 'select',
    '#title' => t('Display widget'),
    '#default_value' => variable_get($widget_var, $default),
    '#options' => $options,
  );
  $slimit_var = "facetapi:soft_limit:{$identifier}";
  $form['realm'][$slimit_var] = array(
    '#type' => 'select',
    '#title' => t('Soft limit'),
    '#default_value' => variable_get($slimit_var, 10),
    '#options' => array(
      0 => t('No limit'),
    ) + drupal_map_assoc(array(
      50,
      40,
      30,
      20,
      15,
      10,
      5,
      3,
    )),
    '#description' => t('Limits the number of displayed facets via JavaScript.'),
  );

  // Gets all sort definitions, sorts active for this facet.
  $sorts = facetapi_sorts_get();
  $active_sorts = facetapi_facet_sorts_get($adapter, $realm, $facet);

  // Initializes the sorting table.
  $form['realm']['sort'] = array(
    '#type' => 'fieldset',
    '#title' => t('Sorting options'),
    '#description' => t('Determine how facet items are sorted. Selecting the checkbox activates the associated sort, and the sorts are applied from the top down. Click and drag the rows to change the order the sorts are applied.'),
    'table' => array(
      '#theme' => 'facetapi_facet_settings_form',
    ),
  );

  // Builds checkbox options for sorts.
  $options = array();
  foreach ($sorts as $sort_name => $sort_info) {
    $options[$sort_name] = $sort_info['title'];
  }
  $sort_var = "facetapi:sort:{$identifier}";
  $form['realm']['sort']['table']['sort'][$sort_var] = array(
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => variable_get($sort_var, drupal_map_assoc(array_keys($active_sorts))),
  );
  foreach ($options as $sort_name => $sort_title) {
    $default = isset($active_sorts[$sort_name]) ? $active_sorts[$sort_name]['order'] : SORT_DESC;
    $order_var = "facetapi:sort_order:{$identifier}:{$sort_name}";
    $form['realm']['sort']['table']['sort_order'][$order_var] = array(
      '#type' => 'select',
      '#title' => '',
      '#options' => array(
        SORT_DESC => t('Descending'),
        SORT_ASC => t('Ascending'),
      ),
      '#default_value' => variable_get($order_var, $default),
    );
  }
  foreach ($options as $sort_name => $sort_title) {
    if (isset($active_sorts[$sort_name])) {
      $default = $active_sorts[$sort_name]['weight'];
    }
    else {
      $default = $sorts[$sort_name]['weight'];
    }
    $weight_var = "facetapi:sort_weight:{$identifier}:{$sort_name}";
    $form['realm']['sort']['table']['sort_weight'][$weight_var] = array(
      '#type' => 'weight',
      '#title' => '',
      '#delta' => 50,
      '#default_value' => variable_get($weight_var, $default),
      '#attributes' => array(
        'class' => 'facetapi-sort-weight',
      ),
    );
  }
  return system_settings_form($form);
}