<?php

/**
 * @file
 * The Views Natural Sort install file.
 */

/**
 * Implements hook_schema().
 */
function views_natural_sort_schema() {

  // Contains relations between two users.
  $schema['views_natural_sort'] = array(
    'description' => 'Compressed titles for natural sorting.',
    'fields' => array(
      'eid' => array(
        'description' => 'Entity id',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'entity_type' => array(
        'description' => 'Entity Type',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => 'node',
      ),
      'field' => array(
        'description' => 'The field name. This will be title or some cck text field, etc.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'delta' => array(
        'description' => 'The sequence number for this data item, used for multi-value fields',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'content' => array(
        'description' => 'Filtered content used for sorting.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('eid', 'entity_type', 'field', 'delta'),
  );

  return $schema;
}

/**
 * Implements hook_install().
 */
function views_natural_sort_install() {
  variable_set(
    'views_natural_sort_beginning_words_remove',
    array(
      t('The'),
      t('A'),
      t('An'),
      t('La'),
      t('Le'),
      t('Il'),
    )
  );
  variable_set(
    'views_natural_sort_words_remove',
    array(
      t('and'),
      t('or'),
      t('of'),
    )
  );
  variable_set('views_natural_sort_symbols_remove', "#\"'\\()[]");
  variable_set('views_natural_sort_days_of_the_week_enabled', FALSE);
  variable_set('views_natural_sort_rebuild_items_per_batch', 500);
}

/**
 * Implements hook_enable().
 */
function views_natural_sort_enable() {
  module_load_include('inc', 'views_natural_sort', 'views_natural_sort.admin');
  views_natural_sort_rebuild_index_submit();
}

/**
 * Implements hook_uninstall().
 */
function views_natural_sort_uninstall() {
  variable_del('views_natural_sort_beginning_words_remove');
  variable_del('views_natural_sort_words_remove');
  variable_del('views_natural_sort_symbols_remove');
  variable_del('views_natural_sort_days_of_the_week_enabled');
  variable_del('views_natural_sort_rebuild_items_per_batch');
}

/**
 * Rebuild the sorting index after changes made for numerical data.
 */
function views_natural_sort_update_7001() {
  module_load_include('inc', 'views_natural_sort', 'views_natural_sort.admin');
  views_natural_sort_rebuild_index_submit();
}

/**
 * Upgrade the 7.x-1.x table structure to the 7.x-2.x table structure.
 */
function views_natural_sort_update_7200() {
  db_drop_primary_key('views_natural_sort');
  db_add_field(
    'views_natural_sort',
    'entity_type',
    array(
      'description' => t('Entity Type'),
      'type' => 'varchar',
      'length' => 128,
      'not null' => TRUE,
      'default' => 'node',
    )
  );
  db_add_field(
    'views_natural_sort',
    'delta',
    array(
      'description' => t('The sequence number for this data item, used for multi-value fields'),
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    )
  );
  db_change_field(
    'views_natural_sort',
    'nid',
    'eid',
    array(
      'description' => t('Entity id'),
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
    ),
    array(
      'primary key' => array('eid', 'entity_type', 'field', 'delta'),
    )
  );
  module_load_include('inc', 'views_natural_sort', 'views_natural_sort.admin');
  views_natural_sort_rebuild_index_submit();
}

/**
 * Upgrading 1.x VNS views to 2.x VNS views.
 */
function views_natural_sort_update_7201() {
  // Load all available views.
  $views = views_get_all_views();
  foreach ($views as $view_name => $view) {
    foreach ($view->display as &$display) {
      if (!empty($display->display_options['sorts'])) {
        foreach ($display->display_options['sorts'] as &$sort) {
          if ($sort['table'] == 'views_natural_sort') {
            $sort['table'] = 'node';
            $sort['field'] = 'title';
            $sort['order'] = 'N' . $sort['order'];
            $view->save();
            drupal_set_message(t('Views Natural Sort Upgraded the view !name.', array('!name' => $view_name)), 'status');
          }
        }
      }
    }
  }
  cache_clear_all();
  return t('If you implement your views using Features, be sure to update the Features that contain View Natural Sort Views immediately after this upgrade and before the next revert.');
}

/**
 * Removing the queue because queuing didn't work out.
 */
function views_natural_sort_update_7202() {
  DrupalQueue::get('views_natural_sort_index_queue')->deleteQueue();
}
