| 1 |
package WebGUI::Macro::RecentThreads; |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
use strict; |
|---|
| 14 |
use WebGUI::Asset::Template; |
|---|
| 15 |
use WebGUI::Asset::Wobject::Collaboration; |
|---|
| 16 |
|
|---|
| 17 |
=head1 NAME |
|---|
| 18 |
|
|---|
| 19 |
Package WebGUI::Macro::RecentPosts |
|---|
| 20 |
|
|---|
| 21 |
=head1 DESCRIPTION |
|---|
| 22 |
|
|---|
| 23 |
Macro for displaying the latest X posts in one or more collaboration systems. |
|---|
| 24 |
|
|---|
| 25 |
=head2 process ( assetId,templateId [,numPosts,orderByField,descendingOrder,summaryField,dateField] ) |
|---|
| 26 |
|
|---|
| 27 |
process takes 2 required parameters and 5 optional ones. |
|---|
| 28 |
|
|---|
| 29 |
=head3 assetId |
|---|
| 30 |
|
|---|
| 31 |
The assetId of the parent of the collaboration system(s) from which recent |
|---|
| 32 |
posts should be pulled. If only pulling from one CS, you can specify its assetId directly. |
|---|
| 33 |
|
|---|
| 34 |
=head3 templateId |
|---|
| 35 |
|
|---|
| 36 |
The templateId of the template the recent posts should be displayed in. |
|---|
| 37 |
|
|---|
| 38 |
=head3 numPosts |
|---|
| 39 |
|
|---|
| 40 |
The number of recent posts to pull; default is 3. |
|---|
| 41 |
|
|---|
| 42 |
=head3 orderByVar |
|---|
| 43 |
|
|---|
| 44 |
The name of the *template* variable to order the posts by; default is none. |
|---|
| 45 |
(i.e. 'post_url', 'post_title', 'post_epoch_date', 'post_date' or 'post_username') |
|---|
| 46 |
|
|---|
| 47 |
=head3 descendingOrder |
|---|
| 48 |
|
|---|
| 49 |
If set to a true value (i.e. 1), posts will be in descending order. |
|---|
| 50 |
|
|---|
| 51 |
=head3 summaryField |
|---|
| 52 |
|
|---|
| 53 |
The name of the field to use for the summary; default is none. |
|---|
| 54 |
|
|---|
| 55 |
=head3 dateField |
|---|
| 56 |
|
|---|
| 57 |
The name of the field to get the date of the post from; default is dateSubmitted. |
|---|
| 58 |
|
|---|
| 59 |
=cut |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
sub process { |
|---|
| 63 |
my $session = shift; |
|---|
| 64 |
my $parentAssetId = shift; |
|---|
| 65 |
my $templateId = shift; |
|---|
| 66 |
my $numPosts = shift; |
|---|
| 67 |
$numPosts ||= 3; |
|---|
| 68 |
my $orderByVar = shift; |
|---|
| 69 |
my $descendingOrder = shift; |
|---|
| 70 |
my $summaryField = shift; |
|---|
| 71 |
my $dateField = shift; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
$dateField ||= 'revisionDate'; |
|---|
| 75 |
|
|---|
| 76 |
my %var; |
|---|
| 77 |
my @postLoop; |
|---|
| 78 |
my $cs = WebGUI::Asset->newByDynamicClass($session,$parentAssetId); |
|---|
| 79 |
my $threads = $cs->getLineage(["descendants"],{returnObjects=>1}); |
|---|
| 80 |
foreach my $thread (@$threads) { |
|---|
| 81 |
my $class = ref ( $thread ); |
|---|
| 82 |
next unless ($class eq 'WebGUI::Asset::Post::Thread'); |
|---|
| 83 |
my $post = $thread->getPosts(); |
|---|
| 84 |
my $post = $post->[0]; |
|---|
| 85 |
|
|---|
| 86 |
next unless ($post->get("status") eq "approved"); |
|---|
| 87 |
my $epochDate; |
|---|
| 88 |
my $date = $post->get($dateField); |
|---|
| 89 |
if ($date =~ /^\d{4}-\d{2}-\d{2}/) { |
|---|
| 90 |
$epochDate = $session->datetime->setToEpoch($date); |
|---|
| 91 |
} else { |
|---|
| 92 |
$epochDate = $date; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
my $humanDate = $session->datetime->epochToHuman($epochDate,"%M/%D/%y @ %H:%n %p"); |
|---|
| 96 |
my %varHash = ( |
|---|
| 97 |
'post_url' => $post->getUrl, |
|---|
| 98 |
'post_title' => $post->get("title"), |
|---|
| 99 |
'post_epoch_date' => $epochDate, |
|---|
| 100 |
'post_date' => $humanDate, |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
'post_username' => $post->get("username"), |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
); |
|---|
| 112 |
if ($summaryField) { |
|---|
| 113 |
$varHash{'post_summary'} = $post->get($summaryField); |
|---|
| 114 |
} |
|---|
| 115 |
push (@postLoop, \%varHash); |
|---|
| 116 |
} |
|---|
| 117 |
my @sortedPostLoop; |
|---|
| 118 |
if ($orderByVar) { |
|---|
| 119 |
if ($descendingOrder) { |
|---|
| 120 |
|
|---|
| 121 |
@sortedPostLoop = sort {$b->{$orderByVar} <=> $a->{$orderByVar}} @postLoop; |
|---|
| 122 |
} else { |
|---|
| 123 |
@sortedPostLoop = sort {$a->{$orderByVar} <=> $b->{$orderByVar}} @postLoop; |
|---|
| 124 |
} |
|---|
| 125 |
} else { |
|---|
| 126 |
@sortedPostLoop = @postLoop; |
|---|
| 127 |
} |
|---|
| 128 |
my @returnPostLoop; |
|---|
| 129 |
if ($numPosts <= $#sortedPostLoop) { |
|---|
| 130 |
@returnPostLoop = @sortedPostLoop[0..($numPosts-1)]; |
|---|
| 131 |
} else { |
|---|
| 132 |
@returnPostLoop = @sortedPostLoop; |
|---|
| 133 |
} |
|---|
| 134 |
$var{postLoop} = \@returnPostLoop; |
|---|
| 135 |
return WebGUI::Asset::Template->new($session,$templateId)->process(\%var); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
1; |
|---|
| 139 |
|
|---|
| 140 |
|
|---|