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

5 years ago
5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\Command;
  3. use Symfony\Component\Console\Command\Command as ContainerAwareCommand;;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use App\Entity\Licitador;
  9. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  10. use PhpOffice\PhpSpreadsheet\IOFactory;
  11. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  12. use DateTime;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use FOS\UserBundle\Model\UserInterface;
  15. use FOS\UserBundle\Model\UserManagerInterface;
  16. use App\Kernel;
  17. use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
  18. use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
  19. use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
  20. use Doctrine\Common\Annotations\AnnotationReader;
  21. use Symfony\Component\PropertyAccess\PropertyAccess;
  22. use Gedmo\Mapping\Annotation as Gedmo;
  23. class NormalizeTranslationsCommand extends ContainerAwareCommand
  24. {
  25. private $em;
  26. private $output;
  27. private $sectores=[];
  28. private $userManager;
  29. private $tokenStorage;
  30. protected $projectDir;
  31. public function __construct(EntityManagerInterface $em,
  32. UserManagerInterface $userManager,
  33. TokenStorageInterface $tokenStorage,
  34. Kernel $kernel)
  35. {
  36. parent::__construct();
  37. $this->em = $em;
  38. $this->userManager = $userManager;
  39. $this->tokenStorage = $tokenStorage;
  40. $this->projectDir = $kernel->getProjectDir();
  41. }
  42. protected function configure()
  43. {
  44. $this
  45. // the name of the command (the part after "app/console")
  46. ->setName('zitec:normalize-translations')
  47. // the short description shown while running "php app/console list"
  48. ->setDescription('Normalizes the translations.')
  49. // the full command description shown when running the command with
  50. // the "--help" option
  51. ->setHelp('This command allows you to normalize the translations...')
  52. ;
  53. }
  54. /**
  55. * @param InputInterface $input
  56. * @param OutputInterface $output
  57. */
  58. protected function execute(InputInterface $input, OutputInterface $output)
  59. {
  60. // all the translatable classes
  61. $classes = [
  62. \App\Entity\BussinessStage::class,
  63. \App\Entity\Indicador::class,
  64. \App\Entity\Contador::class,
  65. \App\Entity\Formulario::class,
  66. \App\Entity\Preguntas::class,
  67. \App\Entity\PreguntasFormulario::class,
  68. \App\Entity\Recursos::class,
  69. \App\Entity\Sector::class,
  70. \App\Entity\Tarea::class,
  71. \App\Entity\TiposRecursos::class,
  72. ];
  73. $defaultLanguages = ['fr','ar']; // fake language
  74. foreach ($classes as $class) {
  75. foreach($defaultLanguages as $defaultLanguage)
  76. $this->processClass($class, $output,$defaultLanguage);
  77. }
  78. }
  79. /**
  80. * @param $class
  81. * @param $output
  82. * @param $defaultLanguage
  83. * @throws \ReflectionException
  84. */
  85. private function processClass($class, $output,$defaultLanguage)
  86. {
  87. $output->writeln(sprintf('Processing class <info>%s</info>', $class));
  88. // gets all the properties
  89. $properties = $this->getProperties($class);
  90. // gets the translatable properties
  91. $translatableProperties = $this->getTranslatableProperties($properties, $class);
  92. $output->writeln(sprintf('Found %d translatable properties: %s', count($translatableProperties), implode(', ', $translatableProperties)));
  93. $em = $this->em;
  94. //$repository = $em->getRepository('Gedmo\\Translatable\\Entity\\Translation');
  95. $items = $em->getRepository($class)->findAll();
  96. $propertyAccessor = PropertyAccess::createPropertyAccessor();
  97. $annotationReader = new AnnotationReader();
  98. //Get class annotation
  99. $reflectionClass = new \ReflectionClass($class);
  100. $classAnnotations = $annotationReader->getClassAnnotations($reflectionClass);
  101. $translationclass=null;
  102. foreach($classAnnotations as $entity){
  103. $entity=(array)$entity;
  104. if(!empty($entity['class']) && strpos($entity['class'], 'Translation')!==false){
  105. $translationclass=$entity['class'];
  106. }
  107. }
  108. $output->writeln(sprintf('Translation class: <info>%s</info>', $translationclass));
  109. foreach ($items as $item) {
  110. foreach ($translatableProperties as $translatableProperty) {
  111. $value = $propertyAccessor->getValue($item, $translatableProperty);
  112. $translations = $item->getTranslations();
  113. $exists=false;
  114. foreach($translations as $translation) {
  115. if($translation->getLocale()==$defaultLanguage && !empty($translation->getContent()))
  116. $exists=true;
  117. }
  118. if (!$exists) {
  119. $item->addTranslation(new $translationclass($defaultLanguage,$translatableProperty, $value));
  120. }
  121. }
  122. }
  123. $em->flush();
  124. }
  125. private function getProperties($class)
  126. {
  127. $phpDocExtractor = new PhpDocExtractor();
  128. $reflectionExtractor = new ReflectionExtractor();
  129. // array of PropertyListExtractorInterface
  130. $listExtractors = array($reflectionExtractor);
  131. // array of PropertyTypeExtractorInterface
  132. $typeExtractors = array($phpDocExtractor, $reflectionExtractor);
  133. // array of PropertyDescriptionExtractorInterface
  134. $descriptionExtractors = array($phpDocExtractor);
  135. // array of PropertyAccessExtractorInterface
  136. $accessExtractors = array($reflectionExtractor);
  137. $propertyInfo = new PropertyInfoExtractor(
  138. $listExtractors,
  139. $typeExtractors,
  140. $descriptionExtractors,
  141. $accessExtractors
  142. );
  143. return $propertyInfo->getProperties($class);
  144. }
  145. private function getTranslatableProperties($properties, $class)
  146. {
  147. $translatableProperties = [];
  148. // https://gist.github.com/Swop/5990316
  149. $annotationReader = new AnnotationReader();
  150. foreach ($properties as $property) {
  151. try {
  152. $reflectionProperty = new \ReflectionProperty($class, $property);
  153. $propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
  154. foreach ($propertyAnnotations as $propertyAnnotation) {
  155. if ($propertyAnnotation instanceof Gedmo\Translatable) {
  156. // this property is translatable
  157. $translatableProperties[] = $property;
  158. }
  159. }
  160. } catch (\ReflectionException $e) {
  161. // missing property
  162. continue;
  163. }
  164. }
  165. return $translatableProperties;
  166. }
  167. }