Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

195 lines
7.0 KiB

<?php
namespace Zitec\RuleEngineBundle\Command;
use Symfony\Component\Console\Command\Command as ContainerAwareCommand;;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Licitador;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use DateTime;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use App\Kernel;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Gedmo\Mapping\Annotation as Gedmo;
class NormalizeTranslationsCommand extends ContainerAwareCommand
{
private $em;
private $output;
private $sectores=[];
private $userManager;
private $tokenStorage;
protected $projectDir;
public function __construct(EntityManagerInterface $em,
UserManagerInterface $userManager,
TokenStorageInterface $tokenStorage,
Kernel $kernel)
{
parent::__construct();
$this->em = $em;
$this->userManager = $userManager;
$this->tokenStorage = $tokenStorage;
$this->projectDir = $kernel->getProjectDir();
}
protected function configure()
{
$this
// the name of the command (the part after "app/console")
->setName('zitec:normalize-translations')
// the short description shown while running "php app/console list"
->setDescription('Normalizes the translations.')
// the full command description shown when running the command with
// the "--help" option
->setHelp('This command allows you to normalize the translations...')
;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// all the translatable classes
$classes = [
\App\Entity\BussinessStage::class,
\App\Entity\Indicador::class,
\App\Entity\Contador::class,
\App\Entity\Formulario::class,
\App\Entity\Preguntas::class,
\App\Entity\PreguntasFormulario::class,
\App\Entity\Recursos::class,
\App\Entity\Sector::class,
\App\Entity\Tarea::class,
\App\Entity\TiposRecursos::class,
];
$defaultLanguages = ['fr','ar']; // fake language
foreach ($classes as $class) {
foreach($defaultLanguages as $defaultLanguage)
$this->processClass($class, $output,$defaultLanguage);
}
}
/**
* @param $class
* @param $output
* @param $defaultLanguage
* @throws \ReflectionException
*/
private function processClass($class, $output,$defaultLanguage)
{
$output->writeln(sprintf('Processing class <info>%s</info>', $class));
// gets all the properties
$properties = $this->getProperties($class);
// gets the translatable properties
$translatableProperties = $this->getTranslatableProperties($properties, $class);
$output->writeln(sprintf('Found %d translatable properties: %s', count($translatableProperties), implode(', ', $translatableProperties)));
$em = $this->em;
//$repository = $em->getRepository('Gedmo\\Translatable\\Entity\\Translation');
$items = $em->getRepository($class)->findAll();
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$annotationReader = new AnnotationReader();
//Get class annotation
$reflectionClass = new \ReflectionClass($class);
$classAnnotations = $annotationReader->getClassAnnotations($reflectionClass);
$translationclass=null;
foreach($classAnnotations as $entity){
$entity=(array)$entity;
if(!empty($entity['class']) && strpos($entity['class'], 'Translation')!==false){
$translationclass=$entity['class'];
}
}
$output->writeln(sprintf('Translation class: <info>%s</info>', $translationclass));
foreach ($items as $item) {
foreach ($translatableProperties as $translatableProperty) {
$value = $propertyAccessor->getValue($item, $translatableProperty);
$translations = $item->getTranslations();
$exists=false;
foreach($translations as $translation) {
if($translation->getLocale()==$defaultLanguage && !empty($translation->getContent()))
$exists=true;
}
if (!$exists) {
$item->addTranslation(new $translationclass($defaultLanguage,$translatableProperty, $value));
}
}
}
$em->flush();
}
private function getProperties($class)
{
$phpDocExtractor = new PhpDocExtractor();
$reflectionExtractor = new ReflectionExtractor();
// array of PropertyListExtractorInterface
$listExtractors = array($reflectionExtractor);
// array of PropertyTypeExtractorInterface
$typeExtractors = array($phpDocExtractor, $reflectionExtractor);
// array of PropertyDescriptionExtractorInterface
$descriptionExtractors = array($phpDocExtractor);
// array of PropertyAccessExtractorInterface
$accessExtractors = array($reflectionExtractor);
$propertyInfo = new PropertyInfoExtractor(
$listExtractors,
$typeExtractors,
$descriptionExtractors,
$accessExtractors
);
return $propertyInfo->getProperties($class);
}
private function getTranslatableProperties($properties, $class)
{
$translatableProperties = [];
// https://gist.github.com/Swop/5990316
$annotationReader = new AnnotationReader();
foreach ($properties as $property) {
try {
$reflectionProperty = new \ReflectionProperty($class, $property);
$propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
foreach ($propertyAnnotations as $propertyAnnotation) {
if ($propertyAnnotation instanceof Gedmo\Translatable) {
// this property is translatable
$translatableProperties[] = $property;
}
}
} catch (\ReflectionException $e) {
// missing property
continue;
}
}
return $translatableProperties;
}
}