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

Revision 1742, 62.9 kB (checked in by bsmith, 20 months ago)

bugzid:75115 - remove 'main_template' variable

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