summaryrefslogtreecommitdiff
path: root/smartyplugins/BlockJstabs.php
blob: f630f3275e9ff599ab925dee54321c444ff46a64 (plain)
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
<?php
namespace Bitweaver\Plugins;

use Bitweaver\BitBase;
use Smarty\BlockHandler\BlockHandlerInterface;
use Smarty\Template;

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty {jstabs} block plugin
 *
 * Type:		block
 * Name:		jstabs
 * Input:		you can use {jstab tab=<tab number>} (staring with 0) to select a given tab
 *              or you can use the url to do so: page.php?jstab=<tab number>
 * Abstract:	Used to enclose a set of tabs
 */
class BlockJstabs implements BlockHandlerInterface {

	public function handle( $params, $content, Template $template, &$repeat): string {
		global $gBitSystem, $jsTabLinks;
		if( $repeat ){
			$jsTabLinks = [];
		} else {
			extract( $params );

			$tabId = !empty( $params['id'] ) ? $params['id'] : substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10);

			if( isset( $_REQUEST['jstab'] ) ) {
				// make sure we aren't passed any evil shit
				if( !isset( $tab ) && isset( $_REQUEST['jstab'] ) && preg_match( "!^\d+$!", $_REQUEST['jstab'] ) ) {
					$tab = $_REQUEST['jstab'];
				}
				$setupJs = '$(\'#'.$tabId.' a[href="#profile"]\').tab(\'show\');';
			} else {
				$setupJs = "$('#$tabId a:first').tab('show');";
			}

			$tabType = BitBase::getParameter( $params, 'tabtype', 'tab' );

			$ret = '<ul class="nav nav-'.$tabType.'s" data-tab="'.$tabType.'" id="'.$tabId.'">';
			foreach( $jsTabLinks as $tabLink ) {
				$ret .= $tabLink;
			}
			$ret .= '</ul><div class="tab-content">'.$content.'</div>';
			$ret .= '<script nonce="{$cspNonce}">/*<![CDATA[*/ $(\'#'.$tabId.' a\').click(function (e) { e.preventDefault(); $(this).tab(\'show\'); }); '.$setupJs .'/*]]>*/</script> ';

			$jsTabLinks = NULL;

			return $ret;
		}
		return '';
	}

	public function isCacheable(): bool {
		return true;
	}
}