root/branches/release-33/php/lib/archive_lib.php @ 1747

Revision 1747, 63.0 kB (checked in by takayama, 20 months ago)

Fixed BugId:70282
* Fixed for nested category based archive count issue.

  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
3# This program is distributed under the terms of the
4# GNU General Public License, version 2.
5#
6# $Id$
7
8require_once("MTUtil.php");
9
10// Create default archivers
11global $_archivers;
12
13// Date-based archives
14register_archiver(new YearlyArchiver());
15register_archiver(new MonthlyArchiver());
16register_archiver(new WeeklyArchiver());
17register_archiver(new DailyArchiver());
18register_archiver(new MonthlyArchiver());
19
20// Indivudual archives
21register_archiver(new IndividualArchiver());
22register_archiver(new PageArchiver());
23
24// Author-based archives
25register_archiver(new AuthorBasedArchiver());
26register_archiver(new YearlyAuthorBasedArchiver());
27register_archiver(new MonthlyAuthorBasedArchiver());
28register_archiver(new WeeklyAuthorBasedArchiver());
29register_archiver(new DailyAuthorBasedArchiver());
30
31// Date-based category archives
32register_archiver(new YearlyCategoryArchiver());
33register_archiver(new MonthlyCategoryArchiver());
34register_archiver(new DailyCategoryArchiver());
35register_archiver(new WeeklyCategoryArchiver());
36
37function register_archiver($archiver) {
38    global $_archivers;
39    $_archivers[$archiver->get_archive_name()] = $archiver;
40}
41
42function _hdlr_archive_prev_next($args, $content, &$ctx, &$repeat, $tag) {
43    global $_archivers;
44    $at = $args['archive_type'];
45    $at or $at = $ctx->stash('current_archive_type');
46    if ($at == 'Category') {
47        require_once("block.mtcategorynext.php");
48        return smarty_block_mtcategorynext($args, $content, $ctx, $repeat);
49    }
50    $archiver = $_archivers[$at];
51    if (!isset($archiver)) {
52        $repeat = false;
53        return '';
54    }
55    return $archiver->archive_prev_next($args, $content, $ctx, $repeat, $tag, $at);
56}
57
58class BaseArchiver {
59    // Abstract Method (needs override)
60    function get_label($args, $ctx) { }
61    function get_title($args, $ctx) { }
62    function get_range(&$ctx, &$row) { }
63    function get_archive_name() { }
64    function &get_archive_list($ctx, $args) { }
65    function get_archive_link_sql($ctx, $ts, $at, $args) { }
66    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag) { }
67    function prepare_list(&$ctx, &$row) { }
68    function setup_args($ctx, &$args) { }
69    function template_params(&$ctx) { }
70    function is_date_based() { return false; }
71}
72
73class PageArchiver extends BaseArchiver {
74    function get_label($args, $ctx) {
75        return $ctx->mt->translate("Page");
76    }
77
78    function get_archive_name() {
79        return 'Page';
80    }
81
82    function template_params(&$ctx) {
83        $vars =& $ctx->__stash['vars'];
84        $vars['archive_template'] = 1;
85        $vars['page_template'] = 1;
86        $vars['feedback_template'] = 1;
87        $vars['page_archive'] = 1;
88        $vars['archive_class'] = 'page-archive';
89    }
90}
91
92class IndividualArchiver extends BaseArchiver {
93    // Override Method
94    function get_label($args, $ctx) {
95        return $ctx->mt->translate("Individual");
96    }
97
98    function get_title($args, $ctx) {
99        return $ctx->tag('EntryTitle', $args);
100    }
101
102    function get_range(&$ctx, &$row) {
103        if (is_array($row)) {
104            $period_start = $row[1];
105        } else {
106            $period_start = $row;
107        }
108        $period_start = preg_replace('/[^0-9]/', '', $period_start);
109        return start_end_day($period_start, $ctx->stash('blog'));
110    }
111
112    function get_archive_name() {
113        return 'Individual';
114    }
115   
116    function &get_archive_list($ctx, $args) {
117        return $ctx->mt->db->get_archive_list($args);
118    }
119
120    function get_archive_link_sql($ctx, $ts, $at, $args) {
121        return '';
122    }
123
124    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag) {
125        return $ctx->error(
126            "You used an <mt$tag> without a date context set up.");
127    }
128
129    function prepare_list(&$ctx, &$row) {
130        $entry_id = $row[0];
131        $entry = $ctx->mt->db->fetch_entry($entry_id);
132        $ctx->stash('entry', $entry);
133        $ctx->stash('entries', array());
134    }
135
136    function template_params(&$ctx) {
137        $vars =& $ctx->__stash['vars'];
138        $vars['archive_template'] = 1;
139        $vars['entry_template'] = 1;
140        $vars['feedback_template'] = 1;
141        $vars['archive_class'] = 'entry-archive';
142    }
143}
144
145class DateBasedArchiver extends BaseArchiver {
146
147    // Override Method
148  function is_date_based() { return true; }
149
150    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag, $at) {
151        $localvars = array('current_timestamp', 'current_timestamp_end', 'entries');
152        if (!isset($content)) {
153            $ctx->localize($localvars);
154            $is_prev = $tag == 'archiveprevious';
155            $ts = $ctx->stash('current_timestamp');
156            if (!$ts) {
157                return $ctx->error(
158                   "You used an <mt$tag> without a date context set up.");
159            }
160            $order = $is_prev ? 'previous' : 'next';
161            $helper = $this->get_helper($at);
162            if ($entry = $this->get_entry($ts, $ctx->stash('blog_id'), $at, $order)) {
163                $ctx->stash('entries', array( $entry ));
164                list($start, $end) = $helper($entry['entry_authored_on']);
165                $ctx->stash('current_timestamp', $start);
166                $ctx->stash('current_timestamp_end', $end);
167            } else {
168                $ctx->restore($localvars);
169                $repeat = false;
170            }
171        } else {
172            $ctx->restore($localvars);
173        }
174        return $content;
175    }
176
177    // Functions
178    function get_helper($at) {
179        if (strtoupper($at) == 'YEARLY') {
180            return 'start_end_year';
181        } elseif (strtoupper($at) == 'MONTHLY') {
182            return 'start_end_month';
183        } elseif (strtoupper($at) == 'WEEKLY') {
184            return 'start_end_week';
185        } elseif (strtoupper($at) == 'DAILY') {
186            return 'start_end_day';
187        } else {
188            return null;
189        }
190    }
191
192    function get_entry($ts, $blog_id, $at, $order) {
193        $helper = $this->get_helper($at);
194        list($start, $end) = $helper($ts);
195        $args = array();
196        if ($order == 'previous') {
197            $args['current_timestamp_end'] = $this->dec_ts($start);
198        } else {
199            $args['current_timestamp'] = $this->inc_ts($end);
200            $args['base_sort_order'] = 'ascend'; # ascending order
201        }
202        $args['lastn'] = 1;
203        $args['blog_id'] = $blog_id;
204        global $mt;
205        list($entry) = $mt->db->fetch_entries($args);
206        return $entry;
207    }
208
209    function dec_ts($ts) {
210        $y = substr($ts, 0, 4);
211        $mo = substr($ts, 4, 2);
212        $d = substr($ts, 6, 2);
213        $h = substr($ts, 8, 2);
214        $m = substr($ts, 10, 2);
215        $s = substr($ts, 12, 2);
216        $s--;
217        if ($s == -1) {
218            $s = 59; $m--;
219            if ($m == -1) {
220                $m = 59; $h--;
221                if ($h == -1) {
222                    $h = 23; $d--;
223                    if ($d == 0) {
224                        $mo--;
225                        if ($mo == 0) {
226                            $mo = 12; $y--;
227                        }
228                        $d = days_in($mo, $y);
229                    }
230                }
231            }
232        }
233        return sprintf("%04d%02d%02d%02d%02d%02d", $y, $mo, $d, $h, $m, $s);
234    }
235
236    function inc_ts($ts) {
237        $y = substr($ts, 0, 4);
238        $mo = substr($ts, 4, 2);
239        $d = substr($ts, 6, 2);
240        $h = substr($ts, 8, 2);
241        $m = substr($ts, 10, 2);
242        $s = substr($ts, 12, 2);
243        $s++;
244        if ($s == 60) {
245            $s = 0; $m++;
246            if ($m == 60) {
247                $m = 0; $h++;
248                if ($h == 24) {
249                    $h = 0; $d++;
250                    $days = days_in($mo, $y);
251                    if ($d > $days) {
252                        $d = 1; $mo++;
253                        if ($mo == 13) {
254                            $mo = 1; $y++;
255                        }
256                    }
257                }
258            }
259        }
260        return sprintf("%04d%02d%02d%02d%02d%02d", $y, $mo, $d, $h, $m, $s);
261    }
262
263    function template_params(&$ctx) {
264        $vars =& $ctx->__stash['vars'];
265        $vars['datebased_archive'] = 1;
266    }
267}
268
269class YearlyArchiver extends DateBasedArchiver {
270
271    function YearlyArchiver() { }
272
273    // Override Method
274    function get_label($args, $ctx) {
275        return $ctx->mt->translate('Yearly');
276    }
277    function get_archive_name() {
278        return 'Yearly';
279    }
280
281    function get_title($args, $ctx) {
282        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
283        list($start) = start_end_year($stamp, $ctx->stash('blog'));
284        $format = $args['format'];
285        $blog = $ctx->stash('blog');
286        global $mt;
287        $lang = ($blog && $blog['blog_language'] ? $blog['blog_language'] :
288            $mt->config('DefaultLanguage'));
289            if (strtolower($lang) == 'jp' || strtolower($lang) == 'ja') {
290            $format or $format = "%Y&#24180;";
291        } else {
292            $format or $format = "%Y";
293        }
294       
295        return $ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
296    }
297
298    function get_range(&$ctx, &$row) {
299        if (is_array($row)) {
300            $period_start = sprintf("%04d0101000000", $row[0]);
301        } else {
302            $period_start = $row;
303        }
304        return start_end_year($period_start, $ctx->stash('blog'));
305    }
306
307    function &get_archive_list($ctx, $args) {
308        return $ctx->mt->db->get_archive_list($args);
309    }
310   
311    function get_archive_link_sql($ctx, $ts, $at, $args) {
312        return '';
313    }
314
315    function template_params(&$ctx) {
316        parent::template_params($ctx);
317        $vars =& $ctx->__stash['vars'];
318        $vars['archive_template'] = 1;
319        $vars['datebased_only_archive'] = 1;
320        $vars['datebased_yearly_archive'] = 1;
321        $vars['archive_class'] = 'datebased-yearly-archive';
322        $vars['module_yearly_archives'] = 1;
323    }
324}
325
326class MonthlyArchiver extends DateBasedArchiver {
327
328    function MonthlyArchiver() { }
329
330    // Override Method
331    function get_label($args, $ctx) {
332        return $ctx->mt->translate('Monthly');
333    }
334    function get_archive_name() {
335        return 'Monthly';
336    }
337
338    function get_title($args, $ctx) {
339        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
340        list($start) = start_end_month($stamp, $ctx->stash('blog'));
341        $format = $args['format'];
342        $format or $format = "%B %Y";
343        return $ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
344    }
345
346    function get_range(&$ctx, &$row) {
347        if (is_array($row)) {
348            $period_start = sprintf("%04d%02d01000000", $row[0], $row[1]);
349        } else {
350            $period_start = $row;
351        }
352        return start_end_month($period_start, $ctx->stash('blog'));
353    }
354
355    function &get_archive_list($ctx, $args) {
356        $inside = $ctx->stash('inside_archive_list');
357        if (!isset($inside)) {
358          $inside = false;
359        }
360        $args['inside_archive_list'] = $inside;
361        $args['current_timestamp'] = $ctx->stash('current_timestamp');
362        $args['current_timestamp_end'] = $ctx->stash('current_timestamp_end');
363        return $ctx->mt->db->get_archive_list($args);
364    }
365   
366    function get_archive_link_sql($ctx, $ts, $at, $args) {
367        return '';
368    }
369
370    function template_params(&$ctx) {
371        parent::template_params($ctx);
372        $vars =& $ctx->__stash['vars'];
373        $vars['archive_template'] = 1;
374        $vars['datebased_only_archive'] = 1;
375        $vars['datebased_monthly_archive'] = 1;
376        $vars['archive_class'] = 'datebased-monthly-archive';
377    }
378}
379
380class DailyArchiver extends DateBasedArchiver {
381
382    function DailyArchiver() { }
383
384    // Override Method
385    function get_label($args, $ctx) {
386        return $ctx->mt->translate('Daily');
387    }
388    function get_archive_name() {
389        return 'Daily';
390    }
391
392    function get_title($args, $ctx) {
393        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
394        list($start) = start_end_day($stamp, $ctx->stash('blog'));
395        $format = $args['format'];
396        $format or $format = "%x";
397        return $ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
398    }
399
400    function get_range(&$ctx, &$row) {
401        if (is_array($row)) {
402            $period_start = sprintf("%04d%02d%02d000000", $row[0], $row[1], $row[2]);
403        } else {
404            $period_start = $row;
405        }
406        return start_end_day($period_start, $ctx->stash('blog'));
407    }
408
409    function &get_archive_list($ctx, $args) {
410        $inside = $ctx->stash('inside_archive_list');
411        if (!isset($inside)) {
412          $inside = false;
413        }
414        $args['inside_archive_list'] = $inside;
415        $args['current_timestamp'] = $ctx->stash('current_timestamp');
416        $args['current_timestamp_end'] = $ctx->stash('current_timestamp_end');
417        return $ctx->mt->db->get_archive_list($args);
418    }
419   
420    function get_archive_link_sql($ctx, $ts, $at, $args) {
421        return '';
422    }
423
424    function template_params(&$ctx) {
425        parent::template_params($ctx);
426        $vars =& $ctx->__stash['vars'];
427        $vars['archive_template'] = 1;
428        $vars['datebased_only_archive'] = 1;
429        $vars['datebased_daily_archive'] = 1;
430        $vars['archive_class'] = 'datebased-daily-archive';
431    }
432}
433
434class WeeklyArchiver extends DateBasedArchiver {
435
436    function WeeklyArchiver() { }
437
438    // Override Method
439    function get_label($args, $ctx) {
440        return $ctx->mt->translate('Weekly');
441    }
442    function get_archive_name() {
443        return 'Weekly';
444    }
445
446    function get_title($args, $ctx) {
447        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
448        list($start, $end) = start_end_week($stamp, $ctx->stash('blog'));
449        $format = $args['format'];
450        $format or $format = "%x";
451        return $ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx)
452            . ' - ' .
453            $ctx->_hdlr_date(array('ts' => $end, 'format' => $format), $ctx);
454    }
455
456    function get_range(&$ctx, &$row) {
457        if (is_array($row)) {
458            $week_yr = substr($row[0], 0, 4);
459            $week_wk = substr($row[0], 4);
460            list($y, $m, $d) = week2ymd($week_yr, $week_wk);
461            $period_start = sprintf("%04d%02d%02d000000", $y, $m, $d);
462        } else {
463            $period_start = $row;
464        }
465        return start_end_week($period_start, $ctx->stash('blog'));
466    }
467
468    function &get_archive_list($ctx, $args) {
469        $inside = $ctx->stash('inside_archive_list');
470        if (!isset($inside)) {
471          $inside = false;
472        }
473        $args['inside_archive_list'] = $inside;
474        $args['current_timestamp'] = $ctx->stash('current_timestamp');
475        $args['current_timestamp_end'] = $ctx->stash('current_timestamp_end');
476        return $ctx->mt->db->get_archive_list($args);
477    }
478   
479    function get_archive_link_sql($ctx, $ts, $at, $args) {
480        return '';
481    }
482
483    function template_params(&$ctx) {
484        parent::template_params($ctx);
485        $vars =& $ctx->__stash['vars'];
486        $vars['archive_template'] = 1;
487        $vars['datebased_only_archive'] = 1;
488        $vars['datebased_weekly_archive'] = 1;
489        $vars['archive_class'] = 'datebased-weekly-archive';
490    }
491}
492
493class AuthorBasedArchiver extends BaseArchiver {
494    // Override Method
495    function get_label($args, $ctx) {
496        return $ctx->mt->translate('Author');
497    }
498    function get_title($args, $ctx) {
499        $author_name = '';
500        $author = $ctx->stash('archive_author');
501        if (!isset($archive_author)) {
502            $author = $ctx->stash('author');
503            if (isset($author)) {
504                $author_name = $author['author_nickname'];
505                $author_name or $author_name =
506                    $ctx->mt->translate('(Display Name not set)');
507            }
508        }
509        return $author_name;
510    }
511   
512    function get_archive_name() {
513        return 'Author';
514    }
515
516    function &get_archive_list($ctx, $args) {
517        global $mt;
518        list($results, $hi, $low) = $this->get_archive_list_data($args);
519        if(is_array($results)) {
520            $blog_id = $args['blog_id'];
521            if (isset($low) && isset($hi)) {
522                $range = "fileinfo_startdate between '$low' and '$hi' and";
523            }
524            $at = $ctx->stash('current_archive_type');
525            $link_cache_sql = "
526                select fileinfo_startdate, fileinfo_url, blog_site_url, blog_file_extension
527                  from mt_fileinfo, mt_templatemap, mt_blog
528                 where  $range
529                   fileinfo_archive_type = '$at'
530                   and blog_id = $blog_id
531                   and fileinfo_blog_id = blog_id
532                   and templatemap_id = fileinfo_templatemap_id
533                   and templatemap_is_preferred = 1
534            ";
535            $cache_results = $mt->db->get_results($link_cache_sql, ARRAY_N);
536            if (is_array($cache_results)) {
537                foreach ($cache_results as $row) {
538                    $date = $ctx->mt->db->db2ts($row[0]);
539                    $blog_url = $row[2];
540                    $blog_url = preg_replace('!(https?://(?:[^/]+))/.*!', '$1', $blog_url);
541                    $url = $blog_url . $row[1];
542                    $url = _strip_index($url, array('blog_file_extension' => $row[3]));
543                    $mt->db->_archive_link_cache[$date.';'.$at] = $url;
544                }
545            }
546        }
547        return $results;
548    }
549
550    function get_archive_link_sql($ctx, $ts, $at, $args) {
551        $blog_id = intval($args['blog_id']);
552        $author = $ctx->stash('author');
553        $auth_id = $author['author_id'];
554        $at or $at = $ctx->stash('current_archive_type');
555        $ts = $ctx->stash('current_timestamp');
556        if ($at == 'Author-Monthly') {
557            $ts = substr($ts, 0, 6) . '01000000';
558        } elseif ($at == 'Author-Daily') {
559            $ts = substr($ts, 0, 8) . '000000';
560        } elseif ($at == 'Author-Weekly') {
561            require_once("MTUtil.php");
562            list($ws, $we) = start_end_week($ts);
563            $ts = $ws;
564        } elseif ($at == 'Author-Yearly') {
565            $ts = substr($ts, 0, 4) . '0101000000';
566        } else {
567            $ts = '';
568        }
569
570        $sql = "select fileinfo_url
571                  from mt_fileinfo, mt_templatemap
572                 where " . ($ts ? "fileinfo_startdate = '$ts' and" : "") .
573                   " fileinfo_blog_id = $blog_id
574                   and fileinfo_archive_type = '".$ctx->mt->db->escape($at)."'
575                   and fileinfo_author_id = '$auth_id'
576                   and templatemap_id = fileinfo_templatemap_id
577                   and templatemap_is_preferred = 1";
578        return $sql;
579    }
580
581    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag) {
582        if ($tag == 'archiveprevious') {
583            require_once('block.mtauthorprevious.php');
584            return smarty_block_mtauthorprevious($args, $content, $ctx, $repeat);
585        } elseif ($tag == 'archivenext') {
586            require_once('block.mtauthornext.php');
587            return smarty_block_mtauthornext($args, $content, $ctx, $repeat);
588        }
589        return $ctx->error("Error in tag: $tag");
590    }
591
592    function prepare_list(&$ctx, &$row) {
593        $author_id = $row[1];
594        $author = $ctx->mt->db->fetch_author($author_id);
595        $ctx->stash('author', $author);
596    }
597
598    function setup_args($ctx, &$args) {
599        if ($auth = $ctx->stash('author')) {
600            $args['author'] = $auth['author_name'];
601        }
602    }
603
604    // Functions
605    function get_archive_list_data($args) {
606        global $mt;
607        $blog_id = $args['blog_id'];
608        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
609        $sql = "
610            select count(*),
611                   entry_author_id
612              from mt_entry
613             where entry_blog_id = $blog_id
614               and entry_status = 2
615             group by
616                   entry_author_id
617             order by
618                   entry_author_id $order
619                   <LIMIT>";
620        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
621        $results = $mt->db->get_results($group_sql, ARRAY_N);
622        return array($results, null, null);;
623    }
624
625    function template_params(&$ctx) {
626        $vars =& $ctx->__stash['vars'];
627        $vars['archive_template'] = 1;
628        $vars['author_archive'] = 1;
629        $vars['archive_class'] = 'author-archive';
630        $vars['module_author-monthly_archives'] = 1;
631    }
632}
633
634class DateBasedAuthorArchiver extends DateBasedArchiver {
635    function setup_args($ctx, &$args) {
636        if ($auth = $ctx->stash('author')) {
637            $args['author'] = $auth['author_name'];
638        }
639    }
640
641    function get_archive_link_sql($ctx, $ts, $at, $args) {
642        $blog_id = intval($args['blog_id']);
643        $author = $ctx->stash('author');
644        $auth_id = $author['author_id'];
645        $at or $at = $ctx->stash('current_archive_type');
646        $ts = $ctx->stash('current_timestamp');
647        if ($at == 'Author-Monthly') {
648            $ts = substr($ts, 0, 6) . '01000000';
649        } elseif ($at == 'Author-Daily') {
650            $ts = substr($ts, 0, 8) . '000000';
651        } elseif ($at == 'Author-Weekly') {
652            require_once("MTUtil.php");
653            list($ws, $we) = start_end_week($ts);
654            $ts = $ws;
655        } elseif ($at == 'Author-Yearly') {
656            $ts = substr($ts, 0, 4) . '0101000000';
657        } else {
658            $ts = '';
659        }
660
661        $sql = "select fileinfo_url
662                  from mt_fileinfo, mt_templatemap
663                 where " . ($ts ? "fileinfo_startdate = '$ts' and" : "") .
664                   " fileinfo_blog_id = $blog_id
665                   and fileinfo_archive_type = '".$ctx->mt->db->escape($at)."'
666                   and fileinfo_author_id = '$auth_id'
667                   and templatemap_id = fileinfo_templatemap_id
668                   and templatemap_is_preferred = 1";
669        return $sql;
670    }
671
672    function &get_archive_list($ctx, $args) {
673        global $mt;
674        list($results, $hi, $low) = $this->get_archive_list_data($args);
675        if(is_array($results)) {
676            $blog_id = $args['blog_id'];
677            if (isset($low) && isset($hi)) {
678                $range = "fileinfo_startdate between '$low' and '$hi' and";
679            }
680            $at = $ctx->stash('current_archive_type');
681            $link_cache_sql = "
682                select fileinfo_startdate, fileinfo_url, blog_site_url, blog_file_extension
683                  from mt_fileinfo, mt_templatemap, mt_blog
684                 where  $range
685                   fileinfo_archive_type = '$at'
686                   and blog_id = $blog_id
687                   and fileinfo_blog_id = blog_id
688                   and templatemap_id = fileinfo_templatemap_id
689                   and templatemap_is_preferred = 1
690            ";
691            $cache_results = $mt->db->get_results($link_cache_sql, ARRAY_N);
692            if (is_array($cache_results)) {
693                foreach ($cache_results as $row) {
694                    $date = $ctx->mt->db->db2ts($row[0]);
695                    $blog_url = $row[2];
696                    $blog_url = preg_replace('!(https?://(?:[^/]+))/.*!', '$1', $blog_url);
697                    $url = $blog_url . $row[1];
698                    $url = _strip_index($url, array('blog_file_extension' => $row[3]));
699                    $mt->db->_archive_link_cache[$date.';'.$at] = $url;
700                }
701            }
702        }
703        return $results;
704    }
705
706    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag) {
707        $localvars = array('current_timestamp', 'current_timestamp_end', 'entries');
708        if (!isset($content)) {
709            $ctx->localize($localvars);
710            $is_prev = $tag == 'archiveprevious';
711            $ts = $ctx->stash('current_timestamp');
712            $author = $ctx->stash('author');
713            if (!$ts || !$author) {
714                return $ctx->error(
715                   "You used an <mt$tag> without a date context set up.");
716            }
717            $order = $is_prev ? 'previous' : 'next';
718
719            $at = $ctx->stash('current_archive_type');
720            if ($at == 'Author-Monthly') {
721                $wide = 'MONTHLY';
722            } elseif ($at == 'Author-Daily') {
723                $wide = 'DAILY';
724            } elseif ($at == 'Author-Weekly') {
725                $wide = 'WEEKLY';
726            } elseif ($at == 'Author-Yearly') {
727                $wide = 'YEARLY';
728            }
729
730            if ($entry = $this->get_author_entry($ts, $ctx->stash('blog_id'), $author['author_name'], $wide, $order)) {
731                $helper = $this->get_helper($wide);
732                $ctx->stash('entries', array( $entry ));
733                list($start, $end) = $helper($entry['entry_authored_on']);
734                $ctx->stash('current_timestamp', $start);
735                $ctx->stash('current_timestamp_end', $end);
736                $ctx->stash('author', $author);
737            } else {
738                $ctx->restore($localvars);
739                $repeat = false;
740            }
741        } else {
742            $ctx->restore($localvars);
743        }
744        return $content;
745    }
746
747    function get_author_entry($ts, $blog_id, $auth_name, $at, $order) {
748        $helper = $this->get_helper($at);
749        list($start, $end) = $helper($ts);
750        $args = array();
751        if ($order == 'previous') {
752            $args['current_timestamp_end'] = $this->dec_ts($start);
753        } else {
754            $args['current_timestamp'] = $this->inc_ts($end);
755            $args['base_sort_order'] = 'ascend'; # ascending order
756        }
757        $args['lastn'] = 1;
758        $args['blog_id'] = $blog_id;
759        $args['author'] = $auth_name;
760        global $mt;
761        list($entry) = $mt->db->fetch_entries($args);
762        return $entry;
763    }
764
765    function template_params(&$ctx) {
766        parent::template_params($ctx);
767        $vars =& $ctx->__stash['vars'];
768        $vars['archive_template'] = 1;
769        $vars['author_archive'] = 1;
770    }
771
772    function get_author_name ($ctx) {
773        global $_archivers;
774        $curr_at = $ctx->stash('current_archive_type');
775        $archiver = $_archivers[$curr_at];
776        $auth = $ctx->stash('author');
777        $author_name = '';
778
779        if ($ctx->stash('index_archive')
780            || !isset($archiver)
781            || (isset($archiver) && !isset($auth))
782            || !$ctx->stash('inside_archive_list'))
783        {
784            $author = $ctx->stash('archive_author');
785            $author or $author = $ctx->stash('author');
786            if (isset($author)) {
787                $author_name = $author['author_nickname'];
788                $author_name or $author_name =
789                    'Author (#'.$author['author_id'].')';
790                $author_name .= ': ';
791            }
792        }
793        return $author_name;
794    }
795}
796
797class YearlyAuthorBasedArchiver extends DateBasedAuthorArchiver {
798
799    // Override Method
800    function get_label($args, $ctx) {
801        return $ctx->mt->translate('Author Yearly');
802    }
803    function get_title($args, $ctx) {
804        $author_name = parent::get_author_name($ctx);
805        $stamp = $ctx->stash('current_timestamp');
806        list($start) = start_end_year($stamp, $ctx->stash('blog'));
807        $format = $args['format'];
808        $blog = $ctx->stash('blog');
809        global $mt;
810        $lang = ($blog && $blog['blog_language'] ? $blog['blog_language'] :
811            $mt->config('DefaultLanguage'));
812            if (strtolower($lang) == 'jp' || strtolower($lang) == 'ja') {
813            $format or $format = "%Y&#24180;";
814        } else {
815            $format or $format = "%Y";
816        }
817
818        return $author_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
819    }
820   
821    function get_archive_name() {
822        return 'Author-Yearly';
823    }
824
825    function get_range(&$ctx, &$row) {
826        if (is_array($row)) {
827            return start_end_year($row[0]);
828        } else {
829            return start_end_year($row);
830        }
831    }
832
833    // Functions
834    function get_archive_list_data($args) {
835        global $mt;
836        $blog_id = $args['blog_id'];
837        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
838        $auth_order = $args['sort_order'] == 'ascend' ? 'asc'
839            :  $args['sort_order'] == 'descend' ? 'desc'
840            : '';
841        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
842        $ctx = $mt->context();
843        $index = $ctx->stash('index_archive');
844        #if (!$index) {
845            $author = $ctx->stash('archive_author');
846            $author or $author = $ctx->stash('author');
847            if (isset($author)) {
848                $author_filter = " and entry_author_id=".$author['author_id'];
849            }
850        #}
851
852        $sql = "
853            select count(*),
854                   $year_ext as y,
855                   entry_author_id
856              from mt_entry
857                   join mt_author on entry_author_id = author_id
858             where entry_blog_id = $blog_id
859               and entry_status = 2
860               $author_filter
861             group by
862                   $year_ext,
863                   entry_author_id
864             order by
865                   $year_ext $order
866                   <LIMIT>";
867        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
868        $results = $mt->db->get_results($group_sql, ARRAY_N);
869        if (is_array($results)) {
870            $hi = sprintf("%04d0000000000", $results[0][1]);
871            $low = sprintf("%04d0000000000", $results[count($results)-1][1]);
872        }
873        return array($results, null, null);;
874    }
875
876    function prepare_list(&$ctx, &$row) {
877        $author_id = $row[2];
878        $author = $ctx->mt->db->fetch_author($author_id);
879        $ctx->stash('author', $author);
880    }
881
882    function template_params(&$ctx) {
883        parent::template_params($ctx);
884        $vars =& $ctx->__stash['vars'];
885        $vars['author_yearly_archive'] = 1;
886        $vars['archive_class'] = 'author-yearly-archive';
887    }
888}
889
890class MonthlyAuthorBasedArchiver extends DateBasedAuthorArchiver {
891
892    // Override Method
893    function get_label($args, $ctx) {
894        return $ctx->mt->translate('Author Monthly');
895    }
896    function get_title($args, $ctx) {
897        $author_name = parent::get_author_name($ctx);
898        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
899        list($start) = start_end_month($stamp, $ctx->stash('blog'));
900        $format = $args['format'];
901        $format or $format = "%B %Y";
902        return $author_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
903    }
904   
905    function get_archive_name() {
906        return 'Author-Monthly';
907    }
908
909    function get_range(&$ctx, &$row) {
910        if (is_array($row)) {
911            return start_end_month(sprintf('%04d%02d%02d', $row[0], $row[1], '01'));
912        } else {
913            return start_end_month($row);
914        }
915    }
916
917    // Functions
918    function get_archive_list_data($args) {
919        global $mt;
920        $blog_id = $args['blog_id'];
921        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
922        $auth_order = $args['sort_order'] == 'ascend' ? 'asc'
923            :  $args['sort_order'] == 'descend' ? 'desc'
924            : '';
925        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
926        $month_ext = $mt->db->apply_extract_date('month', 'entry_authored_on');
927        $ctx = $mt->context(); 
928        $index = $ctx->stash('index_archive');
929
930        #if (!$index) {
931            $author = $ctx->stash('archive_author');
932            $author or $author = $ctx->stash('author');
933            if (isset($author)) {
934                $author_filter = " and entry_author_id=".$author['author_id'];
935            }
936        #}
937        $inside = $ctx->stash('inside_archive_list');
938        if (!isset($inside)) {
939          $inside = false;
940        }
941        if ($inside) {
942            $ts = $ctx->stash('current_timestamp');
943            $tsend = $ctx->stash('current_timestamp_end');
944            if ($ts && $tsend) {
945                $ts = $mt->db->ts2db($ts);
946                $tsend = $mt->db->ts2db($tsend);
947                $date_filter = "and (entry_authored_on between '$ts' and '$tsend')";
948            }
949        }
950
951        $sql = "
952            select count(*),
953                   $year_ext as y,
954                   $month_ext as m,
955                   entry_author_id
956              from mt_entry
957                   join mt_author on entry_author_id = author_id
958             where entry_blog_id = $blog_id
959               and entry_status = 2
960               $date_filter
961               $author_filter
962             group by
963                   $year_ext,
964                   $month_ext,
965                   entry_author_id
966             order by
967                   $year_ext $order,
968                   $month_ext $order
969                   <LIMIT>";
970        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
971        $results = $mt->db->get_results($group_sql, ARRAY_N);
972        if (is_array($results)) {
973            $hi = sprintf("%04d%02d00000000", $results[0][1], $results[0][2]);
974            $low = sprintf("%04d%02d00000000", $results[count($results)-1][1], $results[count($results)-1][2]);
975        }
976        return array($results, null, null);;
977    }
978
979    function prepare_list(&$ctx, &$row) {
980        $author_id = $row[3];
981        $author = $ctx->mt->db->fetch_author($author_id);
982        $ctx->stash('author', $author);
983    }
984
985    function template_params(&$ctx) {
986        parent::template_params($ctx);
987        $vars =& $ctx->__stash['vars'];
988        $vars['author_monthly_archive'] = 1;
989        $vars['archive_class'] = 'author-monthly-archive';
990        $vars['module_author-monthly_archives'] = 1;
991    }
992}
993
994class DailyAuthorBasedArchiver extends DateBasedAuthorArchiver {
995
996    // Override Method
997    function get_label($args, $ctx) {
998        return $ctx->mt->translate('Author Daily');
999    }
1000    function get_title($args, $ctx) {
1001        $author_name = parent::get_author_name($ctx);
1002        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
1003        list($start) = start_end_day($stamp, $ctx->stash('blog'));
1004        $format = $args['format'];
1005        $format or $format = "%x";
1006        return $author_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
1007    }
1008   
1009    function get_archive_name() {
1010        return 'Author-Daily';
1011    }
1012
1013    function get_range(&$ctx, &$row) {
1014        if (is_array($row)) {
1015            $d = sprintf("%04d%02d%02d000000", $row[0], $row[1], $row[2]);
1016            return start_end_day($d);
1017        } else {
1018            return start_end_day($row);
1019        }
1020    }
1021
1022    // Functions
1023    function get_archive_list_data($args) {
1024        global $mt;
1025        $blog_id = $args['blog_id'];
1026        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1027        $auth_order = $args['sort_order'] == 'ascend' ? 'asc'
1028            :  $args['sort_order'] == 'descend' ? 'desc'
1029            : '';
1030        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
1031        $month_ext = $mt->db->apply_extract_date('month', 'entry_authored_on');
1032        $day_ext = $mt->db->apply_extract_date('day', 'entry_authored_on');
1033        $ctx = $mt->context();
1034        $index = $ctx->stash('index_archive');
1035        #if (!$index) {
1036            $author = $ctx->stash('archive_author');
1037            $author or $author = $ctx->stash('author');
1038            if (isset($author)) {
1039                $author_filter = " and entry_author_id=".$author['author_id'];
1040            }
1041        #}
1042        $inside = $ctx->stash('inside_archive_list');
1043        if (!isset($inside)) {
1044          $inside = false;
1045        }
1046        if ($inside) {
1047            $ts = $ctx->stash('current_timestamp');
1048            $tsend = $ctx->stash('current_timestamp_end');
1049            if ($ts && $tsend) {
1050                $ts = $mt->db->ts2db($ts);
1051                $tsend = $mt->db->ts2db($tsend);
1052                $date_filter = "and (entry_authored_on between '$ts' and '$tsend')";
1053            }
1054        }
1055
1056        $sql = "
1057            select count(*),
1058                   $year_ext as y,
1059                   $month_ext as m,
1060                   $day_ext as d,
1061                   entry_author_id
1062              from mt_entry
1063                   join mt_author on entry_author_id = author_id
1064             where entry_blog_id = $blog_id
1065               and entry_status = 2
1066               $date_filter
1067               $author_filter
1068             group by
1069                   $year_ext,
1070                   $month_ext,
1071                   $day_ext,
1072                   entry_author_id
1073             order by
1074                   $year_ext $order,
1075                   $month_ext $order,
1076                   $day_ext $order
1077                   <LIMIT>";
1078        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1079        $results = $mt->db->get_results($group_sql, ARRAY_N);
1080        if (is_array($results)) {
1081            $hi = sprintf("%04d%02d%02d000000", $results[0][1], $results[0][2], $results[0][3]);
1082            $low = sprintf("%04d%02d%02d000000", $results[count($results)-1][1], $results[count($results)-1][2], $results[count($results)-1][3]);
1083        }
1084        return array($results, null, null);;
1085    }
1086
1087    function prepare_list(&$ctx, &$row) {
1088        $author_id = $row[4];
1089        $author = $ctx->mt->db->fetch_author($author_id);
1090        $ctx->stash('author', $author);
1091    }
1092
1093    function template_params(&$ctx) {
1094        parent::template_params($ctx);
1095        $vars =& $ctx->__stash['vars'];
1096        $vars['author_daily_archive'] = 1;
1097        $vars['archive_class'] = 'author-daily-archive';
1098    }
1099}
1100
1101class WeeklyAuthorBasedArchiver extends DateBasedAuthorArchiver {
1102
1103    // Override Method
1104    function get_label($args, $ctx) {
1105        return $ctx->mt->translate('Author Weekly');
1106    }
1107    function get_title($args, $ctx) {
1108        $author_name = parent::get_author_name($ctx);
1109        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
1110        list($start, $end) = start_end_week($stamp, $ctx->stash('blog'));
1111        $format = $args['format'];
1112        $format or $format = "%x";
1113        return $author_name
1114            .$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx)
1115            .' - '.$ctx->_hdlr_date(array('ts' => $end, 'format' => $format), $ctx);
1116    }
1117   
1118    function get_archive_name() {
1119        return 'Author-Weekly';
1120    }
1121
1122    function get_range(&$ctx, &$row) {
1123        if (is_array($row)) {
1124            $week_yr = substr($row[0], 0, 4);
1125            $week_wk = substr($row[0], 4);
1126            list($y, $m, $d) = week2ymd($week_yr, $week_wk);
1127            return start_end_week(sprintf("%04d%02d%02d000000", $y, $m, $d));
1128        } else {
1129            return start_end_week($row);
1130        }
1131    }
1132
1133    // Functions
1134    function get_archive_list_data($args) {
1135        global $mt;
1136        $blog_id = $args['blog_id'];
1137        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1138        $auth_order = $args['sort_order'] == 'ascend' ? 'asc'
1139            :  $args['sort_order'] == 'descend' ? 'desc'
1140            : '';
1141        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
1142        $month_ext = $mt->db->apply_extract_date('month', 'entry_authored_on');
1143        $day_ext = $mt->db->apply_extract_date('day', 'entry_authored_on');
1144        $ctx = $mt->context();
1145        $index = $ctx->stash('index_archive');
1146        #if (!$index) {
1147            $author = $ctx->stash('archive_author');
1148            $author or $author = $ctx->stash('author');
1149            if (isset($author)) {
1150                $author_filter = " and entry_author_id=".$author['author_id'];
1151            }
1152        #}
1153        $inside = $ctx->stash('inside_archive_list');
1154        if (!isset($inside)) {
1155          $inside = false;
1156        }
1157        if ($inside) {
1158            $ts = $ctx->stash('current_timestamp');
1159            $tsend = $ctx->stash('current_timestamp_end');
1160            if ($ts && $tsend) {
1161                $ts = $mt->db->ts2db($ts);
1162                $tsend = $mt->db->ts2db($tsend);
1163                $date_filter = "and (entry_authored_on between '$ts' and '$tsend')";
1164            }
1165        }
1166
1167        $sql = "
1168            select count(*),
1169                   entry_week_number,
1170                   entry_author_id
1171              from mt_entry
1172                   join mt_author on entry_author_id = author_id
1173             where entry_blog_id = $blog_id
1174               and entry_status = 2
1175               $date_filter
1176               $author_filter
1177             group by
1178                   entry_week_number,
1179                   entry_author_id
1180             order by
1181                   entry_week_number $order
1182                   <LIMIT>";
1183        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1184        $results = $mt->db->get_results($group_sql, ARRAY_N);
1185        if (is_array($results)) {
1186            $week_yr = substr($results[0][1], 0, 4);
1187            $week_num = substr($results[0][1], 4);
1188            list($y, $m, $d) = week2ymd($week_yr, $week_num);
1189            $hi = sprintf("%04d%02d%02d000000", $y, $m, $d);
1190            $week_yr = substr($results[count($result)-1][1], 0, 4);
1191            $week_num = substr($results[count($result)-1][1], 4);
1192            list($y, $m, $d) = week2ymd($week_yr, $week_num);
1193            $low = sprintf("%04d%02d%02d000000", $y, $m, $d);
1194        }
1195        return array($results, null, null);;
1196    }
1197
1198    function prepare_list(&$ctx, &$row) {
1199        $author_id = $row[2];
1200        $author = $ctx->mt->db->fetch_author($author_id);
1201        $ctx->stash('author', $author);
1202    }
1203
1204    function template_params(&$ctx) {
1205        parent::template_params($ctx);
1206        $vars =& $ctx->__stash['vars'];
1207        $vars['author_weekly_archive'] = 1;
1208        $vars['archive_class'] = 'author-weekly-archive';
1209    }
1210}
1211
1212class DateBasedCategoryArchiver extends DateBasedArchiver {
1213    // Override method
1214    function &get_archive_list($ctx, $args) {
1215        global $mt;
1216        list($results, $hi, $low) = 
1217            $this->get_archive_list_data($args);
1218        if(is_array($results)) {
1219            $blog_id = $args['blog_id'];
1220            $range = "'$low' and '$hi'";
1221            $at = $ctx->stash('current_archive_type');
1222            $link_cache_sql = "
1223                select fileinfo_startdate, fileinfo_url, blog_site_url, blog_file_extension
1224                  from mt_fileinfo, mt_templatemap, mt_blog
1225                 where fileinfo_startdate between $range
1226                   and fileinfo_archive_type = '$at'
1227                   and blog_id = $blog_id
1228                   and fileinfo_blog_id = blog_id
1229                   and templatemap_id = fileinfo_templatemap_id
1230                   and templatemap_is_preferred = 1
1231            ";
1232            $cache_results = $mt->db->get_results($link_cache_sql, ARRAY_N);
1233            if (is_array($cache_results)) {
1234                foreach ($cache_results as $row) {
1235                    $date = $ctx->mt->db->db2ts($row[0]);
1236                    $blog_url = $row[2];
1237                    $blog_url = preg_replace('!(https?://(?:[^/]+))/.*!', '$1', $blog_url);
1238                    $url = $blog_url . $row[1];
1239                    $url = _strip_index($url, array('blog_file_extension' => $row[3]));
1240                    $mt->db->_archive_link_cache[$date.';'.$at] = $url;
1241                }
1242            }
1243        }
1244        return $results;
1245    }
1246
1247    function get_archive_link_sql($ctx, $ts, $at, $args) {
1248        $blog_id = intval($args['blog_id']);
1249        $blog_id or $blog_id = intval($ctx->stash('blog_id'));
1250        $cat = $ctx->stash('category');
1251        $cat_id = $cat['category_id'];
1252        if (isset($ts)) {
1253            if ($at == 'Category-Monthly') {
1254                $ts = substr($ts, 0, 6) . '01000000';
1255            } elseif ($at == 'Category-Daily') {
1256                $ts = substr($ts, 0, 8) . '000000';
1257            } elseif ($at == 'Category-Weekly') {
1258                require_once("MTUtil.php");
1259                list($ws, $we) = start_end_week($ts);
1260                $ts = $ws;
1261            } elseif ($at == 'Category-Yearly') {
1262                $ts = substr($ts, 0, 4) . '0101000000';
1263            }
1264            $start_filter = "and fileinfo_startdate = '$ts'";
1265        } else {
1266            // find a most oldest link when timestamp was not presented
1267            $order = "order by fileinfo_startdate asc";
1268        }
1269
1270        $sql = "select fileinfo_url
1271                  from mt_fileinfo, mt_templatemap
1272                 where fileinfo_blog_id = $blog_id
1273                   and fileinfo_archive_type = '".$ctx->mt->db->escape($at)."'
1274                   and fileinfo_category_id = '$cat_id'
1275                   and templatemap_id = fileinfo_templatemap_id
1276                   and templatemap_is_preferred = 1
1277                   $start_filter
1278                 $order";
1279        return $sql;
1280    }
1281
1282    function archive_prev_next($args, $content, &$ctx, &$repeat, $tag) {
1283        $localvars = array('current_timestamp', 'current_timestamp_end', 'entries');
1284        if (!isset($content)) {
1285            $ctx->localize($localvars);
1286            $is_prev = $tag == 'archiveprevious';
1287            $ts = $ctx->stash('current_timestamp');
1288            $category = $ctx->stash('category');
1289            if (!$ts || !$category) {
1290                return $ctx->error(
1291                   "You used an <mt$tag> without a date context set up.");
1292            }
1293            $order = $is_prev ? 'previous' : 'next';
1294
1295            $at = $ctx->stash('current_archive_type');
1296            if ($at == 'Category-Monthly') {
1297                $wide = 'MONTHLY';
1298            } elseif ($at == 'Category-Daily') {
1299                $wide = 'DAILY';
1300            } elseif ($at == 'Category-Weekly') {
1301                $wide = 'WEEKLY';
1302            } elseif ($at == 'Category-Yearly') {
1303                $wide = 'YEARLY';
1304            }
1305
1306            if ($entry = $this->get_categorized_entry($ts, $ctx->stash('blog_id'), $category['category_id'], $wide, $order)) {
1307                $helper = $this->get_helper($wide);
1308                $ctx->stash('entries', array( $entry ));
1309                list($start, $end) = $helper($entry['entry_authored_on']);
1310                $ctx->stash('current_timestamp', $start);
1311                $ctx->stash('current_timestamp_end', $end);
1312                $ctx->stash('category', $category);
1313            } else {
1314                $ctx->restore($localvars);
1315                $repeat = false;
1316            }
1317        } else {
1318            $ctx->restore($localvars);
1319        }
1320        return $content;
1321    }
1322
1323    function setup_args($ctx, &$args) {
1324        if ($cat = $ctx->stash('category')) {
1325            $args['category_id'] = $cat['category_id'];
1326        }
1327    }
1328
1329    function get_categorized_entry($ts, $blog_id, $cat_id, $at, $order) {
1330        $helper = $this->get_helper($at);
1331        list($start, $end) = $helper($ts);
1332        $args = array();
1333        if ($order == 'previous') {
1334            $args['current_timestamp_end'] = $this->dec_ts($start);
1335        } else {
1336            $args['current_timestamp'] = $this->inc_ts($end);
1337            $args['base_sort_order'] = 'ascend'; # ascending order
1338        }
1339        $args['lastn'] = 1;
1340        $args['blog_id'] = $blog_id;
1341        $args['category_id'] = $cat_id;
1342        global $mt;
1343        list($entry) = $mt->db->fetch_entries($args);
1344        return $entry;
1345    }
1346
1347    function template_params(&$ctx) {
1348        parent::template_params($ctx);
1349        $vars =& $ctx->__stash['vars'];
1350        $vars['archive_template'] = 1;
1351        $vars['category_archive'] = 1;
1352    }
1353
1354    function get_category_name ($ctx) {
1355        global $_archivers;
1356        $curr_at = $ctx->stash('current_archive_type');
1357        $archiver = $_archivers[$curr_at];
1358        $cat = $ctx->stash('category');
1359        $cat_name = '';
1360
1361        if ($ctx->stash('index_archive')
1362            || !isset($archiver)
1363            || (isset($archiver) && !isset($cat))
1364            || !$ctx->stash('inside_archive_list'))
1365        {
1366            $cat = $ctx->stash('archive_category');
1367            $cat or $cat = $ctx->stash('category');
1368            if (isset($cat)) {
1369                $cat_name = $cat['category_label'].": ";
1370            }
1371        }
1372        return $cat_name;
1373    }
1374}
1375
1376class YearlyCategoryArchiver extends DateBasedCategoryArchiver {
1377    // Override method
1378    function get_label($args, $ctx) {
1379        return $ctx->mt->translate('Category Yearly');
1380    }
1381    function get_title($args, $ctx) {
1382        $cat_name = parent::get_category_name($ctx);
1383        $stamp = $ctx->stash('current_timestamp'); 
1384        list($start) = start_end_year($stamp, $ctx->stash('blog'));
1385        $format = $args['format'];
1386        $blog = $ctx->stash('blog');
1387        global $mt;
1388        $lang = ($blog && $blog['blog_language'] ? $blog['blog_language'] :
1389            $mt->config('DefaultLanguage'));
1390            if (strtolower($lang) == 'jp' || strtolower($lang) == 'ja') {
1391            $format or $format = "%Y&#24180;";
1392        } else {
1393            $format or $format = "%Y";
1394        }
1395        return $cat_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
1396    }
1397
1398    function get_range(&$ctx, &$row) {
1399        if (is_array($row)) {
1400            return start_end_year($row[0]);
1401        } else {
1402            return start_end_year($row);
1403        }
1404    }
1405
1406    function get_archive_name() {
1407        return 'Category-Yearly';
1408    }
1409
1410    function get_archive_list_data($args) {
1411        global $mt;
1412        $blog_id = $args['blog_id'];
1413        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1414        $cat_order = $args['sort_order'] == 'ascend' ? 'asc'
1415            : $args['sort_order'] == 'descend' ? 'desc'
1416            : '';
1417        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
1418        $ctx = $mt->context();
1419        $index = $ctx->stash('index_archive');
1420        $inside = $ctx->stash('inside_archive_list');
1421        if (!isset($inside)) {
1422          $inside = false;
1423        }
1424        if ($inside) {
1425            $ts = $ctx->stash('current_timestamp');
1426            $tsend = $ctx->stash('current_timestamp_end');
1427            if ($ts && $tsend) {
1428                $ts = $mt->db->ts2db($ts);
1429                $tsend = $mt->db->ts2db($tsend);
1430                $date_filter = "and entry_authored_on between '$ts' and '$tsend'";
1431            }
1432        }
1433        #if (!$index) {
1434            $cat = $ctx->stash('archive_category');
1435            $cat or $cat = $ctx->stash('category');
1436            if (isset($cat)){
1437                $cat_filter = " and placement_category_id=".$cat['category_id'];
1438       
1439            }
1440        #}
1441        $sql = "
1442            select count(*),
1443                   $year_ext as y,
1444                   placement_category_id
1445              from mt_entry join mt_placement on entry_id = placement_entry_id
1446                   join mt_category on placement_category_id = category_id
1447             where entry_blog_id = $blog_id
1448               and entry_status = 2
1449               and entry_class = 'entry'
1450               $cat_filter
1451               $date_filter
1452             group by
1453                   $year_ext,
1454                   placement_category_id
1455             order by
1456                   $year_ext $order
1457                   <LIMIT>";
1458        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1459        $results = $mt->db->get_results($group_sql, ARRAY_N);
1460        if (is_array($results)) {
1461            $hi = sprintf("%04d0000000000", $results[0][1]);
1462            $low = sprintf("%04d0000000000", $results[count($results)-1][1]);
1463        }
1464        return array($results, $hi, $low);
1465    }
1466
1467    function prepare_list(&$ctx, &$row) {
1468        $category_id = $row[2];
1469        $category = $ctx->mt->db->fetch_category($category_id);
1470        $ctx->stash('category', $category);
1471    }
1472
1473    function template_params(&$ctx) {
1474        parent::template_params($ctx);
1475        $vars =& $ctx->__stash['vars'];
1476        $vars['category_yearly_archive'] = 1;
1477        $vars['archive_class'] = 'category-yearly-archive';
1478    }
1479}
1480
1481class MonthlyCategoryArchiver extends DateBasedCategoryArchiver {
1482    // Override method
1483    function get_label($args, $ctx) {
1484        return $ctx->mt->translate('Category Monthly');
1485    }
1486    function get_title($args, $ctx) {
1487        $cat_name = parent::get_category_name($ctx);
1488        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
1489        list($start) = start_end_month($stamp, $ctx->stash('blog'));
1490        $format = $args['format'];
1491        $format or $format = "%B %Y";
1492        return $cat_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
1493    }
1494
1495    function get_range(&$ctx, &$row) {
1496        if (is_array($row)) {
1497            return start_end_month(sprintf('%04d%02d%02d', $row[0], $row[1], "01"));
1498        } else {
1499            return start_end_month($row);
1500        }
1501    }
1502
1503    function get_archive_name() {
1504        return 'Category-Monthly';
1505    }
1506
1507    function get_archive_list_data($args) {
1508        global $mt;
1509        $blog_id = $args['blog_id'];
1510        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1511        $cat_order = $args['sort_order'] == 'ascend' ? 'asc'
1512            : $args['sort_order'] == 'descend' ? 'desc'
1513            : '';
1514        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
1515        $month_ext = $mt->db->apply_extract_date('month', 'entry_authored_on');
1516        $ctx = $mt->context();
1517        $index = $ctx->stash('index_archive');
1518        $inside = $ctx->stash('inside_archive_list');
1519        if (!isset($inside)) {
1520          $inside = false;
1521        }
1522        if ($inside) {
1523            $ts = $ctx->stash('current_timestamp');
1524            $tsend = $ctx->stash('current_timestamp_end');
1525            if ($ts && $tsend) {
1526                $ts = $mt->db->ts2db($ts);
1527                $tsend = $mt->db->ts2db($tsend);
1528                $date_filter = "and entry_authored_on between '$ts' and '$tsend'";
1529            }
1530        }
1531        #if (!$index) {
1532            $cat = $ctx->stash('archive_category');
1533            $cat or $cat = $ctx->stash('category');
1534            if(isset($cat)) {
1535                $cat_filter = " and placement_category_id=".$cat['category_id'];
1536       
1537            }
1538        #}
1539        $sql = "
1540            select count(*),
1541                   $year_ext as y,
1542                   $month_ext as m,
1543                   placement_category_id
1544              from mt_entry join mt_placement on entry_id = placement_entry_id
1545                   join mt_category on placement_category_id = category_id
1546             where entry_blog_id = $blog_id
1547               and entry_status = 2
1548               and entry_class = 'entry'
1549               $cat_filter
1550               $date_filter
1551             group by
1552                   $year_ext,
1553                   $month_ext,
1554                   placement_category_id
1555             order by
1556                   $year_ext $order,
1557                   $month_ext $order
1558                   <LIMIT>";
1559        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1560        $results = $mt->db->get_results($group_sql, ARRAY_N);
1561        if (is_array($results)) {
1562            $hi = sprintf("%04d%02d00000000", $results[0][1], $results[0][2]);
1563            $low = sprintf("%04d%02d00000000", $results[count($results)-1][1], $results[count($results)-1][2]);
1564        }
1565        return array($results, $hi, $low);;
1566    }
1567
1568    function prepare_list(&$ctx, &$row) {
1569        $category_id = $row[3];
1570        $category = $ctx->mt->db->fetch_category($category_id);
1571        $ctx->stash('category', $category);
1572    }
1573
1574    function template_params(&$ctx) {
1575        parent::template_params($ctx);
1576        $vars =& $ctx->__stash['vars'];
1577        $vars['archive_class'] = 'category-monthlyl-archive';
1578        $vars['category-monthly_archive'] = 1;
1579        $vars['module_category-monthly_archives'] = 1;
1580    }
1581}
1582
1583class DailyCategoryArchiver extends DateBasedCategoryArchiver {
1584    // Override method
1585    function get_label($args, $ctx) {
1586        return $ctx->mt->translate('Category Daily');
1587    }
1588    function get_title($args, $ctx) {
1589        $cat_name = parent::get_category_name($ctx);
1590        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
1591        list($start) = start_end_day($stamp, $ctx->stash('blog'));
1592        $format = $args['format'];
1593        $format or $format = "%x";
1594        return $cat_name.$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx);
1595    }
1596
1597    function get_range(&$ctx, &$row) {
1598        if (is_array($row)) {
1599            return start_end_day(sprintf('%04d%02d%02d', $row[0], $row[1], $row[2]));
1600        } else {
1601            return start_end_day($row);
1602        }
1603    }
1604
1605    function get_archive_name() {
1606        return 'Category-Daily';
1607    }
1608
1609    function get_archive_list_data($args) {
1610        global $mt;
1611        $blog_id = $args['blog_id'];
1612        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1613        $cat_order = $args['sort_order'] == 'ascend' ? 'asc'
1614            : $args['sort_order'] == 'descend' ? 'desc'
1615            : '';
1616        $year_ext = $mt->db->apply_extract_date('year', 'entry_authored_on');
1617        $month_ext = $mt->db->apply_extract_date('month', 'entry_authored_on');
1618        $day_ext = $mt->db->apply_extract_date('day', 'entry_authored_on');
1619        $ctx = $mt->context();
1620        $index = $ctx->stash('index_archive');
1621        $inside = $ctx->stash('inside_archive_list');
1622        if (!isset($inside)) {
1623          $inside = false;
1624        }
1625        if ($inside) {
1626            $ts = $ctx->stash('current_timestamp');
1627            $tsend = $ctx->stash('current_timestamp_end');
1628            if ($ts && $tsend) {
1629                $ts = $mt->db->ts2db($ts);
1630                $tsend = $mt->db->ts2db($tsend);
1631                $date_filter = "and entry_authored_on between '$ts' and '$tsend'";
1632            }
1633        }
1634        #if (!$index) {
1635            $cat = $ctx->stash('archive_category');
1636            $cat or $cat = $ctx->stash('category');
1637            if(isset($cat)) {
1638                $cat_filter = " and placement_category_id=".$cat['category_id'];
1639       
1640            }
1641        #}
1642        $sql = "
1643            select count(*),
1644                   $year_ext as y,
1645                   $month_ext as m,
1646                   $day_ext as d,
1647                   placement_category_id
1648              from mt_entry join mt_placement on entry_id = placement_entry_id
1649                   join mt_category on placement_category_id = category_id
1650             where entry_blog_id = $blog_id
1651               and entry_status = 2
1652               and entry_class = 'entry'
1653               $cat_filter
1654               $date_filter
1655             group by
1656                   placement_category_id,
1657                   $year_ext,
1658                   $month_ext,
1659                   $day_ext
1660             order by
1661                   $year_ext $order,
1662                   $month_ext $order,
1663                   $day_ext $order
1664                   <LIMIT>";
1665        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1666        $results = $mt->db->get_results($group_sql, ARRAY_N);
1667        if (is_array($results)) {
1668            $hi = sprintf("%04d%02d%02d000000", $results[0][1], $results[0][2], $results[0][3]);
1669            $low = sprintf("%04d%02d%02d000000", $results[count($results)-1][1], $results[count($results)-1][2], $results[count($results)-1][3]);
1670        }
1671        return array($results, $hi, $low);
1672    }
1673
1674    function prepare_list(&$ctx, &$row) {
1675        $category_id = $row[4];
1676        $category = $ctx->mt->db->fetch_category($category_id);
1677        $ctx->stash('category', $category);
1678    }
1679
1680    function template_params(&$ctx) {
1681        parent::template_params($ctx);
1682        $vars =& $ctx->__stash['vars'];
1683        $vars['category_daily_archive'] = 1;
1684        $vars['archive_class'] = 'category-daily-archive';
1685    }
1686}
1687
1688class WeeklyCategoryArchiver extends DateBasedCategoryArchiver {
1689    // Override method
1690    function get_label($args, $ctx) {
1691        return $ctx->mt->translate('Category Weekly');
1692    }
1693    function get_title($args, $ctx) {
1694        $cat_name = parent::get_category_name($ctx);
1695        $stamp = $ctx->stash('current_timestamp'); #$entry['entry_authored_on'];
1696        list($start, $end) = start_end_week($stamp, $ctx->stash('blog'));
1697        $format = $args['format'];
1698        $format or $format = "%x";
1699        return $cat_name
1700            .$ctx->_hdlr_date(array('ts' => $start, 'format' => $format), $ctx)
1701            ." - ".$ctx->_hdlr_date(array('ts' => $end, 'format' => $format), $ctx);
1702    }
1703
1704    function get_range(&$ctx, &$row) {
1705        if (is_array($row)) {
1706            $week_yr = substr($row[0], 0, 4);
1707            $week_wk = substr($row[0], 4);
1708            list($y, $m, $d) = week2ymd($week_yr, $week_wk);
1709            $period_start = sprintf("%04d%02d%02d000000", $y, $m, $d);
1710        } else {
1711            $period_start = $row;
1712        }
1713        return start_end_week($period_start, $ctx->stash('blog'));
1714    }
1715
1716    function get_archive_name() {
1717        return 'Category-Weekly';
1718    }
1719
1720    function get_archive_list_data($args) {
1721        global $mt;
1722        $blog_id = $args['blog_id'];
1723        $order = $args['sort_order'] == 'ascend' ? 'asc' : 'desc';
1724        $cat_order = $args['sort_order'] == 'ascend' ? 'asc'
1725            : $args['sort_order'] == 'descend' ? 'desc'
1726            : '';
1727        $ctx = $mt->context();
1728        $index = $ctx->stash('index_archive');
1729        $inside = $ctx->stash('inside_archive_list');
1730        if (!isset($inside)) {
1731          $inside = false;
1732        }
1733        if ($inside) {
1734            $ts = $ctx->stash('current_timestamp');
1735            $tsend = $ctx->stash('current_timestamp_end');
1736            if ($ts && $tsend) {
1737                $ts = $mt->db->ts2db($ts);
1738                $tsend = $mt->db->ts2db($tsend);
1739                $date_filter = "and entry_authored_on between '$ts' and '$tsend'";
1740            }
1741        }
1742        #if (!$index) {
1743            $cat = $ctx->stash('archive_category');
1744            $cat or $cat = $ctx->stash('category');
1745            if(isset($cat)) {
1746                $cat_filter = " and placement_category_id=".$cat['category_id'];
1747       
1748            }
1749        #}
1750        $sql = "
1751            select count(*),
1752                   entry_week_number,
1753                   placement_category_id
1754              from mt_entry join mt_placement on entry_id = placement_entry_id
1755                   join mt_category on placement_category_id = category_id
1756             where entry_blog_id = $blog_id
1757               and entry_status = 2
1758               and entry_class = 'entry'
1759               $cat_filter
1760               $date_filter
1761             group by
1762                   entry_week_number,
1763                   placement_category_id
1764             order by
1765                   entry_week_number $order
1766                   <LIMIT>";
1767        $group_sql = $mt->db->apply_limit_sql($sql, $args['lastn'], $args['offset']);
1768        $results = $mt->db->get_results($group_sql, ARRAY_N);
1769        if (is_array($results)) {
1770            $week_yr = substr($results[0][1], 0, 4);
1771            $week_num = substr($results[0][1], 4);
1772            list($y, $m, $d) = week2ymd($week_yr, $week_num);
1773            $hi = sprintf("%04d%02d%02d000000", $y, $m, $d);
1774            $week_yr = substr($results[count($result)-1][1], 0, 4);
1775            $week_num = substr($results[count($result)-1][1], 4);
1776            list($y, $m, $d) = week2ymd($week_yr, $week_num);
1777            $low = sprintf("%04d%02d%02d000000", $y, $m, $d);
1778        }
1779        return array($results, $hi, $low);
1780    }
1781
1782    function prepare_list(&$ctx, &$row) {
1783        $category_id = $row[2];
1784        $category = $ctx->mt->db->fetch_category($category_id);
1785        $ctx->stash('category', $category);
1786    }
1787
1788    function template_params(&$ctx) {
1789        parent::template_params($ctx);
1790        $vars =& $ctx->__stash['vars'];
1791        $vars['category_weekly_archive'] = 1;
1792        $vars['archive_class'] = 'category-weekly-archive';
1793    }
1794}
1795?>
Note: See TracBrowser for help on using the browser.