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

7 8 9
/**
 * Views Natural Sort Admin Settings Page callback.
 */
10 11 12 13 14 15 16 17
function views_natural_sort_settings_page() {
  $content = array(
    'settings' => drupal_get_form('views_natural_sort_settings_form'),
    'rebuild' => drupal_get_form('views_natural_sort_rebuild_index_form'),
  );
  return $content;
}

18
/**
19
 * Form callback for Views Natural Sort Rebuild Index page.
20 21 22 23
 *
 * Allows rebuilding index but should also allow things like limiting what node
 * types are indexed and cck text field index options.
 */
24
function views_natural_sort_rebuild_index_form() {
25 26 27
  $form = array();

  $form['rebuild'] = array(
28 29 30 31 32 33 34 35 36 37 38
    '#type' => 'fieldset',
    '#title' => t('Incase of Emergency'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,

    'button' => array(
      '#type' => 'submit',
      '#description' => 'Incase of an emergency.',
      '#value' => t('Rebuild Index'),
      '#submit' => array('views_natural_sort_rebuild_index_submit'),
    ),
39 40 41 42 43
  );

  return $form;
}

44 45 46 47 48 49 50 51 52 53 54 55
/**
 * 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())),
56
    '#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.'),
57 58 59 60 61 62
  );

  $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())),
63
    '#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.'),
64 65 66 67 68 69
  );

  $form['symbols'] = array(
    '#type' => 'textfield',
    '#title' => 'Symbols to filter from anywhere in a phrase',
    '#default_value' => variable_get('views_natural_sort_symbols_remove', ''),
70
    '#description' => t('Most symbols are ignored when performing a sort naturally. Those symbols you want ignored go here. Do not use a separator. EX: &$".'),
71
  );
72 73 74 75 76 77
  $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),
  );
78 79 80 81 82
  $form['batch_items'] = array(
    '#type' => 'textfield',
    '#title' => 'Items per Batch',
    '#default_value' => variable_get('views_natural_sort_rebuild_items_per_batch', '500'),
    '#description' => t('The number of items a batch process will work through at a given time. Raising this number will make the batch go quicker, however, raising it too high can cause timeouts and/or memory limit errors.'),
83
    '#element_validate' => array('element_validate_integer_positive'),
84
  );
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  $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);
106
  variable_set('views_natural_sort_days_of_the_week_enabled', $form_state['values']['days_of_the_week_enabled']);
107
  variable_set('views_natural_sort_rebuild_items_per_batch', $form_state['values']['batch_items']);
108 109 110
  views_natural_sort_rebuild_index_submit();
}

111 112 113 114
/**
 * Submit handler that triggers the rebuild_index batch.
 */
function views_natural_sort_rebuild_index_submit() {
115 116 117 118 119 120 121 122 123 124 125 126
  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));
  }
127
  $batch = array(
128 129
    'operations' => $operations,
    'title' => t('Rebuilding Views Natural Sort Indexing Entries'),
130 131 132 133 134 135 136 137 138 139
    '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.
 */
140
function views_natural_sort_rebuild_index($entry_type, &$context) {
141 142 143 144 145
  // Alias sandbox for easier referencing.
  $sandbox = &$context['sandbox'];

  // Initialize our context.
  if (!isset($sandbox['max'])) {
146 147 148
    // Hook for modules to implement and return data that views_natural_sort can
    // store as an index for that module's entries.
    $sandbox['entries'] = module_invoke_all('views_natural_sort_get_rebuild_data', $entry_type);
149
    $sandbox['progress'] = 0;
150 151
    $sandbox['max'] = count($sandbox['entries']) - 1;
    $sandbox['total'] = count($sandbox['entries']);
152

153 154
    $sandbox['current'] = 0;

155 156 157 158
    if ($sandbox['total'] == 0) {
      $context['finished'] = 1;
      return;
    }
159 160
  }

161
  $results = array_slice($sandbox['entries'], $sandbox['current'], variable_get('views_natural_sort_rebuild_items_per_batch', '500'));
162

163 164
  $entity_type = '';
  $field = '';
165
  foreach ($results as $row) {
166
    views_natural_sort_store($row);
167
    ++$sandbox['progress'];
168
    $sandbox['current'] = $sandbox['progress'];
169 170 171
    $entity_type = $row['entity_type'];
    $field = $row['field'];
    ++$context['results']['entries'];
172 173
  }

174
  $context['message'] = t('Processing %entity_type %field', array('%entity_type' => $entity_type, '%field' => $field));
175 176 177 178 179 180 181 182 183 184 185

  $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(
186
      '%count' => format_plural($results['entries'], '1 entry', '@count entries'),
187 188 189
    )));
  }
}