You are here

search_autocomplete.install in Search Autocomplete 6.4

This file is used to install/update/delete the module tables in database

@author Miroslav Talenberg (Dominique CLAUSE) <http://www.axiomcafe.fr/contact>

Sponsored by: www.axiomcafe.fr

File

search_autocomplete.install
View source
<?php

/**
 * @file
 * This file is used to install/update/delete the module tables in database
 *
 * @author
 * Miroslav Talenberg (Dominique CLAUSE) <http://www.axiomcafe.fr/contact>
 * 
 * Sponsored by:
 * www.axiomcafe.fr
 */

// -----------------------------------------------------------------------.

/**
 * Implements hook_schema().
 *
 * Set the schema of database.
 *
 * @return array
 *   The schema for of the table to create.
 */
function search_autocomplete_schema() {

  // Schema for search_autocomplete database.
  $schema['search_autocomplete_forms'] = array(
    'description' => t('Store the forms to autocomplete using Search Autocomplete.'),
    'fields' => array(
      'fid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'title' => array(
        'description' => 'Human readable name for the form',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'selector' => array(
        'description' => 'Reference id selector of the the form in drupal',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'weight' => array(
        'description' => 'Form weight in table',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'enabled' => array(
        'description' => 'Define if autocomplete is activated or not',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'parent_fid' => array(
        'description' => 'Define if the from follows the configuration of another one',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'min_char' => array(
        'description' => 'Minimum of character before triggering suggestions',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 3,
      ),
      'max_sug' => array(
        'description' => 'Maximum number of suggestions',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 10,
      ),
      'no_results' => array(
        'description' => 'Maximum number of suggestions',
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'default' => '-- no results --',
      ),
      'auto_submit' => array(
        'description' => 'Define if form should be autosubmitted when suggestion is choosen',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'auto_redirect' => array(
        'description' => 'Define if user should be redirected to suggestion directly',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'translite' => array(
        'description' => 'Define if suggestion searches should be translited',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'data_source' => array(
        'description' => 'Should data come from callback or from static resource',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'data_callback' => array(
        'description' => 'Callback URL for data source',
        'type' => 'varchar',
        'length' => 255,
        'default' => '',
      ),
      'data_static' => array(
        'description' => 'Static text as a data',
        'type' => 'text',
        'size' => 'big',
      ),
      'data_view' => array(
        'description' => 'Internal callback view',
        'type' => 'varchar',
        'length' => 255,
        'default' => '',
      ),
      'theme' => array(
        'description' => 'Theme to use with this form',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'lightness',
      ),
    ),
    'primary key' => array(
      'fid',
    ),
  );
  return $schema;
}

// -------------------------------------------------------------------------.

/**
 * Implements hook_install().
 */
function search_autocomplete_install() {
  if (db_table_exists('search_autocomplete_forms') && db_table_exists('search_autocomplete_suggestions')) {
    return;
  }
  $results = drupal_install_schema('search_autocomplete');

  //Install the database specified in  'function search_autocomplete_schema'
  foreach ($results as $result) {

    // Check eventual errors that could have occured
    if (!$result['success']) {
      drupal_set_message(st('An error has occured during table creation, please retry. If the problem persist please post an issue.'), 'error');
    }
  }
  $limit = variable_get('search_autocomplete_limit', 10);
  $trigger = variable_get('search_autocomplete_trigger', 1);
  $enabled = 1;

  // ----------------
  // Declare insertion statement.
  $sql = 'INSERT INTO {search_autocomplete_forms} (title, selector, weight, enabled, min_char, max_sug, auto_submit, auto_redirect, translite, data_source, data_callback, data_static, data_view, theme) VALUES ' . '("%s", "%s", %d, %d, %d, %d, %d, %d, %d, %d, "%s", "%s", "%s", "%s"),' . '("%s", "%s", %d, %d, %d, %d, %d, %d, %d, %d, "%s", "%s", "%s", "%s"),' . '("%s", "%s", %d, %d, %d, %d, %d, %d, %d, %d, "%s", "%s", "%s", "%s"),' . '("%s", "%s", %d, %d, %d, %d, %d, %d, %d, %d, "%s", "%s", "%s", "%s")';
  $variables = array(
    st('Search page - Node Tab') . "  (search/node/%)",
    '#search-form[action="/search/node"] #edit-keys',
    4,
    $enabled,
    $trigger,
    $limit,
    1,
    1,
    1,
    3,
    'search_autocomplete/autocomplete/1',
    '',
    'node_autocomplete',
    'tripadvisor',
    st('Search page - User Tab') . "  (search/user/%)",
    '#search-form[action="/search/user"] #edit-keys',
    3,
    $enabled,
    $trigger,
    $limit,
    1,
    1,
    1,
    1,
    '',
    '',
    '',
    'facebook like',
    st('Search Block'),
    '#edit-search-block-form-1',
    2,
    $enabled,
    $trigger,
    $limit,
    1,
    1,
    1,
    3,
    'search_autocomplete/autocomplete/3/',
    '',
    'node_autocomplete',
    'tripadvisor',
    st('Search box (added by the theme)'),
    "#edit-search-theme-form-1",
    1,
    $enabled,
    $trigger,
    $limit,
    1,
    1,
    1,
    3,
    'search_autocomplete/autocomplete/4/',
    '',
    'node_autocomplete',
    'tripadvisor',
  );
  $ok_result = db_query($sql, $variables);
  if (!$ok_result) {
    drupal_set_message(st('An error has occured while creating default forms, please retry. If the problem persist please post an issue here :') . '  <a href="http://drupal.org/project/issues/search_autocomplete">http://drupal.org/project/issues/search_autocomplete</a>', 'error');
  }
  drupal_set_message(st('Search Autocomplete is now correctly installed!') . "<br/>" . st('If you see some functionalities missing or broken, please post an issue here:') . '  <a href="http://drupal.org/project/issues/search_autocomplete">http://drupal.org/project/issues/search_autocomplete</a>');
}

/**
 * Get ready for Search Autocomplete 6.4-x
 */
function search_autocomplete_update_7400(&$sandbox) {
  $ret = array();
  $num_deleted = db_drop_table($ret, 'search_autocomplete_forms');
  $num_deleted &= db_drop_table($ret, 'search_autocomplete_suggestions');
  db_create_table($ret, 'search_autocomplete_forms', drupal_get_schema('search_autocomplete_forms', TRUE));
  search_autocomplete_install();
  return t('The update process is successfull.');
}

// -----------------------------------------------------------------------------------------------

/**
 * Implementation of hook_uninstall().
 */
function search_autocomplete_uninstall() {
  $results = drupal_uninstall_schema('search_autocomplete');
  foreach ($results as $result) {
    if (!$result['success']) {
      drupal_set_message(t('An error has occured while uninstalling the databases, please retry. If the problem persist please post an issue.'), 'error');
    }
  }
}

/**
 * Change internal callback URL from absolute to relative.
 */
function search_autocomplete_update_6400(&$sandbox) {
  global $base_url;
  $ret = array();
  $result = db_select('search_autocomplete_forms', 'f')
    ->fields('f', array(
    'fid',
    'data_callback',
  ))
    ->execute()
    ->fetchAll();
  foreach ($result as $item) {
    db_update('search_autocomplete_forms')
      ->fields(array(
      'data_callback' => str_replace($base_url . "/", "", $item->data_callback),
    ))
      ->condition('fid', $item->fid)
      ->execute();
  }
  return t('Update has:<br/>- change internal callback URL from absolute to relative.<br/>- clear JS cache.<br/> Done with success.');
}

/**
 * Add a translite optionnal option.
 */
function search_autocomplete_update_6401(&$sandbox) {
  $ret = array();
  $translite_field = array(
    'description' => 'Define if suggestion searches should be translited',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 1,
  );
  db_add_field($ret, 'search_autocomplete_forms', 'translite', $translite_field);
  $no_results_field = array(
    'description' => 'Maximum number of suggestions',
    'type' => 'varchar',
    'length' => 50,
    'not null' => FALSE,
    'default' => '-- no results --',
  );
  db_add_field($ret, 'search_autocomplete_forms', 'no_results', $no_results_field);
  return t('Update has:<br/>- add a column "translite" in the search autocomplete database.<br/>- add a column "no_results" in the search autocomplete database.<br/> Done with success.');
}

// -----------------------------------------------------------------------------------------------

/**
 * Change columns from TEXT to LONGTEXT and add filters in callbacks.
 */
function search_autocomplete_update_6402(&$sandbox) {
  $ret = array();

  // Create the definition for the field.
  $new_field = array(
    'description' => 'Static text as a data',
    'type' => 'text',
    'size' => 'big',
  );
  db_change_field($ret, 'search_autocomplete_forms', 'data_static', 'data_static', $new_field);

  // Select data_callbacks of.
  $result = db_select('search_autocomplete_forms', 'f')
    ->fields('f', array(
    'fid',
    'data_callback',
  ))
    ->execute()
    ->fetchAll();
  foreach ($result as $item) {
    db_update('search_autocomplete_forms')
      ->fields(array(
      'data_callback' => $item->data_callback . '?filter=',
    ))
      ->condition('data_callback', array(
      'autocomplete-nodes',
      'autocomplete-users',
    ), 'IN')
      ->execute();
  }
  return t('Update has:') . '<br/>' . t('- changed column type "data_static" from TEXT to LONGTEXT.') . '<br/>' . t('- Add filter to VIEWS callback for performance improvement.');
}

/**
 * Change user autocompletion data.
 */
function search_autocomplete_update_6403(&$sandbox) {

  // Select data_callbacks of.
  db_update('search_autocomplete_forms')
    ->fields(array(
    'theme' => 'facebook like.css',
  ))
    ->condition('data_callback', 'autocomplete-users?filter=', '=')
    ->execute();
  cache_clear_all();
  return t('Update has been done.');
}

Functions

Namesort descending Description
search_autocomplete_install Implements hook_install().
search_autocomplete_schema Implements hook_schema().
search_autocomplete_uninstall Implementation of hook_uninstall().
search_autocomplete_update_6400 Change internal callback URL from absolute to relative.
search_autocomplete_update_6401 Add a translite optionnal option.
search_autocomplete_update_6402 Change columns from TEXT to LONGTEXT and add filters in callbacks.
search_autocomplete_update_6403 Change user autocompletion data.
search_autocomplete_update_7400 Get ready for Search Autocomplete 6.4-x