root/branches/release-36/php/lib/block.mttags.php @ 2097

Revision 2097, 4.4 kB (checked in by bchoate, 19 months ago)

Support 'top' attribute for MTTags for dynamic publishing. BugId:68999

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_mttags($args, $content, &$ctx, &$repeat) {
9  $localvars = array('_tags', 'Tag', '_tags_counter', 'tag_min_count', 'tag_max_count', 'all_tag_count', 'include_blogs', 'exclude_blogs', 'blog_ids', '__out');
10    if (!isset($content)) {
11        $ctx->localize($localvars);
12        $ctx->stash('include_blogs', $args['include_blogs']);
13        $ctx->stash('exclude_blogs', $args['exclude_blogs']);
14        $ctx->stash('blog_ids', $args['blog_ids']);
15        $blog_id = $ctx->stash('blog_id');
16        $args['blog_id'] = $ctx->stash('blog_id');
17        if (isset($args['top'])) {
18            $post_sort_by = $args['sort_by'];
19            $post_sort_order = $args['sort_order'];
20            $args['sort_by'] = 'rank';
21            $args['sort_order'] = 'descend';
22        }
23        if (isset($args['sort_by'])) {
24            $s = $args['sort_by'];
25            if (($s == 'rank') || ($s == 'count')) { // Aliased
26                $args['sort_by'] = 'count';
27                $args['sort_order'] or $args['sort_order'] = 'descend'; // Inverted default
28            } elseif (($s != 'name') && ($s != 'id')) {
29                $args['sort_by'] = NULL;
30            }
31        }
32        $type = 'entry';
33        if (isset($args['type'])) {
34            $type = strtolower($args['type']);
35        }
36        if ('page' == $type) {
37            $args['class'] = 'page';
38            $tags = $ctx->mt->db->fetch_entry_tags($args);
39        } elseif ('asset' == $type) {
40            $tags = $ctx->mt->db->fetch_asset_tags($args);
41        } else {
42          $args['class'] = 'entry';
43            $tags = $ctx->mt->db->fetch_entry_tags($args);
44        }
45        $min = 0; $max = 0;
46        $all_count = 0;
47        if ($tags) {
48            foreach ($tags as $tag) {
49                $count = $tag['tag_count'];
50                if ($count > $max) $max = $count;
51                if ($count < $min or $min == 0) $min = $count;
52                $all_count += $count;
53            }
54            if (isset($args['limit'])) {
55                $tags = array_slice($tags, 0, $args['limit']);
56            }
57
58            // Handle ordering based on 'top' attribute
59            // implies sorting by rank/descend and limit by # requested
60            // then, resort based on attributes or sane defaults
61            if (isset($args['top'])) {
62                $tags = array_slice($tags, 0, $args['top']);
63                // now, resort by original sort order
64                $post_sort_by or $post_sort_by = 'name';
65                if ($post_sort_by == 'name') {
66                    $post_sort_order or $post_sort_order = 'ascend';
67                    require_once("MTUtil.php");
68                    usort($tags, 'tagarray_name_sort');
69                    if ($post_sort_order && ($post_sort_order == 'descend')) {
70                        $tags = array_reverse($tags);
71                    }
72                } elseif (($post_sort_by == 'rank') || ($post_sort_by == 'count')) {
73                    $post_sort_order or $post_sort_order = 'descend';
74                    // we're already sorted by rank; just check if
75                    // order is not descending
76                    if ($post_sort_order != 'descend') {
77                        $tags = array_reverse($tags);
78                    }
79                }
80            }
81        }
82        $ctx->stash('tag_min_count', $min);
83        $ctx->stash('tag_max_count', $max);
84        $ctx->stash('all_tag_count', $all_count);
85        $ctx->stash('_tags', $tags);
86        $ctx->stash('__out', false);
87        $counter = 0;
88    } else {
89        $tags = $ctx->stash('_tags');
90        $counter = $ctx->stash('_tags_counter');
91        $out = $ctx->stash('__out');
92    }
93
94    if ($counter < count($tags)) {
95        $tag = $tags[$counter];
96        $ctx->stash('Tag', $tag);
97        $ctx->stash('_tags_counter', $counter + 1);
98        $repeat = true;
99        if (isset($args['glue'])) {
100            if ($out && !empty($content))
101                $content = $args['glue'] . $content;
102            if (!$out && !empty($content))
103              $ctx->stash('__out', true);
104        }
105    } else {
106        if (isset($args['glue'])) {
107            if ($out && !empty($content))
108                $content = $args['glue'] . $content;
109        }
110        $ctx->restore($localvars);
111        $repeat = false;
112    }
113    return $content;
114}
Note: See TracBrowser for help on using the browser.