Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to override sorting flags for page header-based or default ordering. #1173

Merged
merged 1 commit into from Nov 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -10,12 +10,13 @@
* Added `getTaxonomyItemKeys` to the Taxonomy object [#1124](https://github.com/getgrav/grav/issues/1124)
* Added a `redirect_me` Twig function [#1124](https://github.com/getgrav/grav/issues/1124)
* Added a Caddyfile for newer Caddy versions [#1115](https://github.com/getgrav/grav/issues/1115)
* Allow to override sorting flags for page header-based or default ordering. If the `intl` PHP extension is loaded, only these flags are available: https://secure.php.net/manual/en/collator.asort.php. Otherwise, you can use the PHP standard sorting flags (https://secure.php.net/manual/en/array.constants.php) [#1169](https://github.com/getgrav/grav/issues/1169)
1. [](#bugfix)
* Fixed an issue with site redirects/routes, not processing with extension (.html, .json, etc.)
* Don't truncate HTML if content length is less than summary size [#1125](https://github.com/getgrav/grav/issues/1125)
* Return max available number when calling random() on a collection passing an int > available items [#1135](https://github.com/getgrav/grav/issues/1135)
* Use correct ratio when applying image filters to image alternatives [#1147](https://github.com/getgrav/grav/issues/1147)
* Fixed URI path in multi-site when query parameters were used in front page
* Fixed URI path in multi-site when query parameters were used in front page

# v1.1.8
## 10/22/2016
Expand Down
6 changes: 3 additions & 3 deletions system/src/Grav/Common/Page/Collection.php
Expand Up @@ -84,7 +84,6 @@ public function copy()
public function setParams(array $params)
{
$this->params = array_merge($this->params, $params);

return $this;
}

Expand Down Expand Up @@ -172,12 +171,13 @@ public function remove($key = null)
* @param string $by
* @param string $dir
* @param array $manual
* @param string $sort_flags
*
* @return $this
*/
public function order($by, $dir = 'asc', $manual = null)
public function order($by, $dir = 'asc', $manual = null, $sort_flags = null)
{
$this->items = $this->pages->sortCollection($this, $by, $dir, $manual);
$this->items = $this->pages->sortCollection($this, $by, $dir, $manual, $sort_flags);

return $this;
}
Expand Down
9 changes: 8 additions & 1 deletion system/src/Grav/Common/Page/Page.php
Expand Up @@ -2291,7 +2291,14 @@ public function collection($params = 'content', $pagination = true)
$by = isset($params['order']['by']) ? $params['order']['by'] : 'default';
$dir = isset($params['order']['dir']) ? $params['order']['dir'] : 'asc';
$custom = isset($params['order']['custom']) ? $params['order']['custom'] : null;
$collection->order($by, $dir, $custom);
$sort_flags = isset($params['order']['sort_flags']) ? $params['order']['sort_flags'] : null;

if (is_array($sort_flags)) {
$sort_flags = array_map('constant', $sort_flags); //transform strings to constant value
$sort_flags = array_reduce($sort_flags, function($a, $b) { return $a | $b; }, 0); //merge constant values using bit or
}

$collection->order($by, $dir, $custom, $sort_flags);
}

/** @var Grav $grav */
Expand Down
22 changes: 13 additions & 9 deletions system/src/Grav/Common/Page/Pages.php
Expand Up @@ -21,6 +21,7 @@
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use Whoops\Exception\ErrorException;
use Collator as Collator;

class Pages
{
Expand Down Expand Up @@ -197,7 +198,7 @@ public function addPage(Page $page, $route = null)
*
* @return array
*/
public function sort(Page $page, $order_by = null, $order_dir = null)
public function sort(Page $page, $order_by = null, $order_dir = null, $sort_flags = null)
{
if ($order_by === null) {
$order_by = $page->orderBy();
Expand All @@ -214,7 +215,7 @@ public function sort(Page $page, $order_by = null, $order_dir = null)
}

if (!isset($this->sort[$path][$order_by])) {
$this->buildSort($path, $children, $order_by, $page->orderManual());
$this->buildSort($path, $children, $order_by, $page->orderManual(), $sort_flags);
}

$sort = $this->sort[$path][$order_by];
Expand All @@ -235,7 +236,7 @@ public function sort(Page $page, $order_by = null, $order_dir = null)
* @return array
* @internal
*/
public function sortCollection(Collection $collection, $orderBy, $orderDir = 'asc', $orderManual = null)
public function sortCollection(Collection $collection, $orderBy, $orderDir = 'asc', $orderManual = null, $sort_flags = null)
{
$items = $collection->toArray();
if (!$items) {
Expand All @@ -244,7 +245,7 @@ public function sortCollection(Collection $collection, $orderBy, $orderDir = 'as

$lookup = md5(json_encode($items) . json_encode($orderManual) . $orderBy . $orderDir);
if (!isset($this->sort[$lookup][$orderBy])) {
$this->buildSort($lookup, $items, $orderBy, $orderManual);
$this->buildSort($lookup, $items, $orderBy, $orderManual, $sort_flags);
}

$sort = $this->sort[$lookup][$orderBy];
Expand Down Expand Up @@ -1023,12 +1024,11 @@ protected function buildRoutes()
* @throws \RuntimeException
* @internal
*/
protected function buildSort($path, array $pages, $order_by = 'default', $manual = null)
protected function buildSort($path, array $pages, $order_by = 'default', $manual = null, $sort_flags = null)
{
$list = [];
$header_default = null;
$header_query = null;
$sort_flags = SORT_NATURAL | SORT_FLAG_CASE;

// do this header query work only once
if (strpos($order_by, 'header.') === 0) {
Expand Down Expand Up @@ -1070,24 +1070,28 @@ protected function buildSort($path, array $pages, $order_by = 'default', $manual
} else {
$list[$key] = $header_default ?: $key;
}
$sort_flags = SORT_REGULAR;
$sort_flags = $sort_flags ?: SORT_REGULAR;
break;
case 'manual':
case 'default':
default:
$list[$key] = $key;
$sort_flags = SORT_REGULAR;
$sort_flags = $sort_flags ?: SORT_REGULAR;
}
}

if (!$sort_flags) {
$sort_flags = SORT_NATURAL | SORT_FLAG_CASE;
}

// handle special case when order_by is random
if ($order_by == 'random') {
$list = $this->arrayShuffle($list);
} else {
// else just sort the list according to specified key
if (extension_loaded('intl')) {
$locale = setlocale(LC_COLLATE, 0); //`setlocale` with a 0 param returns the current locale set
$col = \Collator::create($locale);
$col = Collator::create($locale);
if ($col) {
$col->asort($list, $sort_flags);
} else {
Expand Down