root/branches/release-39/php/lib/block.mtentries.php @ 2496

Revision 2496, 7.3 kB (checked in by bchoate, 18 months ago)

Fix to permit plugins that prepopulate the entries stash to call the entries handler allowing for further filtering using category/tag/author attributes. BugId:80022

  • 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
8function smarty_block_mtentries($args, $content, &$ctx, &$repeat) {
9    $localvars = array('entry', '_entries_counter','entries','current_timestamp','modification_timestamp','_entries_lastn', 'current_timestamp_end', 'DateHeader', 'DateFooter', '_entries_glue', 'blog', 'blog_id', 'conditional', 'else_content', '__out');
10    if (isset($args['sort_by']) && $args['sort_by'] == 'score' && !isset($args['namespace'])) {
11        return $ctx->error($ctx->mt->translate('sort_by="score" must be used in combination with namespace.'));
12    }
13    if (!isset($content)) {
14        $ctx->localize($localvars);
15        // If we have a set of entries that were set based on context,
16        // but the user has specified attributes that effectively
17        // break that context, clear the stashed entries so fetch_entries
18        // can reselect.
19        $this_tag = strtolower($ctx->this_tag());
20        if (($this_tag == 'mtentries') || ($this_tag == 'mtpages')) {
21            if ($ctx->stash('entries') &&
22                (isset($args['category']) || isset($args['categories']) ||
23                 isset($args['tag']) || isset($args['tags']) ||
24                 isset($args['author']) ))
25                $ctx->__stash['entries'] = null;
26        }
27        if ($ctx->__stash['entries'] &&
28            (isset($args['id']) ||
29             isset($args['recently_commented_on']) ||
30             isset($args['include_subcategories']) ||
31             isset($args['days']) ))
32            $ctx->__stash['entries'] = null;
33        $counter = 0;
34        $lastn = $args['lastn'];
35        $ctx->stash('_entries_lastn', $lastn);
36        $ctx->stash('__out', false);
37    } else {
38        $lastn = $ctx->stash('_entries_lastn');
39        $counter = $ctx->stash('_entries_counter');
40        $out = $ctx->stash('__out');
41    }
42    if (!isset($args['class'])) {
43        $args['class'] = 'entry';
44    }
45
46    if ( isset($args['offset']) && ($args['offset'] == 'auto') ) {
47        $l = 0;
48        if ( $args['limit'] ) {
49            if ( $args['limit'] == 'auto' ) {
50                if ( $_REQUEST['limit'] )
51                    $l = $_REQUEST['limit'];
52                else {
53                    $blog_id = intval($ctx->stash('blog_id'));
54                    $blog = $ctx->mt->db->fetch_blog($blog_id);
55                    $l = $blog['blog_entries_on_index'];
56                }
57            }
58            else
59                $l = $args['limit'];
60        }
61        if ( !$l )
62            $l = 20;
63        $ctx->stash('__pager_limit', $l);
64        if ( $_REQUEST['offset'] )
65            $ctx->stash('__pager_offset', $_REQUEST['offset']);
66    }
67
68    $entries = $ctx->stash('entries');
69    if (!isset($entries)) {
70        global $_archivers;
71        if (!isset($_archivers)) {
72            require_once('archive_lib.php');
73        }
74        $at = $ctx->stash('current_archive_type');
75        $archiver = $_archivers[$at];
76        $args['blog_id'] = $ctx->stash('blog_id');
77        if (isset($args['id'])) {
78            $args['entry_id'] = $args['id'];
79        }
80        $ts = $ctx->stash('current_timestamp');
81        $tse = $ctx->stash('current_timestamp_end');
82        if ($ts && $tse) {
83            # assign date range if we have both
84            # start and end date
85            $args['current_timestamp'] = $ts;
86            $args['current_timestamp_end'] = $tse;
87        }
88        if (isset($archiver)) {
89            ($args['limit'] || $args['lastn']) or $args['lastn'] = -1;
90            $archiver->setup_args($ctx, $args);
91        }
92        $cat = $ctx->stash('category');
93        if (isset($cat) && (($args['class'] == 'entry' && $cat['category_class'] == 'category') || ($args['class'] == 'page' && $cat['category_class'] == 'folder'))) {
94            $args['category'] or $args['categories'] or $args['category_id'] = $cat['category_id'];
95            if ($ctx->stash('inside_mt_categories')) {
96                $args['category_id'] = $cat['category_id'];
97                $args['show_empty'] = $ctx->stash('show_empty');
98            } else {
99                $args['category'] or $args['categories'] or $args['category_id'] = $cat['category_id'];
100            }
101        }
102
103        if ($tag = $ctx->stash('Tag')) {
104            $args['tag'] or $args['tags'] or $args['tags'] = is_array($tag) ? $tag['tag_name'] : $tag;
105        }
106        if ( isset($args['offset']) && ($args['offset'] == 'auto') )
107            $total_count = 0;
108        $entries =& $ctx->mt->db->fetch_entries($args, &$total_count);
109        if ( isset($args['offset']) && ($args['offset'] == 'auto') )
110            $ctx->stash('__pager_total_count', $total_count);
111        $ctx->stash('entries', $entries);
112    }
113
114    $ctx->stash('conditional', empty($entries) ? 0 : 1);
115    if (empty($entries)) {
116        $ret = $ctx->_hdlr_if($args, $content, $ctx, $repeat, 0);
117        if (!$repeat)
118              $ctx->restore($localvars);
119        return $ret;
120    }
121
122    $ctx->stash('_entries_glue', $args['glue']);
123    if (($lastn > count($entries)) || ($lastn == -1)) {
124        $lastn = count($entries);
125        $ctx->stash('_entries_lastn', $lastn);
126    }
127    if ($lastn ? ($counter < $lastn) : ($counter < count($entries))) {
128        $blog_id = $ctx->stash('blog_id');
129        $entry = $entries[$counter];
130        if ($blog_id != $entry['entry_blog_id']) {
131            $blog_id = $entry['entry_blog_id'];
132            $ctx->stash('blog_id', $blog_id);
133            $ctx->stash('blog', $ctx->mt->db->fetch_blog($blog_id));
134        }
135        if ($counter > 0) {
136            $last_entry_created_on = $entries[$counter-1]['entry_authored_on'];
137        } else {
138            $last_entry_created_on = '';
139        }
140        if ($counter < count($entries)-1) {
141            $next_entry_created_on = $entries[$counter+1]['entry_authored_on'];
142        } else {
143            $next_entry_created_on = '';
144        }
145        $ctx->stash('DateHeader', !(substr($entry['entry_authored_on'], 0, 8) == substr($last_entry_created_on, 0, 8)));
146        $ctx->stash('DateFooter', (substr($entry['entry_authored_on'], 0, 8) != substr($next_entry_created_on, 0, 8)));
147        $ctx->stash('entry', $entry);
148        $ctx->stash('current_timestamp', $entry['entry_authored_on']);
149        $ctx->stash('current_timestamp_end', null);
150        $ctx->stash('modification_timestamp', $entry['entry_modified_on']);
151        $ctx->stash('_entries_counter', $counter + 1);
152        $_REQUEST['entry_ids_published'][$entry['entry_id']] = 1;
153        $glue = $ctx->stash('_entries_glue');
154        if (isset($glue) && !empty($content)) {
155            if ($out)
156                $content = $glue . $content;
157            else
158                $ctx->stash('__out', true);
159        }
160        $count = $counter + 1;
161        $ctx->__stash['vars']['__counter__'] = $count;
162        $ctx->__stash['vars']['__odd__'] = ($count % 2) == 1;
163        $ctx->__stash['vars']['__even__'] = ($count % 2) == 0;
164        $ctx->__stash['vars']['__first__'] = $count == 1;
165        $ctx->__stash['vars']['__last__'] = ($count == count($entries));
166        $repeat = true;
167    } else {
168        $glue = $ctx->stash('_entries_glue');
169        if (isset($glue) && $out && !empty($content))
170            $content = $glue . $content;
171        $ctx->restore($localvars);
172        $repeat = false;
173    }
174    return $content;
175}
Note: See TracBrowser for help on using the browser.