<?php
/**
 * @file
 * search_api_stats_block.module
 */

/**
 * Implements hook_block_info().
 */
function search_api_stats_block_block_info() {
  $blocks = array();

  foreach (search_api_index_load_multiple(FALSE) as $index) {
    $blocks[$index->server . '__' . $index->machine_name] = array(
      'info' => t('Search API stats block: @identifier', array('@identifier' => $index->server . ' - ' . $index->machine_name)),
      'cache' => DRUPAL_CACHE_GLOBAL,
    );
  }

  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function search_api_stats_block_block_view($delta = '') {
  $block = array();

  list($s_name, $i_name) = explode('__', $delta);
  if (!empty($s_name) && !empty($i_name)) {
    $block = array(
      'subject' => t('Top search phrases'),
      'content' => _search_api_stats_block_show_keywords($s_name, $i_name),
    );
  }

  return $block;
}

/**
 * Implements hook_block_configure().
 */
function search_api_stats_block_block_configure($delta = '') {
  $form = array();

  $config = variable_get('search_api_stats_block_config', array());

  list($s_name, $i_name) = explode('__', $delta);
  if (!empty($s_name) && !empty($i_name)) {

    // Number of top search phrases to display
    $form['search_api_stats_block_num_phrases'] = array(
      '#type' => 'select',
      '#title' => t('Number of top search phrases to display'),
      '#default_value' => empty($config[$s_name][$i_name]['num_phrases']) ? 8 : $config[$s_name][$i_name]['num_phrases'],
      '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)),
    );

    // Path of search page
    $form['search_api_stats_block_path'] = array(
      '#type' => 'textfield',
      '#title' => t('Path of search page'),
      '#default_value' => empty($config[$s_name][$i_name]['path']) ? '' : $config[$s_name][$i_name]['path'],
      '#required' => TRUE,
    );

    // Parameter name for the search phrase
    $form['search_api_stats_block_param_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Parameter name for the search phrase'),
      '#default_value' => empty($config[$s_name][$i_name]['param_name']) ? 'keywords' : $config[$s_name][$i_name]['param_name'],
      '#required' => TRUE,
    );
  }

  return $form;
}

/**
 * Implements hook_block_save().
 */
function search_api_stats_block_block_save($delta = '', $edit = array()) {
  $config = variable_get('search_api_stats_block_config', array());

  list($s_name, $i_name) = explode('__', $delta);
  if (!empty($s_name) && !empty($i_name)) {

    // Number of top search phrases to display
    $config[$s_name][$i_name]['num_phrases'] = (int)$edit['search_api_stats_block_num_phrases'];

    // Path of search page
    $config[$s_name][$i_name]['path'] = $edit['search_api_stats_block_path'];

    // Parameter name for the search phrase
    $config[$s_name][$i_name]['param_name'] = $edit['search_api_stats_block_param_name'];
  }

  variable_set('search_api_stats_block_config', $config);
}

/**
 * Implements hook_theme().
 */
function search_api_stats_block_theme($existing, $type, $theme, $path) {
  return array(
    'search_api_stats_block' => array(
      'variables' => array('phrases' => NULL, 'path' => NULL, 'param_name' => NULL),
      'file' => 'search_api_stats_block.theme.inc',
    ),
  );
}

/**
 * Responsible for querying the database and generating the list of phrases.
 * @param  string $s_name The machine name of the Search API server
 * @param  string $i_name The machine name of the Search API index
 * @return markup         Complete markup for the block content
 */
function _search_api_stats_block_show_keywords($s_name, $i_name) {
  $config = variable_get('search_api_stats_block_config', array());

  // Default variables passed to theming function.
  $phrases = array();
  $path = empty($config[$s_name][$i_name]['path']) ? '' : $config[$s_name][$i_name]['path'];
  $param_name = empty($config[$s_name][$i_name]['param_name']) ? 'keywords' : $config[$s_name][$i_name]['param_name'];

  $result = db_query_range("SELECT *, count(*) as num FROM {search_api_stats} WHERE s_name=:s_name AND i_name=:i_name GROUP BY keywords ORDER BY num DESC", 0, 8, array(':s_name' => $s_name, ':i_name' => $i_name));

  foreach ($result as $phrase) {
    $phrases[$phrase->keywords] = (array)$result;
  }

  return theme('search_api_stats_block', array('phrases' => $phrases, 'path' => $path, 'param_name' => $param_name));
}