Skip to content

Instantly share code, notes, and snippets.

@arfaram
Last active July 7, 2017 11:24
Show Gist options
  • Save arfaram/fb93f520f89384c6f39e5ec77d0cc946 to your computer and use it in GitHub Desktop.
Save arfaram/fb93f520f89384c6f39e5ec77d0cc946 to your computer and use it in GitHub Desktop.
<?php
/**
* eZPlatform (1.7 >): delete specific contenttype location children (see ContentTypeIdentifier and change with yours )
* run the script using : php app/console training:delete_locations PARENT-LOCATION-ID
*/
namespace AppBundle\Command;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
class EzDeleteLocationsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName( 'training:delete_locations' )->setDefinition(
array(
new InputArgument( 'parentLocationId', InputArgument::REQUIRED, 'An existing parent location ID' )
)
);
}
protected function execute( InputInterface $input, OutputInterface $output )
{
/** @var $repository \eZ\Publish\API\Repository\Repository */
$repository = $this->getContainer()->get('ezpublish.api.repository');
$locationService = $repository->getLocationService();
//@deprecated since 6.6 to be removed:
//$repository->setCurrentUser($repository->getUserService()->loadUserByLogin( 'admin' ));
//$repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) );
//Use now:
$repository->getPermissionResolver()->setCurrentUserReference( $repository->getUserService()->loadUser( 14 ) );
// fetch the input arguments
$parentLocationId = $input->getArgument('parentLocationId');
$parentLocation = $locationService->loadLocation($parentLocationId);
$query = new LocationQuery();
$query->query = new Criterion\LogicalAnd([
new Criterion\ContentTypeIdentifier('XXX'),
new Criterion\Subtree($parentLocation->pathString),
]);
$searchService = $this->getContainer()->get('ezpublish.api.service.search');
$searchHits = $searchService->findLocations($query)->searchHits;
foreach ($searchHits as $searchHit) {
try {
//print_r($searchHit->valueObject->id);
$location = $locationService->loadLocation($searchHit->valueObject->id);
$locationService->deleteLocation($location);
} catch (\eZ\Publish\API\Repository\Exceptions\NotFoundException $e) {
$output->writeln("Error when deleting location with id " . $searchHit->valueObject->id);
}
}
$output->writeln('Count deleted locations: '.count($searchHits));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment