From 4773e3366a46cb02d135570330febabf30499747 Mon Sep 17 00:00:00 2001 From: afornerot Date: Fri, 13 Nov 2020 11:20:47 +0100 Subject: [PATCH] purge auto --- .../src/Command/PurgeSurveyCommand.php | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/ninesurvey-1.0/src/Command/PurgeSurveyCommand.php diff --git a/src/ninesurvey-1.0/src/Command/PurgeSurveyCommand.php b/src/ninesurvey-1.0/src/Command/PurgeSurveyCommand.php new file mode 100644 index 0000000..0e5acd3 --- /dev/null +++ b/src/ninesurvey-1.0/src/Command/PurgeSurveyCommand.php @@ -0,0 +1,83 @@ +container = $container; + $this->em = $em; + $this->router=$router; + } + + protected function configure() + { + $this + ->setName('app:purgeSurvey') + ->setDescription("Suppression des sondages obsolètes") + ->setHelp("Suppression des sondages obsolètes") + ->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job') + ->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->output = $output; + $this->filesystem = new Filesystem(); + $this->rootlog = $this->container->get('kernel')->getLogDir()."/"; + + $this->writelnred(''); + $this->writelnred('== app:purgeSurvey'); + $this->writelnred('=========================================================================================================='); + + $now=new \DateTime(); + $surveys=$this->em->getRepository("App:Survey")->findAll(); + foreach($surveys as $survey) { + $datepurge=$survey->getDatepurge(); + if($datepurge<$now) { + $this->writeln($survey->getTitle()." ".$survey->getDatepurge()->format("d/m/Y H:i")); + $this->em->remove($survey); + $this->em->flush(); + } + } + + + $this->writeln(''); + return 1; + } + + private function writelnred($string) { + $this->output->writeln(''.$string.''); + $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); + } + private function writeln($string) { + $this->output->writeln($string); + $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); + } + + + +}