-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcsvmapper.php
More file actions
979 lines (406 loc) · 13.1 KB
/
csvmapper.php
File metadata and controls
979 lines (406 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/*
CSVMAPPER class is created for import csv file in database.
Set Configuration of tool in csv_config.php
********************************
Show upload CSV form, Define in csvmapper class.
show_form()
********************************
Submitted CSV form and upload csv file and unset and set session values, Define in csvmapper class.
upload_csvfile()
********************************
Show Mapping form.
Map table fields with CSV fields, Define in csvmapper class.
show_mapper()
********************************
Create drop-down for csv fields. Internally used in csvmapper class.
show_first_row()
********************************
Check number of lines in CSV
Choose appropriate function in csvmapper class for insert data on the basis ("less then and equal" to $rows<=$presentrows_in_csv)
of line present in CSV file, Define in csvmapper class.
decideInsertFunc()
*******************************
Insert simple csv-data in table.
insert_data()
*******************************
Insert ajax-call base csv-data in table.
insert_counter_data()
********************************
*/
class csvmapper
{
var $upload_path = "";
var $table_name = "";
var $table_fields = array();
var $header_row = array();
var $uploaded_csv_name = "";
var $ajax_page = "";
var $ajax_page_runner = "";
var $lines_percall = 1;
var $check_csv_line = 100;
var $hooks_file = "";
var $errors = "";
// INITIALIZER
function initialize($params = array())
{
if (count($params) > 0)
{
// Settings the variable through configuration passed. "$key" used as config indexes.
foreach ($params as $key => $val)
{
if (isset($this->$key))
{
$this->$key = $val;
}
}
}
}
// This function runs all the options.
function upload_and_process()
{
//Show upload CSV form, Define in csvmapper class.
$this->show_form();
//Submitted CSV form and upload csv file and unset and set session values, Define in csvmapper class.
$this->upload_csvfile();
if(trim($this->errors)=="")
{
//Map table fields with CSV fields, Define in csvmapper class.
$this->show_mapper();
//Choose appropriate function in csvmapper class for insert data on the basis ("less then and equal" to $rows<=$presentrows_in_csv)
//of line present in CSV file, Define in csvmapper class.
$this->decideInsertFunc();
}
}
// Constuctor
function csvmapper($params=array())
{
if (count($params) > 0)
{
$this->initialize($params);
}
}
//Render file upload form
function show_form()
{
if(!isset($_POST['checkpost']))
{
?>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input type="hidden" name="checkpost" id="checkpost">
<input type="submit" name="submit" value="Submit" class="btn btn-primary">
</form>
<?php
}
}
// Upload csv file
function upload_csvfile()
{
if(isset($_POST['checkpost']))
{
if(isset($_FILES) && !empty($_FILES) && (pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION) == "csv"))
{
//upload csv file.
$this->uploaded_csv_name = strtotime(date("Y-m-d H:i:s")).".csv";
move_uploaded_file($_FILES["file"]["tmp_name"],$this->upload_path.$this->uploaded_csv_name);
//Re-intialize the session by unset the indexes.
unset($_SESSION['csvlastcount']);
unset($_SESSION['csv_handle']);
unset($_SESSION['file_name']);
unset($_SESSION['fileLineCount']);
//Set the intial value for file-name and csv-last-count to zero.
$_SESSION['file_name'] = $this->uploaded_csv_name;
$_SESSION['csvlastcount'] = 0;
}
else
{
$this->errors = "File must be CSV format. with a .csv Extention";
}
}
}
function errors()
{
return $this->errors;
}
// Render table-fields and csv fields mapping form
function show_mapper()
{
if(isset($_POST['checkpost']))
{
//echo $this->table_name ."</br></br>";
echo "<form method='post' action=''>
<table class='table table-condensed table-striped table-bordered'>";
echo "<tr>";
echo '<td>Fields</td>';
echo '<td>CSV Columns</td></tr>';
foreach ($this->table_fields as $key=>$value)
{
echo "<tr>";
echo "<td>".$value."</td>";
//show the list of csv field by show_first_row()
echo "<td>".$this->show_first_row()."</td>";
echo "</tr>";
}
echo "<tr>";
echo '<td>
<label class="checkbox">
<input type="checkbox" value="1" name="skipfirstrow" checked="checked" /><span class="lbl"> Skip first row</span>
</label>
</td>';
echo '<td><input type="submit" class="btn btn-primary" name="update" value="Start Import"></td></tr>';
echo "</table>";
echo "</form>";
}
}
// Create drop-down for csv fields.
function show_first_row()
{
$csvfile = fopen($this->upload_path.$this->uploaded_csv_name, "r");
$data = fgetcsv($csvfile, 1024, ",");
$num = count($data);
$str = "<select style='margin-bottom:2px;' name='fieldmaps[]'>";
$str .= "<option value=''>Select an Option</option>";
for ($c=0; $c < $num; $c++)
{
$str .= "<option value='$c'>" . $data[$c] ."</option>";
}
$str .= "</select>";
fclose($csvfile);
return $str;
}
// Form post collected from mapping-form
function decideInsertFunc()
{
if(isset($_POST['update']) && !empty($_POST['update']))
{
// get file lines.
$number_rec = file($this->upload_path.$_SESSION['file_name']);
//Set the post values, file lines in Session
$_SESSION['skipfirstrow']=$_POST['skipfirstrow'];
$_SESSION['fileLineCount']=count($number_rec);
$_SESSION['fieldmaps'] = $_POST['fieldmaps'];
//Check if csv has less lines as specified in variable.
//Use insert_data() for less lines.
//Use Ajax call for too many lines.
if(count($number_rec)<= $this->check_csv_line)
{
$this->insert_data();
}
else
{
// Redirect to "ajax.php" page
echo '<script>location.replace("' . $this->ajax_page . '")</script>';
//header("Location:" . $this->ajax_page);
}
}
}
//Insert simple csv-data in table.
function insert_data()
{
// Including hooks file if it is set and exists
$has_hooks = false;
if($this->hooks_file != "")
{
$hook_file = $this->hooks_file;
if (file_exists($hook_file))
{
// Load the hooks file
include($hook_file);
$has_hooks = true;
}
else
{
echo "Specified Hooks file is Not found <br> " . $hook_file;
}
} // Including hooks file finished.
if(isset($_POST['update']) && !empty($_POST['fieldmaps']))
{
//Open uploaded CSV file
if (($handle = fopen($this->upload_path.$_SESSION['file_name'], "r")) !== FALSE) {
//Start loop on CSV file handler(pointer) for a single line.
while (($csv_data = fgetcsv($handle, 1024, ",")) !== FALSE) {
//Check that first-row(header line) would be skip from csv.
if(isset($_POST['skipfirstrow']) && $_POST['skipfirstrow']==1 )
{
$_POST['skipfirstrow']=0;
continue;
}
$values = array();
//Loop to tables fields for build a insert query.
foreach($this->table_fields as $key => $field)
{
if($_POST['fieldmaps'][$key]!="")
{
$values[$field] = $csv_data[$_POST['fieldmaps'][$key]];
}
else
{
$values[$field] = "";
}
}
if($has_hooks)
{
$values = csv_hook::process_row($values);
}
$final_value= array();
foreach($values as $valloop)
{
$final_value[] = "'" . mysql_real_escape_string( $valloop ) . "'";
}
// Picking the class and the values.
//Insert a csv row data in database.
$query ="INSERT INTO ".$this->table_name."(" . implode( ',' , $this->table_fields ) . ")VALUES(" . implode( ',' ,$final_value ) . ")";
mysql_query($query) or die(mysql_error());
}
// Close the handler.
fclose($handle);
}
}
}
//Render the insert progress-bar
function ajaxRunner()
{
?>
<h2> Processing CSV </h2>
<div id="" class="">Percentage Uploaded:
<span id="progress_text"></span>
</div>
<div class="">Total No. Of record Inserted:
<span id="progress_count"></span>
</div>
<div class="progress progress-striped active">
<div id="inner_loader" class="bar" style="width:0%"></div>
</div>
<p>Please do not refresh the Page</p>
<script language="javascript">
$(document).ready(function(){
//insert csv record in database.
csvProcess();
});
function csvProcess()
{
csvCurrentCount = 0;
$.ajax({
type: "POST",
url: "<?php echo $this->ajax_page_runner; ?>",
async:true,
}).done(function( msg ) {
msg= msg.trim();
if(msg!="done" && msg!="error")
{
csvCurrentCount = parseInt(csvCurrentCount) + parseInt(msg);
percentProgress = (parseInt(csvCurrentCount)/<?php echo $_SESSION['fileLineCount']-$_SESSION['skipfirstrow']; ?>)*100
$("#inner_loader").css("width",percentProgress+"%");
$("#progress_text").html(Math.round(percentProgress)+'%');
$("#progress_count").html(parseInt(csvCurrentCount));
//Loop same process if end of file not reach.
csvProcess();
}
if(msg=="done")
{
$("#inner_loader").css("width","100%");
$("#progress_text").html("100%");
}
});
}
</script >
<?php
}
//Insert ajax-call base csv-data in table.
function insert_counter_data()
{
//Open uploaded CSV file
if(file_exists($this->upload_path.$_SESSION['file_name']))
{
$handle = fopen($this->upload_path.$_SESSION['file_name'], "r");
}
else
{
echo "error";
}
//Check function is calling first time.
if((int)$_SESSION['csvlastcount']!=0){
fseek($handle,$_SESSION['csv_handle']);
}
$count=0;
// Including hooks file if it is set and exists
$has_hooks = false;
if($this->hooks_file != "")
{
$hook_file = $this->hooks_file;
if (file_exists($hook_file))
{
// Load the hooks file
include_once($hook_file);
$has_hooks = true;
}
else
{
echo "Specified Hooks file is Not found <br> " . $hook_file;
}
} // Including hooks file finished.
while(($csv_data = fgetcsv($handle, 1024, ",")) !== FALSE)
{
//Check that first-row(header line) would be skip from csv.
if(isset($_SESSION['skipfirstrow']) && $_SESSION['skipfirstrow']==1 )
{
$_SESSION['skipfirstrow']=0;
continue;
}
// Increment csv-row-count.
$_SESSION['csvlastcount']=$_SESSION['csvlastcount']+1;
//Increment count for add remaining lines to insert in database.
$count = $count+1;
$values = array();
$fieldmapp =array();
$fieldmapp = (array)$_SESSION['fieldmaps'];
//Loop to tables fields for build a insert query.
foreach($this->table_fields as $key => $field)
{
$val =$fieldmapp[$key];
if(!empty($val))
{
$values[$field] = $csv_data[$val];
}
else
{
$values[$field] = "";
}
}
if($has_hooks)
{
$values = csv_hook::process_row($values);
}
// Giving the data to be processed by the hooks.
$final_value= array();
foreach($values as $valloop)
{
$final_value[] = "'" . mysql_real_escape_string( $valloop ) . "'";
}
//Insert a csv row data in database.
$query ="INSERT INTO ".$this->table_name."(" . implode( ',' , $this->table_fields ) . ")VALUES(" . implode( ',' ,$final_value ) . ")";
mysql_query($query) or die(mysql_error());
//Set last handler pointer in Session.
$_SESSION['csv_handle']=ftell($handle);
//Check count that is reaches to set line-per-call, then loop will break.
if($count>=$this->lines_percall)
{
break;
}
}
//Check for end file reached, if not ajax return the CSV-row-count else done.
if ((!feof($handle) === true)) {
echo $_SESSION['csvlastcount'];
}
else
{
// Done will return when complete CSV-data imported in database.
echo "done";
}
// Close the handler.
fclose($handle);
}
}
?>