Commit 6e05af69 authored by generalredneck's avatar generalredneck

[#1614234] Moving text field specific code to it's own module. Start on...

[#1614234] Moving text field specific code to it's own module. Start on Refactor of Rebuild functionality. NOT WORKING CONDITION.
parent 8ff9e52c
......@@ -98,17 +98,21 @@ function views_natural_sort_rebuild_index(&$context) {
// Alias sandbox for easier referencing.
$sandbox = &$context['sandbox'];
// Hook for modules to implement and return data that views_natural_sort can
// store as an index for that module's entries.
$index_entries = module_invoke_all('views_natural_sort_get_rebuild_data');
//TODO: this seems like it has the possiblity to produce unused data.
// Consider a delete of some sort.
// Initialize our context.
if (!isset($sandbox['max'])) {
$sandbox['progress'] = 0;
$sandbox['max'] = db_query('SELECT MAX(nid) FROM {node}')->fetchField();
$sandbox['total'] = db_query(
'SELECT COUNT(nid) FROM {node} WHERE nid <= :max',
array('max' => $sandbox['max'])
)->fetchField();
$sandbox['max'] = count($index_entries) - 1;
$sandbox['total'] = count($index_entries);
$sandbox['current'] = 0;
$context['results']['nodes'] = 0;
$context['results']['entries'] = 0;
if ($sandbox['total'] == 0) {
$context['finished'] = 1;
......@@ -116,23 +120,20 @@ function views_natural_sort_rebuild_index(&$context) {
}
}
$results = db_query_range(
'SELECT nid, title FROM {node} WHERE nid > :current AND nid <= :max',
0,
10,
array('current' => $sandbox['current'], 'max' => $sandbox['max'])
);
$results = array_slice($index_entries, $sandbox['current'], 10);
$title = '';
$entity_type = '';
$field = '';
foreach ($results as $row) {
_views_natural_sort_store_node($row);
views_natural_sort_store($row);
++$sandbox['progress'];
$sandbox['current'] = $row->nid;
$title = $row->title;
++$context['results']['nodes'];
$entity_type = $row['entity_type'];
$field = $row['field'];
++$context['results']['entries'];
}
$context['message'] = t('Processing node %title', array('%title' => $title));
$context['message'] = t('Processing %entity_type %field', array('%entity_type' => $entity_type, '%field' => $field));
$context['finished'] = $sandbox['progress'] / $sandbox['total'];
}
......@@ -144,7 +145,7 @@ function views_natural_sort_rebuild_index_finished($success, $results, $operatio
if ($success) {
drupal_set_message(t('Index update has completed.'));
drupal_set_message(t('Indexed %count.', array(
'%count' => format_plural($results['nodes'], '1 node', '@count nodes'),
'%count' => format_plural($results['entries'], '1 entry', '@count entries'),
)));
}
}
name = Views Natural Sort
description = Sort results naturaly on a text field skipping articles like "the" and "a."
description = Sort results naturaly on a node's title skipping articles like "the" and "a."
dependencies[] = views
package = Views
core = 7.x
; Views handlers
files[] = handlers/views_natural_sort_handler_sort.inc
files[] = handlers/views_natural_sort_handler_sort_text_field.inc
......@@ -57,80 +57,44 @@ function views_natural_sort_menu() {
return $items;
}
/**
* Implementation of hook_views_api().
*/
function views_natural_sort_views_api() {
return array(
'api' => 2.0,
);
}
/**
* Implements hook_node_insert().
*
* This keeps our natural sort index up to date.
*/
function views_natural_sort_node_insert($node) {
_views_natural_sort_store_node($node);
}
/**
* Implementation of hook_node_update().
*
* This keeps our natural sort index up to date.
*/
function views_natural_sort_node_update($node) {
_views_natural_sort_store_node($node);
}
/**
* Implementation of hook_node_delete().
*
* This keep sour natural sort index clean.
*/
function views_natural_sort_node_delete($node) {
_views_natural_sort_remove_node($node);
}
/**
* Helper function for writing node data to our sort index.
*
* @param $node
* A drupal node object containing at least a nid and title.
*
* @return int
* MergeQuery::STATUS_UPDATE or MergeQuery::STATUS_INSERT
*/
function _views_natural_sort_store_node($node) {
function views_natural_sort_store($index_entry) {
//This should take a formatted object and store it into the views_natural_sort table.
return db_merge('views_natural_sort')
->key(array(
'nid' => $node->nid,
'field' => 'title'
'eid' => $index_entry['eid'],
'entity_type' => $index_entry['entity_type'],
'field' => $index_entry['field'],
'delta' => $index_entry['delta'],
))
->fields(array(
'nid' => $node->nid,
'field' => 'title',
'content' => views_natural_sort_encode($node->title)
'eid' => $index_entry['eid'],
'entity_type' => $index_entry['entity_type'],
'field' => $index_entry['field'],
'delta' => $index_entry['delta'],
'content' => views_natural_sort_encode($index_entry),
))
->execute();
}
/**
* Helper function for removing node data from our sort index.
*
* @param $node
* A drupal node object containing at least a nid.
*
* @return int
* The number of rows deleted.
*/
function _views_natural_sort_remove_node($node) {
function views_natural_sort_remove($index_entry) {
//TODO: Make sure all the keys are in the index_entry.
return db_delete('views_natural_sort')
->condition('nid', $node->nid)
->condition('eid', $index_entry['eid'])
->condition('entity_type', $index_entry['entity_type'])
->condition('field', $index_entry['field'])
->condition('delta', $index_entry['delta'])
->execute();
}
/**
* Implementation of hook_views_api().
*/
function views_natural_sort_views_api() {
return array(
'api' => 2.0,
);
}
/**
* Encodes a string into an ascii-sortable such:
* - Leading articles in common languages are ingored: The A An El La Le Il
......@@ -152,53 +116,88 @@ function _views_natural_sort_remove_node($node) {
* @return string
* The encoded string
*/
function views_natural_sort_encode($string) {
function views_natural_sort_encode($index_entry) {
$words = variable_get('views_natural_sort_words_remove', array());
$beginning_words = variable_get('views_natural_sort_beginning_words_remove', array());
// Get copy the original string.
$string = $index_entry['content'];
$symbols = variable_get('views_natural_sort_symbols_remove', '');
foreach (views_natural_sort_get_transformations($index_entry) as $transformation_method) {
$string = $transformation_method($string);
}
// Get the words ready for being put in a regex.
array_walk($beginning_words, 'preg_quote');
array_walk($words, 'preg_quote');
return $string;
}
$regex = array();
$replace = array();
function views_natural_sort_get_transformations($index_entry) {
$transformations = array(
'views_natural_sort_remove_beginning_words',
'views_natural_sort_remove_words',
'views_natural_sort_remove_symbols',
'views_natural_sort_numbers',
);
// Remove words from the beginning only!
if (!empty($beginning_words)) {
$regex[] = '/^(' . implode('|', $beginning_words) . ')\s+/i';
$replace[]='';
}
// Remove words reguardless where they are as long as they are a word.
if (!empty($words)) {
$regex[] = '/\s(' . implode('|', $words) . ')\s+/i';
$replace[] = ' ';
$regex[] = '/^(' . implode('|', $words) . ')\s+/i';
$replace[] = '';
//Allow other modules to modify the transformation that happens here if needed.
drupal_alter('views_ntural_sort_transformations', $transformations, $index_entry);
return $transformations;
}
function views_natural_sort_remove_beginning_words($string) {
$beginning_words = variable_get('views_natural_sort_beginning_words_remove', array());
if (empty($beginning_words)) {
return $string;
}
// Remove symbols.
if (strlen($symbols) != 0) {
$regex[] = '/[' . preg_quote($symbols) . ']/';
$replace[] = '';
array_walk($beginning_words, 'preg_quote');
return preg_replace(
'/^(' . implode('|', $beginning_words) . ')\s+/i',
'',
$string
);
}
function views_natural_sort_remove_words($string) {
$words = variable_get('views_natural_sort_words_remove', array());
if (empty($words)) {
return $string;
}
if (!empty($regex) && !empty($replace)) {
$string = preg_replace($regex, $replace, $string);
array_walk($words, 'preg_quote');
return preg_replace(
array(
'/\s(' . implode('|', $words) . ')\s+/i',
'/^(' . implode('|', $words) . ')\s+/i',
),
array(
' ',
''
),
$string
);
}
function views_natural_sort_remove_symbols($string) {
$symbols = variable_get('views_natural_sort_symbols_remove', '');
if (strlen($symbols) == 0) {
return $string;
}
return preg_replace(
'/[' . preg_quote($symbols) . ']/',
'',
$string
);
}
function views_natural_sort_numbers($string) {
// Find an optional leading dash (either preceded by whitespace or the first character) followed
// by either:
// - an optional series of digits (with optional imbedded commas), then a period, then an optional series of digits OR
// - a series of digits (with optional imbedded commas)
$string = preg_replace_callback(
return preg_replace_callback(
'/(\s-|^-)?(?:(\d[\d,]*)?\.(\d+)|(\d[\d,]*))/',
'_views_natural_sort_number_encode_match_callback',
$string
);
return $string;
}
/**
......@@ -235,3 +234,83 @@ function _views_natural_sort_number_encode_match_callback($match) {
}
return $encode;
}
function views_natural_sort_views_natural_sort_get_rebuild_data(){
$result = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->execute();
$data = array();
foreach ($result as $row ) {
$data[] = _views_natural_sort_node_to_vns($node);
}
return $data;
}
/**
* Implements hook_node_insert().
*
* This keeps our natural sort index up to date.
*/
function views_natural_sort_node_insert($node) {
_views_natural_sort_store_node($node);
}
/**
* Implementation of hook_node_update().
*
* This keeps our natural sort index up to date.
*/
function views_natural_sort_node_update($node) {
_views_natural_sort_store_node($node);
}
/**
* Implementation of hook_node_delete().
*
* This keep sour natural sort index clean.
*/
function views_natural_sort_node_delete($node) {
_views_natural_sort_remove_node($node);
}
/**
* Helper function for writing node data to our sort index.
*
* @param $node
* A drupal node object containing at least a nid and title.
*
* @return int
* MergeQuery::STATUS_UPDATE or MergeQuery::STATUS_INSERT
*/
function _views_natural_sort_store_node($node) {
views_natural_sort_store(_views_natural_sort_node_to_vns($node));
}
function _views_natural_sort_node_to_vns($node) {
return array(
'eid' => $node->nid,
'entity_type' => 'node',
'field' => 'title',
'delta' => 0,
'content' => $node->title,
);
}
/**
* Helper function for removing node data from our sort index.
*
* @param $node
* A drupal node object containing at least a nid.
*
* @return int
* The number of rows deleted.
*/
function _views_natural_sort_remove_node($node) {
return views_natural_sort_remove(array(
'eid' => $node->nid,
'entity_type' => 'node',
'field' => 'title',
'delta' => 0,
));
}
......@@ -11,27 +11,3 @@ function views_natural_sort_views_data_alter(&$data) {
$data['node']['title']['sort']['handler'] = 'views_natural_sort_handler_sort';
}
function views_natural_sort_field_views_data_alter(&$data, $field, $module) {
// TODO: check to see if field is "registered" to be sort naturally.
if ($module != 'text' || $field['storage']['type'] != 'field_sql_storage') {
return;
}
foreach ($field['bundles'] as $entity_type => $bundles) {
$entity_info = entity_get_info($entity_type);
$field_table = _field_sql_storage_tablename($field);
$view_fields = array();
foreach($field['columns'] as $column_name => $column_data) {
// See field_views_field_default_views_data. it says only varchar is worth sorting.
if ($column_data['type'] != 'varchar') {
continue;
}
// Stash the field names so we can join on them later.
$current_view_field_column = $field['field_name'] . '_' . $column_name;
$view_fields[] = $current_view_field_column;
$data[$field_table][$current_view_field_column]['sort']['handler'] = 'views_natural_sort_handler_sort_text_field';
}
}
}
name = Views Natural Sort Text Field Support
description = Sort results naturaly on a text field skipping articles like "the" and "a."
dependencies[] = views_natural_sort
package = Views
core = 7.x
; Views handlers
files[] = handlers/views_natural_sort_handler_sort_text_field.inc
<?php
function views_natural_sort_text_field_enable() {
module_load_include('inc', 'views_natural_sort', 'views_natural_sort.admin');
views_natural_sort_rebuild_index_submit();
}
<?php
function views_natural_sort_text_field_views_api() {
return array(
'api' => 2.0,
);
}
function views_natural_sort_text_field_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
$form['field']['views_natural_sort'] = array(
'#type' => 'checkbox',
'#title' => t('Enable natural sorting.'),
'#description' => t('Selecting this option will process and index this field\'s value for natural sorting in views.'),
'#default_value' => 0,
);
}
<?php
function views_natural_sort_text_field_field_views_data_alter(&$data, $field, $module) {
// TODO: check to see if field is "registered" to be sort naturally.
if ($module != 'text' || $field['storage']['type'] != 'field_sql_storage' || $field['columns']['value']['type'] != 'varchar') {
return;
}
$field_table = _field_sql_storage_tablename($field);
$data[$field_table][$field['field_name'] . '_value']['sort']['handler'] = 'views_natural_sort_handler_sort_text_field';
dvm($data[$field_table]);
}
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