123 lines
4.0 KiB
PHP
123 lines
4.0 KiB
PHP
|
<?php
|
||
|
namespace Cadoles\CoreBundle\Command;
|
||
|
|
||
|
use Symfony\Component\Console\Command\Command;
|
||
|
use Symfony\Component\Console\Input\InputInterface;
|
||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||
|
use Symfony\Component\Finder\Finder;
|
||
|
use Symfony\Component\HttpKernel\KernelInterface;
|
||
|
use Doctrine\DBAL\Connection as DBALConnection;
|
||
|
use Doctrine\ORM\EntityManager;
|
||
|
|
||
|
use Cadoles\CoreBundle\Entity\Group;
|
||
|
use Cadoles\CoreBundle\Entity\UserGroup;
|
||
|
|
||
|
class InitDataCommand extends Command
|
||
|
{
|
||
|
protected function configure()
|
||
|
{
|
||
|
$this
|
||
|
// the name of the command (the part after "bin/console")
|
||
|
->setName('Core:InitData')
|
||
|
|
||
|
// the short description shown while running "php bin/console list"
|
||
|
->setDescription('Init Data for Core')
|
||
|
|
||
|
// the full command description shown when running the command with
|
||
|
// the "--help" option
|
||
|
->setHelp('This command Init Data for Core')
|
||
|
;
|
||
|
}
|
||
|
|
||
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||
|
{
|
||
|
$kernel = new \AppKernel('dev', true);
|
||
|
$kernel->boot();
|
||
|
$kernel->loadClassCache();
|
||
|
$em = $kernel->getContainer()->get('service_container')->get('doctrine.orm.default_entity_manager');
|
||
|
|
||
|
|
||
|
$finder = new Finder();
|
||
|
$finder->in('src/Cadoles/CoreBundle/Command');
|
||
|
$output->writeln('Init Data for Core !');
|
||
|
|
||
|
// Init 01 = ce qui templetisé
|
||
|
$finder->name('core-init-01.sql');
|
||
|
foreach( $finder as $file ){
|
||
|
$content = $file->getContents();
|
||
|
|
||
|
$stmt = $em->getConnection()->prepare($content);
|
||
|
$stmt->execute();
|
||
|
}
|
||
|
|
||
|
// Init 02 = ce qui est toujours le cas
|
||
|
$finder->name('core-init-02.sql');
|
||
|
foreach( $finder as $file ){
|
||
|
$content = $file->getContents();
|
||
|
|
||
|
$stmt = $em->getConnection()->prepare($content);
|
||
|
$stmt->execute();
|
||
|
}
|
||
|
|
||
|
// Init country
|
||
|
$finder->name('country.sql');
|
||
|
foreach( $finder as $file ){
|
||
|
$content = $file->getContents();
|
||
|
|
||
|
$stmt = $em->getConnection()->prepare($content);
|
||
|
$stmt->execute();
|
||
|
|
||
|
}
|
||
|
|
||
|
// Init city
|
||
|
$finder->name('city.sql');
|
||
|
foreach( $finder as $file ){
|
||
|
$content = $file->getContents();
|
||
|
|
||
|
$stmt = $em->getConnection()->prepare($content);
|
||
|
$stmt->execute();
|
||
|
|
||
|
}
|
||
|
$stmt->closeCursor();
|
||
|
|
||
|
// On s'assure que le groupe 'Tout le Monde' existe
|
||
|
$group=$em->getRepository('CadolesCoreBundle:Group')->findOneBy(array('fgall'=>true));
|
||
|
if (!$group) {
|
||
|
$output->writeln('Création du groupe Tout le Monde');
|
||
|
$group = new Group();
|
||
|
$group->setLabel("Tout le Monde");
|
||
|
$group->setFgopen(false);
|
||
|
$group->setFgall(true);
|
||
|
$em->persist($group);
|
||
|
$em->flush();
|
||
|
}
|
||
|
|
||
|
$sub = $em->createQueryBuilder();
|
||
|
$sub->select("usergroup");
|
||
|
$sub->from("CadolesCoreBundle:UserGroup","usergroup");
|
||
|
$sub->andWhere('usergroup.user = user.id');
|
||
|
$sub->andWhere('usergroup.group = :groupid');
|
||
|
|
||
|
$qb = $em->createQueryBuilder();
|
||
|
$qb->select('user')
|
||
|
->from('CadolesCoreBundle:User','user')
|
||
|
->where($qb->expr()->not($qb->expr()->exists($sub->getDQL())))
|
||
|
->setParameter("groupid",$group->getId());
|
||
|
$datas=$qb->getQuery()->getResult();
|
||
|
foreach($datas as $data) {
|
||
|
$output->writeln("Ratachement ".$data->getId()." ".$data->getUsername());
|
||
|
$usergroup=new UserGroup();
|
||
|
$usergroup->setUser($data);
|
||
|
$usergroup->setGroup($group);
|
||
|
$em->persist($usergroup);
|
||
|
$em->flush();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected static function determineKernelRootDir(Event $event) {
|
||
|
$extra = $event->getComposer()->getPackage()->getExtra();
|
||
|
$rootdir = rtrim(getcwd(), '/');
|
||
|
return $rootdir . '/' . trim($extra['symfony-app-dir'], '/');
|
||
|
}
|
||
|
}
|