Commit 3c2e1e7f authored by neclimdul's avatar neclimdul

Creating a natural sort module for views.

parents
<?php
// $Id$
/**
* @file
* Callbacks for managing Views Natural Sort.
*/
/**
* Form callback for Views Natural Sort settings 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() {
$form = array();
$form['rebuild'] = array(
'#type' => 'submit',
'#value' => t('Rebuild title index'),
'#submit' => array('views_natural_sort_rebuild_index_submit'),
);
return $form;
}
/**
* Submit handler that triggers the rebuild_index batch.
*/
function views_natural_sort_rebuild_index_submit() {
$batch = array(
'operations' => array(
array('views_natural_sort_rebuild_index', array()),
),
'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) {
// Alias sandbox for easier referencing.
$sandbox = &$context['sandbox'];
// Initialize our context.
if (!isset($sandbox['max'])) {
$sandbox['progress'] = 0;
$sandbox['max'] = db_result(db_query('SELECT MAX(nid) FROM {node}'));
$sandbox['total'] = db_result(db_query('SELECT COUNT(nid) FROM {node} WHERE nid < %d', $sandbox['max']));
$sandbox['current'] = 0;
$context['results']['nodes'] = 0;
}
$results = db_query_range('SELECT nid, title FROM {node} WHERE nid > %d AND nid < %d', $sandbox['current'], $sandbox['max'], 0, 10);
$title = '';
while($row = db_fetch_object($results)) {
_views_natural_sort_store_node($row);
++$sandbox['progress'];
$sandbox['current'] = $row->nid;
$title = $row->title;
++$context['results']['nodes'];
}
$context['message'] = t('Processing node %title', array('%title' => $title));
$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(
'%count' => format_plural($results['nodes'], '1 node', '@count nodes'),
)));
}
}
; $Id$
name = Views Natural Sort
description = Sort results naturaly on a text field skipping articles like "the" and "a."
dependencies[] = views
package = Views
core = 6.x
<?php
// $Id$
/**
* @file
*
*/
/**
* Implementation of hook_schema().
*/
function views_natural_sort_schema() {
// Contains relations between two users.
$schema['views_natural_sort'] = array(
'description' => t('Compressed titles for natural sorting.'),
'fields' => array(
'nid' => array(
'description' => t('Node id'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'field' => array(
'description' => t('The field name. This will be title or some cck text field, etc.'),
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'content' => array(
'description' => t('Filtered content used for sorting.'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
),
'key' => array('nid', 'field', 'content'),
'primary key' => array('nid', 'field'),
);
return $schema;
}
/**
* Implementation of hook_install().
*/
function views_natural_sort_install() {
drupal_install_schema('views_natural_sort');
}
/**
* Implementation of hook_uninstall().
*/
function views_natural_sort_uninstall() {
drupal_uninstall_schema('views_natural_sort');
}
<?php
// $Id$
/**
* @file
* Provides a views filter that sorts titles by a more natural manner by
* ignoring articles like "The" and "A."
*
* Normal sort:
* A Chorus Line
* All American
* Fiddler on the Roof
* Oklahoma!
* The King And I
*
* Natural sort:
* All American
* A Chorus Line
* Fiddler on the Roof
* The King And I
* Oklahoma!
*/
/**
* Implementation of hook_menu().
*/
function views_natural_sort_menu() {
//
$items = array();
$items['admin/settings/views_natural_sort'] = array(
'title' => 'Views Natural Sort',
'description' => 'Provides options for what should be indexed for sorting.',
'page callback' => 'drupal_get_form',
'page arguments' => array('views_natural_sort_settings_form'),
'access callback' => 'user_access',
'access arguments' => array('administer views'),
'file' => 'views_natural_sort.admin.inc',
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/**
* Implementation of hook_views_api().
*/
function views_natural_sort_views_api() {
return array(
'api' => 2.0,
);
}
/**
* Implementation of hook_nodeapi().
*
* This keeps our natural sort index up to date.
*/
function views_natural_sort_nodeapi(&$node, $op) {
//
switch ($op) {
case 'update':
case 'insert':
_views_natural_sort_store_node($node);
break;
}
}
/**
* Helper function for writing node data to our sort index.
*
* @param $node
* A drupal node object containing at least a nid and title.
*/
function _views_natural_sort_store_node($node) {
$record = new stdClass();
$record->nid = $node->nid;
$record->field = 'title';
$record->content = _views_natural_sort_filter_content($node->title);
// Try to update. On fail, try inserting.
$return = drupal_write_record('views_natural_sort', $record, array('nid', 'field'));
if (!db_affected_rows()) {
$return = drupal_write_record('views_natural_sort', $record);
}
return $return;
}
/**
* Helper function for filtering out what the value we should sort on is.
* @param $content
* Original content.
* @return
* Filtered value.
*/
function _views_natural_sort_filter_content($content) {
return preg_replace('/^(The|A)\s/i', '', $content);
}
<?php
// $Id$
/**
* @file
* Views related hooks.
*
* Automatically included by views. Because of the nature of this module this
* is most of the code.
*/
/**
* Implementation of hook_views_data().
*/
function views_natural_sort_views_data() {
$data['views_natural_sort']['table']['join'] = array(
'node' => array(
'left_field' => 'nid',
'field' => 'nid',
// We only support sorting on title so go ahead and build filter it into the join.
'extra' => array(
array(
'field' => 'field',
'value' => 'title',
'operator' => '=',
)
)
),
);
$data['views_natural_sort']['content'] = array(
'title' => 'Title - Natural',
'group' => t('Node'),
'help' => t('Sort title using a natural sort.'),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
return $data;
}
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