<?php

/**
 * @file
 * Module file for Google Programmable Search.
 */

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\google_cse\Plugin\Search\GoogleSearch;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\search\Entity\SearchPage;

/**
 * Alter the core-provided search input block primarily for stylistic purposes.
 */
function google_cse_form_search_block_form_alter(&$form, &$form_state, $form_id) {
  // This block form alter is needed and serves a different purpose from
  // GoogleSearch::searchFormAlter(). This modifies the Drupal core search
  // block form, whereas searchFormAlter() modifies the form on the search
  // page itself. This alter uses the configuration for the *default* Drupal
  // search. If a search configuration other than Google PSE is set as the
  // default, this alter will not do anything.
  $activeSearchPages = \Drupal::service('search.search_page_repository');
  $default = $activeSearchPages->getDefaultSearchPage();
  $implementation = SearchPage::load($default);
  if ($implementation->getPlugin()->getPluginId() !== 'google_cse_search') {
    return;
  }
  $config = $implementation->getPlugin()->getConfiguration();
  $query_key = $config['query_key'] ?? GoogleSearch::$defaultQueryKey;
  if ($query_key !== 'keys') {
    $form[$query_key] = $form['keys'];
    unset($form['keys']);
  }
  $form['#attached']['library'][] = 'google_cse/googlecseWatermark';
  $form['#attributes']['class'][] = 'google-cse';
  $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $watermark = $config['watermark'] ?? FALSE;
  $form['#attached']['drupalSettings']['googlePSE'] = [
    'language' => $language,
    'displayWatermark' => $watermark,
  ];
  if ($watermark === 0) {
    $form['#attached']['library'][] = 'google_cse/noWatermark';
  }
  if (intval($config['results_searchbox_width']) > 0) {
    $form[$query_key]['#size'] = intval($config['results_searchbox_width']);
  }
  if ($config['results_display'] === 'google') {
    // This search has been configured to display results on Google.
    // Redirect data there.
    $form['#action'] = 'https://cse.google.com/cse';
    $form['cx'] = [
      '#type' => 'hidden',
      '#value' => $config['cx'],
    ];
    $form['q'] = $form[$query_key];
    unset($form[$query_key]);
  }
}

/**
 * Implements hook_theme().
 */
function google_cse_theme($existing, $type, $theme, $path) {
  // Register the Google Search Results theme template.
  return [
    'google_cse_results' => [
      'variables' => [
        'path' => $path,
        'noscript' => NULL,
        'results_prefix' => NULL,
        'results_suffix' => NULL,
        'primary_attributes' => NULL,
        'secondary_attributes' => NULL,
      ],
      'template' => 'google_cse_results',
    ],
  ];
}

/**
 * Implements hook_preprocess_item_list__search_results().
 */
function google_cse_preprocess_item_list__search_results(&$variables) {
  if (!isset($variables['context']['plugin']) || $variables['context']['plugin'] !== 'google_cse_search') {
    return;
  }
  // In the context of Google PSE, we rely on Google to render the content.
  // Therefore, we do not want to use Drupal's default list
  // (see Drupal\search\Controller\SearchController::view()).
  // The simplest way to achieve this in a Drupal context is to
  // unset the "items" sent to the list template and render the
  // Google PSE results as the 'empty' value.
  if (isset($variables['items'][0]['value'])) {
    $variables['empty'] = $variables['items'][0];
    unset($variables['empty']['attributes']);
    unset($variables['items']);
  }
  // Clear 'Your search yielded no results' from Drupal SearchController().
  $variables['empty']['#markup'] = '';
}

/**
 * Implements hook_library_info_build().
 */
function google_cse_library_info_build() {
  $libraries = [];
  $search_implementations = SearchPage::loadMultiple();
  // Add separate CSS libraries for any search plugins that set external CSS.
  // These are attached in template_preprocess_google_cse_results().
  foreach ($search_implementations as $search) {
    if ($search->getPlugin()->getPluginId() !== 'google_cse_search') {
      continue;
    }
    $config = $search->getPlugin()->getConfiguration();
    $external_css = $config['custom_css'];
    if ($external_css) {
      $libraries['customCSS_' . md5($external_css)] = [
        'css' => [
          'theme' => [
            $external_css => [
              'type' => 'external',
            ],
          ],
        ],
      ];
    }
  }
  return $libraries;
}

/**
 * Implements hook_entity_insert().
 *
 * Clear appropriate caches on storing Google Search plugin entity instance.
 */
function google_cse_entity_insert(EntityInterface $entity) {
  if ($entity instanceof SearchPage) {
    $plugin = $entity->getPlugin()->getPluginId();
    if ($plugin == 'google_cse_search') {
      \Drupal::service('router.builder')->rebuild();
    }
  }
}

/**
 * Implements hook_migration_plugins_alter().
 */
