ninegate/src/ninegate-1.0/src/Cadoles/CoreBundle/Command/InitDataCommand.php

142 lines
5.0 KiB
PHP

<?php
namespace Cadoles\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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 ContainerAwareCommand
{
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)
{
$output->writeln('');
$output->writeln('CORE = Default Data');
$em = $this->getContainer()->get('doctrine')->getEntityManager();
$masteridentity=$this->getContainer()->getParameter('masteridentity');
$finder = new Finder();
$finder->in('src/Cadoles/CoreBundle/Command/data');
// Init 01 = ce qui templetisé
$output->writeln(' > Parametres 01');
$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
$output->writeln(' > Parametres 02');
$finder->name('core-init-02.sql');
foreach( $finder as $file ){
$content = $file->getContents();
$stmt = $em->getConnection()->prepare($content);
$stmt->execute();
}
// Init country
$output->writeln(' > Pays');
$finder->name('country.sql');
foreach( $finder as $file ){
$content = $file->getContents();
$stmt = $em->getConnection()->prepare($content);
$stmt->execute();
}
// Init city
$output->writeln(' > Villes');
$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);
$group->setFgtemplate(false);
$group->setFgcanshare(false);
$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();
}
// On s'assure si masteridentity est à LDAP qu'au minimum un niveau01 possède un filtre LDAP
if($masteridentity=="LDAP") {
$niveau01=$em->createQueryBuilder()->select('n')->from('CadolesCoreBundle:Niveau01','n')->where('n.ldapfilter IS NOT NULL')->getQuery()->getResult();
if(!$niveau01) {
// Si ce n'est pas le cas on positionne un filtre ultra large sur le niveau01 de base
$niveau01=$group=$em->getRepository('CadolesCoreBundle:Niveau01')->find(-100);
$niveau01->setLdapfilter("(uid=*)");
$em->persist($niveau01);
$em->flush();
}
}
$output->writeln('');
}
protected static function determineKernelRootDir(Event $event) {
$extra = $event->getComposer()->getPackage()->getExtra();
$rootdir = rtrim(getcwd(), '/');
return $rootdir . '/' . trim($extra['symfony-app-dir'], '/');
}
}