-
Notifications
You must be signed in to change notification settings - Fork 6
/
publish_daily_archive.mc
104 lines (77 loc) · 2.93 KB
/
publish_daily_archive.mc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<%doc>
=head1 NAME
publish_daily_archive.mc - Look up or create and publish a daily archive
=head1 VERSION
1.0
=head1 DESCRIPTION
Here's some example code demonstrating how to look up and publish a "Daily
Archive" story, or, if no such archive exists for the cover date of the
current story, to create a new "Daily Archive" story for that date and publish
it.
We first try to look up the appropriate "Daily Archive" story. If it exists,
we simply publish it with the C<publish_another()> burner method. If it does
not exist, we have to do a few tricks to create one. Once it's created,
it can be published.
=head1 PREREQUISITES
=over 4
=item Bricolage 1.8
=item A "Daily Archive" story type element
=back
=head1 AUTHOR
David Wheeler <[email protected]>
=head1 COPYRIGHT AND LICENSE
Copyright 2004 by Kineticode, Inc.
This template is free software; you can redistribute it and/or modify it under
the same terms as Bricolage itself.
=cut
</%doc>
<%once>;
# Set the ID for your "Daily Archive" element here.
my $daily_archive_element_id = 1032;
</%once>
<%cleanup>;
# Just bail unless we're publishing or the current story has been published
# before (and is therefore already in the archive--I hope its URI or cover
# date hasn't changed!
return unless $burner->get_mode == PUBLISH_MODE
&& $story->get_publish_status;
# Create the URI for the daily archive with the same date as the current
# story. Replace "/archive/daily" with whatever category you want for
# your archive documents.
my $daily_archive_uri = $story->get_cover_date("/archive/daily/%Y/%m/%d");
# Look for an existing daily archive for this date.
my ($daily_archive_story) = Bric::Biz::Asset::Business::Story->list({
primary_uri => $daily_archive_uri,
element_key_name => 'daily_archive',
Limit => 1,
});
unless ($daily_archive_story) {
# We need to create a new daily archive for this date.
$daily_archive_story = Bric::Biz::Asset::Business::Story->new({
element__id => $daily_archive_element_id,
source__id => $story->get_source__id,
site_id => $story->get_site_id,
user__id => Bric::App::Session::get_user_id(),
title => 'Daily Archive',
});
my ($category) = Bric::Biz::Category->list({
uri => "/archive/daily"
});
$burner->throw_error("There is no daily archive category")
unless $category;
# Set it up and check it in.
$daily_archive_story->add_categories( [ $category->get_id ] );
$daily_archive_story->set_primary_category( $category->get_id );
$daily_archive_story->set_cover_date(
$story->get_cover_date("%Y-%m-%d 00:00:00")
);
$daily_archive_story->checkin;
$daily_archive_story->save;
}
# Make sure to mark the current story published so that it will show up in
# the daily archive.
$story->set_publish_status(1);
$story->save;
# Publish the daily archive.
$burner->$action($daily_archive_story);
</%cleanup>