File: function.mtsetvar.php

package info (click to toggle)
movabletype-opensource 4.2.3-1%2Blenny3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 21,268 kB
  • ctags: 15,862
  • sloc: perl: 178,892; php: 26,178; sh: 161; makefile: 82
file content (149 lines) | stat: -rw-r--r-- 4,657 bytes parent folder | download
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id: function.mtsetvar.php 2872 2008-08-01 00:45:44Z bchoate $

function smarty_function_mtsetvar($args, &$ctx) {
    // status: complete
    // parameters: name, value
    $name = $args['name'];
    $name or $name = $args['var'];
    if (!$name) return '';

    $value = $args['value'];

    $vars =& $ctx->__stash['vars'];

    if (strtolower($name) == 'page_layout') {
        # replaces page layout for current page
        require_once("MTUtil.php");
        $columns = get_page_column($value);
        $vars['page_columns'] = $columns;
        $vars['page_layout'] = $value;
    }

    if (preg_match('/^(\w+)\((.+)\)$/', $name, $matches)) {
        $func = $matches[1];
        $name = $matches[2];
    } else {
        if (array_key_exists('function', $args))
            $func = $args['function'];
    }

    # pick off any {...} or [...] from the name.
    if (preg_match('/^(.+)([\[\{])(.+)[\]\}]$/', $name, $matches)) {
        $name = $matches[1];
        $br = $matches[2];
        $ref = $matches[3];
        if (preg_match('/^\$(.+)/', $ref, $ref_matches)) {
            $ref = $vars[$ref_matches[1]];
            if (!isset($ref))
                $ref = chr(0);
        }
        $br == '[' ? $index = $ref : $key = $ref;
    } else {
        if (array_key_exists('index', $args))
            $index = $args['index'];
        else if (array_key_exists('key', $args))
            $key = $args['key'];
    }

    if (preg_match('/^\$/', $name)) {
        $name = $vars[$name];
        if (!isset($name))
            return $ctx->error($ctx->mt->translate(
                "You used a [_1] tag without a valid name attribute.", "<MT$tag>" ));
    }

    $existing = $vars[$name];

    require_once("MTUtil.php");
    if (isset($key)) {
        if (!isset($existing))
            $existing = array($key => $value);
        elseif (is_hash($existing))
            $existing = $existing[$key];
        else
            return $ctx->error( $ctx->mt->translate("'[_1]' is not a hash.", $name) );
    }
    elseif (isset($index)) {
        if (!isset($existing))
            $existing[$index] = $value;
        elseif (is_array($existing)) {
            if ( is_numeric($index) )
                $existing = $existing[$index];
            else
                return $ctx->error( $ctx->mt->translate("Invalid index.") );
        }
        else
            return $ctx->error( $ctx->mt->translate("'[_1]' is not an array.", $name) );
    }

    if (array_key_exists('append', $args) && $args['append']) {
        $value = isset($existing) ? $existing . $value : $value;
    }
    elseif (array_key_exists('prepend', $args) && $args['prepend']) {
        $value = isset($existing) ?  $value . $existing : $value;
    }
    elseif ( isset($existing) && array_key_exists('op', $args) ) {
        $op = $args['op'];
        $value = _math_operation($op, $existing, $value);
        if (!isset($value))
            return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", $existing, $op, $value));
    }

    $data = $vars[$name];
    if ( isset($key) ) {
        if ( ( isset($func) )
          && ( 'delete' == strtolower( $func ) ) ) {
            unset($data[$key]);
        }
        else {
            $data[$key] = $value;
        }
    }
    elseif ( isset($index) ) {
        $data[$index] = $value;
    }
    elseif ( isset($func) ) {
        if ( 'undef' == strtolower( $func ) ) {
            unset($data);
        }
        else {
            if (isset($data) && !is_array($data))
                return $ctx->error( $ctx->mt->translate("'[_1]' is not an array.", $name) );
            if (!isset($data))
                $data = array();
            if ( 'push' == strtolower( $func ) ) {
                array_push($data, $value);
            }
            elseif ( 'unshift' == strtolower( $func ) ) {
                array_unshift($data, $value);
            }
            else {
                return $ctx->error(
                    $ctx->mt->translate("'[_1]' is not a valid function.", $func)
                );
            }
        }
    }
    else {
        $data = $value;
    }
    $hash = $ctx->stash('__inside_set_hashvar');
    if (isset($hash)) {
        $hash[$name] = $data;
        $ctx->stash('__inside_set_hashvar', $hash);
    }
    else {
        if (is_array($vars)) {
            $vars[$name] = $data;
        } else {
            $vars = array($name => $data);
            $ctx->__stash['vars'] =& $vars;
        }
    }
    return '';
}