/lib/MT/ArchiveType/CategoryDaily.pm

http://github.com/openmelody/melody · Perl · 284 lines · 239 code · 40 blank · 5 comment · 21 complexity · 6b3edab05e90582cc45638f0d7b4d56e MD5 · raw file

  1. # Movable Type (r) Open Source (C) 2001-2010 Six Apart, Ltd.
  2. # This program is distributed under the terms of the
  3. # GNU General Public License, version 2.
  4. #
  5. # $Id$
  6. package MT::ArchiveType::CategoryDaily;
  7. use strict;
  8. use base qw( MT::ArchiveType::Category MT::ArchiveType::Daily );
  9. use MT::Util qw( dirify start_end_day );
  10. sub name {
  11. return 'Category-Daily';
  12. }
  13. sub archive_label {
  14. return MT->translate('CATEGORY-DAILY_ADV');
  15. }
  16. sub default_archive_templates {
  17. return [ {
  18. label => 'category/sub-category/yyyy/mm/dd/index.html',
  19. template => '%-c/%y/%m/%d/%i',
  20. default => 1
  21. },
  22. {
  23. label => 'category/sub_category/yyyy/mm/dd/index.html',
  24. template => '%c/%y/%m/%d/%i'
  25. },
  26. ];
  27. }
  28. sub dynamic_template {
  29. return 'category/<$MTCategoryID$>/<$MTArchiveDate format="%Y%m%d"$>';
  30. }
  31. sub template_params {
  32. return {
  33. archive_class => "category-daily-archive",
  34. category_daily_archive => 1,
  35. archive_template => 1,
  36. archive_listing => 1,
  37. datebased_archive => 1,
  38. };
  39. }
  40. sub archive_file {
  41. my $obj = shift;
  42. my ( $ctx, %param ) = @_;
  43. my $timestamp = $param{Timestamp};
  44. my $file_tmpl = $param{Template};
  45. my $blog = $ctx->{__stash}{blog};
  46. my $cat = $ctx->{__stash}{cat} || $ctx->{__stash}{category};
  47. my $entry = $ctx->{__stash}{entry};
  48. my $file;
  49. my $this_cat = $cat ? $cat : ( $entry ? $entry->category : undef );
  50. if ($file_tmpl) {
  51. ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} )
  52. = start_end_day( $timestamp, $blog );
  53. $ctx->stash( 'archive_category', $this_cat );
  54. $ctx->{inside_mt_categories} = 1;
  55. $ctx->{__stash}{category} = $this_cat;
  56. }
  57. else {
  58. if ( !$this_cat ) {
  59. return "";
  60. }
  61. my $label = '';
  62. $label = dirify( $this_cat->label );
  63. if ( $label !~ /\w/ ) {
  64. $label = $this_cat ? "cat" . $this_cat->id : "";
  65. }
  66. my $start = start_end_day( $timestamp, $blog );
  67. my ( $year, $month, $day ) = unpack 'A4A2A2', $start;
  68. $file = sprintf( "%s/%04d/%02d/%02d/index",
  69. $this_cat->category_path, $year, $month, $day );
  70. }
  71. $file;
  72. } ## end sub archive_file
  73. sub archive_title {
  74. my $obj = shift;
  75. my ( $ctx, $entry_or_ts ) = @_;
  76. my $stamp = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts;
  77. my $start = start_end_day( $stamp, $ctx->stash('blog') );
  78. my $date = MT::Template::Context::_hdlr_date( $ctx,
  79. { ts => $start, 'format' => "%x" } );
  80. my $cat = $obj->display_name($ctx);
  81. sprintf( "%s%s", $cat, $date );
  82. }
  83. sub archive_group_iter {
  84. my $obj = shift;
  85. my ( $ctx, $args ) = @_;
  86. my $blog = $ctx->stash('blog');
  87. my $sort_order
  88. = ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend';
  89. my $cat_order = $args->{sort_order} ? $args->{sort_order} : 'ascend';
  90. my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc';
  91. my $limit = exists $args->{lastn} ? delete $args->{lastn} : undef;
  92. my $tmpl = $ctx->stash('template');
  93. my $cat = $ctx->stash('archive_category') || $ctx->stash('category');
  94. my @data = ();
  95. my $count = 0;
  96. my $ts = $ctx->{current_timestamp};
  97. my $tsend = $ctx->{current_timestamp_end};
  98. require MT::Placement;
  99. require MT::Entry;
  100. my $loop_sub = sub {
  101. my $c = shift;
  102. my $entry_iter =
  103. MT::Entry->count_group_by( {
  104. blog_id => $blog->id,
  105. status => MT::Entry::RELEASE(),
  106. (
  107. $ts && $tsend ? ( authored_on => [ $ts, $tsend ] )
  108. : ()
  109. ),
  110. },
  111. { (
  112. $ts && $tsend
  113. ? ( range_incl => { authored_on => 1 } )
  114. : ()
  115. ),
  116. group => [
  117. "extract(year from authored_on)",
  118. "extract(month from authored_on)",
  119. "extract(day from authored_on)"
  120. ],
  121. sort => [ {
  122. column => "extract(year from authored_on)",
  123. desc => $order
  124. },
  125. {
  126. column => "extract(month from authored_on)",
  127. desc => $order
  128. },
  129. {
  130. column => "extract(day from authored_on)",
  131. desc => $order
  132. },
  133. ],
  134. 'join' => [
  135. 'MT::Placement', 'entry_id',
  136. { category_id => $c->id }
  137. ]
  138. }
  139. ) or return $ctx->error("Couldn't get yearly archive list");
  140. while ( my @row = $entry_iter->() ) {
  141. my $hash = {
  142. year => $row[1],
  143. month => $row[2],
  144. day => $row[3],
  145. category => $c,
  146. count => $row[0],
  147. };
  148. push( @data, $hash );
  149. return $count + 1
  150. if ( defined($limit) && ( $count + 1 ) == $limit );
  151. $count++;
  152. }
  153. };
  154. if ($cat) {
  155. $loop_sub->($cat);
  156. }
  157. else {
  158. require MT::Category;
  159. my $iter =
  160. MT::Category->load_iter(
  161. { blog_id => $blog->id },
  162. {
  163. 'sort' => 'label',
  164. direction => $cat_order
  165. }
  166. );
  167. while ( my $category = $iter->() ) {
  168. $loop_sub->($category);
  169. last if ( defined($limit) && $count == $limit );
  170. }
  171. }
  172. my $loop = @data;
  173. my $curr = 0;
  174. return sub {
  175. if ( $curr < $loop ) {
  176. my $date = sprintf( "%04d%02d%02d000000",
  177. $data[$curr]->{year},
  178. $data[$curr]->{month},
  179. $data[$curr]->{day} );
  180. my ( $start, $end ) = start_end_day($date);
  181. my $count = $data[$curr]->{count};
  182. my %hash = (
  183. category => $data[$curr]->{category},
  184. year => $data[$curr]->{year},
  185. month => $data[$curr]->{month},
  186. day => $data[$curr]->{day},
  187. start => $start,
  188. end => $end,
  189. );
  190. $curr++;
  191. return ( $count, %hash );
  192. }
  193. undef;
  194. }
  195. } ## end sub archive_group_iter
  196. sub archive_group_entries {
  197. my $obj = shift;
  198. my ( $ctx, %param ) = @_;
  199. my $ts = $param{year}
  200. ? sprintf( "%04d%02d%02d000000",
  201. $param{year}, $param{month}, $param{day} )
  202. : $ctx->stash('current_timestamp');
  203. my $cat = $param{category} || $ctx->stash('archive_category');
  204. my $limit = $param{limit};
  205. $obj->dated_category_entries( $ctx, 'Category-Daily', $cat, $ts, $limit );
  206. }
  207. sub archive_entries_count {
  208. my $obj = shift;
  209. my ( $blog, $at, $entry, $cat ) = @_;
  210. $cat = $entry->category unless $cat;
  211. return 0 unless $cat;
  212. return
  213. $obj->SUPER::archive_entries_count( {
  214. Blog => $blog,
  215. ArchiveType => $at,
  216. Timestamp => $entry->authored_on,
  217. Category => $cat
  218. }
  219. );
  220. }
  221. *date_range = \&MT::ArchiveType::Daily::date_range;
  222. *next_archive_entry = \&MT::ArchiveType::Date::next_archive_entry;
  223. *previous_archive_entry = \&MT::ArchiveType::Date::previous_archive_entry;
  224. 1;
  225. __END__
  226. =head1 NAME
  227. MT::ArchiveType::CategoryDaily
  228. =head1 METHODS
  229. =head2 archive_entries_count
  230. =head2 archive_file
  231. =head2 archive_group_entries
  232. =head2 archive_group_iter
  233. =head2 archive_label
  234. =head2 archive_title
  235. =head2 date_range
  236. =head2 default_archive_templates
  237. =head2 dynamic_template
  238. =head2 name
  239. =head2 next_archive_entry
  240. =head2 previous_archive_entry
  241. =head2 template_params
  242. =head1 AUTHOR & COPYRIGHT
  243. Please see L<MT/AUTHOR & COPYRIGHT>.
  244. =cut