Example #1
0
 /**
  * @Route("/mengimpor-gabung", name="siswa_mengimporgabung")
  * @Template("LanggasSisdikBundle:Siswa:impor-gabung.html.twig")
  * @Method("POST")
  * @Secure(roles="ROLE_ADMIN")
  */
 public function mengimporGabungAction()
 {
     $sekolah = $this->getSekolah();
     $this->setCurrentMenu();
     $form = $this->createForm('sisdik_siswagabung');
     $form->submit($this->getRequest());
     $filedata = $form['file']->getData();
     if ($filedata instanceof UploadedFile) {
         $reader = new SpreadsheetReader($filedata->getPathname(), false, $filedata->getClientMimeType());
         $sheets = $reader->Sheets();
         if (count($sheets) > 1) {
             $message = $this->get('translator')->trans('alert.hanya.boleh.satu.lembar.kerja');
             $form->get('file')->addError(new FormError($message));
         }
         unset($reader);
     }
     if ($form->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $file = $form['file']->getData();
         $targetfilename = $file->getClientOriginalName();
         if ($file->move(self::DOCUMENTS_OUTPUTDIR, $targetfilename)) {
             $reader = new SpreadsheetReader(self::DOCUMENTS_OUTPUTDIR . $targetfilename);
             $fieldnames = [];
             $content = [];
             foreach ($reader as $row) {
                 $cellContent = [];
                 foreach ($row as $cell) {
                     $fieldCocok = [];
                     if (preg_match("/^(\\d+:)(.*)/", $cell['data'], $fieldCocok)) {
                         $fieldnames[] = $fieldCocok[2];
                     } elseif (preg_match("/^\\[.*\\]\$/", $cell['data'])) {
                         // baris header perlu diabaikan
                     } else {
                         $cellContent[] = $cell['data'];
                     }
                 }
                 if (count($cellContent) > 0) {
                     $content[] = $cellContent;
                 }
             }
             foreach ($content as $value) {
                 $this->gabungSiswa($value, $fieldnames, $sekolah);
             }
             try {
                 $em->flush();
             } catch (DBALException $e) {
                 $message = $this->get('translator')->trans('exception.studentid.unique');
                 throw new DBALException($message);
             } catch (\Exception $e) {
                 $message = $this->get('translator')->trans('exception.import.error');
                 throw new \Exception($message);
             }
             $this->get('session')->getFlashBag()->add('success', $this->get('translator')->trans('flash.data.student.merged', ['%count%' => $this->gabungSiswaJumlah]));
             return $this->redirect($this->generateUrl('siswa_imporgabung'));
         }
     }
     $dlform = $this->createForm('sisdik_siswaexport');
     return ['form' => $form->createView(), 'dlform' => $dlform->createView()];
 }
 /**
  * Menempatkan kelompok siswa ke kelas-kelas
  *
  * @Route("/menempatkan-kelompok", name="penempatan-siswa-kelas_menempatkan-kelompok")
  * @Method("POST")
  * @Template("LanggasSisdikBundle:PenempatanSiswaKelas:tempatkan-kelompok.html.twig")
  */
 public function menempatkanKelompokAction()
 {
     $sekolah = $this->getSekolah();
     $this->setCurrentMenu();
     $em = $this->getDoctrine()->getManager();
     $filename = $this->get('session')->get(self::PENEMPATAN_FILE);
     $reader = new SpreadsheetReader(self::DOCUMENTS_OUTPUTDIR . $filename);
     $sheets = $reader->Sheets();
     $sheetCollection = new ArrayCollection();
     $form = $this->createForm('collection', $sheetCollection, ['type' => 'sisdik_penempatansiswakelaskelompok', 'required' => true, 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false, 'options' => ['label_render' => false], 'label_render' => false]);
     $form->submit($this->getRequest());
     if ($form->isValid()) {
         $formdata = $form->getData();
         foreach ($formdata as $data) {
             $reader->ChangeSheet($data['index']);
             $fieldnames = [];
             $content = [];
             foreach ($reader as $row) {
                 $cellContent = [];
                 foreach ($row as $cell) {
                     if (array_key_exists('table:style-name', $cell['attributes']) && $cell['attributes']['table:style-name'] == 'nama-kolom') {
                         $fieldnames[] = $cell['data'];
                     } elseif (array_key_exists('table:style-name', $cell['attributes']) && $cell['attributes']['table:style-name'] == 'nama-kolom-deskriptif') {
                         // baris yang tak perlu dibaca
                     } else {
                         $cellContent[] = $cell['data'];
                     }
                 }
                 if (count($cellContent) > 0) {
                     $content[] = $cellContent;
                 }
             }
             array_walk($fieldnames, [&$this, "formatNamaField"]);
             foreach ($content as $value) {
                 $this->menempatkanSiswa($value, $fieldnames, $sekolah, $data['tahunAkademik'], $data['kelas']);
             }
             try {
                 $em->flush();
             } catch (DBALException $e) {
                 $message = $this->get('translator')->trans('exception.studentclass.unique');
                 throw new DBALException($message);
             } catch (\Exception $e) {
                 $message = $this->get('translator')->trans('exception.import.error');
                 throw new \Exception($message);
             }
         }
         $this->get('session')->getFlashBag()->add('success', $this->get('translator')->trans('flash.data.studentclass.imported.group', ['%count%' => $this->siswaDitempatkanJumlah]));
         return $this->redirect($this->generateUrl('penempatan-siswa-kelas'));
     }
     return ['form' => $form->createView(), 'sheetCollection' => $sheetCollection];
 }