Exemple #1
0
    public function readexcel($idtbl)
    {
        if ($this->session->userdata('logged_in')) {
            $cekmng = cekurlid('manage', 'idmng');
            if (empty($idtbl) || strlen($idtbl) === 0) {
                $this->session->unset_userdata('logged_in');
                $this->session->sess_destroy();
                redirect('login', 'refresh');
            } else {
                if (!in_array($idtbl, $cekmng)) {
                    echo 'tidak terdaftar';
                } else {
                    //import-----------------------------------------------------------------------------------------
                    if (empty($_FILES['userfile']['name'])) {
                        ?>
						<script type="text/javascript">alert('tolong upload filenya ');</script>
						<?php 
                        redirect('login', 'refresh');
                    } else {
                        $session_data = $this->session->userdata('logged_in');
                        $idu = $session_data['id'];
                        $tabel = $this->mweb->getmanage_by_id($idtbl);
                        $target_path = APPPATH . 'file_temp/';
                        $target_path = $target_path . basename($_FILES['userfile']['name']);
                        $filetype = pathinfo($target_path, PATHINFO_EXTENSION);
                        if ($filetype == 'xlsx' || $filetype == 'xls') {
                            $objPHPExcel = new PHPExcel();
                            $objReader = PHPExcel_IOFactory::createReader('Excel2007');
                            $objReader->setReadDataOnly(true);
                            $objPHPExcel = $objReader->load($_FILES['userfile']['tmp_name']);
                            $objWorksheet = $objPHPExcel->getActiveSheet();
                            $bariss = array();
                            $koolom = array();
                            $highestcoll = $objWorksheet->getHighestColumn();
                            $highestRow = $objWorksheet->getHighestRow();
                            $hcoll = toNum(strtolower($highestcoll)) + 1;
                            for ($i = 1; $i <= $highestRow; $i++) {
                                $row = $objWorksheet->getRowIterator($i)->current();
                                $cellIterator = $row->getCellIterator();
                                $cellIterator->setIterateOnlyExistingCells(false);
                                $isi = 0;
                                if ($i == 1) {
                                    foreach ($cellIterator as $cell) {
                                        $koolom[] = preg_replace('/\\s+/', '_', $cell->getValue());
                                    }
                                } else {
                                    foreach ($cellIterator as $cell) {
                                        $isi++;
                                        if ($isi == $hcoll) {
                                            $bariss[] = $cell->getValue() . "#";
                                        } else {
                                            $bariss[] = $cell->getValue();
                                        }
                                    }
                                }
                            }
                            switch ($tabel) {
                                case 'barang':
                                    $exclude = array('kdbarang');
                                    break;
                                case 'cus':
                                    $exclude = array('idcus');
                                    break;
                                case 'supplier':
                                    $exclude = array('idsupp');
                                    break;
                                default:
                                    $exclude = array();
                                    break;
                            }
                            $stmt_list = create_statement($tabel, $exclude);
                            $kolomdb = explode(',', $stmt_list);
                            $per = implode("^", $bariss);
                            $arrDelimiters = array('#,', '#');
                            $uniformText = str_replace($arrDelimiters, "-|-", $per);
                            $arrdata = explode("-|-", $uniformText);
                            $jmlkoll = count($koolom);
                            $listkolex = array_diff($koolom, array(''));
                            $harusadd = array_diff($listkolex, $kolomdb);
                            if (count($harusadd) > 0) {
                                foreach ($harusadd as $k => $valadd) {
                                    $rtrimstr = rtrim($valadd);
                                    if (!empty($rtrimstr)) {
                                        $hrsadd = preg_replace('/\\s+/', '_', $valadd);
                                        $this->db->query('ALTER TABLE ' . $tabel . ' ADD ' . $hrsadd . ' varchar(100)');
                                    } else {
                                        ?>
											<script type="text/javascript">alert("kolom pada excel tidak boleh kosong !");</script>;
										<?php 
                                        redirect(site_url('gudang'), 'refresh');
                                    }
                                }
                            }
                            $jmlbaris = count($arrdata);
                            for ($y = 0; $y < $jmlbaris; $y++) {
                                $isidata = explode("^", $arrdata[$y]);
                                $isiasli = array_diff($isidata, array(''));
                                if ($tabel === 'cus') {
                                    array_push($kolomdb, 'iduser');
                                    array_push($isiasli, $idu);
                                }
                                $nb = count($isiasli);
                                $nk = count($kolomdb);
                                $datanya = '';
                                if ($nb === $nk) {
                                    $datanya = array_combine($kolomdb, $isiasli);
                                }
                                if (!empty($datanya)) {
                                    $this->mweb->settable($tabel, $datanya);
                                }
                            }
                            ?>
								<script type="text/javascript">alert("import data berhasil :)");</script>;
							<?php 
                            redirect(site_url('gudang'), 'refresh');
                        } else {
                            ?>
							<script type="text/javascript">alert("silahkan import file yang berextensi xlsx atau xls");</script>;
						<?php 
                            redirect(site_url('gudang'), 'refresh');
                        }
                    }
                    //import-----------------------------------------------------------------------------------------
                }
            }
        } else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    }
$uname = trimString($data["uname"], 255);
$S_SID = getMagic($os . "_" . $platform . "_" . $uname, 768);
$S_ID = getSQLID($conn, "select S_ID from System where S_SID=? LIMIT 1", $S_SID);
if ($S_ID == 0) {
    // this operating system is not in the database
    $sql = $conn->prepare("INSERT INTO System (S_SID, Platform, OS, Uname) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE S_SID=S_SID");
    if (!$sql) {
        dieSQL();
    }
    $sql->bind_param("ssss", $S_SID, $platform, $os, $uname);
    $sql->execute();
    $sql->close();
    $S_ID = $conn->insert_id;
    if ($S_ID == 0) {
        $S_ID = getSQLID($conn, "select S_ID from System where S_SID=? LIMIT 1", $S_SID);
    }
}
// we can now add all of this information to the runlog
if ($C_ID == 0 or $P_ID == 0 or $V_ID == 0 or $S_ID == 0) {
    die("Something went wrong adding data: {$C_ID}, {$P_ID}, {$V_ID}, {$S_ID}");
}
$exe = trimString($data["executable"], 255);
$clockspeed = toNum($data["clockspeed"], 0);
$numcores = toInt($data["numcores"], 0);
$sql = $conn->prepare("INSERT INTO RunLog (RunTime, C_ID, Executable, P_ID, S_ID, V_ID, NumCores, ClockSpeed) VALUES (now(), ?, ?, ?, ?, ?, ?, ?)");
if (!$sql) {
    dieSQL();
}
$sql->bind_param("isiiiid", $C_ID, $exe, $P_ID, $S_ID, $V_ID, $numcores, $clockspeed);
$sql->execute();
$sql->close();