summaryrefslogtreecommitdiffstats
path: root/views_natural_sort.admin.inc
blob: 628f21893319167ac76eb17b8eb16236a7805220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

/**
 * @file
 * Callbacks for managing Views Natural Sort.
 */

/**
 * Views Natural Sort Admin Settings Page callback.
 */
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;
}

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

  $form['rebuild'] = array(
    '#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'),
    ),
  );

  return $form;
}

/**
 * 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())),
    '#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.'),
  );

  $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())),
    '#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.'),
  );

  $form['symbols'] = array(
    '#type' => 'textfield',
    '#title' => 'Symbols to filter from anywhere in a phrase',
    '#default_value' => variable_get('views_natural_sort_symbols_remove', ''),
    '#description' => t('Most symbols are ignored when performing a sort naturally. Those symbols you want ignored go here. Do not use a separator. EX: &$".'),
  );
  $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),
  );
  $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.'),
    '#element_validate' => array('element_validate_integer_positive'),
  );
  $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);
  variable_set('views_natural_sort_days_of_the_week_enabled', $form_state['values']['days_of_the_week_enabled']);
  variable_set('views_natural_sort_rebuild_items_per_batch', $form_state['values']['batch_items']);
  views_natural_sort_rebuild_index_submit();
}

/**
 * Submit handler that triggers the rebuild_index batch.
 */
function views_natural_sort_rebuild_index_submit() {
  views_natural_sort_rebuild_index_batch_set();
}

/**
 * Sets up the batch job for reindexing all or specified VNS entry types.
 */
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');
  }

  foreach ($entry_types as $entry_type) {
    // Queue up all the data that needs to be rebuilt.
    module_invoke_all('views_natural_sort_queue_rebuild_data', $entry_type);
  }

  // Run the queue.
  $batch = array(
    'operations' => array(array('views_natural_sort_rebuild_index', array())),
    'title' => t('Rebuilding Views Natural Sort Indexing Entries'),
    '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.
 */
function views_natural_sort_rebuild_index(&$context) {
  $queue = views_natural_sort_get_queue();
  // Alias sandbox for easier referencing.
  $sandbox = &$context['sandbox'];
  // Alias results for easier referencing.
  $results = &$context['results'];
  if (empty($sandbox)) {
    $sandbox['current'] = 0;
    $sandbox['max'] = $queue->numberOfItems();
    $sandbox['items_per_batch'] = variable_get('views_natural_sort_rebuild_items_per_batch', '500');
  }
  for ($i = 0; $i < $sandbox['items_per_batch'] && $sandbox['current'] < $sandbox['max']; $i++) {
    $item = $queue->claimItem(10);
    if ($item) {
      views_natural_sort_store($item->data);
      $queue->deleteItem($item);
    }
    $sandbox['current']++;
  }
  $results['entries'] = $sandbox['current'];
  if ($sandbox['current'] != $sandbox['max']) {
    $context['finished'] = $sandbox['current'] / $sandbox['max'];
  }
}

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