| 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 | |
|---|
| 8 | function smarty_block_mtloop($args, $content, &$ctx, &$repeat) { |
|---|
| 9 | $localvars = array('__loop_keys', '__loop_values'); |
|---|
| 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 | } |
|---|
| 70 | else { |
|---|
| 71 | $counter = $ctx->__stash['vars']['__counter__'] + 1; |
|---|
| 72 | $keys = $ctx->stash('__loop_keys'); |
|---|
| 73 | $value = $ctx->stash('__loop_values'); |
|---|
| 74 | if (!isset($keys) || $keys == 0) { |
|---|
| 75 | $ctx->restore($localvars); |
|---|
| 76 | $repeat = false; |
|---|
| 77 | return $content; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | $key = array_shift($keys); |
|---|
| 81 | $this_value = $value[$key]; |
|---|
| 82 | $ctx->stash('__loop_keys', $keys); |
|---|
| 83 | |
|---|
| 84 | $ctx->__stash['vars']['__counter__'] = $counter; |
|---|
| 85 | $ctx->__stash['vars']['__odd__'] = ($counter % 2) == 1; |
|---|
| 86 | $ctx->__stash['vars']['__even__'] = ($counter % 2) == 0; |
|---|
| 87 | $ctx->__stash['vars']['__first__'] = $counter == 1; |
|---|
| 88 | $ctx->__stash['vars']['__last__'] = count($value) == 0; |
|---|
| 89 | $ctx->__stash['vars']['__key__'] = $key; |
|---|
| 90 | $ctx->__stash['vars']['__value__'] = $this_value; |
|---|
| 91 | if ( is_array($this_value) && (0 < count($this_value)) ) { |
|---|
| 92 | require_once("MTUtil.php"); |
|---|
| 93 | if ( is_hash($this_value) ) { |
|---|
| 94 | foreach (array_keys($this_value) as $inner_key) { |
|---|
| 95 | $ctx->__stash['vars'][strtolower($inner_key)] = $this_value[$inner_key]; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | if (array_key_exists('glue', $args)) { |
|---|
| 100 | if (1 < $counter) |
|---|
| 101 | $content = $content . $args['glue']; |
|---|
| 102 | } |
|---|
| 103 | if ( 0 === count($keys) ) |
|---|
| 104 | $ctx->stash('__loop_keys', 0); |
|---|
| 105 | |
|---|
| 106 | $repeat = true; |
|---|
| 107 | return $content; |
|---|
| 108 | } |
|---|
| 109 | ?> |
|---|