@@ -280,134 +280,139 @@ public function do_upload()
280
280
$ this ->upload ->initialize ($ config );
281
281
$ this ->form_validation ->set_rules ('fileURL ' , 'Upload File ' , 'callback_checkFileValidation ' );
282
282
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 ();
285
302
} else {
303
+ $ reader = new \PhpOffice \PhpSpreadsheet \Reader \Xls ();
304
+ }
286
305
287
- if ($ this ->upload ->do_upload ('fileURL ' )) {
306
+ // Loading a Spreadsheet File
307
+ $ spreadsheet = $ reader ->load ($ inputFileName );
308
+ $ sheetData = $ spreadsheet ->getActiveSheet ()->toArray ();
288
309
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 ;
292
313
293
- $ inputFileName = $ upload_data ['full_path ' ];
294
314
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 ;
302
323
}
324
+ }
325
+ }
303
326
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 ;
323
355
}
324
356
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">×</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">×</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 ;
400
379
}
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 ();
402
386
$ this ->session ->set_flashdata (
403
387
'message ' ,
404
- '<div class="alert alert-success alert-dismissible">
388
+ '<div class="alert alert-danger alert-dismissible">
405
389
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
406
- Berhasil Mengimport Data </div> '
390
+ Terdapat username ganda silahkan cek kembali data anda! </div> '
407
391
);
392
+ unlink ('./assets/uploads/ ' . $ fileName );
408
393
redirect ('admin/pemilih ' , 'refresh ' );
394
+ } else {
395
+ $ data ['dataInfo ' ] = $ fetchData ;
396
+ $ this ->Data_pemilih_model ->setBatchImport ($ fetchData );
397
+ $ this ->Data_pemilih_model ->importData ();
409
398
}
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">×</button>
404
+ Please import correct file, did not match excel sheet column </div> '
405
+ );
406
+ unlink ('./assets/uploads/ ' . $ fileName );
410
407
}
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">×</button>
413
+ Berhasil Mengimport Data </div> '
414
+ );
415
+ redirect ('admin/pemilih ' , 'refresh ' );
411
416
}
412
417
413
418
/**
0 commit comments