ShowSortlinkViewHelper

Eigener ViewHelper für Ausgabe von Links zum Sortieren

Rendert einen Link zum Sortieren von Ergebnissen aus Listen oder Suchergebniss-Listen. Bei letzterem werden Parameter aus der URL gelöscht, die keine Einträge haben (z.B. tx_rsysproductbase_productlist[searchProduct]= würde eliminiert), damit sich keine unnötig langen URLs ergeben. 

Ferner werden alle Parameter aus GET und POST zusammengewürfelt und ggf. an den Link angehängt um Werte aus einem geposteten Suchformular beim Link nicht zu verlieren.

Siehe auch LinkViewHelper

Aufruf

<rsys:List.ShowSortlink sortby="title" label="tx_rsysproductbase_domain_model_product.title"/>       

Konfiguration

plugin.tx_rsysproductbase {
   
    List {
        sortlink_active_wrap (
        <th>
            <span class="active">
                |
            </span>
        </th>
        )
        sortlink_wrap (
        <th>
            <span class="sort">
                |
            </span>
        </th>
        )
    }
}

Listing

<?php

/*                                                                        *
 * This script belongs to the FLOW3 package "Fluid".                      *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License as published by the *
 * Free Software Foundation, either version 3 of the License, or (at your *
 * option) any later version.                                             *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
 * General Public License for more details.                               *
 *                                                                        *
 * You should have received a copy of the GNU Lesser General Public       *
 * License along with the script.                                         *
 * If not, see www.gnu.org/licenses/lgpl.html                    &nbsp; *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

/**
 *
 * = Examples =
 *
 * <code title="default notation">
 * {namespace rsys=Tx_Rsysproductbase_ViewHelpers}
 * <rsys:Detail.ShowSortlink value="{product.title}" label="tx_rsysproductbase_domain_model_product.title"/>
 * </code>
 * <output>
 * row with label wrapped by TS plugin.tx_rsysproductbase.List.sortlink_wrap / sortlink_active_wrap
 * </output>
 *
 * @license www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
 * @api
 */
class Tx_Rsysproductbase_ViewHelpers_List_ShowSortlinkViewHelper extends Tx_Fluid_ViewHelpers_Format_AbstractEncodingViewHelper implements t3lib_Singleton {

    /**
     * Disable the escaping interceptor because otherwise the child nodes would be escaped before this view helper
     * can decode the text's entities.
     *
     * @var boolean
     */
    protected $escapingInterceptorEnabled = FALSE;


    /**
     * @var Tx_Extbase_MVC_Web_Routing_UriBuilder
     */
    protected $uriBuilder;

    /**
     * Returns all the get and post arguments to be passed along by the pager.
     *
     * @return array All the get and post arguments.
     */
    public static function getAllGPArguments() {
        $postParameter = t3lib_div::_POST();
        $getParameter = t3lib_div::_GET();

        $mergedParameters = t3lib_div::array_merge_recursive_overrule($getParameter, $postParameter);

        return $mergedParameters;
    }

    /**
     * shows a sort link wrapped with TS settings
     *
     * @param string $sortby string sort order
     * @param string $label string for label
     * @param boolean $keepQuotes if TRUE, single and double quotes won't be replaced (sets ENT_NOQUOTES flag)
     * @param string $encoding
     * @param boolean $doubleEncode If FALSE existing html entities won't be encoded, the default is to convert everything.
     * @return string the altered string
     * @author Erwin Knoll <typo3coding@rootsystem.de>, Rootsystem
     * @api
     */
    public function render($sortby = NULL, $label=NULL, $keepQuotes = FALSE, $encoding = NULL, $doubleEncode = TRUE) {

        // get uri builder
        $uriBuilder = $this->controllerContext->getUriBuilder();

        // get the proper arguments
        $this->linkArguments = Tx_Rsysproductbase_ViewHelpers_LinkViewHelper::getAllGPArguments();
        $initial_sorting = $this->linkArguments['tx_rsysproductbase_productlist']['sort'];

        // unset some vars
        unset($this->linkArguments['L']);
        unset($this->linkArguments['cHash']);
        unset($this->linkArguments['tx_rsysproductbase_productlist']['__referrer']);
        unset($this->linkArguments['tx_rsysproductbase_productlist']['__hmac']);
        // unset empty search params
        if(isset($this->linkArguments['tx_rsysproductbase_productlist']['searchProduct'] )) {
            foreach ($this->linkArguments['tx_rsysproductbase_productlist']['searchProduct'] as $key=>$value) {
                if(empty($value)) {
                    unset($this->linkArguments['tx_rsysproductbase_productlist']['searchProduct'][$key]);
                }
            }
        }

        // add sorting
        $this->linkArguments['tx_rsysproductbase_productlist']['sort'] = $sortby;

        // generate the uri to the page
        $uri = $uriBuilder
        ->reset()
        ->setArguments($this->linkArguments)
        ->build();

        // label
        if($label && (strpos($label,'tx_rsysproductbase') === 0)) {
            $label = Tx_Extbase_Utility_Localization::translate($label, 'Rsysproductbase');
        }
           
        // link
        $value = '<a href="' . $uri . '" title="' . $title . '">' . $label . '</a>';

        // wrap
        $settings = $this->templateVariableContainer->get('settings');
        $wrap_normal = $settings['List']['sortlink_wrap'];
        $wrap_active = $settings['List']['sortlink_active_wrap'];

        $wrap = $sortby == $initial_sorting ? $wrap_active : $wrap_normal;

        $ret = preg_replace ('/\|/',$value, $wrap, 1);

        return $ret;
    }
}
?>

Erstellt: 09/2012| Geändert: 10/2015