<?php

/**
 * @file
 * Install, update and uninstall functions for the Search API saved searches
 * module.
 */

/**
 * Implements hook_schema().
 */
function search_api_saved_searches_schema() {
  $schema['search_api_saved_searches_settings'] = array(
    'description' => 'The "saved searches" settings for a specific index.',
    'fields' => array(
      'id' => array(
        'description' => 'The primary identifier for saved search settings.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'delta' => array(
        'description' => 'The {block}.delta used for these saved searches.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '0',
      ),
      'index_id' => array(
        'description' => 'The {search_api_index}.machine_name this settings record is for.',
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
      ),
      'enabled' => array(
        'description' => 'A flag indicating whether saved searches for this index are enabled.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 1,
      ),
      'options' => array(
        'description' => 'The actual settings for saved searches.',
        'type' => 'blob',
        'serialize' => TRUE,
        'not null' => TRUE,
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0x01,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'indexes' => array(
      'index_id' => array('index_id'),
    ),
    'unique keys' => array(
      'delta' => array('delta'),
    ),
    'primary key' => array('id'),
  );
  $schema['search_api_saved_search'] = array(
    'description' => 'A saved search that will be periodically executed.',
    'fields' => array(
      'id' => array(
        'description' => 'The primary identifier for a saved search.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'The {users}.uid that created this saved search.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'settings_id' => array(
        'description' => 'The {search_api_saved_searches_settings}.delta this saved search uses.',
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
      ),
      'enabled' => array(
        'description' => 'The state of this saved search, enabled (1) or not (0).',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'name' => array(
        'description' => 'The displayed name for this saved search.',
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
      ),
      'mail' => array(
        'description' => 'The mail address that should receive notifications.',
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the saved search was created.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'last_execute' => array(
        'description' => 'The Unix timestamp when the saved search was last executed.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'notify_interval' => array(
        'description' => 'The interval in which this saved search will be checked for new results, in seconds.',
        'type' => 'int',
        'not null' => FALSE,
      ),
      'query' => array(
        'description' => 'The actual search query to be executed.',
        'type' => 'blob',
        'serialize' => TRUE,
        'not null' => TRUE,
      ),
      'options' => array(
        'description' => 'Additional options for this saved search.',
        'type' => 'blob',
        'serialize' => TRUE,
        'not null' => TRUE,
      ),
      'results' => array(
        'description' => 'A comma-separated list of all "old" results for this saved search.',
        'type' => 'text',
        'not null' => FALSE,
      ),
    ),
    'indexes' => array(
      'uid' => array('uid'),
      'mail' => array('mail'),
    ),
    'primary key' => array('id'),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function search_api_saved_searches_uninstall() {
  variable_del('search_api_saved_searches_search_ids');
}

/**
 * Update searches to correctly use the settings' delta, not numeric ID, as the identifier.
 */
function search_api_saved_searches_update_7101() {
  $settings = db_query('SELECT id, delta FROM {search_api_saved_searches_settings}')->fetchAllKeyed();
  db_delete('search_api_saved_search')
    ->condition('settings_id', array_keys($settings), 'NOT IN')
    ->execute();
  foreach ($settings as $id => $delta) {
    db_update('search_api_saved_search')
      ->condition('settings_id', $id)
      ->fields(array('settings_id' => $delta))
      ->execute();
  }
}
