/** * @param array $content * @param array $fieldnames * @param boolean $andFlush */ private function gabungSiswa($content, $fieldnames, $andFlush = false) { /* @var $em EntityManager */ $em = $this->getDoctrine()->getManager(); // cari kolom yang berisi nomor induk sistem $keyNomorIndukSistem = array_search('nomorIndukSistem', $fieldnames); if (is_int($keyNomorIndukSistem)) { if (array_key_exists($keyNomorIndukSistem, $content)) { $entity = $em->getRepository('LanggasSisdikBundle:Siswa')->findOneBy(['nomorIndukSistem' => $content[$keyNomorIndukSistem]]); if (!$entity && !$entity instanceof Siswa) { return; } $reflectionClass = new \ReflectionClass('Langgas\\SisdikBundle\\Entity\\Siswa'); $entityFields = []; foreach ($reflectionClass->getProperties() as $property) { $entityFields[] = $property->getName(); } foreach ($fieldnames as $keyfield => $valuefield) { if (preg_match("/(.+)\\.(.+)/", $valuefield, $matches)) { $valuefield = $matches[1]; $childfield = $matches[2]; } $key = array_search($valuefield, $entityFields); if (is_int($key)) { if (array_key_exists($keyfield, $content)) { $value = $content[$keyfield]; if ($value == "0" || $value == "") { $value = null; } if ($valuefield != 'nomorIndukSistem') { if ($valuefield == 'orangtuaWali') { if ($value != null) { $ortu = $em->getRepository('LanggasSisdikBundle:OrangtuaWali')->findOneBy(['aktif' => true, 'siswa' => $entity]); if (is_object($ortu) && $ortu instanceof OrangtuaWali) { $ortu->{'set' . ucfirst($childfield)}($value); $em->persist($ortu); } } } elseif ($valuefield == 'sekolahAsal') { if ($value != null) { $querySekolahAsal = $em->createQueryBuilder()->select('sekolahasal')->from('LanggasSisdikBundle:SekolahAsal', 'sekolahasal')->where('sekolahasal.nama LIKE :nama')->setParameter('nama', "%{$value}%"); $resultSekolahAsal = $querySekolahAsal->getQuery()->getResult(); if (count($resultSekolahAsal) >= 1) { $sekolahAsal = $resultSekolahAsal[0]; } else { $sekolahAsal = new SekolahAsal(); $sekolahAsal->setSekolah($entity->getSekolah()); $sekolahAsal->{'set' . ucfirst($childfield)}($value); } $entity->setSekolahAsal($sekolahAsal); } } elseif ($valuefield == 'tanggalLahir') { if ($value) { $entity->{'set' . ucfirst($valuefield)}(new \DateTime($value)); } } else { $value = $value !== null ? trim($value) : $value; $entity->{'set' . ucfirst($valuefield)}($value); } } } } } $entity->setDiubahOleh($this->getUser()); $em->persist($entity); $this->gabungSiswaJumlah++; if ($andFlush) { $em->flush(); } } } }
/** * @Route("/{id}", name="applicant_update") * @Method("POST") * @Template("LanggasSisdikBundle:SiswaPendaftar:edit.html.twig") */ public function updateAction(Request $request, $id) { $sekolah = $this->getSekolah(); $this->setCurrentMenu(); $panitiaAktif = $this->getPanitiaAktif(); if (!is_array($panitiaAktif) || count($panitiaAktif) <= 0) { throw new AccessDeniedException($this->get('translator')->trans('exception.tidak.ada.panitia.pendaftaran')); } if (!(is_array($panitiaAktif[0]) && in_array($this->getUser()->getId(), $panitiaAktif[0]) || $panitiaAktif[1] == $this->getUser()->getId())) { throw new AccessDeniedException($this->get('translator')->trans('exception.register.as.committee')); } $em = $this->getDoctrine()->getManager(); $entity = $em->getRepository('LanggasSisdikBundle:Siswa')->find($id); $this->verifyTahun($entity->getTahun()->getTahun()); if (!$entity) { throw $this->createNotFoundException('Entity Siswa tak ditemukan.'); } if ($this->get('security.authorization_checker')->isGranted('edit', $entity) === false) { throw new AccessDeniedException($this->get('translator')->trans('akses.ditolak')); } $deleteForm = $this->createDeleteForm($id); $editForm = $this->createForm('sisdik_siswapendaftar', $entity, ['tahun_aktif' => $panitiaAktif[2], 'mode' => 'edit']); $editForm->submit($request); if ($editForm->isValid()) { try { if ($editForm['referensi']->getData() === null && $editForm['namaReferensi']->getData() != "") { $referensi = new Referensi(); $referensi->setNama($editForm['namaReferensi']->getData()); $referensi->setSekolah($sekolah); $entity->setReferensi($referensi); } if ($editForm['sekolahAsal']->getData() === null && $editForm['namaSekolahAsal']->getData() != "") { $sekolahAsal = new SekolahAsal(); $sekolahAsal->setNama($editForm['namaSekolahAsal']->getData()); $sekolahAsal->setSekolah($sekolah); $entity->setSekolahAsal($sekolahAsal); } // force unit of work detect entity 'changes' // possible problem source: too many objects handled by doctrine $entity->setWaktuUbah(new \DateTime()); $entity->setDiubahOleh($this->getUser()); $em->persist($entity); $em->flush(); $this->get('session')->getFlashBag()->add('success', $this->get('translator')->trans('flash.applicant.updated', ['%name%' => $entity->getNamaLengkap()])); } catch (DBALException $e) { $message = $this->get('translator')->trans('exception.unique.applicant'); throw new DBALException($message); } return $this->redirect($this->generateUrl('applicant_edit', ['id' => $id])); } return ['entity' => $entity, 'edit_form' => $editForm->createView(), 'delete_form' => $deleteForm->createView()]; }