<?php
/**
 * @file
 * Drupal needs this blank file.
 */
 
include_once 'diy_search.features.inc';

/**
 * Implements hook_entity_property_info_alter().
 *
 * Adds an entity property for the domain access of the node.
 */

/**
 * Implements HOOK_entity_property_info_alter().
 *
 * @param $info
 */
function diy_search_entity_property_info_alter(&$info) {

  $properties = &$info['node']['properties'];

  $properties['diy_search_domain_access'] = array(
    'label' => t('Domain Access Information'),
    'description' => t('The domains to which the node is published.'),
    'type' => 'list<integer>',
    'getter callback' => 'diy_search_get_domain_info',
  );
}

/**
 * Property getter callback
 */
function diy_search_get_domain_info($data, array $options, $name, $type, $info) {
  if(isset($data->domain_site) && $data->domain_site == 1) {
    $domains = domain_domains();
    return array_keys($domains);
  }

  return (!empty($data->domains) ? $data->domains : NULL);
}

/**
 * Implements hook_search_api_query_alter().
 *
 * Adds a filter for the current domain.
 */
function diy_search_search_api_query_alter(SearchApiQueryInterface $query) {
  $domain = domain_get_domain();
  $query->condition('diy_search_domain_access', $domain['domain_id']);
}


/**
 * Implementation of hook_init().
 */
function diy_search_init() {
  global $base_url;

  $local_site_name = variable_get('site_name', '');

  // Add schema.org markup for site search box
  // https://developers.google.com/structured-data/slsb-overview
  $script = <<<EOT
<script type="application/ld+json">
{
   "@context": "http://schema.org",
   "@type": "WebSite",
   "url": "$base_url",
   "name" : "$local_site_name",
   "potentialAction": {
     "@type": "SearchAction",
     "target": "$base_url/page-search?search={search_term_string}",
     "query-input": "required name=search_term_string"
   }
}
</script>
EOT;

  if (drupal_is_front_page()) {
    $element = array(
      '#type' => 'markup',
      '#markup' => $script,
    );
    drupal_add_html_head($element, 'google-schema-search-box');
  }
}

/**
* Implementation of hook_form_alter().
*/
function diy_search_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'views_exposed_form') {
    if($form['#id'] == 'views-exposed-form-page-search-page') { 
      $form['search']['#attributes']['placeholder'] = t('Search');
    }
  }
}

/**
 * Implements HOOK_block_info().
 */
function diy_search_block_info() {

  $blocks['diy_search_search_page_block'] = array(
    'info' => t('DIY Search search page block form'),
  );

  $blocks['diy_suche_facet_wrapper_start'] = array(
    'info' => t('DIY Suche facet wrapper start'),
  );

  $blocks['diy_suche_facet_wrapper_end'] = array(
    'info' => t('DIY Suche facet wrapper end'),
  );

  return $blocks;
}

/**
 * Implements HOOK_block_view().
 */
function diy_search_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'diy_search_search_page_block':
      $block['content'] = diy_search_search_page_block_content('field_channel_header_title');
      break;
    case 'diy_suche_facet_wrapper_start':
      $block['content'] = '<section class="diy-suche-facet-wrapper">';
      break;
    case 'diy_suche_facet_wrapper_end':
      $block['content'] = '</section>';
      break;
  }

  return $block;
}

/**
 * Block callback
 */
function diy_search_search_page_block_content() {
  // add block content for search
  $block = block_load('views', '-exp-page_search-page');
  $block_content = _block_render_blocks(array($block));
  $block_renderable = _block_get_renderable_array($block_content);
  $output = drupal_render($block_renderable);

  return $output;
}

/**
 * Implements hook_search_api_solr_query_alter().
 *
 * Alters the query sent to SOLR to add boost query parameters for the various
 * node types.
 */
function diy_search_search_api_solr_query_alter(array &$call_args, SearchApiQueryInterface $query) {
  $index = $query->getIndex();
  if($index->machine_name == 'page_search') {
    $node_types = node_type_get_types();
    foreach ($node_types as $node_type) {
      $boost_factor = variable_get('diy_defaults_node_rank_by_type_' . $node_type->type, 0) * 10;
      if ($boost_factor == 0 || !is_numeric($boost_factor)) {
        continue;
      }
      $call_args['params']['bq'][$node_type->type] = "ss_type:\"$node_type->type\"^$boost_factor";
    }
  }
}

/**
 * Implementation of hook_theme()
 */
function diy_search_theme() {
  return array(
    'diy_search_navigation' => array(
      'variables' => array('searchphrase' => NULL),
    )
  );
}

/**
 * Default theme implementation
 *
 * @param $variables
 */
function theme_diy_search_navigation($variables) {
  $options = array();

  if($variables['searchphrase']) {
    $options['query'] = array('search' => $variables['searchphrase']);
  }

  $list_vars = array(
    'items' => array(
      l(t('Articles, Tools and Materials'), 'page-search', $options),
      l(t('Videos'), 'page-search-videos', $options),
      l(t('News'), 'page-search-news', $options)
    ),
    'attributes' => array(
      'class' => 'tabs search-tabs'
    )
  );

  return theme('item_list', $list_vars);
}

/**
 * Implements HOOK_preprocess_views_view().
 */
function diy_search_preprocess_views_view(&$vars) {

  if($vars['name'] == "page_search" && in_array($vars['display_id'], array('page', 'page_1', 'page_2'))) {
    $params = drupal_get_query_parameters();
    $variables = array();

    if(isset($params['search'])) {
      $variables['searchphrase'] = $params['search'];
    }

    $vars['attachment_before'] = theme('diy_search_navigation', $variables);
  }
}