PageRenderTime 31ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/smarty_tiki/function.button.php

https://gitlab.com/ElvisAns/tiki
PHP | 230 lines | 166 code | 22 blank | 42 comment | 67 complexity | 19a2ed8ce405dabe157dd18595dac4fd MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. /*
  8. * smarty_function_button: Display a Tiki button
  9. *
  10. * params will be used as params for as smarty self_link params, except those special params specific to smarty button :
  11. * - _icon: DEPRECATED previously used for file path for legacy icons
  12. * - _icon_name: use icon name to show appropriate icon regardless of iconset chosen
  13. * - _text: Text that will be shown in the button
  14. * - _auto_args: comma-separated list of URL arguments that will be kept from _REQUEST (like $auto_query_args) (in addition of course of those you can specify in the href param)
  15. * You can also use _auto_args='*' to specify that every arguments listed in the global var $auto_query_args has to be kept from URL
  16. * - _flip_id: id HTML atribute of the element to show/hide (for type 'flip'). This will automatically generate an 'onclick' attribute that will use tiki javascript function flip() to show/hide some content.
  17. * - _flip_hide_text: if set to 'n', do not display a '(Hide)' suffix after _text when status is not 'hidden'
  18. * - _flip_default_open: if set to 'y', the flip is open by default (if no cookie jar)
  19. * - _escape: if set to 'y', will escape the apostrophes in onclick
  20. * - _type: button styling. Possible values: primary, secondary, success, info, warning, danger, link (following bootstrap conventions)
  21. * Set different class, title, text and icon depending on whether the button is disabled or selected
  22. * - _disabled: set to y to disable the button
  23. * - _disabled_class: class to use if _disabled is set to y. Default is 'disabled'
  24. * - _disabled_title: button title to use if _disabled is set to y
  25. * - _disabled_text: button text to use if _disabled is set to y
  26. * - _disabled_icon_name: button icon to use if _disabled is set to y
  27. * - _selected: set to y to enable the button.
  28. * - _selected_class: class to use if _selected set to y. Default is 'selected'
  29. * - _selected_title: button title to use if _selected is set to y
  30. * - _selected_text: button text to use if _selected is set to y
  31. * - _selected_icon_name: button icon to use if _selected is set to y
  32. */
  33. /**
  34. * @param array $params
  35. * @param \Smarty_Internal_Template $smarty
  36. * @return string
  37. */
  38. function smarty_function_button($params, $smarty)
  39. {
  40. if (! is_array($params) || (! isset($params['_text']) && ! isset($params['_icon_name']))) {
  41. return '';
  42. }
  43. global $tikilib, $prefs, $auto_query_args;
  44. $class = null;
  45. $smarty->loadPlugin('smarty_block_self_link');
  46. $selected = false ;
  47. if (! empty($params['_selected'])) {
  48. if ($params['_selected'] == 'y') {
  49. $selected = true;
  50. if (! empty($params['_selected_class'])) {
  51. $params['_class'] = $params['_selected_class'];
  52. } else {
  53. $params['_class'] = 'selected';
  54. }
  55. if (! empty($params['_selected_text'])) {
  56. $params['_text'] = $params['_selected_text'];
  57. }
  58. if (! empty($params['_selected_title'])) {
  59. $params['_title'] = $params['_selected_title'];
  60. }
  61. if (! empty($params['_selected_icon'])) {
  62. $params['_icon'] = $params['_selected_icon'];
  63. }
  64. if (! empty($params['_selected_icon_name'])) {
  65. $params['_icon_name'] = $params['_selected_icon_name'];
  66. }
  67. }
  68. }
  69. $disabled = false ;
  70. //check if it's a wiki page in format ((Page Name)) and check permission
  71. if (isset($params['href']) && preg_match('|^\(\((.+?)\)\)$|', $params['href'], $matches)) {
  72. $params['href'] = rawurlencode($matches[1]);
  73. $perms = Perms::get(['type' => 'wiki page', 'object' => $matches[1]]);
  74. if (! ($perms->view && $perms->wiki_view_ref)) {
  75. $disabled = true;
  76. $params['_title'] = tr('You don\'t have permission to view the linked page');
  77. }
  78. }
  79. if (! empty($params['_disabled'])) {
  80. if ($params['_disabled'] == 'y') {
  81. $disabled = true;
  82. if (! empty($params['_disabled_class'])) {
  83. $params['_class'] = $params['_disabled_class'];
  84. } else {
  85. $params['_class'] = 'disabled';
  86. }
  87. if (! empty($params['_disabled_text'])) {
  88. $params['_text'] = $params['_disabled_text'];
  89. }
  90. if (! empty($params['_disabled_title'])) {
  91. $params['_title'] = $params['_disabled_title'];
  92. }
  93. if (! empty($params['_disabled_icon'])) {
  94. $params['_icon'] = $params['_disabled_icon'];
  95. }
  96. if (! empty($params['_disabled_icon_name'])) {
  97. $params['_icon_name'] = $params['_disabled_icon_name'];
  98. }
  99. }
  100. unset($params['_disabled']);
  101. }
  102. //apply class only to the button
  103. if (! empty($params['_class'])) {
  104. $class = $params['_class'];
  105. }
  106. if (! empty($params['_id'])) {
  107. $id = ' id="' . $params['_id'] . '"';
  108. } else {
  109. $id = '';
  110. }
  111. unset($params['_class']);
  112. //target parameter
  113. if (! empty($params['_target'])) {
  114. $target = $params['_target'];
  115. } else {
  116. $target = '';
  117. }
  118. unset($params['_target']);
  119. if (! $disabled) {
  120. $flip_id = '';
  121. if (! empty($params['_flip_id'])) {
  122. $params['_onclick'] = "flip('"
  123. . $params['_flip_id']
  124. . "');flip('"
  125. . $params['_flip_id']
  126. . "_close','inline');return false;";
  127. if (! empty($params['_escape']) && $params['_escape'] === 'y') {
  128. $params['_onclick'] = addslashes($params['_onclick']);
  129. }
  130. if (! isset($params['_flip_hide_text']) || $params['_flip_hide_text'] != 'n') {
  131. $cookie_key = 'show_' . $params['_flip_id'];
  132. $params['_text'] .= '<span id="' . $params['_flip_id'] . '_close" style="display:'
  133. . ( ((isset($_SESSION['tiki_cookie_jar'][$cookie_key]) && $_SESSION['tiki_cookie_jar'][$cookie_key] == 'y') || (isset($params['_flip_default_open']) && $params['_flip_default_open'] == 'y')) ? 'inline' : 'none' )
  134. . ';"> (' . tra('Hide') . ')</span>';
  135. }
  136. }
  137. $auto_query_args_orig = $auto_query_args;
  138. if (! empty($params['_auto_args'])) {
  139. if ($params['_auto_args'] != '*') {
  140. if (! isset($auto_query_args)) {
  141. $auto_query_args = null;
  142. }
  143. $auto_query_args = explode(',', $params['_auto_args']);
  144. }
  145. } else {
  146. $params['_noauto'] = 'y';
  147. }
  148. // Remove params that does not start with a '_', since we don't want them to modify the URL except when in auto_query_args
  149. if (! isset($params['_keepall']) || $params['_keepall'] != 'y') {
  150. foreach ($params as $k => $v) {
  151. if (
  152. $k[0] != '_'
  153. && $k != 'href'
  154. && $k != 'data'
  155. && (empty($auto_query_args) || ! in_array($k, $auto_query_args))
  156. ) {
  157. unset($params[$k]);
  158. }
  159. }
  160. }
  161. $url_args = [];
  162. if (! empty($params['href'])) {
  163. // Handle anchors
  164. if (strpos($params['href'], '#')) {
  165. list($params['href'], $params['_anchor']) = explode('#', $params['href'], 2);
  166. }
  167. // Handle script and URL arguments
  168. if (( $pos = strpos($params['href'], '?') ) !== false) {
  169. $params['_script'] = substr($params['href'], 0, $pos);
  170. parse_str($tikilib->htmldecode(substr($params['href'], $pos + 1)), $url_args);
  171. $params = array_merge($params, $url_args);
  172. } else {
  173. $params['_script'] = $params['href'];
  174. }
  175. unset($params['href']);
  176. }
  177. if (! isset($params['_text'])) { // avoid NOTICE (E_NOTICE): Undefined index
  178. $params['_text'] = '';
  179. }
  180. $html = smarty_block_self_link(
  181. $params,
  182. $params['_text'],
  183. $smarty
  184. );
  185. $url = str_replace('+', ' ', str_replace('&amp;', '&', urldecode($_SERVER['REQUEST_URI'])));
  186. $dom = new DOMDocument();
  187. $dom->loadHTML($html);
  188. foreach ($dom->getElementsByTagName('a') as $link) {
  189. if ($url == $link->getAttribute('href')) {
  190. $selected = true;
  191. if ($class === null) {
  192. $class = 'active';
  193. }
  194. }
  195. }
  196. } else {
  197. $params['_disabled'] = 'y';
  198. $html = smarty_block_self_link(
  199. $params,
  200. $params['_text'],
  201. $smarty
  202. );
  203. }
  204. $type = isset($params['_type']) ? $params['_type'] : 'primary';
  205. $auto_query_args = $auto_query_args_orig;
  206. $html = preg_replace('/<a /', '<a class="btn btn-' . $type . ' ' . $class . '" target="' . $target . '" data-role="button" data-inline="true" ' . $id . ' ', $html);
  207. return $html;
  208. }