Created
September 21, 2015 12:22
-
-
Save emodric/089be27ec2e6709a8658 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
namespace Netgen\Bundle\ToolsBundle\Helper; | |
use eZ\Publish\API\Repository\Values\Content\Query; | |
use eZ\Publish\API\Repository\Values\Content\Query\SortClause; | |
use eZ\Publish\API\Repository\Values\Content\Location; | |
class SortClauseHelper | |
{ | |
/** | |
* Returns query sort order by location sort order | |
* | |
* @param int $sortOrder | |
* | |
* @return string | |
*/ | |
public function getQuerySortOrder( $sortOrder ) | |
{ | |
if ( $sortOrder === Location::SORT_ORDER_DESC ) | |
{ | |
return Query::SORT_DESC; | |
} | |
return Query::SORT_ASC; | |
} | |
/** | |
* Instantiates a correct sort clause object based on provided location sort field and sort order | |
* | |
* @param string $sortField | |
* @param int $sortOrder | |
* | |
* @return \eZ\Publish\API\Repository\Values\Content\Query\SortClause | |
*/ | |
public function getSortClauseBySortField( $sortField, $sortOrder = Location::SORT_ORDER_ASC ) | |
{ | |
$querySortOrder = $this->getQuerySortOrder( $sortOrder ); | |
switch ( $sortField ) | |
{ | |
case 'path': | |
case 'path_string': | |
case Location::SORT_FIELD_PATH: | |
return new SortClause\Location\Path( $querySortOrder ); | |
case 'published': | |
case Location::SORT_FIELD_PUBLISHED: | |
return new SortClause\DatePublished( $querySortOrder ); | |
case 'modified': | |
case Location::SORT_FIELD_MODIFIED: | |
return new SortClause\DateModified( $querySortOrder ); | |
case 'section': | |
case Location::SORT_FIELD_SECTION: | |
return new SortClause\SectionIdentifier( $querySortOrder ); | |
case 'depth': | |
case Location::SORT_FIELD_DEPTH: | |
return new SortClause\Location\Depth( $querySortOrder ); | |
//@todo: sort clause not yet implemented | |
// case 'class_identifier' | |
// case Location::SORT_FIELD_CLASS_IDENTIFIER: | |
//@todo: sort clause not yet implemented | |
// case 'class_name' | |
// case Location::SORT_FIELD_CLASS_NAME: | |
case 'priority': | |
case Location::SORT_FIELD_PRIORITY: | |
return new SortClause\Location\Priority( $querySortOrder ); | |
case 'name': | |
case Location::SORT_FIELD_NAME: | |
return new SortClause\ContentName( $querySortOrder ); | |
//@todo: sort clause not yet implemented | |
// case Location::SORT_FIELD_MODIFIED_SUBNODE: | |
case Location::SORT_FIELD_NODE_ID: | |
return new SortClause\Location\Id( $querySortOrder ); | |
case Location::SORT_FIELD_CONTENTOBJECT_ID: | |
return new SortClause\ContentId( $querySortOrder ); | |
default: | |
return new SortClause\Location\Path( $querySortOrder ); | |
} | |
} | |
} |
This file contains hidden or 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
$query->sortClauses = array( | |
$this->container->get( 'netgen.tools.helper.sort_clause_helper' )->getSortClauseBySortField( | |
$location->sortField, | |
$location->sortOrder | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment