<?php

include_once 'panopoly_search.features.inc';

/**
 * Implementation of hook_ctools_plugin_directory()
 */
function panopoly_search_ctools_plugin_directory($module, $plugin) {
  return 'plugins/' . $plugin;
}

/**
 * Implementation of hook_form_alter()
 */
function panopoly_search_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_form' || $form_id == 'search_theme_form') {
    $form['#submit'] = array('panopoly_search_all_form_submit');
    if (!empty($form['basic']['keys'])) {
      $form['basic']['keys']['#size'] = '';
    }
  }
}

/**
 * Custom form submit handler to change redirect state of the form
 */
function panopoly_search_all_form_submit($form, &$form_state) {
  $keys = trim($form_state['values']['keys']);
  $form_state['redirect'] = array('search/site/' . $keys);
}

/** 
 * Implementation of hook_modules_enabled()
 */
function panopoly_search_modules_enabled($modules) {

  // If we are running on Pantheon enable Solr support
  if (in_array('panopoly_search', $modules) && variable_get('pantheon_tier') && module_enable(array('pantheon_apachesolr'))) {

    // Supress the message from enabling Panopoly Apachesolr since we are configuring it automatically
    drupal_get_messages('status');

    // Push Pantheon's Search API Schema
    $schema_uri = drupal_get_path('module', 'search_api_solr') . '/solr-conf/3.x/schema.xml';
    pantheon_apachesolr_update_schema($schema_uri);

    // Enable Panopoly's Solr Server and Solr Index
    require_once(drupal_get_path('module', 'search_api') . '/search_api.admin.inc');
    $solr_server = search_api_server_load('solr_server');
    $solr_server->update(array('enabled' => 1));
    $solr_index = search_api_index_load('node_index');
    $solr_index->update(array('enabled' => 1));

    // Clear Solr Search Index and Rebuild
    $solr_index->clear();
    $solr_index->reindex();
  }
}
