Commit 1779ed5e authored by generalredneck's avatar generalredneck

[#1042242] Adding ability to Define words and letters to ignore

Added a form in Configure->Views Natural Sort->Configure Word Removal Lists
Allowed the ability for words specifically at the beginning of a phrase to be removed
Allowed the ability for words found anyehwere but the beginning of the phrase to be removed
Allowed for any symbol to be removed.
This all works globally.
parent 2d63dc90
......@@ -5,12 +5,12 @@
*/
/**
* Form callback for Views Natural Sort settings page..
* 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_settings_form() {
function views_natural_sort_rebuild_index_form() {
$form = array();
$form['rebuild'] = array(
......@@ -22,6 +22,60 @@ function views_natural_sort_settings_form() {
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.'),
);
$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. Those type of words go here.'),
);
$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 seperated.'),
);
$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();
}
/**
* Submit handler that triggers the rebuild_index batch.
*/
......
......@@ -46,6 +46,26 @@ function views_natural_sort_schema() {
* Implementation of 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', "#\"'\\()[]");
}
/**
......@@ -60,6 +80,9 @@ function views_natural_sort_enable() {
* Implementation of 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');
}
/**
......
......@@ -33,10 +33,20 @@ function views_natural_sort_menu() {
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/views_natural_sort/rebuild'] = array(
$items['admin/config/views_natural_sort/rebuild_index'] = array(
'title' => 'Rebuild Search Index',
'description' => 'Rebuild Views Natural Sort\'s search index',
'page callback' => 'drupal_get_form',
'page arguments' => array('views_natural_sort_rebuild_index_form'),
'access callback' => 'user_access',
'access arguments' => array('administer views'),
'file' => 'views_natural_sort.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/views_natural_sort/settings'] = array(
'title' => 'Configure Word Removal Lists',
'description' => 'Set what words should be ignored when performing a natural sort.',
'page callback' => 'drupal_get_form',
'page arguments' => array('views_natural_sort_settings_form'),
'access callback' => 'user_access',
'access arguments' => array('administer views'),
......@@ -118,10 +128,23 @@ function _views_natural_sort_store_node($node) {
* The encoded string
*/
function views_natural_sort_encode($string) {
$string = preg_replace(array(
'/^(The|A|An|El|La|Le|Il)\s+/i',
'/[\#\'\"\(\)]/',
'/\s(and|of|or)\s+/i',
$beginning_words = variable_get('views_natural_sort_beginning_words_remove', array());
$words = variable_get('views_natural_sort_words_remove', array());
$symbols = variable_get('views_natural_sort_symbols_remove', '');
// Get the words ready for being put in a regex.
array_walk($beginning_words, 'preg_quote');
array_walk($words, 'preg_quote');
$beginning_words_regex = '/^(' . implode('|', $beginning_words) . ')\s+/i';
$words_regex = '/\s(' . implode('|', $words) . ')\s+/i';
$symbols_regex = '/[' . preg_quote($symbols) . ']/';
$string = preg_replace(array(
$beginning_words_regex,
$symbols_regex,
$words_regex,
), array(
'',
'',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment