/** * mengimpor data siswa baru * * @param array $content * @param array $fieldnames * @param \Langgas\SisdikBundle\Entity\Sekolah $sekolah * @param \Langgas\SisdikBundle\Entity\Tahun $tahun * @param boolean $andFlush */ private function imporSiswaBaru($content, $fieldnames, $sekolah, $tahun, $andFlush = false) { /* @var $em EntityManager */ $em = $this->getDoctrine()->getManager(); $atleastone = false; foreach ($content as $key => $val) { if ($val != "") { $atleastone = true; } } if (!$atleastone) { return; } $entity = new Siswa(); $reflectionClass = new \ReflectionClass('Langgas\\SisdikBundle\\Entity\\Siswa'); $entityFields = []; foreach ($reflectionClass->getProperties() as $property) { $entityFields[] = $property->getName(); } $orangtuaWali = new ArrayCollection(); $ortu = new OrangtuaWali(); $sekolahAsal = null; 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 == 'orangtuaWali') { $ortu->setAktif(true); $ortu->{'set' . ucfirst($childfield)}(trim($value)); } elseif ($valuefield == 'sekolahAsal') { if (trim($value) != '') { $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->{'set' . ucfirst($childfield)}(trim($value)); $sekolahAsal->setSekolah($sekolah); } } } elseif ($valuefield == 'tanggalLahir') { if ($value) { $entity->{'set' . ucfirst($valuefield)}(new \DateTime($value)); } } else { $value = $value !== null ? trim($value) : $value; $entity->{'set' . ucfirst($valuefield)}($value); } } } } if ($this->nomorUrutPersekolah == 0) { $qbe = $em->createQueryBuilder(); $querynomor = $em->createQueryBuilder()->select($qbe->expr()->max('siswa.nomorUrutPersekolah'))->from('LanggasSisdikBundle:Siswa', 'siswa')->where('siswa.sekolah = :sekolah')->setParameter('sekolah', $sekolah); $nomorUrutPersekolah = $querynomor->getQuery()->getSingleScalarResult(); $nomorUrutPersekolah = $nomorUrutPersekolah === null ? 100000 : $nomorUrutPersekolah; $nomorUrutPersekolah++; $this->nomorUrutPersekolah = $nomorUrutPersekolah; } else { $this->nomorUrutPersekolah++; } $entity->setNomorUrutPersekolah($this->nomorUrutPersekolah); $entity->setNomorIndukSistem($this->nomorUrutPersekolah . $sekolah->getNomorUrut()); $entity->setCalonSiswa(false); $entity->setSekolah($sekolah); $entity->setTahun($tahun); $entity->setGelombang(null); $orangtuaWali->add($ortu); $entity->setOrangtuaWali($orangtuaWali); $entity->setSekolahAsal($sekolahAsal); $entity->setDibuatOleh($this->getUser()); $tanggalBulan = preg_split("/[\\/,\\s,\\-,\\+,_,\\*]/", $sekolah->getAwalPembiayaan()); if (is_array($tanggalBulan)) { $tanggal = $tanggalBulan[0]; $bulan = $tanggalBulan[1]; } else { $tanggal = '01'; $bulan = '07'; } $entity->setPembiayaanSejak(new \DateTime($entity->getTahun()->getTahun() . '-' . $bulan . '-' . $tanggal)); $em->persist($entity); $this->imporSiswaJumlah++; if ($andFlush) { $em->flush(); $em->clear($entity); $em->clear($ortu); $em->clear($sekolahAsal); } }