<?php

/**
 * @file
 * Installation file for Panopoly Search
 */

/**
 * Implementation of hook_install()
 */
function panopoly_search_install() {

  // Allow anonymous and authenticated users to search content
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('search content'));
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('search content'));
}

/**
 * Implemenetation of hook_uninstall()
 */
function panopoly_search_uninstall() {

}

/** 
 * Panopoly Beta 4 Update: Enabling Search API DB Module
 */
function panopoly_search_update_7001(&$sandbox) {
  module_enable(array('search_api_db'));
}

/**
 * Panopoly Beta 5 Update: Using Pantheon Solr Search System
 */
function panopoly_search_update_7002(&$sandbox) {

  // If we are running on Pantheon...
  if (variable_get('pantheon_tier')) {

    // Enable Pantheon's Search Module
    module_enable(array('pantheon_apachesolr'));

    // Load the Solr PHP Client Library
    _search_api_solr_autoload('Apache_Solr_Service');
    _search_api_solr_autoload('Apache_Solr_HttpTransport_Abstract');

    // Push Pantheon's Search API Schema
    $schema_uri = drupal_get_path('module', 'search_api_solr') . '/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();

    return t('Panopoly has been configured to use Pantheon\'s Solr Service');
  }

  return ('Panopoly was *not* configured to use Pantheon\'s Solr Service since you are not running on Pantheon');
}

/**
 * Panopoly Beta 5 Update: Updating Search API DB Search System
 */
function panopoly_search_update_7003(&$sandbox) {
  $database_server = search_api_server_load('database_server');
  $database_index = search_api_index_load('database_node_index'); 
  $database_server->fieldsUpdated($database_index);
  return t('Panopoly\'s Search DB Search API index has been updated');
}

/**
 * Panopoly RC4 Update: Disabling the Search API Solr System
 */
function panopoly_search_update_7004(&$sandbox) {

  // If we are running on Pantheon...
  if (variable_get('pantheon_tier')) {

    // Load the Solr PHP Client Library
    _search_api_solr_autoload('Apache_Solr_Service');
    _search_api_solr_autoload('Apache_Solr_HttpTransport_Abstract');

    // Disable 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' => 0));
    $solr_index = search_api_index_load('node_index');
    $solr_index->update(array('enabled' => 0));

    return t('Panopoly\'s Search API Solr index has been temporarily disabled. Search API DB will now be used for faceted search results.');
  }
}
