summaryrefslogtreecommitdiff
path: root/smartyplugins/function.alphabar.php
blob: 6a67dc121fac25e48d69292b903191c99aa38677 (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\KernelTools;
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 * @author xing <xing$synapse.plus.com>
 */

/**
 * Smarty {alphabar} function plugin
 *
 * Type:	function
 * Name:	alphabar
 * Input:
 *			- iskip		(optional)	array of chars that can be skipped
 *			- iall		(optional)	if set to anything, it will include a link to all
 *			- ifile		(optional)	set the file where the link should point (default is the current file)
 *			- ipackage	(optional)	set the package the link should point to (default is the current package)
 *			- *			(optional)	anything else that gets added to the pile of items is appended using &amp;$key=$val
 * Example	- {alphabar}
 */
function smarty_function_alphabar( $params, &$gBitSmarty ) {
	global $gBitSystem;
	extract( $params );

	// work out what the url is
	$url = isset( $ifile )
		? ( isset( $ipackage )
			? ( $ipackage == 'root' ? BIT_ROOT_URL.$ifile : constant( strtoupper( $ipackage ).'_PKG_URL' ).$ifile )
			: constant( strtoupper( $gBitSystem->getActivePackage() ).'_PKG_URL' ).$ifile )
		: $_SERVER['SCRIPT_NAME'];

	$alphabar_params = [ 'ifile', 'ipackage', 'iall' ];
	// append any other paramters that were passed in
	$url_params = '';
	foreach( $params as $key => $val ) {
		if( !empty( $val ) && !in_array( $key, $alphabar_params ) ) {
			$url_params .= '&amp;'.$key."=".$val;
		}
	}

	$ret = '<div class="pagination alphabar">';
	$alpha = [ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0-9','+' ];
	foreach( $alpha as $char ) {
		if( empty( $iskip ) || !in_array( $char, $iskip )) {
			$wrap = [ 'open' => '', 'close' => '' ];
			if( !empty( $_REQUEST['char'] ) && $_REQUEST['char'] == strtolower( $char )) {
				$wrap = [ 'open' => '<strong>', 'close' => '</strong>' ];
			}
			$ret .= $wrap['open'].'<a href="'.$url.'?char='.urlencode( strtolower( $char )).$url_params.'">'.$char.'</a>'.$wrap['close'].' ';
		}
	}

	if( !empty( $params['iall'] ) ) {
		$ret .= '<a href="'.$url.'?char='.urlencode( strtolower( 'All' ) ).$url_params.'">'.KernelTools::tra( 'All' ).'</a> ';
	}
	$ret .= '</div>';

	return $ret;
}