-
Notifications
You must be signed in to change notification settings - Fork 0
/
emfu-file-upload.php
389 lines (308 loc) · 10.8 KB
/
emfu-file-upload.php
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
<?php
/*
Plugin Name: Events Manager - File Upload
Version: 1.2.2
Plugin URI: https://github.com/EelcoA/event-manager-file-import
Description: A non-official 'Events Manager' add-on to import events from a CSV file.
Author: Eelco Aartsen, AESSET IT
Author URI: https://www.aesset.nl
*/
include("emfu-functions.php");
// TODO: internationalization
global $wpdb;
// Hook the 'admin_menu' action hook, run the function named
add_action('admin_menu', 'emfu_Add_My_Admin_Link');
// Add a new top level menu link to the ACP
function emfu_Add_My_Admin_Link()
{
add_menu_page(
'Import Events', // Title of the page
'Import Events', // Text to show on the menu link
'manage_options', // Capability requirement to see the link
'events-manager-file-upload/index.php' // The 'slug' - file to display when clicking the link
);
}
// Make JSRender javascript library available
add_action( 'admin_enqueue_scripts', 'emfu_add_JSRender' );
function emfu_add_JSRender(){
wp_enqueue_script('emfu_jsrender',
'https://cdnjs.cloudflare.com/ajax/libs/jsrender/1.0.8/jsrender.min.js');
}
// Make the javascript file available for admin pages
add_action( 'admin_enqueue_scripts', 'emfu_scripts' );
function emfu_scripts() {
wp_register_script('ajaxHandle',
plugin_dir_url( __FILE__ ) . '/js/emfu-file-upload.js',
array('jquery'),
false,
true);
wp_localize_script(
'ajaxHandle',
'ajax_object',
array( 'ajaxurl' => admin_url('admin-ajax.php') )
);
wp_enqueue_script('ajaxHandle');
}
/*
* upload the files, read content and return as json data
*/
add_action('wp_ajax_emfu_file_upload', 'emfu_file_upload_callback');
add_action('wp_ajax_nopriv_emfu_file_upload','emfu_file_upload_callback');
function emfu_file_upload_callback() {
$result = [];
if(!isset($_FILES)){
$result['status'] = "FAILURE";
$result['message'] = "No file choosen";
echo json_encode($result);
wp_die();
}
if(empty($_FILES['file'])) {
$result['status'] = "FAILURE";
$result['message'] = "No file choosen";
echo json_encode($result);
wp_die();
}
$uploadResult = emfu_file_upload($_FILES['file']);
if($uploadResult['status']!="SUCCESS"){
$result['status'] = "FAILURE";
$result['message'] = 'Error while uploading file: ' . $uploadResult['message'];
echo json_encode($result);
wp_die();
}
$emfu_events = emfu_get_events_from_file($uploadResult['file']);
$data = json_encode($emfu_events);
$result['status'] = "SUCCESS";
$result['message'] = 'File has been successfully uploaded';
$result['data'] = $data;
echo json_encode($result);
wp_die();
}
/**
* Process the file into rows with events to display on the screen
* @param $filename
* @return array
*
* TODO: make it monkey-proof for invalid file and file types
*/
function emfu_get_events_from_file($filename){
$events = array();
$rown_number = 0;
// Open the file for reading
if (($event_file = fopen("{$filename}", "r")) !== FALSE)
{
// Each line in the file is converted into an individual array that we call $data
// The items of the array are comma separated
while (($rowdata_array = fgetcsv($event_file, 3000, ",")) !== FALSE)
{
// add rownumber to the row data
array_unshift($rowdata_array, $rown_number);
// Each individual array is being pushed into the nested array
$events[] = $rowdata_array;
$rown_number+=1;
}
fclose($event_file);
}
return $events;
}
/*
* Callback function for importing events
*/
add_action('wp_ajax_emfu_import_events', 'emfu_import_events_callback');
add_action('wp_ajax_nopriv_emfu_import_events','emfu_import_events_callback');
function emfu_import_events_callback(){
$result = [];
// First check the incoming data, expecting an entry 'events' with String data in $_POST
if(!isset($_POST)){
$result['status'] = "FAILURE";
$result['message'] = "Programming error, no $_POST";
echo json_encode($result);
wp_die();
}
if(empty($_POST['events'])) {
$result['status'] = "FAILURE";
$result['message'] = "Programming error, no 'events' in $_POST";
echo json_encode($result);
wp_die();
}
$events_to_import_json = $_POST['events'];
$datatype = gettype($events_to_import_json);
if(!$datatype=="string"){
$result['status'] = "FAILURE";
$result['message'] = 'Programming error: events_to_import_json is not a string but a '.$datatype;
echo json_encode($result);
wp_die();
}
// Strip the JSON from redundant slashes and decode it
$events_to_import_json_with_correct_html = stripslashes($events_to_import_json);
$events_to_import = json_decode($events_to_import_json_with_correct_html);
// Check if the decoded JSON gave us a non-empty Array with Arrays
$datatype = gettype($events_to_import);
if(!$datatype=='array'){
$result['status'] = "FAILURE";
$result['message'] = 'Programming error: events_to_import is not an array but a '.$datatype;
echo json_encode($result);
wp_die();
}
if(sizeof($events_to_import)==0){
$result['status'] = "FAILURE";
$result['message'] = 'Programming error: events_to_import is an empty array, size='.sizeof($events_to_import);
echo json_encode($result);
wp_die();
}
if(!gettype($events_to_import[0])=='array'){
$result['status'] = "FAILURE";
$result['message'] = 'Programming error: events_to_import[0] is not an arrays';
echo json_encode($result);
wp_die();
}
// Call the importing function which returns the results in an Array of Arrays
$import_result = emfu_import_events($events_to_import);
// When no SUCCESS, report the error
if($import_result['status']!="SUCCESS"){
$result['status'] = "FAILURE";
$result['message'] = 'Error while importing events: ' . $import_result['message'];
echo json_encode($result);
wp_die();
}
$imported_events = $import_result['result_details'];
// Some checking of things that shouldn't happen, but you never know
if(!gettype($imported_events)=='array'){
$result['status'] = "FAILURE";
$result['message'] = 'Programming error: imported_events is not an array';
echo json_encode($result);
wp_die();
}
if(sizeof($imported_events)==0){
$result['status'] = "FAILURE";
$result['message'] = 'Programming error: imported_events is an empty array';
echo json_encode($result);
wp_die();
}
if(!gettype($imported_events[0])=='array'){
$result['status'] = "FAILURE";
$result['message'] = 'Programming error: imported_events[0] is not an arrays';
echo json_encode($result);
wp_die();
}
// Encode the resulting array and return it
$data = json_encode($imported_events);
$result['status'] = "SUCCESS";
$result['message'] = 'Events have been successfully imported into the database';
$result['data'] = $data;
echo json_encode($result);
wp_die();
}
/*
* Import events into the database and return an array with result data back
*/
function emfu_import_events($event_rows){
$result = array();
// Check the number of columns
$row0 = $event_rows[0];
if(sizeof($row0)!=10){
if(sizeof($row0)!=0){
$num_columns=sizeof($row0)-1;
} else {
$num_columns=0;
}
$result['status'] = "FAILURE";
$result['message'] = 'File should contain 9 columns but it has ' . $num_columns . ' columns';
return $result;
}
$result_details = [];
$row_nr = 0;
foreach ($event_rows as $event_row) {
/*
* Add the smaller fields (meaning all except post_excerpt and post_content)
* to the result row to display later including the result
* (= message about event creation or error).
*/
$result_row = array($event_row[0], // rown nr
$event_row[1], // event_start_date
$event_row[2], // event_start_time
$event_row[3], // event_end_date
$event_row[4], // event_end_time
$event_row[5], // event_name
$event_row[8], // location_slug
$event_row[9]); // category-slug
try {
// skip first row with headers
if ($row_nr == 0) {
$result_row[] = "Result";
} else {
$event = emfu_create_EM_Event_from_row($event_row);
if (emfu_event_exists($event)) {
throw new Exception( "Event already exists with that location/title/date/time." );
}
// Save the Event
if ( ! $event->save() ) {
throw new Exception( 'Something went wrong saving your event.' );
}
// Success, so add that to the array to display in the results!
$result_row[] = "Event created.";
}
} catch (Exception $e){
// Add an error message to display in the results
$result_row[] = "Error creating event:<br><strong>" . $e->getMessage() . "</strong>";
} finally {
$result_details[] = $result_row;
$row_nr += 1;
}
}
$result['status'] = "SUCCESS";
$result['message'] = "Event import succeeded.";
$result['result_details'] = $result_details;
return $result;
}
/*
* file upload
*/
function emfu_file_upload($file) {
$result = array();
$file_name = $file['name'];
$source_path = $file['tmp_name'];
$file_type = $file['type'];
$file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
$target_file_name = $file_name;
$plugin_dir = plugin_dir_path( __FILE__ );
$target_dir = $plugin_dir . "uploaded/";
$target_filepath = $target_dir.$target_file_name;
// ------------ [ File Validation ] --------------------------
if($file['size'] > 2048000) {
$result['status'] = "FAILED";
$result['message'] = "File size is larger than 2 MB";
return $result;
}
// ------------- [ Check and/or create output dir ] ---------------
if (!is_dir($target_dir))
mkdir($target_dir, 0755, true);
// ------------- [ Empty output dir ] ------------------------------
$existing_files = glob($target_dir . "*");
foreach($existing_files as $existing_file){
if(is_file($existing_file))
unlink($existing_file); // delete file
}
// ------------------ [ File upload ] ---------------
if(move_uploaded_file($source_path, $target_filepath)) {
$result['status'] = "SUCCESS";
$result['message'] = "File uploaded successfully to: ".$target_filepath ;
$result['file'] = $target_filepath;
} else {
$result['status'] = "FAILED";
$result['message'] = "File uploaded failed.";
$result['file'] = $target_filepath;
}
return $result;
}
/*
* Return a String representation form an Array with Arrays
*/
function subArraysToString($ar, $sep = ', ') {
$str = '';
foreach ($ar as $val) {
$str .= implode($sep, $val);
$str .= $sep; // add separator between sub-arrays
}
$str = rtrim($str, $sep); // remove last separator
return $str;
}