Source for file function.smartlink.php
Documentation is available at function.smartlink.php
* @author xing <xing$synapse.plus.com>
* @link http://www.bitweaver.org/wiki/function_smartlink function.smartlink
* Smarty {smartlink} function plugin
* - ititle (required) words that are displayed<br>
* - iatitle (optional) alternative text for the link title (rollover, etc.) <br>
* - ianchor (optional) set the anchor where the link should point to<br>
* - isort (optional) name of the sort column without the orientation (e.g.: title)<br>
* - isort_mode(optional) this can be used to manually pass the sort mode to smartlink<br>
* overrides the value given in $_REQUEST['sort_mode'], which is the default<br>
* - iorder (optional) if set to asc or desc, it sets the default sorting order of this particular column<br>
* - idefault (optional) if set, it will highlight this link if no $isort_mode is given<br>
* this should only be set once per sorting group since it represents the default sorting column<br>
* - itra (optional) if present then don't translate<br>
* - itype (optional) can be set to<br>
* url --> outputs only url<br>
* li --> outputs link as <li><a ... ></li><br>
* - ionclick (optional) pass in any actions that should occur onclick<br>
* - ibiticon (optional) if you want to display an icon instead of text use ibiticon<br>
* format is: '<ipackage>/<iname>'<br>
* e.g.: 'liberty/edit'<br>
* - iforce (optional) pass iforce parameter through to biticon
* - iurl (optional) pass in a full url
* - ifile (optional) set the file where the link should point (default is the current file)<br>
* - ipackage (optional) set the package the link should point to (default is the current package)<br>
* - icontrol (optional) the hash sent out by postGetList()
* - * (optional) anything else that gets added to the pile of items is appended using &$key=$val<br>
* - ihash (optional) you can pass in all the above as an array called ihash or secondary * items common to all links<br>
* Output: any kind of link. especially useful when it comes to links used to sort a table, due to the simplified syntax and loss of cumbersome if clauses
* also useful if the you want to display an icon as link since smartlink takes biticon parameters<br>
* Example - {smartlink ititle="Page Name" isort="title"}<br>
* - {smartlink ititle="Page Name" isort="title" iorder="desc" idefault=1}<br>
* setting iorder and idefault here, makes this link sort in a descending order by default (iorder)<br>
* and it is highlighted when $isort_mode ( or $_REQUEST['sort_mode'] ) is not set (idefault)<br>
* Note Be careful if ititle is generated dynamically since it is passed through tra() by default, use itra to override<br>
if( !empty( $params['ihash'] ) ) {
// maybe params were passed in separately
if( !isset ( $hash['ititle'] ) ) {
return 'You need to supply "ititle" for {smartlink} to work.';
// work out what the url is
if( !empty( $hash['iurl'] ) ) {
} elseif( !empty( $hash['ifile'] ) ) {
if( !empty( $hash['ipackage'] ) ) {
if( $hash['ipackage'] == 'root' ) {
$url = $_SERVER['SCRIPT_NAME'];
if( isset ( $hash['itra'] ) && !empty( $hash['itra'] ) ) {
// present and non-zero value
$ititle = $hash['ititle'];
$iatitle = empty( $hash['iatitle'] ) ? $ititle : $hash['iatitle'];
$ititle = tra( $hash['ititle'] );
$iatitle = empty( $hash['iatitle'] ) ? $ititle : tra ( $hash['iatitle'] );
$atitle = 'title="'. $iatitle. '"';
// if isort is set, we need to deal with all the sorting stuff
if( !empty( $hash['isort'] ) ) {
$isort_mode = isset ( $hash['isort_mode'] ) ? $hash['isort_mode'] : isset ( $_REQUEST['sort_mode'] ) ? $_REQUEST['sort_mode'] : NULL ;
$sort_asc = $hash['isort']. '_asc';
$sort_desc = $hash['isort']. '_desc';
$atitle = 'title="'. tra( 'Sort by' ). ": ". $iatitle. '"';
$url_params .= 'sort_mode=';
// check if we have to highlight this link, when $isort_mode isn't set
if( isset ( $hash['idefault'] ) && empty( $isort_mode ) ) {
$isort_mode .= $hash['isort']. '_'. ( isset ( $hash['iorder'] ) ? $hash['iorder'] : 'asc' );
// check if sort_mode has anything to do with our link
if( $sort_asc == $isort_mode ) {
'iname' => 'icon-sort-up',
'iexplain' => 'ascending',
$url_params .= $sort_desc;
} elseif( $sort_desc == $isort_mode ) {
'iname' => 'icon-sort-down',
'iexplain' => 'descending',
$url_params .= $sort_asc;
$url_params .= $hash['isort']. '_'. ( isset ( $hash['iorder'] ) ? $hash['iorder'] : 'asc' );
$ignore = array( 'iatitle', 'icontrol', 'isort', 'ianchor', 'isort_mode', 'iorder', 'ititle', 'idefault', 'ifile', 'ipackage', 'itype', 'iurl', 'ionclick', 'ibiticon', 'iforce', 'itra' );
// append any other paramters that were passed in
foreach( $hash as $key => $val ) {
if( !empty( $val ) && !in_array( $key, $ignore ) ) {
// normally the key is a string
$url_params .= empty( $url_params ) ? '?' : '&';
$url_params .= $key. "=". $val;
// but sometimes it can be an array
$url_params .= empty( $url_params ) ? '?' : '&';
$url_params .= $key. "[]=". $v;
if( !empty( $hash['icontrol'] ) && is_array( $hash['icontrol'] ) ) {
$sep = empty( $url_params ) ? '?' : '&';
$url_params .= !empty( $hash['icontrol']['current_page'] ) ? $sep. 'list_page='. $hash['icontrol']['current_page'] : '';
$sep = empty( $url_params ) ? '?' : '&';
$url_params .= !empty( $hash['icontrol']['find'] ) ? $sep. 'find='. $hash['icontrol']['find'] : '';
if( !empty( $hash['icontrol']['parameters'] ) && is_array( $hash['icontrol']['parameters'] ) ) {
foreach( $hash['icontrol']['parameters'] as $key => $value ) {
$sep = empty( $url_params ) ? '?' : '&';
$url_params .= $sep. $key. "=". $value;
// encode quote marks so we not break href="" construction
if( isset ( $hash['itype'] ) && $hash['itype'] == 'url' ) {
$ret = '<a class="icon" '. $atitle. ' '. ( !empty( $params['ionclick'] ) ? 'onclick="'. $params['ionclick']. '" ' : '' ). 'href="'. $url. $url_params. ( !empty( $params['ianchor'] ) ? '#'. $params['ianchor'] : '' ). '">';
// if we want to display an icon instead of text, do that
if( isset ( $hash['booticon'] ) ) {
'iname' => $hash['booticon'],
'iexplain' => $hash['ititle'], // use untranslated ititle - booticon has a tra()
if( !empty( $hash['iforce'] ) ) {
$booticon['iforce'] = $hash['iforce'];
} elseif( isset ( $hash['ibiticon'] ) ) {
$tmp = explode( '/', $hash['ibiticon'] );
'iexplain' => $hash['ititle'], // use untranslated ititle - biticon has a tra()
if( !empty( $hash['iforce'] ) ) {
$ibiticon['iforce'] = $hash['iforce'];
if( isset ( $sorticon ) ) {
if( isset ( $params['itype'] ) && $params['itype'] == 'li' ) {
$ret = '<li>'. $ret. '</li>';
|