public function executeImportfile(sfWebRequest $request) { $allowed = array('xlsx', 'xls'); $file = $request->getFiles('file'); $fileSize = $file['size']; $fileType = $file['type']; $theFileName = $file['name']; $uploadDir = sfConfig::get("sf_upload_dir"); $import = $uploadDir . '/import'; if (!is_dir($import)) { mkdir($import, 0777); } $ext = pathinfo($theFileName, PATHINFO_EXTENSION); if (!in_array($ext, $allowed)) { $this->msg = 'file type not allowed. File Type Must '; $y = 0; foreach ($allowed as $x) { if ($y != count($allowed) - 1) { $this->msg .= $x . " or "; } else { $this->msg .= $x; } $y++; } $this->setTemplate('import'); } else { move_uploaded_file($file['tmp_name'], "{$import}/{$theFileName}"); $inputFileName = $uploadDir . '/import/' . $theFileName; try { $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); $data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true); for ($x = 3; $x <= count($data); $x++) { $c = new Criteria(); $c->add(KategoriPeer::NAMA_KATEGORI, $data[$x]['C'], Criteria::EQUAL); $kategori = $this->getIdColumn(KategoriPeer::doSelect($c)); $k = new Criteria(); $k->add(KemasanPeer::NAMA_KEMASAN, $data[$x]['E'], Criteria::EQUAL); $kemasan = $this->getIdColumn(KemasanPeer::doSelect($k)); $p = new Criteria(); $p->add(ProdusenPeer::NAMA_PRODUSEN, $data[$x]['F'], Criteria::EQUAL); $produsen = $this->getIdColumn(ProdusenPeer::doSelect($p)); $barang = BarangQuery::create()->findOneById($data[$x]['A']); if (isset($barang)) { $barang->setNamaBarang($data[$x]['B']); $barang->setIdKategori("" . $kategori); $barang->setStock($data[$x]['D']); $barang->setIdKemasan("" . $kemasan); $barang->setIdProdusen("" . $produsen); $barang->setDescription($data[$x]['G']); $barang->save(); } else { $barang = new Barang(); $barang->setNamaBarang($data[$x]['B']); $barang->setIdKategori("" . $kategori); $barang->setStock($data[$x]['D']); $barang->setIdKemasan("" . $kemasan); $barang->setIdProdusen("" . $produsen); $barang->setDescription($data[$x]['G']); $barang->save(); } } unlink($inputFileName); $this->getUser()->setFlash('notice', $notice . ' You can add another one below.'); $this->redirect('@barang'); } catch (Exception $e) { die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage()); } } }