Created
June 18, 2022 13:37
-
-
Save hissy/3c5d1679f6a0314f7bf95a87da0be86b to your computer and use it in GitHub Desktop.
#ConcreteCMS A Command to delete entries of the specific express entity
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
<?php | |
// application/bootstrap/app.php | |
/* @var Concrete\Core\Application\Application $app */ | |
/* @var Concrete\Core\Console\Application $console only set in CLI environment */ | |
if ($app->isInstalled()) { | |
if (\Concrete\Core\Application\Application::isRunThroughCommandLineInterface()) { | |
$console->add($app->make(\Application\Express\Command\ClearExpressEntries::class)); | |
} | |
} |
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
<?php | |
// Add lines below in the bottom of application/bootstrap/autoload.php | |
$classLoader = new \Symfony\Component\ClassLoader\Psr4ClassLoader(); | |
$classLoader->addPrefix('Application\\Express', DIR_APPLICATION . '/' . DIRNAME_CLASSES . '/Express'); | |
$classLoader->register(); |
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
<?php | |
// application/src/Express/Command/ClearExpressEntries.php | |
namespace Application\Express\Command; | |
use Concrete\Core\Console\Command; | |
use Concrete\Core\Entity\Express\Entity; | |
use Concrete\Core\Entity\Express\Entry; | |
use Concrete\Core\Support\Facade\Application; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Question\ConfirmationQuestion; | |
class ClearExpressEntries extends Command | |
{ | |
/** | |
* @inheritDoc | |
*/ | |
protected function configure() | |
{ | |
$this | |
->setName('app:express:clear-entries') | |
->addArgument('entity', InputArgument::REQUIRED, t('Handle of an Express Entity')) | |
->addOption('force', 'f', InputOption::VALUE_NONE, t('Force to delete entries without confirmation')) | |
->addEnvOption() | |
; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
if (!$input->getOption('force')) { | |
if (!$input->isInteractive()) { | |
throw new \Exception("You have to specify the --force option in order to run this command"); | |
} | |
$confirmQuestion = new ConfirmationQuestion( | |
'Are you sure you want to delete express entries? (y/n)', | |
false | |
); | |
if (!$this->getHelper('question')->ask($input, $output, $confirmQuestion)) { | |
throw new \Exception("Operation aborted."); | |
} | |
} | |
$count = 0; | |
$app = Application::getFacadeApplication(); | |
/** @var EntityManagerInterface $entityManager */ | |
$entityManager = $app->make(EntityManagerInterface::class); | |
$entityHandle = $input->getArgument('entity'); | |
if ($entityHandle) { | |
/** @var Entity $entity */ | |
$entity = $entityManager->getRepository(Entity::class)->findOneBy(['handle' => $entityHandle]); | |
if ($entity) { | |
/** @var Entry $entry */ | |
foreach ($entity->getEntries() as $entry) { | |
if ($output->isVerbose()) { | |
$output->writeln(sprintf('Deleting %s...', $entry->getLabel())); | |
} | |
$entityManager->remove($entry); | |
$count++; | |
} | |
$entityManager->flush(); | |
} | |
} | |
$output->writeln(sprintf('%s entries deleted.', $count)); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment