PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/php/lib/function.mtsetvar.php

https://code.google.com/p/movabletype/
PHP | 150 lines | 130 code | 11 blank | 9 comment | 45 complexity | d29acda2bb037184ac880b499a05059d MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. # Movable Type (r) Open Source (C) 2001-2010 Six Apart, Ltd.
  3. # This program is distributed under the terms of the
  4. # GNU General Public License, version 2.
  5. #
  6. # $Id: function.mtsetvar.php 5225 2010-01-27 07:14:14Z takayama $
  7. function smarty_function_mtsetvar($args, &$ctx) {
  8. // status: complete
  9. // parameters: name, value
  10. $name = $args['name'];
  11. $name or $name = $args['var'];
  12. if (!$name) return '';
  13. $value = $args['value'];
  14. $vars =& $ctx->__stash['vars'];
  15. if (strtolower($name) == 'page_layout') {
  16. # replaces page layout for current page
  17. require_once("MTUtil.php");
  18. $columns = get_page_column($value);
  19. $vars['page_columns'] = $columns;
  20. $vars['page_layout'] = $value;
  21. }
  22. if (preg_match('/^(\w+)\((.+)\)$/', $name, $matches)) {
  23. $func = $matches[1];
  24. $name = $matches[2];
  25. } else {
  26. if (array_key_exists('function', $args))
  27. $func = $args['function'];
  28. }
  29. # pick off any {...} or [...] from the name.
  30. if (preg_match('/^(.+)([\[\{])(.+)[\]\}]$/', $name, $matches)) {
  31. $name = $matches[1];
  32. $br = $matches[2];
  33. $ref = $matches[3];
  34. if (preg_match('/^\$(.+)/', $ref, $ref_matches)) {
  35. $ref = $vars[$ref_matches[1]];
  36. if (!isset($ref))
  37. $ref = chr(0);
  38. }
  39. $br == '[' ? $index = $ref : $key = $ref;
  40. } else {
  41. if (array_key_exists('index', $args))
  42. $index = $args['index'];
  43. else if (array_key_exists('key', $args))
  44. $key = $args['key'];
  45. }
  46. if (preg_match('/^\$/', $name)) {
  47. $name = $vars[$name];
  48. if (!isset($name))
  49. return $ctx->error($ctx->mt->translate(
  50. "You used a [_1] tag without a valid name attribute.", "<MT$tag>" ));
  51. }
  52. $existing = $vars[$name];
  53. require_once("MTUtil.php");
  54. if (isset($key)) {
  55. if (!isset($existing))
  56. $existing = array($key => $value);
  57. elseif (is_hash($existing))
  58. $existing = $existing[$key];
  59. else
  60. return $ctx->error( $ctx->mt->translate("'[_1]' is not a hash.", $name) );
  61. }
  62. elseif (isset($index)) {
  63. if (!isset($existing))
  64. $existing[$index] = $value;
  65. elseif (is_array($existing)) {
  66. if ( is_numeric($index) )
  67. $existing = $existing[$index];
  68. else
  69. return $ctx->error( $ctx->mt->translate("Invalid index.") );
  70. }
  71. else
  72. return $ctx->error( $ctx->mt->translate("'[_1]' is not an array.", $name) );
  73. }
  74. if (array_key_exists('append', $args) && $args['append']) {
  75. $value = isset($existing) ? $existing . $value : $value;
  76. }
  77. elseif (array_key_exists('prepend', $args) && $args['prepend']) {
  78. $value = isset($existing) ? $value . $existing : $value;
  79. }
  80. elseif ( isset($existing) && array_key_exists('op', $args) ) {
  81. $op = $args['op'];
  82. $value = _math_operation($op, $existing, $value);
  83. if (!isset($value))
  84. return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", $existing, $op, $value));
  85. }
  86. $data = $vars[$name];
  87. if ( isset($key) ) {
  88. if ( ( isset($func) )
  89. && ( 'delete' == strtolower( $func ) ) ) {
  90. unset($data[$key]);
  91. }
  92. else {
  93. $data[$key] = $value;
  94. }
  95. }
  96. elseif ( isset($index) ) {
  97. $data[$index] = $value;
  98. }
  99. elseif ( isset($func) ) {
  100. if ( 'undef' == strtolower( $func ) ) {
  101. unset($data);
  102. }
  103. else {
  104. if (isset($data) && !is_array($data))
  105. return $ctx->error( $ctx->mt->translate("'[_1]' is not an array.", $name) );
  106. if (!isset($data))
  107. $data = array();
  108. if ( 'push' == strtolower( $func ) ) {
  109. array_push($data, $value);
  110. }
  111. elseif ( 'unshift' == strtolower( $func ) ) {
  112. array_unshift($data, $value);
  113. }
  114. else {
  115. return $ctx->error(
  116. $ctx->mt->translate("'[_1]' is not a valid function.", $func)
  117. );
  118. }
  119. }
  120. }
  121. else {
  122. $data = $value;
  123. }
  124. $hash = $ctx->stash('__inside_set_hashvar');
  125. if (isset($hash)) {
  126. $hash[$name] = $data;
  127. $ctx->stash('__inside_set_hashvar', $hash);
  128. }
  129. else {
  130. if (is_array($vars)) {
  131. $vars[$name] = $data;
  132. } else {
  133. $vars = array($name => $data);
  134. $ctx->__stash['vars'] =& $vars;
  135. }
  136. }
  137. return '';
  138. }
  139. ?>