Guest User

Untitled

a guest
Nov 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.18 KB | None | 0 0
  1. in srcBackendBundleFormEventListenerAddProvinceFieldSubscriber.php (line 56)
  2. 54) $accessor = PropertyAccess::createPropertyAccessor();
  3. 55) $municipio = $accessor->getValue($data, $this->propertyPathToMunicipio);
  4. 56) $provincia = ($municipio) ? $municipio->getIdMunicipio()->getProvincia() : null;
  5. 57) $this->addProvinceForm($form, $provincia); }
  6.  
  7. class InscritosType extends AbstractType {
  8.  
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function buildForm(FormBuilderInterface $builder, array $options) {
  13. $builder
  14. ->add('nombreUsuario', TextType::class, array("label" => "Nombre: ",
  15. "required" => "required",
  16. "attr" => array("class" => "form-name form-control col-sm-4 col-md-3"))) ;
  17. $builder->addEventSubscriber(new AddProvinceFieldSubscriber('idMunicipio'));
  18. $builder->addEventSubscriber(new AddMunicipioFieldSubscriber('idMunicipio'));
  19. }
  20.  
  21. //AddMunicipioFieldSubscriber
  22. namespace BackendBundleFormEventListener;
  23.  
  24. use BackendBundleEntityMunicipios;
  25. use SymfonyComponentEventDispatcherEventSubscriberInterface;
  26. use SymfonyComponentFormFormEvents;
  27. use SymfonyComponentFormFormEvent;
  28. use SymfonyComponentPropertyAccessPropertyAccess;
  29. use SymfonyBridgeDoctrineFormTypeEntityType;
  30. use DoctrineORMEntityRepository;
  31.  
  32. class AddMunicipioFieldSubscriber implements EventSubscriberInterface {
  33.  
  34. private $propertyPathToMunicipio;
  35.  
  36. public function __construct($propertyPathToMunicipio) {
  37. $this->propertyPathToMunicipio = $propertyPathToMunicipio;
  38. }
  39.  
  40. public static function getSubscribedEvents() {
  41. return array(
  42. FormEvents::PRE_SET_DATA => 'preSetData',
  43. FormEvents::PRE_SUBMIT => 'preSubmit'
  44. );
  45. }
  46.  
  47. private function addCityForm($form, $province_id) {
  48. $formOptions = array(
  49. 'class' => 'BackendBundle:Municipios',
  50. 'label' => 'Municipio',
  51. "required" => "required",
  52. 'attr' => array(
  53. 'class' => 'form-name form-control class_select_municipio',
  54. ),
  55. 'query_builder' => function (EntityRepository $repository) use ($province_id) {
  56. $qb = $repository->createQueryBuilder('municipios')
  57. ->innerJoin('municipios.idProvincia', 'provincia')
  58. ->where('provincia.id = :provincia')
  59. ->setParameter('provincia', $province_id)
  60. ;
  61.  
  62. return $qb;
  63. }
  64. );
  65.  
  66. $form->add($this->propertyPathToMunicipio, EntityType::class, $formOptions);
  67. }
  68.  
  69. public function preSetData(FormEvent $event) {
  70. $data = $event->getData();
  71. $form = $event->getForm();
  72.  
  73. if (null === $data) {
  74. return;
  75. }
  76.  
  77. $accessor = PropertyAccess::createPropertyAccessor();
  78.  
  79. $municipios = $accessor->getValue($data, $this->propertyPathToMunicipio);
  80. $province_id = ($municipios) ? $municipios->getIdMunicipio()->getProvincia()->getId() : null;
  81.  
  82. $this->addCityForm($form, $province_id);
  83. }
  84.  
  85. public function preSubmit(FormEvent $event) {
  86. $data = $event->getData();
  87. $form = $event->getForm();
  88.  
  89. $province_id = array_key_exists('provincia', $data) ? $data['provincia'] : null;
  90.  
  91. $this->addCityForm($form, $province_id);
  92. }
  93.  
  94. }
  95.  
  96. //AddProvinceFieldSubscriber
  97. namespace BackendBundleFormEventListener;
  98.  
  99. use SymfonyComponentEventDispatcherEventSubscriberInterface;
  100. use SymfonyComponentFormFormEvents;
  101. use SymfonyComponentFormFormEvent;
  102. use SymfonyComponentPropertyAccessPropertyAccess;
  103. use SymfonyBridgeDoctrineFormTypeEntityType;
  104. use BackendBundleEntityProvincia;
  105. use DoctrineORMEntityRepository;
  106.  
  107. class AddProvinceFieldSubscriber implements EventSubscriberInterface {
  108.  
  109. private $propertyPathToMunicipio;
  110.  
  111. public function __construct($propertyPathToMunicipio) {
  112. $this->propertyPathToMunicipio = $propertyPathToMunicipio;
  113. }
  114.  
  115. public static function getSubscribedEvents() {
  116. return array(
  117. FormEvents::PRE_SET_DATA => 'preSetData',
  118. FormEvents::PRE_SUBMIT => 'preSubmit'
  119. );
  120. }
  121.  
  122. private function addProvinceForm($form, $Province = null) {
  123. $formOptions = array(
  124. 'class' => 'BackendBundle:Provincia',
  125. 'mapped' => false,
  126. "required" => "required",
  127. 'label' => 'Provincia',
  128. 'attr' => array(
  129. 'class' => 'form-name form-control class_select_provincia',
  130. ),
  131. );
  132.  
  133. if ($Province) {
  134. $formOptions['data'] = $Province;
  135. }
  136.  
  137. $form->add('provincia', EntityType::class, $formOptions);
  138. }
  139.  
  140. public function preSetData(FormEvent $event) {
  141. $data = $event->getData();
  142. $form = $event->getForm();
  143.  
  144. if (null === $data) {
  145. return;
  146. }
  147.  
  148. $accessor = PropertyAccess::createPropertyAccessor();
  149.  
  150. $municipio = $accessor->getValue($data, $this->propertyPathToMunicipio);
  151. $provincia = ($municipio) ? $municipio->getIdMunicipio()->getProvincia() : null;
  152.  
  153. $this->addProvinceForm($form, $provincia);
  154. }
  155.  
  156. public function preSubmit(FormEvent $event) {
  157. $form = $event->getForm();
  158.  
  159. $this->addProvinceForm($form);
  160. }
  161.  
  162. }
  163.  
  164. namespace BackendBundleEntity;
  165.  
  166. /**
  167. * Municipios
  168. */
  169. class Municipios
  170. {
  171. /**
  172. * @var integer
  173. */
  174. private $idMunicipio;
  175.  
  176. /**
  177. * @var integer
  178. */
  179. private $codMunicipio;
  180.  
  181. /**
  182. * @var integer
  183. */
  184. private $dc;
  185.  
  186. /**
  187. * @var string
  188. */
  189. private $nombre = '';
  190.  
  191. /**
  192. * @var DoctrineCommonCollectionsCollection
  193. */
  194. private $userMunicipio;
  195.  
  196. /**
  197. * @var DoctrineCommonCollectionsCollection
  198. */
  199. private $inscritosMunicipio;
  200.  
  201. /**
  202. * @var BackendBundleEntityProvincia
  203. */
  204. /**
  205. * @ORMManyToOne(targetEntity="BackendBundleEntityProvincia", inversedBy="municipio")
  206. * @ORMJoinColumn(name="municipio_id", referencedColumnName="id")
  207. * @AssertNotBlank
  208. */
  209. private $idProvincia;
  210.  
  211. /**
  212. * Constructor
  213. */
  214. public function __construct()
  215. {
  216. $this->userMunicipio = new DoctrineCommonCollectionsArrayCollection();
  217. $this->inscritosMunicipio = new DoctrineCommonCollectionsArrayCollection();
  218. $this->idMunicipio= new DoctrineCommonCollectionsArrayCollection;
  219. $this->idProvincia= new DoctrineCommonCollectionsArrayCollection;
  220. }
  221.  
  222. public function __toString() {
  223. // return strval($this->idCcaa);
  224. return $this->nombre;
  225. }
  226.  
  227. /**
  228. * Set idMunicipio
  229. *
  230. * @param integer $idMunicipio
  231. *
  232. * @return Municipios
  233. */
  234. public function setIdMunicipio($idMunicipio)
  235. {
  236. $this->idMunicipio = $idMunicipio;
  237.  
  238. return $this;
  239. }
  240.  
  241. /**
  242. * Get idMunicipio
  243. *
  244. * @return integer
  245. */
  246. public function getIdMunicipio()
  247. {
  248. return $this->idMunicipio;
  249. }
  250.  
  251. /**
  252. * Set codMunicipio
  253. *
  254. * @param integer $codMunicipio
  255. *
  256. * @return Municipios
  257. */
  258. public function setCodMunicipio($codMunicipio)
  259. {
  260. $this->codMunicipio = $codMunicipio;
  261.  
  262. return $this;
  263. }
  264.  
  265. /**
  266. * Get codMunicipio
  267. *
  268. * @return integer
  269. */
  270. public function getCodMunicipio()
  271. {
  272. return $this->codMunicipio;
  273. }
  274.  
  275. /**
  276. * Set dc
  277. *
  278. * @param integer $dc
  279. *
  280. * @return Municipios
  281. */
  282. public function setDc($dc)
  283. {
  284. $this->dc = $dc;
  285.  
  286. return $this;
  287. }
  288.  
  289. /**
  290. * Get dc
  291. *
  292. * @return integer
  293. */
  294. public function getDc()
  295. {
  296. return $this->dc;
  297. }
  298.  
  299. /**
  300. * Set nombre
  301. *
  302. * @param string $nombre
  303. *
  304. * @return Municipios
  305. */
  306. public function setNombre($nombre)
  307. {
  308. $this->nombre = $nombre;
  309.  
  310. return $this;
  311. }
  312.  
  313. /**
  314. * Get nombre
  315. *
  316. * @return string
  317. */
  318. public function getNombre()
  319. {
  320. return $this->nombre;
  321. }
  322.  
  323. /**
  324. * Add userMunicipio
  325. *
  326. * @param BackendBundleEntityUsers $userMunicipio
  327. *
  328. * @return Municipios
  329. */
  330. public function addUserMunicipio(BackendBundleEntityUsers $userMunicipio)
  331. {
  332. $this->userMunicipio[] = $userMunicipio;
  333.  
  334. return $this;
  335. }
  336.  
  337. /**
  338. * Remove userMunicipio
  339. *
  340. * @param BackendBundleEntityUsers $userMunicipio
  341. */
  342. public function removeUserMunicipio(BackendBundleEntityUsers $userMunicipio)
  343. {
  344. $this->userMunicipio->removeElement($userMunicipio);
  345. }
  346.  
  347. /**
  348. * Get userMunicipio
  349. *
  350. * @return DoctrineCommonCollectionsCollection
  351. */
  352. public function getUserMunicipio()
  353. {
  354. return $this->userMunicipio;
  355. }
  356.  
  357. /**
  358. * Add inscritosMunicipio
  359. *
  360. * @param BackendBundleEntityInscritos $inscritosMunicipio
  361. *
  362. * @return Municipios
  363. */
  364. public function addInscritosMunicipio(BackendBundleEntityInscritos $inscritosMunicipio)
  365. {
  366. $this->inscritosMunicipio[] = $inscritosMunicipio;
  367.  
  368. return $this;
  369. }
  370.  
  371. /**
  372. * Remove inscritosMunicipio
  373. *
  374. * @param BackendBundleEntityInscritos $inscritosMunicipio
  375. */
  376. public function removeInscritosMunicipio(BackendBundleEntityInscritos $inscritosMunicipio)
  377. {
  378. $this->inscritosMunicipio->removeElement($inscritosMunicipio);
  379. }
  380.  
  381. /**
  382. * Get inscritosMunicipio
  383. *
  384. * @return DoctrineCommonCollectionsCollection
  385. */
  386. public function getInscritosMunicipio()
  387. {
  388. return $this->inscritosMunicipio;
  389. }
  390.  
  391. /**
  392. * Set idProvincia
  393. *
  394. * @param BackendBundleEntityProvincia $idProvincia
  395. *
  396. * @return Municipios
  397. */
  398. public function setIdProvincia(BackendBundleEntityProvincia $idProvincia = null)
  399. {
  400. $this->idProvincia = $idProvincia;
  401.  
  402. return $this;
  403. }
  404.  
  405. /**
  406. * Get idProvincia
  407. *
  408. * @return BackendBundleEntityProvincia
  409. */
  410. public function getIdProvincia()
  411. {
  412. return $this->idProvincia;
  413. }
  414. }
  415.  
  416. namespace BackendBundleEntity;
  417.  
  418. /**
  419. * Provincia
  420. */
  421. class Provincia
  422. {
  423. /**
  424. * @var integer
  425. */
  426. private $id;
  427.  
  428. /**
  429. * @var string
  430. */
  431. private $provincia;
  432.  
  433. /**
  434. * @var integer
  435. */
  436. private $especial;
  437.  
  438. /**
  439. * @var string
  440. */
  441. private $correoNotificaciones;
  442.  
  443. /**
  444. * @var DoctrineCommonCollectionsCollection
  445. */
  446. private $userProvincia;
  447.  
  448. /**
  449. * @var DoctrineCommonCollectionsCollection
  450. */
  451. private $municipio;
  452.  
  453. /**
  454. * @var DoctrineCommonCollectionsCollection
  455. */
  456. private $inscritosProvincia;
  457.  
  458. /**
  459. * @var BackendBundleEntityCcaa
  460. */
  461. private $idCcaa;
  462.  
  463. /**
  464. * Constructor
  465. */
  466. public function __construct()
  467. {
  468. $this->userProvincia = new DoctrineCommonCollectionsArrayCollection();
  469. $this->municipio = new DoctrineCommonCollectionsArrayCollection();
  470. $this->inscritosProvincia = new DoctrineCommonCollectionsArrayCollection();
  471. $this->idMunicipio= new DoctrineCommonCollectionsArrayCollection;
  472. $this->idProvincia= new DoctrineCommonCollectionsArrayCollection;
  473. }
  474.  
  475. public function __toString() {
  476. // return strval($this->idCcaa);
  477. return $this->provincia;
  478. }
  479.  
  480. /**
  481. * Set id
  482. *
  483. * @param integer $id
  484. *
  485. * @return Provincia
  486. */
  487. public function setId($id)
  488. {
  489. $this->id = $id;
  490.  
  491. return $this;
  492. }
  493.  
  494. /**
  495. * Get id
  496. *
  497. * @return integer
  498. */
  499. public function getId()
  500. {
  501. return $this->id;
  502. }
  503.  
  504. /**
  505. * Set provincia
  506. *
  507. * @param string $provincia
  508. *
  509. * @return Provincia
  510. */
  511. public function setProvincia($provincia)
  512. {
  513. $this->provincia = $provincia;
  514.  
  515. return $this;
  516. }
  517.  
  518. /**
  519. * Get provincia
  520. *
  521. * @return string
  522. */
  523. public function getProvincia()
  524. {
  525. return $this->provincia;
  526. }
  527.  
  528. /**
  529. * Set especial
  530. *
  531. * @param integer $especial
  532. *
  533. * @return Provincia
  534. */
  535. public function setEspecial($especial)
  536. {
  537. $this->especial = $especial;
  538.  
  539. return $this;
  540. }
  541.  
  542. /**
  543. * Get especial
  544. *
  545. * @return integer
  546. */
  547. public function getEspecial()
  548. {
  549. return $this->especial;
  550. }
  551.  
  552. /**
  553. * Set correoNotificaciones
  554. *
  555. * @param string $correoNotificaciones
  556. *
  557. * @return Provincia
  558. */
  559. public function setCorreoNotificaciones($correoNotificaciones)
  560. {
  561. $this->correoNotificaciones = $correoNotificaciones;
  562.  
  563. return $this;
  564. }
  565.  
  566. /**
  567. * Get correoNotificaciones
  568. *
  569. * @return string
  570. */
  571. public function getCorreoNotificaciones()
  572. {
  573. return $this->correoNotificaciones;
  574. }
  575.  
  576. /**
  577. * Add userProvincium
  578. *
  579. * @param BackendBundleEntityUsers $userProvincium
  580. *
  581. * @return Provincia
  582. */
  583. public function addUserProvincium(BackendBundleEntityUsers $userProvincium)
  584. {
  585. $this->userProvincia[] = $userProvincium;
  586.  
  587. return $this;
  588. }
  589.  
  590. /**
  591. * Remove userProvincium
  592. *
  593. * @param BackendBundleEntityUsers $userProvincium
  594. */
  595. public function removeUserProvincium(BackendBundleEntityUsers $userProvincium)
  596. {
  597. $this->userProvincia->removeElement($userProvincium);
  598. }
  599.  
  600. /**
  601. * Get userProvincia
  602. *
  603. * @return DoctrineCommonCollectionsCollection
  604. */
  605. public function getUserProvincia()
  606. {
  607. return $this->userProvincia;
  608. }
  609.  
  610. /**
  611. * Add municipio
  612. *
  613. * @param BackendBundleEntityMunicipios $municipio
  614. *
  615. * @return Provincia
  616. */
  617. public function addMunicipio(BackendBundleEntityMunicipios $municipio)
  618. {
  619. $this->municipio[] = $municipio;
  620.  
  621. return $this;
  622. }
  623.  
  624. /**
  625. * Remove municipio
  626. *
  627. * @param BackendBundleEntityMunicipios $municipio
  628. */
  629. public function removeMunicipio(BackendBundleEntityMunicipios $municipio)
  630. {
  631. $this->municipio->removeElement($municipio);
  632. }
  633.  
  634. /**
  635. * Get municipio
  636. *
  637. * @return DoctrineCommonCollectionsCollection
  638. */
  639. public function getMunicipio()
  640. {
  641. return $this->municipio;
  642. }
  643.  
  644. /**
  645. * Add inscritosProvincium
  646. *
  647. * @param BackendBundleEntityInscritos $inscritosProvincium
  648. *
  649. * @return Provincia
  650. */
  651. public function addInscritosProvincium(BackendBundleEntityInscritos $inscritosProvincium)
  652. {
  653. $this->inscritosProvincia[] = $inscritosProvincium;
  654.  
  655. return $this;
  656. }
  657.  
  658. /**
  659. * Remove inscritosProvincium
  660. *
  661. * @param BackendBundleEntityInscritos $inscritosProvincium
  662. */
  663. public function removeInscritosProvincium(BackendBundleEntityInscritos $inscritosProvincium)
  664. {
  665. $this->inscritosProvincia->removeElement($inscritosProvincium);
  666. }
  667.  
  668. /**
  669. * Get inscritosProvincia
  670. *
  671. * @return DoctrineCommonCollectionsCollection
  672. */
  673. public function getInscritosProvincia()
  674. {
  675. return $this->inscritosProvincia;
  676. }
  677.  
  678. /**
  679. * Set idCcaa
  680. *
  681. * @param BackendBundleEntityCcaa $idCcaa
  682. *
  683. * @return Provincia
  684. */
  685. public function setIdCcaa(BackendBundleEntityCcaa $idCcaa = null)
  686. {
  687. $this->idCcaa = $idCcaa;
  688.  
  689. return $this;
  690. }
  691.  
  692. /**
  693. * Get idCcaa
  694. *
  695. * @return BackendBundleEntityCcaa
  696. */
  697. public function getIdCcaa()
  698. {
  699. return $this->idCcaa;
  700. }
  701. }
Add Comment
Please, Sign In to add comment