Skip to content

Instantly share code, notes, and snippets.

@pspanja
Created October 22, 2013 08:26
Show Gist options
  • Save pspanja/7097063 to your computer and use it in GitHub Desktop.
Save pspanja/7097063 to your computer and use it in GitHub Desktop.
<?php
/**
* File containing the SiteHelper class.
*
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/
namespace eZ\Publish\Core\Helper;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
use eZ\Publish\API\Repository\Values\Content\Location;
/**
* Helper class for building multilingual Sites "eZ" way
*/
class SiteHelper
{
/**
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface
*/
protected $configResolver;
/**
* @var \eZ\Publish\API\Repository\LocationService
*/
protected $locationService;
public function __construct(
ConfigResolverInterface $configResolver,
LocationService $locationService,
SearchService $searchService
)
{
$this->configResolver = $configResolver;
$this->locationService = $locationService;
$this->searchService = $searchService;
}
/**
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
public function loadContentByContentInfo( ContentInfo $contentInfo )
{
$criterion = new Criterion\LogicalAnd(
array(
new Criterion\ContentId( $contentInfo->id ),
$this->getLanguageCriteria()
)
);
return $this->searchService->findSingle( $criterion );
}
/**
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
public function loadContentByVersionInfo( VersionInfo $versionInfo )
{
$criterion = new Criterion\LogicalAnd(
array(
new Criterion\ContentId( $versionInfo->contentInfo->id ),
$this->getLanguageCriteria()
)
);
return $this->searchService->findSingle( $criterion );
}
/**
* @param mixed $contentId
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
public function loadContent( $contentId )
{
$criterion = new Criterion\LogicalAnd(
array(
new Criterion\ContentId( $contentId ),
$this->getLanguageCriteria()
)
);
return $this->searchService->findSingle( $criterion );
}
/**
* @param string $remoteId
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
public function loadContentByRemoteId( $remoteId )
{
$criterion = new Criterion\LogicalAnd(
array(
new Criterion\RemoteId( $remoteId ),
$this->getLanguageCriteria()
)
);
return $this->searchService->findSingle( $criterion );
}
/**
* @param mixed $locationId
*
* @return \eZ\Publish\API\Repository\Values\Content\Location
*/
public function loadLocation( $locationId )
{
$criterion = new Criterion\LogicalAnd(
array(
new Criterion\LocationId( $locationId ),
$this->getLanguageCriteria()
)
);
$this->searchService->findSingle( $criterion );
return $this->locationService->loadLocation( $locationId );
}
/**
* @param string $remoteId
*
* @return \eZ\Publish\API\Repository\Values\Content\Location
*/
public function loadLocationByRemoteId( $remoteId )
{
$criterion = new Criterion\LogicalAnd(
array(
new Criterion\LocationRemoteId( $remoteId ),
$this->getLanguageCriteria()
)
);
$this->searchService->findSingle( $criterion );
return $this->locationService->loadLocationByRemoteId( $remoteId );
}
/**
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
* @param \eZ\Publish\API\Repository\Values\Content\Location $rootLocation
*
* @return \eZ\Publish\API\Repository\Values\Content\Location[]
*/
public function loadLocations( ContentInfo $contentInfo, Location $rootLocation = null )
{
$criteria = array(
new Criterion\ContentId( $contentInfo->id ),
$this->getLanguageCriteria()
);
if ( $rootLocation !== null )
{
$criteria[] = new Criterion\Subtree( $rootLocation->pathString );
}
$query = new Query( array( "criterion" => new Criterion\LogicalAnd( $criteria ) ) );
$searchResult = $this->searchService->findContent( $query );
$locations = array();
foreach ( $searchResult->searchHits as $searchHit )
{
/** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
$content = $searchHit->valueObject;
$contentLocations = $this->locationService->loadLocations(
$content->contentInfo,
$rootLocation
);
foreach ( $contentLocations as $contentLocation )
{
if ( $rootLocation === null || strpos( $rootLocation->pathString, $contentLocation->pathString ) === 0 )
{
$locations[] = $contentLocation;
}
}
}
return $locations;
}
/**
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
* @param int $offset
* @param int $limit
*
* @return \eZ\Publish\API\Repository\Values\Content\Location[]
*/
public function loadLocationChildren( Location $location, $offset = 0, $limit = -1 )
{
$query = new Query(
array(
"criterion" => new Criterion\LogicalAnd(
array(
new Criterion\Subtree( $location->pathString ),
new Criterion\Depth( Criterion\Operator::EQ, $location->depth + 1 ),
$this->getLanguageCriteria()
)
),
"offset" => $offset,
"limit" => $limit === -1 ? null : $limit
)
);
$searchResult = $this->searchService->findContent( $query );
$locations = array();
foreach ( $searchResult->searchHits as $searchHit )
{
/** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
$content = $searchHit->valueObject;
$contentLocations = $this->locationService->loadLocations(
$content->contentInfo,
$location
);
foreach ( $contentLocations as $contentLocation )
{
if ( $contentLocation->parentLocationId == $location->id )
{
$locations[] = $contentLocation;
}
}
}
return $locations;
}
/**
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*
* @return int
*/
public function getLocationChildCount( Location $location )
{
$query = new Query(
array(
"criterion" => new Criterion\LogicalAnd(
array(
new Criterion\Subtree( $location->pathString ),
new Criterion\Depth( Criterion\Operator::EQ, $location->depth + 1 ),
$this->getLanguageCriteria()
)
),
"offset" => 0,
"limit" => 0
)
);
return $this->searchService->findContent( $query )->searchHits;
}
/**
* @param \eZ\Publish\API\Repository\Values\Content\Query $query
*
* @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult
*/
public function findContent( Query $query )
{
$query->criterion = new Criterion\LogicalAnd(
array(
$query->criterion,
$this->getLanguageCriteria()
)
);
return $this->searchService->findContent( $query );
}
/**
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
public function findSingle( Criterion $criterion )
{
return $this->searchService->findSingle(
new Criterion\LogicalAnd(
array(
$criterion,
$this->getLanguageCriteria()
)
)
);
}
/**
* @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\LanguageCode
*/
protected function getLanguageCriteria()
{
$languages = $this->configResolver->getParameter( 'languages' );
return new Criterion\LanguageCode( $languages, true );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment