views_natural_sort.admin.inc 5.29 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  );

  $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);

  views_natural_sort_rebuild_index_submit();
}

79 80 81 82
/**
 * Submit handler that triggers the rebuild_index batch.
 */
function views_natural_sort_rebuild_index_submit() {
83 84 85 86 87 88 89 90 91 92 93 94
  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));
  }
95
  $batch = array(
96 97
    'operations' => $operations,
    'title' => t('Rebuilding Views Natural Sort Indexing Entries'),
98 99 100 101 102 103 104 105 106 107
    '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.
 */
108 109
function views_natural_sort_rebuild_index($entry_type, &$context) {
  dvm($entry_type);
110 111 112
  // Alias sandbox for easier referencing.
  $sandbox = &$context['sandbox'];

113 114
  // Hook for modules to implement and return data that views_natural_sort can
  // store as an index for that module's entries.
115
  $index_entries = module_invoke_all('views_natural_sort_get_rebuild_data', $entry_type);
116 117 118 119

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

120 121 122
  // Initialize our context.
  if (!isset($sandbox['max'])) {
    $sandbox['progress'] = 0;
123 124
    $sandbox['max'] = count($index_entries) - 1;
    $sandbox['total'] = count($index_entries);
125

126 127
    $sandbox['current'] = 0;

128 129 130 131
    if ($sandbox['total'] == 0) {
      $context['finished'] = 1;
      return;
    }
132 133
  }

134
  $results = array_slice($index_entries, $sandbox['current'], 100);
135

136 137
  $entity_type = '';
  $field = '';
138
  foreach ($results as $row) {
139
    views_natural_sort_store($row);
140
    ++$sandbox['progress'];
141
    $sandbox['current'] = $sandbox['progress'];
142 143 144
    $entity_type = $row['entity_type'];
    $field = $row['field'];
    ++$context['results']['entries'];
145 146
  }

147
  $context['message'] = t('Processing %entity_type %field', array('%entity_type' => $entity_type, '%field' => $field));
148 149 150 151 152 153 154 155 156 157 158

  $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(
159
      '%count' => format_plural($results['entries'], '1 entry', '@count entries'),
160 161 162
    )));
  }
}