views_natural_sort.admin.inc 5.8 KB
Newer Older
1 2 3 4 5 6 7
<?php
/**
 * @file
 * Callbacks for managing Views Natural Sort.
 */

/**
8
 * Form callback for Views Natural Sort Rebuild Index page.
9 10 11 12
 *
 * Allows rebuilding index but should also allow things like limiting what node
 * types are indexed and cck text field index options.
 */
13
function views_natural_sort_rebuild_index_form() {
14 15 16 17 18 19 20 21 22 23 24
  $form = array();

  $form['rebuild'] = array(
    '#type' => 'submit',
    '#value' => t('Rebuild title index'),
    '#submit' => array('views_natural_sort_rebuild_index_submit'),
  );

  return $form;
}

25 26 27 28 29 30 31 32 33 34 35 36
/**
 * Form callback for the Views Natural Sort settings page
 *
 * Allows the removal of specific words and symbols from all your titles.
 */
function views_natural_sort_settings_form() {
  $form = array();

  $form['beginning_words'] = array(
    '#type' => 'textfield',
    '#title' => 'Words to filter from the beginning of a phrase',
    '#default_value' => implode(',', variable_get('views_natural_sort_beginning_words_remove', array())),
37
    '#description' => t('Commonly, the words "A", "The", and "An" are removed when sorting book titles if they appear at the beginning of the title. Those would be great candidates for this field. Separate words with a comma.'),
38 39 40 41 42 43
  );

  $form['words'] = array(
    '#type' => 'textfield',
    '#title' => 'Words to filter from anywhere in a phrase',
    '#default_value' => implode(',', variable_get('views_natural_sort_words_remove', array())),
44
    '#description' => t('Commonly used words like "of", "and", and "or" are removed when sorting book titles. Words you would like filtered go here. Separate words with a comma.'),
45 46 47 48 49 50
  );

  $form['symbols'] = array(
    '#type' => 'textfield',
    '#title' => 'Symbols to filter from anywhere in a phrase',
    '#default_value' => variable_get('views_natural_sort_symbols_remove', ''),
51
    '#description' => t('Most symbols are ignored when performing a sort naturally. Those symbols you want ignored go here. Do not use a separator. EX: &$".'),
52
  );
53 54 55 56 57 58
  $form['days_of_the_week_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => 'Sort days of the week and their abbreviations',
    '#description' => "Checking this setting will allow sorting of days of the week in their proper order starting with the day of the week that is configurable by you and for each language.",
    '#efault_value' => variable_get('views_natural_sort_days_of_the_week_enabled', FALSE),
  );
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save Settings'),
  );

  return $form;
}

/**
 * Submit handler that saves custom word handlers and other settings.
 */
function views_natural_sort_settings_form_submit($form, &$form_state) {
  $beginning_words = explode(',',$form_state['values']['beginning_words']);
  array_walk($beginning_words, create_function('&$val', '$val = trim($val);'));
  $words = explode(',',$form_state['values']['words']);
  array_walk($words, create_function('&$val', '$val = trim($val);'));
  $symbols = trim($form_state['values']['symbols']);

  variable_set('views_natural_sort_beginning_words_remove', $beginning_words);
  variable_set('views_natural_sort_words_remove', $words);
  variable_set('views_natural_sort_symbols_remove', $symbols);
80
  variable_set('views_natural_sort_days_of_the_week_enabled', $form_state['values']['days_of_the_week_enabled']);
81 82 83 84

  views_natural_sort_rebuild_index_submit();
}

85 86 87 88
/**
 * Submit handler that triggers the rebuild_index batch.
 */
function views_natural_sort_rebuild_index_submit() {
89 90 91 92 93 94 95 96 97 98 99 100
  views_natural_sort_rebuild_index_batch_set();
}

function views_natural_sort_rebuild_index_batch_set(array $entry_types = array()) {
  if (empty($entry_types)) {
    $entry_types = module_invoke_all('views_natural_sort_get_entry_types');
  }
  $operations = array();

  foreach($entry_types as $entry_type) {
    $operations[] = array('views_natural_sort_rebuild_index', array($entry_type));
  }
101
  $batch = array(
102 103
    'operations' => $operations,
    'title' => t('Rebuilding Views Natural Sort Indexing Entries'),
104 105 106 107 108 109 110 111 112 113
    'finished' => 'views_natural_sort_rebuild_index_finished',
    'file' => drupal_get_path('module', 'views_natural_sort') . '/views_natural_sort.admin.inc',
  );

  batch_set($batch);
}

/**
 * Batch API callback for rebuild_index.
 */
114
function views_natural_sort_rebuild_index($entry_type, &$context) {
115 116 117
  // Alias sandbox for easier referencing.
  $sandbox = &$context['sandbox'];

118 119
  // Hook for modules to implement and return data that views_natural_sort can
  // store as an index for that module's entries.
120
  $index_entries = module_invoke_all('views_natural_sort_get_rebuild_data', $entry_type);
121 122 123 124

  //TODO: this seems like it has the possiblity to produce unused data.
  // Consider a delete of some sort.

125 126 127
  // Initialize our context.
  if (!isset($sandbox['max'])) {
    $sandbox['progress'] = 0;
128 129
    $sandbox['max'] = count($index_entries) - 1;
    $sandbox['total'] = count($index_entries);
130

131 132
    $sandbox['current'] = 0;

133 134 135 136
    if ($sandbox['total'] == 0) {
      $context['finished'] = 1;
      return;
    }
137 138
  }

139
  $results = array_slice($index_entries, $sandbox['current'], 100);
140

141 142
  $entity_type = '';
  $field = '';
143
  foreach ($results as $row) {
144
    views_natural_sort_store($row);
145
    ++$sandbox['progress'];
146
    $sandbox['current'] = $sandbox['progress'];
147 148 149
    $entity_type = $row['entity_type'];
    $field = $row['field'];
    ++$context['results']['entries'];
150 151
  }

152
  $context['message'] = t('Processing %entity_type %field', array('%entity_type' => $entity_type, '%field' => $field));
153 154 155 156 157 158 159 160 161 162 163

  $context['finished'] = $sandbox['progress'] / $sandbox['total'];
}

/**
 * Finished callback for rebuild_index batch.
 */
function views_natural_sort_rebuild_index_finished($success, $results, $operations) {
  if ($success) {
    drupal_set_message(t('Index update has completed.'));
    drupal_set_message(t('Indexed %count.', array(
164
      '%count' => format_plural($results['entries'], '1 entry', '@count entries'),
165 166 167
    )));
  }
}