1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
<?php
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
function smarty_block_mtloop($args, $content, &$ctx, &$repeat) {
$localvars = array('__loop_keys', '__loop_values', '__out');
if (!isset($content)) {
$ctx->localize($localvars);
$vars =& $ctx->__stash['vars'];
$value = '';
$name = $args['name'];
$name or $name = $args['var'];
if (!$name) return '';
if (isset($vars[$name]))
$value = $vars[$name];
if ( !is_array($value)
&& preg_match('/^smarty_fun_[a-f0-9]+$/', $value) ) {
if (function_exists($value)) {
ob_start();
$value($ctx, array());
$value = ob_get_contents();
ob_end_clean();
} else {
$value = '';
}
}
if ( !is_array($value) || (0 == count($value)) ) {
$repeat = false;
return '';
}
$sort = $args['sort_by'];
$keys = array_keys($value);
if ($sort) {
$sort = strtolower($sort);
if (preg_match('/\bkey\b/', $sort)) {
usort($keys, create_function(
'$a,$b',
'return strcmp($a, $b);'
));
} elseif (preg_match('/\bvalue\b/', $sort)) {
$sort_fn = '';
foreach (array_keys($value) as $key) {
$v = $value[$key];
$sort_fn .= "\$value['$key']='$v';";
}
if (preg_match('/\bnumeric\b/', $sort)) {
$sort_fn .= 'return $value[$a] === $value[$b] ? 0 : ($value[$a] > $value[$b] ? 1 : -1);';
$sorter = create_function(
'$a,$b',
$sort_fn);
} else {
$sort_fn .= 'return strcmp($value[$a], $value[$b]);';
$sorter = create_function(
'$a,$b',
$sort_fn);
}
usort($keys, $sorter);
}
if (preg_match('/\breverse\b/', $sort)) {
$keys = array_reverse($keys);
}
}
$counter = 1;
$ctx->stash('__loop_values', $value);
$ctx->stash('__out', false);
}
else {
$counter = $ctx->__stash['vars']['__counter__'] + 1;
$keys = $ctx->stash('__loop_keys');
$value = $ctx->stash('__loop_values');
$out = $ctx->stash('__out');
if (!isset($keys) || $keys == 0) {
$ctx->restore($localvars);
$repeat = false;
if (isset($args['glue']) && $out && !empty($content))
$content = $args['glue'] . $content;
return $content;
}
}
$key = array_shift($keys);
$this_value = $value[$key];
$ctx->stash('__loop_keys', $keys);
$ctx->__stash['vars']['__counter__'] = $counter;
$ctx->__stash['vars']['__odd__'] = ($counter % 2) == 1;
$ctx->__stash['vars']['__even__'] = ($counter % 2) == 0;
$ctx->__stash['vars']['__first__'] = $counter == 1;
$ctx->__stash['vars']['__last__'] = count($value) == 0;
$ctx->__stash['vars']['__key__'] = $key;
$ctx->__stash['vars']['__value__'] = $this_value;
if ( is_array($this_value) && (0 < count($this_value)) ) {
require_once("MTUtil.php");
if ( is_hash($this_value) ) {
foreach (array_keys($this_value) as $inner_key) {
$ctx->__stash['vars'][strtolower($inner_key)] = $this_value[$inner_key];
}
}
}
if (isset($args['glue']) && !empty($content)) {
if ($out)
$content = $args['glue'] . $content;
else
$ctx->stash('__out', true);
}
if ( 0 === count($keys) )
$ctx->stash('__loop_keys', 0);
$repeat = true;
return $content;
}
?>
|