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/**
9 * Smarty plugin
10 * @package Smarty
11 * @subpackage plugins
12 *
13 * smarty_block_self_link : add a link (with A tag) to the current page on a text (passed through $content argument).
14 *
15 *   The generated link uses other smarty functions like query and show_sort to handle AJAX, sorting fields, and sorting icons.
16 *   This block is very useful to handle table columns sorting links.
17 *
18 * params are the same as smarty 'query' function + some special params starting with an underscore:
19 *   _sort_field : name of the field used for sorting,
20 *   _sort_arg : name of the URL argument that contains the field to use for sorting. Defaults to 'sort',
21 *   _ajax : if set to 'n', will force disabling AJAX even if the ajax xajax feature is enabled,	AJAX_TODO
22 *   _tag : if set to 'n', will only return an URL, not the full A tag + text (AJAX and sorting features are not available in this case),
23 *   _class : CSS class to use for the A tag
24 *   _template : (see smarty query function 'template' param)
25 *   _htmlelement : (see smarty query function 'htmlelement' param)
26 *   _icon : file name of the icon to use (e.g. 'page_edit', 'cross', ...) - only works with legacy icons
27 *   _icon_name : name of the icon to use in order to use iconsets
28 *   _icon_class : CSS class to use for the icon's IMG tag
29 *   _icon_size : size of icon when _icon_name is used
30 *   _menu_text : (see smarty icon function)
31 *   _menu_icon : (see smarty icon function)
32 *   _title : tooltip to display when the mouse is over the link. Use $content when _icon is used.
33 *   _text : show text as part of the link (for instance, after the icon for a menu item)
34 *   _alt : alt attribute for the icon's IMG tag (use _title if _alt is not specified).
35 *   _script : specify another script than the current one (this disable AJAX for this link when the current script is different).
36 *   _on* : specify values of on* (e.g. onclick) HTML attributes used for javascript events
37 */
38
39//this script may only be included - so its better to die if called directly.
40if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
41	header("location: index.php");
42	exit;
43}
44
45function smarty_block_self_link($params, $content, $smarty, &$repeat = false)
46{
47	$default_type = 'absolute_path';
48	$default_icon_type = 'relative';
49
50	if ($repeat) {
51		return;
52	}
53	$smarty->loadPlugin('smarty_function_query');
54
55	if (is_array($params)) {
56		if (! isset($content)) {
57			$content = '';
58		}
59		if (! isset($params['_ajax'])) {
60			$params['_ajax'] = 'y';
61		}
62		if (! isset($params['_script'])) {
63			$params['_script'] = '';
64		}
65		if (! isset($params['_tag'])) {
66			$params['_tag'] = 'y';
67		}
68		if (! empty($params['_anchor'])) {
69			$anchor = $params['_anchor'];
70		} else {
71			$anchor = '';
72		}
73		if (empty($params['_disabled'])) {
74			if (! isset($params['_sort_arg'])) {
75				$params['_sort_arg'] = 'sort';
76			}
77			if (! isset($params['_sort_field'])) {
78				$params['_sort_field'] = '';
79			} elseif ($params['_sort_arg'] != '' and ! isset($params[$params['_sort_arg']])) {
80				$params[$params['_sort_arg']] = $params['_sort_field'] . '_asc,' . $params['_sort_field'] . '_desc';
81			}
82			// Complete _script path if needed (not empty, not an anchor, ...)
83			if (! empty($params['_script']) && $params['_script'][0] != '#' && $params['_script'] != 'javascript:void(0)' && stripos($params['_script'], 'mailto:') !== 0) {
84				if ($_SERVER['PHP_SELF'][0] == '/' && strpos($params['_script'], '/') === false) {
85					$self_dir = str_replace('\\', '/', dirname($_SERVER['PHP_SELF']));
86					$params['_script'] = ( $self_dir == '/' ? '' : $self_dir ) . '/' . $params['_script'];
87				}
88				if ($params['_script'] == $_SERVER['PHP_SELF']) {
89					$params['_script'] = '';
90				}
91			}
92
93			$dataAttributes = '';
94			if (!empty($params['data'])) {
95				parse_str($params['data'], $attrs);
96
97				foreach ($attrs as $attr => $value) {
98					$dataAttributes .= " data-$attr=\"$value\"";
99				}
100			}
101			unset($params['data']);
102
103			$params['_type'] = $default_type;
104
105			$ret = smarty_function_query($params, $smarty);
106		}
107
108		if ($params['_tag'] == 'y') {
109			if (empty($params['_disabled'])) {
110				if ($params['_ajax'] === 'y' && $params['_script'] === '') {
111					$smarty->loadPlugin('smarty_block_ajax_href');
112					if (! isset($params['_htmlelement'])) {
113						$params['_htmlelement'] = 'role_main';
114					}
115					if (! isset($params['_onclick'])) {
116						$params['_onclick'] = '';
117					}
118					if (! isset($params['_template'])) {
119						$params['_template'] = basename($_SERVER['PHP_SELF'], '.php') . '.tpl';
120						if ($params['_template'] == 'tiki-index.tpl') {
121							$params['_template'] = 'tiki-show_page.tpl';
122						}
123					}
124					if (! file_exists('templates/' . $params['_template']) || $params['_template'] == 'noauto') {
125						$params['_htmlelement'] = '';
126						$params['_template'] = '';
127					}
128					$ret = smarty_block_ajax_href(
129						[
130							'template' => $params['_template'],
131							'htmlelement' => $params['_htmlelement'],
132							'_onclick' => $params['_onclick'],
133							'_anchor' => $anchor
134						],
135						$ret,
136						$smarty,
137						$tmp = false
138					);
139					unset($params['_onclick']); // Prevent addition to $link later
140				} else {
141					$ret = 'href="' . $ret . '"';
142				}
143			}
144
145			if (isset($params['_icon']) || isset($params['_icon_name'])) {
146				if (! isset($params['_title']) && $content != '' && (! isset($params['_rel']) ||
147						strpos($params['_rel'], 'box') === false)) {
148					$params['_title'] = $content;
149				}
150				$smarty->loadPlugin('smarty_function_icon');
151				if (isset($params['_icon'])) {
152					$icon_params['_id'] = $params['_icon'];
153				} else {
154					$icon_params['name'] = $params['_icon_name'];
155				}
156				$icon_params['_type'] = $default_icon_type;
157				if (isset($params['_alt'])) {
158					$icon_params['alt'] = $params['_alt'];
159				} elseif (isset($params['_title'])) {
160					$icon_params['alt'] = $params['_title'];
161					$icon_params['title'] = ''; // will already be included in the surrounding A tag
162				}
163
164				if (isset($params['_menu_text']) && $params['_menu_text'] == 'y') {
165					$icon_params['_menu_text'] = $params['_menu_text'];
166					$icon_params['title'] = $params['_title']; // Used as the menu text
167					$params['_title'] = ''; // will already be displayed as the menu text
168				}
169				if (isset($params['_menu_icon'])) {
170					$icon_params['_menu_icon'] = $params['_menu_icon'];
171				}
172				if (isset($params['_icon_class'])) {
173					$icon_params['class'] = $params['_icon_class'];
174				}
175				if (isset($params['_icon_size'])) {
176					$icon_params['size'] = $params['_icon_size'];
177				}
178
179				if (isset($params['_width'])) {
180					$icon_params['width'] = $params['_width'];
181				}
182				if (isset($params['_height'])) {
183					$icon_params['height'] = $params['_height'];
184				}
185
186				$content = smarty_function_icon($icon_params, $smarty);
187
188				if (isset($params['_text'])) {
189					$content .= ' ' . $params['_text'];
190				}
191			}
192
193			$link = ( ! empty($params['_class']) ? 'class="' . $params['_class'] . '" ' : '' )
194				. ( ! empty($params['_style']) ? 'style="' . $params['_style'] . '" ' : '' )
195				. ( ! empty($params['_title']) ? 'title="' . str_replace('"', '\"', $params['_title']) . '" ' : '' )
196				. $dataAttributes;
197
198			if (! empty($params['_rel'])) {
199				if (strpos($params['_rel'], 'box') !== false) {
200					$rel = 'data-box="box" ';
201				} else {
202					$rel = 'rel="' . str_replace('"', '\"', $params['_rel']) . '" ';
203				}
204			} else {
205				$rel = '';
206			}
207			$link .= $rel;
208			foreach ($params as $k => $v) {
209				if (strlen($k) > 3 && substr($k, 0, 3) == '_on' && ! empty($v)) {
210					$link .= htmlentities(substr($k, 1)) . '="' . $v . '" '; // $v should be already htmlentitized in the template
211					unset($params[$k]);
212				}
213			}
214			$link .= $ret;
215
216			if (isset($params['_confirm'])) {
217				$link .= ' data-confirm="' . smarty_modifier_escape($params['_confirm']) . '"';
218			}
219
220			$ret = "<a $link>" . $content . '</a>';
221
222			if (! empty($params['_sort_field'])) {
223				$smarty->loadPlugin('smarty_function_show_sort');
224				$ret = "<a $link style='text-decoration:none;'>" . $content .
225								smarty_function_show_sort(
226									['sort' => $params['_sort_arg'], 'var' => $params['_sort_field']],
227									$smarty
228								) . '</a>';
229			}
230		}
231	} else {
232		$params = ['_type' => $default_type];
233		$ret = smarty_function_query($params, $smarty);
234	}
235
236	return $ret;
237}
238