Skip to content

Commit 14f0bf2

Browse files
authored
Merge pull request #16 from manh21/fix/duplicate
fix: duplicate pemilih when importing
2 parents ba68dc2 + 8bd5c48 commit 14f0bf2

File tree

3 files changed

+135
-111
lines changed

3 files changed

+135
-111
lines changed

application/controllers/admin/Data_pemilih.php

+116-111
Original file line numberDiff line numberDiff line change
@@ -280,134 +280,139 @@ public function do_upload()
280280
$this->upload->initialize($config);
281281
$this->form_validation->set_rules('fileURL', 'Upload File', 'callback_checkFileValidation');
282282

283-
if ($this->form_validation->run() == false) {
284-
$this->import();
283+
if (!$this->form_validation->run()) {
284+
$this->import();
285+
return;
286+
}
287+
if (!$this->upload->do_upload('fileURL')) {
288+
return;
289+
}
290+
291+
$upload_data = $this->upload->data();
292+
$fileName = $upload_data['file_name']; //Nama File
293+
$fileType = $upload_data['file_ext'];
294+
295+
$inputFileName = $upload_data['full_path'];
296+
297+
// Creating a Reader
298+
if ($fileType == '.csv') {
299+
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
300+
} elseif ($fileType == '.xlsx') {
301+
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
285302
} else {
303+
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
304+
}
286305

287-
if ($this->upload->do_upload('fileURL')) {
306+
// Loading a Spreadsheet File
307+
$spreadsheet = $reader->load($inputFileName);
308+
$sheetData = $spreadsheet->getActiveSheet()->toArray();
288309

289-
$upload_data = $this->upload->data();
290-
$fileName = $upload_data['file_name']; //Nama File
291-
$fileType = $upload_data['file_ext'];
310+
// menghitung jumlah baris data yang ada
311+
$arrayCount = count($sheetData);
312+
$flag = 0;
292313

293-
$inputFileName = $upload_data['full_path'];
294314

295-
// Creating a Reader
296-
if ($fileType == '.csv') {
297-
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
298-
} elseif ($fileType == '.xlsx') {
299-
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
300-
} else {
301-
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
315+
$createArray = array('nis', 'username', 'password', 'nama', 'kelas', 'jk');
316+
$makeArray = array('nis' => 'nis', 'username' => 'username', 'password' => 'password', 'nama' => 'nama', 'kelas' => 'kelas', 'jk' => 'jk');
317+
$SheetDataKey = array();
318+
foreach ($sheetData as $dataInSheet) {
319+
foreach ($dataInSheet as $key => $value) {
320+
if (in_array(trim($value), $createArray)) {
321+
$value = preg_replace('/\s+/', '', $value);
322+
$SheetDataKey[trim($value)] = $key;
302323
}
324+
}
325+
}
303326

304-
// Loading a Spreadsheet File
305-
$spreadsheet = $reader->load($inputFileName);
306-
$sheetData = $spreadsheet->getActiveSheet()->toArray();
307-
308-
// menghitung jumlah baris data yang ada
309-
$arrayCount = count($sheetData);
310-
$flag = 0;
311-
312-
313-
$createArray = array('nis', 'username', 'password', 'nama', 'kelas', 'jk');
314-
$makeArray = array('nis' => 'nis', 'username' => 'username', 'password' => 'password', 'nama' => 'nama', 'kelas' => 'kelas', 'jk' => 'jk');
315-
$SheetDataKey = array();
316-
foreach ($sheetData as $dataInSheet) {
317-
foreach ($dataInSheet as $key => $value) {
318-
if (in_array(trim($value), $createArray)) {
319-
$value = preg_replace('/\s+/', '', $value);
320-
$SheetDataKey[trim($value)] = $key;
321-
}
322-
}
327+
$dataDiff = array_diff_key($makeArray, $SheetDataKey);
328+
if (empty($dataDiff)) {
329+
$flag = 1;
330+
}
331+
// match excel sheet column
332+
if ($flag == 1) {
333+
$fetchData = array();
334+
for ($i = 1; $i < $arrayCount; $i++) {
335+
$nis = $SheetDataKey['nis'];
336+
$userName = $SheetDataKey['username'];
337+
$password = $SheetDataKey['password'];
338+
$nama = $SheetDataKey['nama'];
339+
$kelas = $SheetDataKey['kelas'];
340+
$jk = $SheetDataKey['jk'];
341+
342+
$nis = filter_var(trim($sheetData[$i][$nis]), FILTER_SANITIZE_STRING);
343+
$userName = filter_var(trim($sheetData[$i][$userName]), FILTER_SANITIZE_STRING);
344+
$password = filter_var(trim($sheetData[$i][$password]), FILTER_SANITIZE_STRING);
345+
$nama = filter_var(trim($sheetData[$i][$nama]), FILTER_SANITIZE_STRING);
346+
$kelas = filter_var(trim($sheetData[$i][$kelas]), FILTER_SANITIZE_STRING);
347+
$jk = filter_var(trim($sheetData[$i][$jk]), FILTER_SANITIZE_STRING);
348+
349+
// Get idkelas
350+
$idKelas = $this->Data_pemilih_model->get_idKelas($kelas);
351+
if ($idKelas == false) {
352+
$idKelas = '';
353+
} else {
354+
$idKelas = $idKelas->idkelas;
323355
}
324356

325-
$dataDiff = array_diff_key($makeArray, $SheetDataKey);
326-
if (empty($dataDiff)) {
327-
$flag = 1;
328-
}
329-
// match excel sheet column
330-
if ($flag == 1) {
331-
$fetchData = array();
332-
for ($i = 1; $i < $arrayCount; $i++) {
333-
$nis = $SheetDataKey['nis'];
334-
$userName = $SheetDataKey['username'];
335-
$password = $SheetDataKey['password'];
336-
$nama = $SheetDataKey['nama'];
337-
$kelas = $SheetDataKey['kelas'];
338-
$jk = $SheetDataKey['jk'];
339-
340-
$nis = filter_var(trim($sheetData[$i][$nis]), FILTER_SANITIZE_STRING);
341-
$userName = filter_var(trim($sheetData[$i][$userName]), FILTER_SANITIZE_STRING);
342-
$password = filter_var(trim($sheetData[$i][$password]), FILTER_SANITIZE_STRING);
343-
$nama = filter_var(trim($sheetData[$i][$nama]), FILTER_SANITIZE_STRING);
344-
$kelas = filter_var(trim($sheetData[$i][$kelas]), FILTER_SANITIZE_STRING);
345-
$jk = filter_var(trim($sheetData[$i][$jk]), FILTER_SANITIZE_STRING);
346-
347-
// Get idkelas
348-
$idKelas = $this->Data_pemilih_model->get_idKelas($kelas);
349-
if ($idKelas == false) {
350-
$idKelas = '';
351-
} else {
352-
$idKelas = $idKelas->idkelas;
353-
}
354-
$fetchData[] = array(
355-
'nis' => $nis,
356-
'username' => $userName,
357-
'password' => $password,
358-
'nama' => $nama,
359-
'kelas' => $kelas,
360-
'jk' => $jk,
361-
'status' => 'Belum Memilih',
362-
'aktif' => '1',
363-
'idkelas' => $idKelas,
364-
);
365-
}
366-
367-
$dups = [];
368-
foreach($fetchData as $keys => $val){
369-
$res = searchForUsername($val['username'], $fetchData);
370-
if(count($res) > 1){
371-
$dups[] = $res;
372-
}
373-
}
374-
375-
if(!empty($dups)){
376-
$data['dataInfo'] = $fetchData;
377-
$this->Data_pemilih_model->setBatchImport($fetchData);
378-
$this->Data_pemilih_model->importData();
379-
$this->session->set_flashdata(
380-
'message',
381-
'<div class="alert alert-danger alert-dismissible">
382-
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
383-
Terdapat username ganda silahkan cek kembali data anda! </div>'
384-
);
385-
unlink('./assets/uploads/' . $fileName);
386-
redirect('admin/pemilih', 'refresh');
387-
} else {
388-
$data['dataInfo'] = $fetchData;
389-
$this->Data_pemilih_model->setBatchImport($fetchData);
390-
$this->Data_pemilih_model->importData();
391-
}
392-
} else {
393-
$this->session->set_flashdata(
394-
'message',
395-
'<div class="alert alert-danger alert-dismissible">
396-
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
397-
Please import correct file, did not match excel sheet column </div>'
398-
);
399-
unlink('./assets/uploads/' . $fileName);
357+
// Check duplicate
358+
$exist = $this->Data_pemilih_model->is_exist($userName, $nis);
359+
if($exist) { continue; }
360+
361+
$fetchData[] = array(
362+
'nis' => $nis,
363+
'username' => $userName,
364+
'password' => $password,
365+
'nama' => $nama,
366+
'kelas' => $kelas,
367+
'jk' => $jk,
368+
'status' => 'Belum Memilih',
369+
'aktif' => '1',
370+
'idkelas' => $idKelas,
371+
);
372+
}
373+
374+
$dups = [];
375+
foreach($fetchData as $keys => $val){
376+
$res = searchForUsername($val['username'], $fetchData);
377+
if(count($res) > 1){
378+
$dups[] = $res;
400379
}
401-
unlink('./assets/uploads/' . $fileName);
380+
}
381+
382+
if(!empty($dups)){
383+
$data['dataInfo'] = $fetchData;
384+
$this->Data_pemilih_model->setBatchImport($fetchData);
385+
$this->Data_pemilih_model->importData();
402386
$this->session->set_flashdata(
403387
'message',
404-
'<div class="alert alert-success alert-dismissible">
388+
'<div class="alert alert-danger alert-dismissible">
405389
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
406-
Berhasil Mengimport Data </div>'
390+
Terdapat username ganda silahkan cek kembali data anda! </div>'
407391
);
392+
unlink('./assets/uploads/' . $fileName);
408393
redirect('admin/pemilih', 'refresh');
394+
} else {
395+
$data['dataInfo'] = $fetchData;
396+
$this->Data_pemilih_model->setBatchImport($fetchData);
397+
$this->Data_pemilih_model->importData();
409398
}
399+
} else {
400+
$this->session->set_flashdata(
401+
'message',
402+
'<div class="alert alert-danger alert-dismissible">
403+
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
404+
Please import correct file, did not match excel sheet column </div>'
405+
);
406+
unlink('./assets/uploads/' . $fileName);
410407
}
408+
unlink('./assets/uploads/' . $fileName);
409+
$this->session->set_flashdata(
410+
'message',
411+
'<div class="alert alert-success alert-dismissible">
412+
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
413+
Berhasil Mengimport Data </div>'
414+
);
415+
redirect('admin/pemilih', 'refresh');
411416
}
412417

413418
/**

application/controllers/api/Vote.php

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public function vote_post()
155155
'error' => 403,
156156
'message' => 'user sudah memilih'
157157
);
158+
$this->response($message, 200);
158159
}
159160
} else {
160161
$message = array(

application/models/Data_pemilih_model.php

+18
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ public function delete_data_vote_user($id)
191191
return true;
192192

193193
}
194+
195+
/**
196+
* Check if exist user
197+
* @param string username
198+
* @param string nis
199+
* @return boolean
200+
*/
201+
public function is_exist($username, $nis)
202+
{
203+
$this->db->where('username', $username);
204+
$this->db->or_where('nis', $nis);
205+
$q = $this->db->get('data_pemilih');
206+
if($q->num_rows() > 0){
207+
return true;
208+
}
209+
210+
return false;
211+
}
194212
}
195213

196214
/* End of file Data_pemilih_model.php */

0 commit comments

Comments
 (0)