Smarty
[ class tree: Smarty ] [ index: Smarty ] [ all elements ]

Source for file function.smartlink.php

Documentation is available at function.smartlink.php

  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * @package Smarty
  5.  * @subpackage plugins
  6.  * @author xing <xing$synapse.plus.com>
  7.  * @link http://www.bitweaver.org/wiki/function_smartlink function.smartlink
  8.  */
  9.  
  10. /**
  11.  * Smarty {smartlink} function plugin
  12.  *
  13.  * Type:    function<br>
  14.  * Name:    smartlink<br>
  15.  * Input:<br>
  16.  *            - ititle    (required)    words that are displayed<br>
  17.  *            - iatitle    (optional)    alternative text for the link title (rollover, etc.) <br>
  18.  *            - ianchor    (optional)    set the anchor where the link should point to<br>
  19.  *            - isort        (optional)    name of the sort column without the orientation (e.g.: title)<br>
  20.  *            - isort_mode(optional)    this can be used to manually pass the sort mode to smartlink<br>
  21.  *                                    overrides the value given in $_REQUEST['sort_mode'], which is the default<br>
  22.  *            - iorder    (optional)    if set to asc or desc, it sets the default sorting order of this particular column<br>
  23.  *                                    asc is default<br>
  24.  *            - idefault    (optional)    if set, it will highlight this link if no $isort_mode is given<br>
  25.  *                                    this should only be set once per sorting group since it represents the default sorting column<br>
  26.  *            - itra        (optional)    if present then don't translate<br>
  27.  *            - itype        (optional)    can be set to<br>
  28.  *                                    url        -->        outputs only url<br>
  29.  *                                    li        -->        outputs link as &lt;li&gt;&lt;a ... &gt;&lt;/li&gt;<br>
  30.  *            - ionclick    (optional)    pass in any actions that should occur onclick<br>
  31.  *            - ibiticon    (optional)    if you want to display an icon instead of text use ibiticon<br>
  32.  *                                    format is:    '&lt;ipackage&gt;/&lt;iname&gt;'<br>
  33.  *                                    e.g.:        'liberty/edit'<br>
  34.  *            - iforce    (optional)    pass iforce parameter through to biticon
  35.  *            - iurl        (optional)    pass in a full url
  36.  *            - ifile        (optional)    set the file where the link should point (default is the current file)<br>
  37.  *            - ipackage    (optional)    set the package the link should point to (default is the current package)<br>
  38.  *            - icontrol    (optional)    the hash sent out by postGetList()
  39.  *            - *            (optional)    anything else that gets added to the pile of items is appended using &amp;$key=$val<br>
  40.  *            - ihash        (optional)    you can pass in all the above as an array called ihash or secondary * items common to all links<br>
  41.  * 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
  42.  *            also useful if the you want to display an icon as link since smartlink takes biticon parameters<br>
  43.  * Example    - {smartlink ititle="Page Name" isort="title"}<br>
  44.  *            - {smartlink ititle="Page Name" isort="title" iorder="desc" idefault=1}<br>
  45.  *                setting iorder and idefault here, makes this link sort in a descending order by default (iorder)<br>
  46.  *                and it is highlighted when $isort_mode ( or $_REQUEST['sort_mode'] ) is not set (idefault)<br>
  47.  * Note Be careful if ititle is generated dynamically since it is passed through tra() by default, use itra to override<br>
  48.  */
  49. function smarty_function_smartlink( $params, &$gBitSmarty ) {
  50.     if( !empty( $params['ihash'] ) ) {
  51.         $hash = array_merge( $params['ihash'], $params );
  52.         $hash['ihash'] = NULL;
  53.     } else {
  54.         // maybe params were passed in separately
  55.         $hash = &$params;
  56.     }
  57.  
  58.     if( !isset( $hash['ititle'] ) ) {
  59.         return 'You need to supply "ititle" for {smartlink} to work.';
  60.     }
  61.  
  62.     // work out what the url is
  63.     if( !empty( $hash['iurl'] ) ) {
  64.         $url = $hash['iurl'];
  65.     } elseif( !empty( $hash['ifile'] ) ) {
  66.         if( !empty( $hash['ipackage'] ) ) {
  67.             if( $hash['ipackage'] == 'root' ) {
  68.                 $url = BIT_ROOT_URL.$hash['ifile'];
  69.             } else {
  70.                 $url = constant( strtoupper( $hash['ipackage'] ).'_PKG_URL' ).$hash['ifile'];
  71.             }
  72.         } else {
  73.             $url = constant( strtoupper( ACTIVE_PACKAGE ).'_PKG_URL' ).$hash['ifile'];
  74.         }
  75.     } else {
  76.         $url = $_SERVER['SCRIPT_NAME'];
  77.     }
  78.  
  79.     $url_params = NULL;
  80.     if( isset( $hash['itra'] ) && !empty( $hash['itra'] ) ) {
  81.         // present and non-zero value
  82.         $ititle = $hash['ititle'];
  83.         $iatitle =  empty( $hash['iatitle'] ) ? $ititle : $hash['iatitle'];
  84.     } else {
  85.         $ititle = tra( $hash['ititle'] );
  86.         $iatitle =  empty( $hash['iatitle'] ) ? $ititle : tra ( $hash['iatitle'] );
  87.     }
  88.  
  89.     $atitle = 'title="'.$iatitle.'"';
  90.  
  91.     // if isort is set, we need to deal with all the sorting stuff
  92.     if( !empty( $hash['isort'] ) ) {
  93.         $isort_mode = isset( $hash['isort_mode'] ) ? $hash['isort_mode'] : isset( $_REQUEST['sort_mode'] ) ? $_REQUEST['sort_mode'] : NULL ;
  94.         $sort_asc = $hash['isort'].'_asc';
  95.         $sort_desc = $hash['isort'].'_desc';
  96.  
  97.         $atitle = 'title="'.tra( 'Sort by' ).": ".$iatitle.'"';
  98.         $url .= '?';
  99.         $url_params .= 'sort_mode=';
  100.  
  101.         // check if we have to highlight this link, when $isort_mode isn't set
  102.         if( isset( $hash['idefault'] ) && empty( $isort_mode ) ) {
  103.             $isort_mode .= $hash['isort'].'_'.( isset( $hash['iorder'] ) ? $hash['iorder'] : 'asc' );
  104.         }
  105.  
  106.         // check if sort_mode has anything to do with our link
  107.         if( $sort_asc == $isort_mode ) {
  108.             $sorticon = array(
  109.                 'ipackage' => 'icons',
  110.                 'iname' => 'icon-sort-up',
  111.                 'iexplain' => 'ascending',
  112.                 'iforce' => 'icon',
  113.             );
  114.             $url_params .= $sort_desc;
  115.         } elseif( $sort_desc == $isort_mode ) {
  116.             $sorticon = array(
  117.                 'ipackage' => 'icons',
  118.                 'iname' => 'icon-sort-down',
  119.                 'iexplain' => 'descending',
  120.                 'iforce' => 'icon',
  121.             );
  122.             $url_params .= $sort_asc;
  123.         } else {
  124.             $url_params .= $hash['isort'].'_'.( isset( $hash['iorder'] ) ? $hash['iorder'] : 'asc' );
  125.         }
  126.     }
  127.  
  128.     $ignore = array( 'iatitle', 'icontrol', 'isort', 'ianchor', 'isort_mode', 'iorder', 'ititle', 'idefault', 'ifile', 'ipackage', 'itype', 'iurl', 'ionclick', 'ibiticon', 'iforce', 'itra' );
  129.     // append any other paramters that were passed in
  130.     foreach( $hash as $key => $val ) {
  131.         if( !empty( $val ) && !in_array( $key, $ignore ) ) {
  132.             // normally the key is a string
  133.             if( !is_array( $val ) ){
  134.                 $url_params .= empty( $url_params ) ? '?' : '&amp;';
  135.                 $url_params .= $key."=".$val;
  136.             // but sometimes it can be an array
  137.             }else{
  138.                 foreach( $val as $v ){
  139.                     $url_params .= empty( $url_params ) ? '?' : '&amp;';
  140.                     $url_params .= $key."[]=".$v;
  141.                 }
  142.             }
  143.         }
  144.     }
  145.  
  146.     if( !empty( $hash['icontrol'] ) && is_array( $hash['icontrol'] ) ) {
  147.         $sep = empty( $url_params ) ? '?' : '&amp;';
  148.         $url_params .= !empty( $hash['icontrol']['current_page'] ) ? $sep.'list_page='.$hash['icontrol']['current_page'] : '';
  149.         $sep = empty( $url_params ) ? '?' : '&amp;';
  150.         $url_params .= !empty( $hash['icontrol']['find'] ) ? $sep.'find='.$hash['icontrol']['find'] : '';
  151.         if( !empty( $hash['icontrol']['parameters'] ) && is_array( $hash['icontrol']['parameters'] ) ) {
  152.             foreach( $hash['icontrol']['parameters'] as $key => $value ) {
  153.                 if( !empty( $value )) {
  154.                     $sep = empty( $url_params ) ? '?' : '&amp;';
  155.                     $url_params .= $sep.$key."=".$value;
  156.                 }
  157.             }
  158.         }
  159.     }
  160.  
  161.     // encode quote marks so we not break href="" construction
  162.     $url_params = preg_replace('/"/', '%22', $url_params);
  163.  
  164.     if( isset( $hash['itype'] ) && $hash['itype'] == 'url' ) {
  165.         $ret = $url.$url_params;
  166.     } else {
  167.         $ret = '<a class="icon" '.$atitle.' '.( !empty( $params['ionclick'] ) ? 'onclick="'.$params['ionclick'].'" ' : '' ).'href="'.$url.$url_params.( !empty( $params['ianchor'] ) ? '#'.$params['ianchor'] : '' ).'">';
  168.  
  169.         // if we want to display an icon instead of text, do that
  170.         if( isset( $hash['booticon'] ) ) {
  171.             if( !empty( $tmp[2] )) {
  172.                 $tmp[1] .= "/".$tmp[2];
  173.             }
  174.             $booticon = array(
  175.                 'iname' => $hash['booticon'],
  176.                 'iexplain' => $hash['ititle'], // use untranslated ititle - booticon has a tra()
  177.             );
  178.             if( !empty( $hash['iforce'] ) ) {
  179.                 $booticon['iforce'] = $hash['iforce'];
  180.             }
  181.             $ret .= smarty_function_booticon( $booticon, $gBitSmarty );
  182.         } elseif( isset( $hash['ibiticon'] ) ) {
  183.             $tmp = explode( '/', $hash['ibiticon'] );
  184.             if( !empty( $tmp[2] )) {
  185.                 $tmp[1] .= "/".$tmp[2];
  186.             }
  187.             $ibiticon = array(
  188.                 'ipackage' => $tmp[0],
  189.                 'iname' => $tmp[1],
  190.                 'iexplain' => $hash['ititle'], // use untranslated ititle - biticon has a tra()
  191.             );
  192.             if( !empty( $hash['iforce'] ) ) {
  193.                 $ibiticon['iforce'] = $hash['iforce'];
  194.             }
  195.             $ret .= smarty_function_biticon( $ibiticon, $gBitSmarty );
  196.         } else {
  197.             $ret .= $ititle;
  198.         }
  199.  
  200.         if( isset( $sorticon ) ) {
  201.             $ret .= '&nbsp;'.smarty_function_booticon( $sorticon, $gBitSmarty );
  202.         }
  203.         $ret .= '</a>';
  204.     }
  205.     if( isset( $params['itype'] ) && $params['itype'] == 'li' ) {
  206.         $ret = '<li>'.$ret.'</li>';
  207.     }
  208.     return $ret;
  209. }
  210. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:40 +0000 by phpDocumentor 1.5.0-lsces