function google_cse_migration_plugins_alter(array &$migrations) {
  if (!in_array('d7_search_settings', array_keys($migrations))) {
    return;
  }
  $variable_source = \Drupal::service('plugin.manager.migration')
    ->createStubMigration([
      'id' => 'foo',
      'idMap' => ['plugin' => 'null'],
      'source' => [
        'plugin' => 'variable',
        'ignore_map' => TRUE,
      ],
      'destination' => ['plugin' => 'null'],
    ])
    ->getSourcePlugin();
  if (!$variable_source instanceof DrupalSqlBase) {
    return;
  }
  try {
    $variable_source->checkRequirements();
  }
  catch (RequirementsException $e) {
    // Variable source plugin requirements aren't met, this is not a Drupal
    // source.
    return;
  }
  $system_data = $variable_source->getSystemData();
  if (empty($system_data['module']['google_cse']['status'])) {
    unset($migrations['d7_google_cse']);
    return;
  }
  if (
    !empty($migrations['d7_search_settings']['process']['default_page']) &&
    array_key_exists('map', $migrations['d7_search_settings']['process']['default_page'])
  ) {
    $migrations['d7_search_settings']['process']['default_page']['map']['google_cse'] = 'google_cse_search';
  }
}

/**
 * Implements hook_help().
 */
function google_cse_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.google_cse':

      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Google Programmable Search is an embedded search engine that can be used to search any set of one or more sites.  No Google API key is required. Read more at <a href="https://developers.google.com/custom-search" target="blank">https://developers.google.com/custom-search</a>.') . '</p>';
      $output .= '<h3>' . t('Setup') . '</h3>';
      $output .= '<ol>' . t('<li>Before installing this module, register a Google Search engine at <a href="https://programmablesearchengine.google.com/cse/all">https://programmablesearchengine.google.com/cse/all</a>.</li><li>Install this module and create a search instance at <a href="/admin/config/search/pages">/admin/config/search/pages</a> and configure it by entering your Google Search ID.</li><li>Optionally set it as the default search module</li><li>Grant the <a href="/admin/people/permissions/module/google_cse">"View Google Programmable Search"</a> permission to one or more roles to use Google Search.</li>') . '</ol>';
      $output .= '<p>If you set this search instance as the default Drupal search, the core search block will redirect directly to your site\'s Google search results page.</p><p>If you instead want to embed the search form and its results within a page, use the Google Programmable Search block, described below.</p>';
      $output .= '<h4>' . t('Search as Block') . '</h4>';
      $output .= '<p>' . t('For sites that do not want search results to display on a standalone page, this module includes a Google Programmable Search block which can be enabled at <a href="/admin/structure/block">/admin/structure/block</a>. This block provides a combined search box and with search results. After entering search terms, the user will be returned to the same page and the results will be displayed. <strong>Important: Do not configure this block to appear on the search page, as the search results will fail to display</strong>.') . '</p>';
      $output .= '<h4>' . t('Customizing Programmable Search Elements') . '</h4>';
      $output .= '<p>' . t("You can use optional attributes to overwrite configurations created in the Programmable Search Engine control panel. This enables you to create a page-specific search experience. These attributes can be added via key/value inputs in the Drupal search configuration form. This module does not document these attributes; rather, it is the responsibility of the site maintainer to understand the behavior of the available attributes, which are listed at <a href='https://developers.google.com/custom-search/docs/element'>https://developers.google.com/custom-search/docs/element</a>.") . '</p>';
      return $output;
  }
}

/**
 * Callback function for database updates. See google_cse_update_10001().
 */
function _google_cse_convert_from_3_to_4() {
  $search_implementations = SearchPage::loadMultiple();
  $migrations = [
    'lr' => 'data-lr',
    'gl' => 'data-gl',
    'cr' => 'data-cr',
    'limit_domain' => 'data-as_sitesearch',
  ];
  $deprecated_settings = [
    'results_tab',
    'results_width',
    'cof_here',
    'cof_google',
    'results_display_images',
    'sitesearch',
    'sitesearch_form',
    'sitesearch_option',
    'sitesearch_default',
    'domain',
    'hl',
    'locale_hl',
    'locale_lr',
    'gl',
    'cr',
    'lr',
    'ie',
    'oe',
    'safe',
  ];
  foreach ($search_implementations as $search) {
    if ($search->getPlugin()->getPluginId() !== 'google_cse_search') {
      continue;
    }
    $config = $search->getPlugin()->getConfiguration();
    foreach ($migrations as $legacy => $new) {
      if (!empty($config[$legacy])) {
        $config['data_attributes'][] = [
          'key' => $new,
          'value' => $config[$legacy],
        ];
      }
    }
    // SafeSearch is done separately to manipulate values.
    $safe_migrations = [
      '' => 'off',
      'off' => 'off',
      'medium' => 'moderate',
      'high' => 'active',
    ];
    if (isset($config['safe'])) {
      $config['data_attributes'][] = [
        'key' => 'data-safeSearch',
        'value' => $safe_migrations[$config['safe']],
      ];
    }
    foreach ($deprecated_settings as $deprecated) {
      unset($config[$deprecated]);
    }
    $search->getPlugin()->setConfiguration($config);
    $search->save();
  }
}
