|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KI\UserBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use KI\UserBundle\Entity\Club; |
|
6
|
|
|
use KI\UserBundle\Entity\ClubUser; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
class ClubUpdateCommand extends ContainerAwareCommand |
|
14
|
|
|
{ |
|
15
|
|
|
protected function configure() |
|
16
|
|
|
{ |
|
17
|
|
|
$this |
|
18
|
|
|
->setName('upont:update:clubs') |
|
19
|
|
|
->setDescription('Enable or Disable listed clubs according to the existence of members in current associative promo') |
|
20
|
|
|
->addArgument('clubs', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Clubs to update') |
|
21
|
|
|
->addOption('all', 'a', InputOption::VALUE_NONE, 'Update all clubs') |
|
22
|
|
|
->addOption('preview', 'p', InputOption::VALUE_NONE, 'Show clubs to be updated without modification') |
|
23
|
|
|
; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
27
|
|
|
{ |
|
28
|
|
|
$em = $this->getContainer()->get('doctrine')->getManager(); |
|
29
|
|
|
$userRepo = $this->getContainer()->get('doctrine')->getRepository(Club::class); |
|
30
|
|
|
$clubUserRepo = $this->getContainer()->get('doctrine')->getRepository(ClubUser::class); |
|
31
|
|
|
$assoPromo = $this->getContainer()->getParameter('upont')['promos']['assos']; |
|
32
|
|
|
$clubSlugs = $input->getArgument('clubs'); |
|
33
|
|
|
|
|
34
|
|
|
if ($input->getOption('all')) { |
|
35
|
|
|
$clubsToUpdate = $userRepo->findAll(); |
|
36
|
|
|
} |
|
37
|
|
|
else { |
|
38
|
|
|
$clubsToUpdate = array_map([$userRepo, 'findOneBySlug'], $clubSlugs); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$clubNumber = -1; |
|
42
|
|
|
foreach ($clubsToUpdate as $clubToUpdate) { |
|
43
|
|
|
$clubNumber++; |
|
44
|
|
|
if (count($clubToUpdate) == 0) { |
|
45
|
|
|
$output->writeln('<error>The slug "'.$clubSlugs[$clubNumber].'" doesn\'t match with any club</error>'); |
|
46
|
|
|
continue; |
|
47
|
|
|
} |
|
48
|
|
|
$countUsers = $clubUserRepo->getCountUsersInClubWithPromo($clubToUpdate, $assoPromo); |
|
49
|
|
|
|
|
50
|
|
|
if ($countUsers == 0 && $clubToUpdate->getActive()) { |
|
51
|
|
View Code Duplication |
if ($input->getOption('preview')) { |
|
52
|
|
|
$output->writeln('<comment>'.$clubToUpdate->getFullName().' to be disabled'.'</comment>'); |
|
53
|
|
|
} |
|
54
|
|
|
else { |
|
55
|
|
|
$clubToUpdate->setActive(false); |
|
56
|
|
|
$output->writeln('<comment>'.$clubToUpdate->getFullName().' disabled'.'</comment>'); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
else if ($countUsers > 0 && !$clubToUpdate->getActive()) { |
|
60
|
|
View Code Duplication |
if ($input->getOption('preview')) { |
|
61
|
|
|
$output->writeln('<info>'.$clubToUpdate->getFullName().' to be enabled'.'</info>'); |
|
62
|
|
|
} |
|
63
|
|
|
else { |
|
64
|
|
|
$clubToUpdate->setActive(true); |
|
65
|
|
|
$output->writeln('<info>'.$clubToUpdate->getFullName().' enabled'.'</info>'); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$em->flush(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|