/**
  * 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];
 }