function sort($title, $key = null , $options = array()) {
$options = array_merge(array( 'url' => array(), 'model' => null ), $options);
$url = $options[ 'url' ];
unset($options[ 'url' ]);
if (empty($key)) {
$key = $title;
$title = __(Inflector::humanize(preg_replace( '/_id$/' , '' , $title)), true );
}
$dir = 'asc' ;
$sortKey = $ this ->sortKey($options[ 'model' ]);
$isSorted = ($sortKey === $key || $sortKey === $ this ->defaultModel() . '.' . $key);
if ($isSorted && $ this ->sortDir($options[ 'model' ]) === 'asc' ) {
$dir = 'desc' ;
}
if (is_array($title) && array_key_exists($dir, $title)) {
$title = $title[$dir];
}
$url = array_merge(array( 'sort' => $key, 'direction' => $dir), $url, array( 'order' => null ));
return $ this ->link($title, $url, $options);
}
* This source code was highlighted with Source Code Highlighter .
unset($options['url']);
add:// patch:
if ($title == @$ this -> params [ 'named' ][ 'sort' ]) {
$options[ 'class' ] = $ this -> params [ 'named' ][ 'direction' ];
}
// endpatch
* This source code was highlighted with Source Code Highlighter .
<?php
App::import( 'Helper' , 'Paginator' );
class MyPaginatorHelper extends PaginatorHelper {
/**
* Generates a sorting link
*
* @param string $title Title for the link.
* @param string $key The name of the key that the recordset should be sorted.
* @param array $options Options for sorting link. See #options for list of keys.
* @return string A link sorting default by 'asc'. If the resultset is sorted 'asc' by the specified
* key the returned link will sort by 'desc'.
*/
function sort($title, $key = null , $options = array()) {
$options = array_merge(array( 'url' => array(), 'model' => null ), $options);
$url = $options[ 'url' ];
unset($options[ 'url' ]);
// patch:
if ($title == @$ this -> params [ 'named' ][ 'sort' ]) {
$options[ 'class' ] = $ this -> params [ 'named' ][ 'direction' ];
}
// endpath
if (empty($key)) {
$key = $title;
$title = __(Inflector::humanize(preg_replace( '/_id$/' , '' , $title)), true );
}
$dir = 'asc' ;
$sortKey = $ this ->sortKey($options[ 'model' ]);
$isSorted = ($sortKey === $key || $sortKey === $ this ->defaultModel() . '.' . $key);
if ($isSorted && $ this ->sortDir($options[ 'model' ]) === 'asc' ) {
$dir = 'desc' ;
}
if (is_array($title) && array_key_exists($dir, $title)) {
$title = $title[$dir];
}
$url = array_merge(array( 'sort' => $key, 'direction' => $dir), $url, array( 'order' => null ));
return $ this ->link($title, $url, $options);
}
}
?>
* This source code was highlighted with Source Code Highlighter .
// ...
class AppController extends Controller {
var $helpers = array( 'Html' , 'Form' , 'Ajax' , 'Javascript' , 'MyPaginator' );
// ...
* This source code was highlighted with Source Code Highlighter .
< a class ="asc" id ="link1091361951" href ="/search/page:1/sort:first_name/direction:desc" > First Name </ a >
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/84660/
All Articles