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.

113 lines
4.1 KiB

5 years ago
5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\Command;
  3. use Doctrine\Common\Annotations\AnnotationReader as Reader;
  4. use Symfony\Component\Console\Command\Command as ContainerAwareCommand;;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\PropertyAccess\PropertyAccess;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. // use Doctrine\Common\Annotations\Reader;
  11. use App\Entity\Licitador;
  12. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  13. use PhpOffice\PhpSpreadsheet\IOFactory;
  14. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  15. use DateTime;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. use FOS\UserBundle\Model\UserInterface;
  18. use FOS\UserBundle\Model\UserManagerInterface;
  19. use App\Kernel;
  20. class FillTranslationCommand extends ContainerAwareCommand
  21. {
  22. private $em;
  23. private $output;
  24. private $sectores=[];
  25. private $userManager;
  26. private $tokenStorage;
  27. protected $projectDir;
  28. protected $propertyAccessor;
  29. public function __construct(EntityManagerInterface $em,
  30. UserManagerInterface $userManager,
  31. TokenStorageInterface $tokenStorage,
  32. Kernel $kernel)
  33. {
  34. parent::__construct();
  35. $this->em = $em;
  36. $this->userManager = $userManager;
  37. $this->tokenStorage = $tokenStorage;
  38. $this->projectDir = $kernel->getProjectDir();
  39. $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function configure(): void
  45. {
  46. $this->setName('zitec:fill:translations');
  47. $this->addArgument(
  48. 'language',
  49. InputArgument::REQUIRED,
  50. 'language to translate'
  51. );
  52. $this->setDescription(
  53. 'Translation tables filler'
  54. );
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function execute(InputInterface $input, OutputInterface $output): int
  60. {
  61. $language=$input->getArgument('language');
  62. $folderentity='src/Entity';
  63. $translationClass=null;
  64. foreach (glob($folderentity."/*.php") as $index=>$nombre_fichero) {
  65. $tokenize=explode('/',$nombre_fichero);
  66. $clase= preg_replace('/\\.[^.\\s]{3,4}$/', '', end($tokenize));
  67. $clase='App\Entity\\'.$clase;
  68. $reader = new Reader();
  69. $ref = new \ReflectionClass($clase);
  70. //Check if class is translatable
  71. if($reader->getClassAnnotation($ref, 'Gedmo\TranslationEntity')){
  72. $translationClass=$reader->getClassAnnotation($ref, 'Gedmo\TranslationEntity')->getClass();
  73. echo "Identificada Clase ".$translationClass;
  74. }else{
  75. continue;
  76. }
  77. //Get properties
  78. $properties= array_map(function (\ReflectionProperty $prop) {
  79. return $prop->getName();
  80. }, $ref->getProperties());
  81. $translatable=[];
  82. foreach($properties as $property) {
  83. if($type=$reader->getPropertyAnnotation(
  84. $ref->getProperty($property),
  85. 'Gedmo\Mapping\Annotation\Translatable'
  86. )) {
  87. $translatable[] = $property;
  88. }
  89. }
  90. echo "Propiedades Traducibles de Clase ".$translationClass."\n".print_r($translatable,true);
  91. foreach($translatable as $prop){
  92. $this->insertTranslation($prop, $language,$clase,$translationClass);
  93. }
  94. }
  95. exit(0);
  96. }
  97. private function insertTranslation($prop, $language,$class, $translationClass){
  98. $records = $this->em->getRepository($class)->findAll();
  99. echo "Translation class $class with class Translatos $translationClass to language $language";
  100. foreach($records as $record){
  101. //$record->addTranslation(new Entity\CategoryTranslation($language, $prop, $propertyAccessor->getValue($record,$prop)));
  102. echo "'$language', '$prop', '".$propertyAccessor->getValue($record,$prop)."'\n";
  103. //$this->em->persist($record);
  104. }
  105. //$this->em->flush();
  106. }
  107. }