Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: Added default support for sorting enhanced selection options b…
…y enhanced selection option priority and added copyright and license documentation for bundle
- Loading branch information
1 parent
3758a61
commit 58bdd0b
Showing
6 changed files
with
308 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
COPYRIGHT NOTICE: Copyright (C) 2014-2015 Netgen and Brookins Consulting | ||
SOFTWARE LICENSE: GNU General Public License v2.0 | ||
NOTICE: > | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of version 2.0 of the GNU General | ||
Public License as published by the Free Software Foundation. | ||
|
||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of version 2.0 of the GNU General | ||
Public License along with this program; if not, write to the Free | ||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
MA 02110-1301, USA. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
parameters: | ||
ezpublish.fieldType.sckenhancedselection.converter.class: Netgen\Bundle\EnhancedSelectionBundle\Core\Persistence\Legacy\Content\FieldValue\Converter\EnhancedSelectionConverter | ||
ezpublish.twig.twig_extension.sortbyfield.class: Netgen\Bundle\EnhancedSelectionBundle\Twig\Extension\SortBySelectionFieldExtension | ||
|
||
services: | ||
ezpublish.fieldType.sckenhancedselection.converter: | ||
class: %ezpublish.fieldType.sckenhancedselection.converter.class% | ||
tags: | ||
- {name: ezpublish.storageEngine.legacy.converter, alias: sckenhancedselection, lazy: true, callback: "::create"} | ||
ezpublish.twig.twig_extension.sortbyfield: | ||
class: %ezpublish.twig.twig_extension.sortbyfield.class% | ||
tags: | ||
- { name: twig.extension } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 21 additions & 2 deletions
23
Resources/views/sckenhancedselection_content_field.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,26 @@ | ||
{% block sckenhancedselection_field %} | ||
{% spaceless %} | ||
{% for identifier in field.value.identifiers %} | ||
{{ identifier }}{% if not loop.last %}, {% endif %} | ||
|
||
{% set available_options = fieldSettings.options|sort_by_selection_field( 'priority', 'asc' ) %} | ||
{% set identifiers = field.value.identifiers %} | ||
|
||
{% if fieldSettings.delimiter is not empty %} | ||
{% set delimiter = fieldSettings.delimiter %} | ||
{% else %} | ||
{% set delimiter = ', ' %} | ||
{% endif %} | ||
|
||
{% if available_options is not empty and identifiers is not empty %} | ||
{% for option in available_options %} | ||
{% if option.identifier in identifiers %} | ||
{{ option.name }}{% if not loop.last %}{{ delimiter }}{% endif %} | ||
{% endif %} | ||
{% endfor %} | ||
{% elseif identifiers is not empty %} | ||
{% for identifier in identifiers %} | ||
{{ identifier }}{% if not loop.last %}{{ delimiter }}{% endif %} | ||
{% endfor %} | ||
{% endif %} | ||
|
||
{% endspaceless %} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
/** | ||
* File containing the SortBySelectionFieldExtension class. | ||
* | ||
* @copyright Copyright (C) Brookins Consulting. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
* @version //autogentag// | ||
*/ | ||
|
||
namespace Netgen\Bundle\EnhancedSelectionBundle\Twig\Extension; | ||
|
||
use Twig_Extension; | ||
use Twig_SimpleFilter; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* Twig extension providing the 'sort_by_selection_field' filter | ||
*/ | ||
class SortBySelectionFieldExtension extends Twig_Extension | ||
{ | ||
/** | ||
* The "sort_by_selection_field" twig filter sorts an array of items ( objects or arrays ) by the specified sort by field value | ||
* | ||
* Usage: {% set available_options = options|sort_by_selection_field( 'priority', 'desc' ) %} | ||
* | ||
* @return array The array of Twig_SimpleFilter objects | ||
*/ | ||
public function getFilters() | ||
{ | ||
return array( | ||
new Twig_SimpleFilter( 'sort_by_selection_field', array( $this, 'sortBySelectionField' ) ) | ||
); | ||
} | ||
|
||
/** | ||
* The "sortBySelectionField" method sorts an array of items by the specified sort by field value | ||
* if the available options priority values do not contain duplicates which returns the options without modifications | ||
* | ||
* @param array $content Array of selection options | ||
* @param string $sortByField String of selection sort field. Supports: priority|id|identifier|name. Default: priority | ||
* @param string $sortDirection String of selection sort direction. Supports: asc|desc. Default: asc | ||
* | ||
* @return array The array of FieldType selection options sorted as best as possible | ||
*/ | ||
public function sortBySelectionField( $content, $sortByField = 'priority', $sortDirection = 'asc' ) | ||
{ | ||
$selection_options_field_content_not_sortable = false; | ||
|
||
if( !is_array( $content ) ) | ||
{ | ||
throw new InvalidArgumentException( 'Input passed to sort_by_selection_field twig filter is not an array' ); | ||
} | ||
else if ( $sortByField === null ) | ||
{ | ||
throw new InvalidArgumentException( 'Sort by field parameter passed to the sort_by_selection_field twig filter can not be null' ); | ||
} | ||
else if ( !isset( $content[ 0 ][ $sortByField ] ) ) | ||
{ | ||
throw new InvalidArgumentException( 'Sort by field parameter passed to the sort_by_selection_field twig filter must be either: priority, id, identifier or name' ); | ||
} | ||
else | ||
{ | ||
for( $i = 0, $j = 0, $n = count( $content ); $i < $n; ++$i ) | ||
{ | ||
for( $g = $i + 1; $g < $n; ++$g ) | ||
{ | ||
if( $content[ $i ][ $sortByField ] == $content[ $g ][ $sortByField ] ) | ||
{ | ||
$selection_options_field_content_not_sortable = true; | ||
} | ||
} | ||
|
||
if( $selection_options_field_content_not_sortable == true ) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
if( $selection_options_field_content_not_sortable == false ) | ||
{ | ||
usort( $content, self::usortByPriority( $sortByField, $sortDirection ) ); | ||
} | ||
} | ||
|
||
return $content; | ||
} | ||
|
||
/** | ||
* Provides usort value_compare_func function to sort selection options | ||
* | ||
* @return int The usort value_compare_func function result to determin sort order | ||
*/ | ||
public function usortByPriority( $sortByField = 'priority', $sortDirection = 'asc' ) | ||
{ | ||
return function ( $a, $b ) use ( $sortByField, $sortDirection ) | ||
{ | ||
$sortDirectionInt = $sortDirection === 'desc' ? -1 : 1; | ||
|
||
if ( $a[ $sortByField ] == $b[ $sortByField ] ) | ||
{ | ||
return 0; | ||
} | ||
elseif ( $a[ $sortByField ] > $b[ $sortByField ] ) | ||
{ | ||
return ( 1 * $sortDirectionInt ); | ||
} | ||
else | ||
{ | ||
return ( -1 * $sortDirectionInt ); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Returns the name of the twig extension. | ||
* | ||
* @return string The twig extension name | ||
*/ | ||
public function getName() | ||
{ | ||
return 'sort_by_selection_field'; | ||
} | ||
} |