root/branches/release-35/php/lib/block.mtloop.php @ 1926

Revision 1926, 3.9 kB (checked in by takayama, 20 months ago)

Fixed BugId:76389
* Loop tags does not add 'glue', if row in loop tag was empty

  • MTFor
  • MTLoop
  • MTTags
  • MTEntryTags
  • MTEntries
  • MTEntryCategories
  • MTCategories
  • MTEntryAdditionalCategories
  • MTParentCategories
  • MTAssetTags
  • MTPagerBlock
  • Property svn:keywords set to Id Author Date 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_mtloop($args, $content, &$ctx, &$repeat) {
9    $localvars = array('__loop_keys', '__loop_values', '__out');
10
11    if (!isset($content)) {
12        $ctx->localize($localvars);
13        $vars =& $ctx->__stash['vars'];
14        $value = '';
15        $name = $args['name'];
16        $name or $name = $args['var'];
17        if (!$name) return '';
18        if (isset($vars[$name]))
19            $value = $vars[$name];
20        if ( !is_array($value)
21          && preg_match('/^smarty_fun_[a-f0-9]+$/', $value) ) {
22            if (function_exists($value)) {
23                ob_start();
24                $value($ctx, array());
25                $value = ob_get_contents();
26                ob_end_clean();
27            } else {
28                $value = '';
29            }
30        }
31        if ( !is_array($value) || (0 == count($value)) ) {
32            $repeat = false;
33            return '';
34        }
35        $sort = $args['sort_by'];
36        $keys = array_keys($value);
37        if ($sort) {
38            $sort = strtolower($sort);
39            if (preg_match('/\bkey\b/', $sort)) {
40                usort($keys, create_function(
41                    '$a,$b',
42                    'return strcmp($a, $b);'
43                ));
44            } elseif (preg_match('/\bvalue\b/', $sort)) {
45                $sort_fn = '';
46                foreach (array_keys($value) as $key) {
47                    $v = $value[$key];
48                    $sort_fn .= "\$value['$key']='$v';";
49                }
50                if (preg_match('/\bnumeric\b/', $sort)) {
51                    $sort_fn .= 'return $value[$a] === $value[$b] ? 0 : ($value[$a] > $value[$b] ? 1 : -1);';
52                    $sorter = create_function(
53                        '$a,$b',
54                        $sort_fn);
55                } else {
56                    $sort_fn .= 'return strcmp($value[$a], $value[$b]);';
57                    $sorter = create_function(
58                        '$a,$b',
59                        $sort_fn);
60                }
61                usort($keys, $sorter);
62            }
63            if (preg_match('/\breverse\b/', $sort)) {
64                $keys = array_reverse($keys);
65            }
66        }
67        $counter = 1;
68        $ctx->stash('__loop_values', $value);
69        $ctx->stash('__out', false);
70    }
71    else {
72        $counter = $ctx->__stash['vars']['__counter__'] + 1;
73        $keys = $ctx->stash('__loop_keys');
74        $value = $ctx->stash('__loop_values');
75        $out = $ctx->stash('__out');
76        if (!isset($keys) || $keys == 0) {
77            $ctx->restore($localvars);
78            $repeat = false;
79            if (isset($args['glue']) && $out && !empty($content))
80                $content = $args['glue'] . $content;
81            return $content;
82        }
83    }
84    $key = array_shift($keys);
85    $this_value = $value[$key];
86    $ctx->stash('__loop_keys', $keys);
87
88    $ctx->__stash['vars']['__counter__'] = $counter;
89    $ctx->__stash['vars']['__odd__'] = ($counter % 2) == 1;
90    $ctx->__stash['vars']['__even__'] = ($counter % 2) == 0;
91    $ctx->__stash['vars']['__first__'] = $counter == 1;
92    $ctx->__stash['vars']['__last__'] = count($value) == 0;
93    $ctx->__stash['vars']['__key__'] = $key;
94    $ctx->__stash['vars']['__value__'] = $this_value;
95    if ( is_array($this_value) && (0 < count($this_value)) ) {
96        require_once("MTUtil.php");
97        if ( is_hash($this_value) ) {
98            foreach (array_keys($this_value) as $inner_key) {
99                $ctx->__stash['vars'][strtolower($inner_key)] = $this_value[$inner_key];
100            }
101        }
102    }
103    if (isset($args['glue']) && !empty($content)) {
104        if ($out)
105            $content = $args['glue'] . $content;
106        else
107            $ctx->stash('__out', true);
108    }
109    if ( 0 === count($keys) )
110        $ctx->stash('__loop_keys', 0);
111
112    $repeat = true;
113    return $content;
114}
115?>
Note: See TracBrowser for help on using the browser.