Commit 92da2ce6 authored by generalredneck's avatar generalredneck

adding Natural Sorting for days of the week.

parent bd86d42e
......@@ -50,7 +50,12 @@ function views_natural_sort_settings_form() {
'#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['save'] = array(
'#type' => 'submit',
'#value' => t('Save Settings'),
......@@ -72,6 +77,7 @@ function views_natural_sort_settings_form_submit($form, &$form_state) {
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']);
views_natural_sort_rebuild_index_submit();
}
......
......@@ -107,3 +107,73 @@ function _views_natural_sort_number_transform_match_callback($match) {
}
return $encode;
}
/**
* Simply convert days of the week over to numbers.
*
* This "should" be multilingual safe.
*/
function views_natural_sort_days_of_the_week_sort_days($string) {
global $language;
// Adding a configuration so that in the future an admin page can be made
// to switch which day is the first day of the week based on local.
$used_language = isset($language->language) ? $language->language : 'en';
$first_day = variable_get('views_natural_sort_days_of_the_week_first_day_' . $used_language, "Sunday");
$day_list = views_natural_sort_days_of_the_week_get_default_days();
$sorted_days = $day_list;
// Go through list and resort it and Translate it.
$start = array_search($first_day, $day_list);
for ($i=0; $i<7; $i++) {
$current_day = ($i+$start)%7;
$abbreviations = views_natural_sort_days_of_the_week_get_acceptable_day_abbreviations($day_list[$current_day], $used_language);
$translated_day = t($day_list[$current_day]);
$string = preg_replace(
array(
'/\b' . $translated_day . '\b/i',
'/\b(' . implode(views_natural_sort_days_of_the_week_get_acceptable_day_abbreviations($day_list[$current_day]), '\.?|') . ')\b/i'
),
' ' . $i .' ',
$string
);
}
return $string;
}
function views_natural_sort_days_of_the_week_get_default_days() {
return array (
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
);
}
function views_natural_sort_days_of_the_week_get_acceptable_day_abbreviations($day, $lang = 'en') {
$default_days = views_natural_sort_days_of_the_week_get_default_days();
$index = array_search($day,$default_days);
$default_abbrev = views_natural_sort_days_of_the_week_get_default_day_abbreviations();
return variable_get(
'views_natural_sort_days_of_the_week_' . $lang . '_' . $day,
$default_abbrev[$index]
);
}
function views_natural_sort_days_of_the_week_get_default_day_abbreviations() {
return array (
array("Sun"),
array("Mon"),
array("Tu", "Tue", "Tues"),
array("Wed"),
array("Th", "Thu", "Thur", "Thurs"),
array("Fri"),
array("Sat"),
);
}
......@@ -44,8 +44,8 @@ function views_natural_sort_menu() {
'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.',
'title' => 'Configure Natural Sort Settings',
'description' => 'Set the settings for how particular transformations should behave.',
'page callback' => 'drupal_get_form',
'page arguments' => array('views_natural_sort_settings_form'),
'access callback' => 'user_access',
......@@ -246,7 +246,12 @@ function views_natural_sort_get_transformations($index_entry) {
'views_natural_sort_remove_symbols',
'views_natural_sort_numbers',
);
//Allow other modules to modify the transformation that happens here if needed.
if (variable_get('views_natural_sort_days_of_the_week_enabled', FALSE)) {
$transformations[] = "views_natural_sort_days_of_the_week_sort_days";
}
// Allow other modules to modify the transformation that happens here if
// needed.
drupal_alter('views_natural_sort_transformations', $transformations, $index_entry);
return $transformations;
}
<?php
class ViewsNaturalSortTest extends DrupalUnitTestCase {
class ViewsNaturalSortDaysOfTheWeekTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Views Natural Sort Tests',
'description' => 'Test that Views Natural Sort Module works correctly.',
'group' => 'VNS',
'name' => 'Views Natural Sort Days of the Week',
'description' => 'Tests Days of the Week Sorting',
'group' => 'Views Natural Sort',
);
}
public function testNodeAdd(){
$node = new StdClass();
$node->type = 'article';
node_object_prepare($node);
$node->title = 'All American';
$node->status = 1;
node_save($node);
$this->assertNotNull($node->nid, 'The test node did not save properly.');
$vns_entry = db_select('views_natural_sort','vns')
->fields('vns')
->condition('eid', $node->nid)
->execute()
->fetchAssoc();
$this->assertEqual($vns_entry, array(
'eid' => $node->nid,
'entity_type' => 'node',
'field' => 'title',
'delta' => '0',
'content' => 'All American'
));
}
public function testNodeRemove(){
$this->assertTrue(FALSE);
}
public function testNodeChange(){
$this->assertTrue(FALSE);
}
public function testRemoveBeginningWordsTransformation(){
$this->assertTrue(FALSE);
}
public function testRemoveWordsTransformation(){
$this->assertTrue(FALSE);
}
public function testNumberTransfomration(){
$this->assertTrue(FALSE);
function testDefaultDayReplace() {
variable_set('views_natural_sort_days_of_the_week_enabled', TRUE);
foreach(views_natural_sort_days_of_the_week_get_default_days() as $replaced => $day) {
$this->assertEqual(views_natural_sort_days_of_the_week_sort_days($day), $replaced);
}
}
public function testViewSort(){
$this->assertTrue(FALSE);
function testFirstDayMonday() {
variable_set('views_natural_sort_days_of_the_week_enabled', TRUE);
$variable_original = variable_get("views_natural_sort_days_of_the_week_first_day_en", "Sunday");
variable_set("views_natural_sort_days_of_the_week_first_day_en", "Monday");
db_set_active("default");
$expected = array(
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
);
foreach($expected as $replaced => $day) {
$this->assertEqual(views_natural_sort_days_of_the_week_sort_days($day), $replaced);
}
variable_set("views_natural_sort_days_of_the_week_first_day_en", $variable_original);
}
function testAbbreviations() {
variable_set('views_natural_sort_days_of_the_week_enabled', TRUE);
$expected = array(
"Sun",
"Mon.",
"Tues",
"Wed",
"Thur",
"Fri.",
"Sat",
);
foreach($expected as $replaced => $day) {
$this->assertEqual(views_natural_sort_days_of_the_week_sort_days($day), $replaced);
}
}
}
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