From e033192328a5ec3280c271a2f548f78a724523fe Mon Sep 17 00:00:00 2001 From: Scott Walters Date: Tue, 27 Jul 2010 18:42:31 -0400 Subject: [PATCH] StoryTopic 'Alphabetically' sort order --- docs/changelog/7.x.x.txt | 1 + docs/upgrades/upgrade_7.9.9-7.9.10.pl | 19 ++++++++++++ lib/WebGUI/Asset/Wobject/StoryTopic.pm | 21 ++++++++++--- lib/WebGUI/Keyword.pm | 13 ++++++-- t/Asset/Wobject/StoryTopic.t | 43 +++++++++++++++++++++++++- t/Keyword.t | 22 +++++++++++-- 6 files changed, 110 insertions(+), 9 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 559c5f8d3..0bdb6f3cf 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -8,6 +8,7 @@ - fixed #11742: linktag FilePump macro not xhtml valid - fixed #11744: Default DataForm list template does not contain pagination - fixed #11683: URL with UTF8 (for äÄöÖüÜß) + - added option for sort order of Alphabetically on title to StoryArchive, StoryTopic and Keyword 7.9.9 - fixed #11693: Shopping cart does not show for visitor user diff --git a/docs/upgrades/upgrade_7.9.9-7.9.10.pl b/docs/upgrades/upgrade_7.9.9-7.9.10.pl index 2c4d1e3e0..a9015b263 100644 --- a/docs/upgrades/upgrade_7.9.9-7.9.10.pl +++ b/docs/upgrades/upgrade_7.9.9-7.9.10.pl @@ -33,6 +33,7 @@ my $session = start(); # this line required # upgrade functions go here addSpamStopWordsToConfig($session); alterStoryArchiveTable($session); +alterStoryTopicTable($session); finish($session); # this line required @@ -74,6 +75,24 @@ sub alterStoryArchiveTable { print "DONE!\n" unless $quiet; } +#---------------------------------------------------------------------------- +# Describe what our function does +sub alterStoryTopicTable { + my $session = shift; + print "\tAdd story sort order column to the StoryTopic table... " unless $quiet; + + my $sth = $session->db->read('DESCRIBE `StoryTopic`'); + while (my ($col) = $sth->array) { + if ($col eq 'storySortOrder') { + print "Skipped.\n" unless $quiet; + return; + } + } + + $session->db->write("ALTER TABLE StoryTopic ADD COLUMN storySortOrder CHAR(22)"); + $session->db->write("UPDATE StoryTopic SET storySortOrder = 'Chronologically' WHERE storySortOrder IS NULL"); + print "DONE!\n" unless $quiet; +} # -------------- DO NOT EDIT BELOW THIS LINE -------------------------------- diff --git a/lib/WebGUI/Asset/Wobject/StoryTopic.pm b/lib/WebGUI/Asset/Wobject/StoryTopic.pm index 48474ca3f..7706ef506 100644 --- a/lib/WebGUI/Asset/Wobject/StoryTopic.pm +++ b/lib/WebGUI/Asset/Wobject/StoryTopic.pm @@ -37,6 +37,7 @@ sub definition { my $session = shift; my $definition = shift; my $i18n = WebGUI::International->new($session, 'Asset_StoryTopic'); + my $other_i18n = WebGUI::International->new($session, 'Asset_StoryArchive'); my %properties; tie %properties, 'Tie::IxHash'; %properties = ( @@ -72,6 +73,17 @@ sub definition { namespace => 'Story', defaultValue => 'TbDcVLbbznPi0I0rxQf2CQ', }, + storySortOrder => { + fieldType => "selectBox", + tab => 'display', + defaultValue => 'Chronologically', + options => { + Alphabetically => $other_i18n->get('alphabetically'), + Chronologically => $other_i18n->get('chronologically') + }, + label => $other_i18n->get('sortAlphabeticallyChronologically'), + hoverHelp => $other_i18n->get('sortAlphabeticallyChronologically description'), + }, ); push(@{$definition}, { assetName=>$i18n->get('assetName'), @@ -174,10 +186,11 @@ sub viewTemplateVariables { my $wordList = WebGUI::Keyword::string2list($self->get('keywords')); my $key = WebGUI::Keyword->new($session); my $p = $key->getMatchingAssets({ - keywords => $wordList, - isa => 'WebGUI::Asset::Story', - usePaginator => 1, - rowsPerPage => $numberOfStories, + sortOrder => $self->get('storySortOrder') || 'Chronologically', + keywords => $wordList, + isa => 'WebGUI::Asset::Story', + usePaginator => 1, + rowsPerPage => $numberOfStories, }); my $storyIds = $p->getPageData(); $var->{story_loop} = []; diff --git a/lib/WebGUI/Keyword.pm b/lib/WebGUI/Keyword.pm index 66f5e84ae..e27aaf6ed 100644 --- a/lib/WebGUI/Keyword.pm +++ b/lib/WebGUI/Keyword.pm @@ -367,9 +367,18 @@ sub getMatchingAssets { push @clauses, 'keyword in ('.join(',', @placeholders).')'; } + my $sortOrder = $options->{sortOrder} || 'Chronologically'; + + my $orderBy = $sortOrder eq 'Alphabetically' ? ' order by upper(title), lineage' : ' order by creationDate desc, lineage'; + # write the query - my $query = 'select distinct assetKeyword.assetId from assetKeyword left join asset using (assetId) - where '.join(' and ', @clauses).' order by creationDate desc, lineage'; + my $query = q{ + select distinct assetKeyword.assetId + from assetKeyword + left join asset using (assetId) + left join assetData using (assetId) + where } . + join(' and ', @clauses) . $orderBy; # perform the search if ($options->{usePaginator}) { diff --git a/t/Asset/Wobject/StoryTopic.t b/t/Asset/Wobject/StoryTopic.t index 8fa048afa..f1fd0936a 100644 --- a/t/Asset/Wobject/StoryTopic.t +++ b/t/Asset/Wobject/StoryTopic.t @@ -31,7 +31,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -plan tests => 18; +plan tests => 19; #---------------------------------------------------------------------------- # put your tests here @@ -316,3 +316,44 @@ cmp_deeply( ], 'rssFeedItems' ); + +################################################################ +# Sort Order +################################################################ + +$pastStory->update( { title => "aaaay was history but isn't any more" } ); +$pastStory->requestAutoCommit; + +$topic->update({ storiesPer => 4, storiesShort => 4, }); # storiesPer is used when _standAlone is true, storiesShort otherwise +$topic->{_standAlone} = 0; +$topic->update( { storySortOrder => 'Alphabetically' } ); + +$templateVars = $topic->viewTemplateVariables(); + +cmp_deeply( + $templateVars->{story_loop}, + [ + { + title => "aaaay was history but isn't any more", + url => ignore(), + creationDate => $yesterday, + }, + { + title => 'andy', + url => ignore(), + creationDate => $now, + }, + { + title => 'bogs', + url => ignore(), + creationDate => $now, + }, + { + title => 'brooks', + url => ignore(), + creationDate => $now, + }, + ], + 'viewTemplateVars has right number and contents in the story_loop in sort order Alphabetically mode' +); + diff --git a/t/Keyword.t b/t/Keyword.t index 4f2fd5687..1581f7920 100644 --- a/t/Keyword.t +++ b/t/Keyword.t @@ -17,7 +17,7 @@ use WebGUI::Keyword; use WebGUI::Asset; # load your modules here -use Test::More tests => 15; # increment this value for each test you create +use Test::More tests => 16; # increment this value for each test you create use Test::Deep; use Data::Dumper; @@ -76,10 +76,28 @@ my $assetIds = $keyword->getMatchingAssets({ keyword => 'webgui', }); cmp_deeply( $assetIds, - [$snippet->getId, $home->getId, ], + [ $snippet->getId, $home->getId, ], 'getMatchingAssets, by keyword, assetIds in order by creationDate, descending' ); +# sorted by title, alphabetically + +my $aa_story = $home->addChild({ className => 'WebGUI::Asset::Story', title => "aaaa", keywords => 'webgui' }); +WebGUI::Test->addToCleanup($aa_story); + +$assetIds = $keyword->getMatchingAssets({ keyword => 'webgui', sortOrder => 'Alphabetically', }); + +cmp_deeply( + $assetIds, + [ $aa_story->getId, $home->getId, $snippet->getId, ], # 'aaa', 'Home', 'keyword snippet' + 'getMatchingAssets, by keyword, assetIds in order by title' +); + +$aa_story->trash(); +$aa_story->purge(); + +# trashed assets + $snippet->trash(); cmp_deeply(