<?php

/**
 * @file
 * Provides a test adapter and plugins.
 */

/**
 * Placeholder text on the admin settings form.
 */
define('FACETAPI_TEST_FORM_TEXT', t('Facet API test form'));

/**
 * Implements hook_menu().
 */
function facetapi_test_menu() {
  $items = array();

  $items['admin/config/search/facetapi_test'] = array(
    'title' => 'Facet API test',
    'description' => 'Settings for the Facet API module.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('facetapi_test_admin_settings'),
    'access arguments' => array('administer search'),
  );

  $items['admin/config/search/facetapi_test/settings'] = array(
    'title' => 'Settings',
    'weight' => -10,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  $items['facetapi_test/search'] = array(
    'title' => 'Search',
    'description' => 'Fake search page callback.',
    'page callback' => 'facetapi_test_search_callback',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Implements hook_ctools_plugin_api().
 */
function facetapi_test_ctools_plugin_api($owner, $api) {
  if ($owner == 'current_search' && $api == 'current_search') {
    return array('version' => 1);
  }
}

/**
 * Implements hook_facetapi_searcher_info().
 */
function facetapi_test_facetapi_searcher_info() {
  $info = array();

  $info['facetapi_test'] = array(
    'label' => t('Facet API Test'),
    'adapter' => 'facetapi_test',
    'types' => array('test'),
    'path' => 'admin/config/search/facetapi_test',
    'supports facet missing' => FALSE,
  );

  $info['facetapi_test2'] = array(
    'label' => t('Facet API Seond Searcher'),
    'adapter' => 'facetapi_test',
    'types' => array('test'),
    'supports facet missing' => FALSE,
  );

  return $info;
}

/**
 * Implements hook_facetapi_translate_string().
 *
 * Returns a serialized array for testing.
 */
function facetapi_test_facetapi_translate_string($name, $string, $langcode = NULL) {
  return serialize(array(
    'name' => $name,
    'string' => $string,
    'langcode' => (string) $langcode,
    'return' => 'translated',
  ));
}

/**
 * Placeholder for the admin settings form.
 */
function facetapi_test_admin_settings($form, &$form_state) {
  $form['text'] = array('#markup' => FACETAPI_TEST_FORM_TEXT);
  return $form;
}

/**
 * Implements hook_facetapi_facet_info().
 */
function facetapi_test_facetapi_facet_info($searcher_info) {
  $facets = array();
  if ('test' == $searcher_info['type']) {

    $facets['enabled'] = array(
      'label' => t('Enabled facet'),
      'description' => t('Facet that tests enabling.'),
      'dependency plugins' => array('role'),
    );

    $facets['disabled'] = array(
      'label' => t('Disabled facet'),
      'description' => t('Facet that tests disabling.'),
      'dependency plugins' => array('role'),
    );

    $facets['colon:test'] = array(
      'label' => t('Colon test'),
      'description' => t('Test facets names with colons.'),
      'dependency plugins' => array('role'),
    );

    $facets['defaults'] = array(
      'label' => t('Default test'),
      'description' => t('Tests query type and operator defaults stored in settings.'),
      'dependency plugins' => array('role'),
      'query types' => array('nonterm'),
      'default widget' => 'nonterm',
      'allowed operators' => array(
        FACETAPI_OPERATOR_AND => FALSE,
        FACETAPI_OPERATOR_OR => TRUE
      ),
    );

    $facets['hierarchical'] = array(
      'label' => t('Hierarchical test'),
      'description' => t('Test hierarchical facets.'),
      'dependency plugins' => array('role'),
      'hierarchy callback' => 'facetapi_test_process_hierarchy',
    );
  }
  return $facets;
}

/**
 * Hierarchy processing callback.
 *
 * @param array $values
 *   An array containing the term ids.
 *
 * @return
 *   An associative array keyed by term ID to parent ID.
 */
function facetapi_test_process_hierarchy(array $values) {

}

/**
 * Fake search page callback.
 */
function facetapi_test_search_callback() {
  $build = array();

  // Load adapter, throw exception on error.
  if (!$adapter = facetapi_adapter_load('facetapi_test')) {
    throw new Exception(t('Error loading adapter.'));
  }

  // Set result count if passed through query string.
  if (isset($_GET['count'])) {
    $adapter->setResultCount((int) $_GET['count']);
  }

  // Instantiate a dummy query class, initialize facets.
  $query = new stdClass();
  $adapter->addActiveFilters($query);

  // Set keywords if passed through query string.
  $keys = isset($_GET['keys']) ? $_GET['keys'] : arg(2);
  $adapter->setSearchKeys($keys);

  $build['placeholder'] = array(
    '#markup' => t('Placeholder'),
  );

  return $build;
}
