-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_import.admin.inc
1243 lines (1002 loc) · 37.5 KB
/
user_import.admin.inc
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Provide administration configuration pages to import users.
*/
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_confirm_delete($form, &$form_state, $import_id) {
$form['import_id'] = array(
'#type' => 'value',
'#value' => $import_id,
);
return confirm_form($form,
t('Are you sure you want to delete the user import process?'),
'admin/people/user_import',
t('Deleting a user import process will also delete the file from which user data was being read. This action cannot be undone.'),
t('Delete'), t('Cancel'));
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_confirm_delete_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
user_import_delete($form_state['values']['import_id']);
}
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_list($action = NULL, $import_id = NULL) {
// clear incomplete imports
_user_import_incomplete_deletion();
if (!empty($import_id) && is_numeric($import_id)) {
$pager_id = 1;
$limit = 10;
$rows = array();
$import = _user_import_settings_select($import_id);
$total = db_query('SELECT count(data) FROM {user_import_errors} WHERE import_id = :import_id', array(':import_id' => $import_id))->fetchField();
// Select table
$query = db_select('user_import_errors', 'imp_usr_errors');
// Select fields
$query->fields('imp_usr_errors', array('import_id', 'data', 'errors'));
// Set conditions.
$query->condition('import_id', $import['import_id']);
// For pagination
$query = $query->extend('TableSort')->extend('PagerDefault')->limit($limit);
// Execute query
$result = $query->execute();
foreach ($result as $line) {
$rows[] = array(
'import_id' => $line->import_id,
'data' => unserialize($line->data),
'errors' => unserialize($line->errors),
);
}
$output = theme('user_import_errors_display', array('import' => $import, 'file_lines' => $rows, 'total' => $total));
}
else {
$output = theme('user_import_list');
}
return $output;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_preferences($import_id = NULL, $template_id = NULL) {
if (empty($import_id)) {
$private_path = config_get('system.core', 'file_private_path');
// Private path hasn't been set, show a message instead of the form.
if (!$private_path) {
backdrop_set_message(t('!path needs to be set before users can be imported.', array('!path' => l('Private file system path', 'admin/config/media/file-system', array('query' => array('destination' => 'admin/people/user_import/add'))))), 'warning');
return ' ';
}
$output = backdrop_get_form('user_import_add_form');
}
else {
$output = backdrop_get_form('user_import_edit', $import_id, $template_id);
}
return $output;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_confirm_continue($form, &$form_state, $import_id) {
$form['import_id'] = array(
'#type' => 'value',
'#value' => $import_id,
);
return confirm_form($form,
t('Are you sure you want to continue to import users?'),
'admin/people/user_import',
t('Restart user import from where it last finished.'),
t('Continue'), t('Cancel'));
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_confirm_continue_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
user_import_continue($form_state['values']['import_id']);
}
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_continue($import_id = NULL) {
if (!empty($import_id) && is_numeric($import_id)) {
module_load_include('inc', 'user_import', 'user_import.import');
$import = _user_import_settings_select($import_id);
_user_import_process($import);
}
backdrop_goto('admin/people/user_import');
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_confirm_import($form, &$form_state, $import_id) {
$form['import_id'] = array(
'#type' => 'value',
'#value' => $import_id,
);
return confirm_form($form,
t('Are you sure you want to import users?'),
'admin/people/user_import',
t('Start importing users.'),
t('Import'), t('Cancel'));
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_confirm_import_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
user_import_import($form_state['values']['import_id']);
}
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_import($import_id = NULL) {
if (!empty($import_id) && is_numeric($import_id)) {
$import = _user_import_settings_select($import_id);
_user_import_initialise_import($import);
}
backdrop_goto('admin/people/user_import');
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_delete($import_id = NULL, $return_path = 'admin/people/user_import') {
if (empty($import_id) || !is_numeric($import_id)) {
backdrop_goto($return_path);
}
$import = _user_import_settings_select($import_id);
_user_import_settings_deletion($import_id);
//_user_import_file_deletion($import['filepath'], $import['filename'], $import['oldfilename'], $import['ftp']);
file_unmanaged_delete($import['filepath']);
backdrop_goto($return_path);
return;
}
/**
* Configuration form define (settings affect all user imports)
*/
function user_import_configure_form() {
$config = config('user_import.settings');
$form['general'] = array(
'#type' => 'fieldset',
'#title' => t('General'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['general']['delimiter'] = array(
'#type' => 'textfield',
'#title' => t('Delimiter'),
'#size' => 4,
'#default_value' => $config->get('delimiter'),
'#required' => TRUE,
'#description' => t("The default column delimiter. Use '\\t' for Tab."),
);
$form['general']['encoding'] = array(
'#type' => 'select',
'#title' => t('Character Encoding'),
'#options' => array(
'UTF-8' => t('UTF-8'),
'Windows-1252' => t('Windows-1252'),
),
'#default_value' => $config->get('encoding'),
'#required' => TRUE,
'#description' => t('Select the default encoding.'),
);
$form['selectable_files'] = array(
'#type' => 'fieldset',
'#title' => t('Uploads Directory'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
if ($config->get('selectable_files') == 0) {
$selectable_files_description = t('This option provides a directory where files can be uploaded, and then selected when setting up an import.');
}
else {
$selectable_files_description = t('');
}
$form['selectable_files']['selectable_files'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Uploads Directory'),
'#description' => $selectable_files_description,
'#default_value' => $config->get('selectable_files'),
);
$form['auto_imports'] = array(
'#type' => 'fieldset',
'#title' => t('Automated Imports'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['auto_imports']['auto_imports_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Automated Imports'),
'#description' => t('Each import template will have the option to create a directory which will be scanned for any files that have been uploaded to it,
and when a file is found it will automatically be used to create new user accounts. Directories are scanned durring !cron runs.', array('!cron' => l(t('cron'), 'admin/config/system/cron'))),
'#default_value' => $config->get('auto_imports_enabled'),
);
$form['performance'] = array(
'#type' => 'fieldset',
'#title' => t('Performance'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['performance']['user_import_max'] = array(
'#type' => 'number',
'#title' => t('Maximum Users/Process'),
'#default_value' => $config->get('user_import_max'),
'#size' => 10,
'#min' => 10,
'#maxlength' => 10,
'#description' => t('Maximum number of users to import each time the file is processed, useful for controlling the rate at which emails are sent out.'),
);
$form['performance']['line_max'] = array(
'#type' => 'number',
'#title' => t('Maximum length of line'),
'#default_value' => $config->get('line_max'),
'#size' => 10,
'#min' => 1000,
'#max' => 1000000,
'#maxlength' => 10,
'#description' => t('The default is set at 1,000 characters, if a line in your csv is longer than this you should set a higher maximum here. Setting higher maximums will slow down imports.'),
);
$saved_templates = _user_import_settings_select(NULL, TRUE);
if (!empty($saved_templates)) {
$form['settings_templates'] = array(
'#type' => 'fieldset',
'#title' => t('Settings Templates'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$templates_list = array('-- none --');
foreach ($saved_templates as $template) {
$templates_list[$template['import_id']] = $template['name'];
$templates_delete[$template['import_id']] = $template['name'];
}
$form['settings_templates']['default_settings'] = array(
'#type' => 'select',
'#title' => t('Default Settings'),
'#description' => t('Select if you want to use a previously saved set of settings as default for all imports.'),
'#default_value' => $config->get('default_settings'),
'#options' => $templates_list,
);
$form['settings_templates']['templates'] = array(
'#type' => 'checkboxes',
'#title' => t('Delete Templates'),
'#options' => $templates_delete,
);
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_configure_form_validate($form, &$form_state) {
$config = config('system.core');
// Check private files path is set.
if (!empty($form_state['values']['auto_imports_enabled'])) {
$file_private_path = $config->get('file_private_path');
if (!$file_private_path) {
form_set_error('auto_imports_enabled', t('The !private_file_path must be set to use this feature.', array('!private_file_path' => l(t('Private file system path'), 'admin/config/media/file-system'))));
}
}
// Check private files path is set.
if (!empty($form_state['values']['selectable_files'])) {
$file_private_path = $config->get('file_private_path');
if (!$file_private_path) {
form_set_error('selectable_files', t('The !private_file_path must be set to use the Uploads Directory feature.', array('!private_file_path' => l(t('Private file system path'), 'admin/config/media/file-system'))));
}
}
return;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_configure_form_submit($form, &$form_state) {
$deleted = '';
$config = config('user_import.settings');
$config->set('delimiter', $form_state['values']['delimiter']);
$config->set('encoding', $form_state['values']['encoding']);
$config->set('user_import_max', $form_state['values']['user_import_max']);
$config->set('line_max', $form_state['values']['line_max']);
$default_settings = isset($form_state['values']['default_settings']) ? $form_state['values']['default_settings'] : 0;
$config->set('default_settings', $default_settings);
$config->set('selectable_files', $form_state['values']['selectable_files']);
$config->set('auto_imports_enabled', $form_state['values']['auto_imports_enabled']);
$config->save();
// Create a directory for processing incoming files if auto imports are enabled.
if (!empty($form_state['values']['auto_imports_enabled'])) {
user_import_create_directory('processing', '');
}
// Create a directory for processing incoming files if auto imports are enabled.
if (!empty($form_state['values']['selectable_files'])) {
user_import_create_directory('selectable');
}
if (!empty($form_state['values']['templates'])) {
foreach ($form_state['values']['templates'] as $import_id) {
if (!empty($import_id)) {
$template = _user_import_settings_select($import_id);
if (!empty($deleted)) {
$deleted .= ', ';
}
$deleted .= $template['name'];
_user_import_settings_deletion($import_id);
}
}
}
if (!empty($deleted)) {
backdrop_set_message(t('Settings templates deleted: @deleted', array('@deleted' => $deleted)));
}
backdrop_set_message(t('Configuration settings have been saved.'));
$form_state['redirect'] = 'admin/people/user_import';
}
/**
* Start new import.
* Form to select file.
**/
function user_import_add_form($import_id = NULL) {
$config = config('user_import.settings');
$form = array();
$ftp_files = array();
if ($config->get('selectable_files') == 1) {
$ftp_files = _user_import_ftp_files();
}
$form['browser'] = array(
'#type' => 'fieldset',
'#title' => t('Browser Upload'),
'#collapsible' => TRUE,
'#description' => t("Upload a CSV file."),
);
if (function_exists('file_upload_max_size')) {
$file_size = t('Maximum file size: @size.', array('@size' => format_size(file_upload_max_size())));
}
$form['browser']['file_upload'] = array(
'#type' => 'file',
'#title' => t('CSV File'),
'#size' => 40,
'#description' => check_plain(t('Select the CSV file to be imported.') . ' ' . $file_size),
);
if (!empty($ftp_files)) {
$form['ftp'] = array(
'#type' => 'fieldset',
'#title' => t('FTP Upload'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t("Any files uploaded to the 'user_import' directory using FTP can be selected for import here. Useful if the import file is too large for upload via the browser."),
);
$form['ftp']['file_ftp'] = array(
'#type' => 'radios',
'#title' => t('Files'),
'#default_value' => '0',
'#options' => $ftp_files,
);
// reload the page to show any files that have been added by FTP
$form['ftp']['scan'] = array(
'#type' => 'submit',
'#value' => t('Check for new files'),
'#validate' => array(),
'#submit' => array(),
);
}
$form['file_settings'] = array(
'#type' => 'fieldset',
'#title' => t('File Settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#description' => t("File column delimiter"),
);
$form['file_settings']['delimiter'] = array(
'#type' => 'textfield',
'#title' => t('Delimiter'),
'#size' => 4,
'#default_value' => $config->get('delimiter'),
'#required' => TRUE,
'#description' => t("The column delimiter for the file. Use '/t' for Tab."),
);
$form['file_settings']['encoding'] = array(
'#type' => 'select',
'#title' => t('Character Encoding'),
'#options' => array(
'UTF-8' => t('UTF-8'),
'Windows-1252' => t('Windows-1252'),
),
'#default_value' => $config->get('encoding'),
'#required' => TRUE,
'#description' => t('Select the encoding used in the file.'),
);
$settings = _user_import_settings_select(NULL, TRUE);
if ($settings) {
$saved_settings = array(t('-- none --'));
foreach ($settings as $settings_set) {
$saved_settings[$settings_set['import_id']] = $settings_set['name'];
}
$form['import_template_select'] = array(
'#type' => 'select',
'#title' => t('Saved Settings'),
'#description' => t('Select if you want to use a previously saved set of settings.'),
'#default_value' => $config->get('default_settings'),
'#options' => $saved_settings,
);
}
$form['actions']['#type'] = 'actions';
$form['actions']['next'] = array(
'#type' => 'submit',
'#value' => t('Next'),
);
// Set form parameters so we can accept file uploads.
$form['#attributes'] = array('enctype' => 'multipart/form-data');
return $form;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_add_form_validate($form, &$form_state) {
if (isset($form_state['values']['file_ftp']) && !empty($form_state['values']['file_ftp'])) {
$file_ftp = $form_state['values']['file_ftp'];
}
else {
$file_ftp = FALSE;
}
$file = _user_import_file(NULL, $file_ftp);
// check file uploaded OK
if (empty($file->filename)) {
form_set_error('file_upload', t('A file must be uploaded or selected from FTP updates.'));
}
else {
$form_state['values']['file_info'] = $file;
}
return;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_add_form_submit($form, &$form_state) {
$redirect = 'admin/people/user_import/add/';
if (isset($form_state['values']['file_ftp']) && !empty($form_state['values']['file_ftp'])) {
$file_ftp = $form_state['values']['file_ftp'];
}
else {
$file_ftp = FALSE;
}
$file = _user_import_file(NULL, $file_ftp);
$file_name = user_import_move_file_for_processing($file->uri, $file->filename);
$form_state['values']['ftp'] = $file_ftp;
$form_state['values']['filename'] = $file_name;
$form_state['values']['oldfilename'] = $file->filename;
$form_state['values']['filepath'] = 'private://user_import/processing/' . $file_name;
$form_state['values']['setting'] = 'file set';
// create import setting
$import = _user_import_settings_save($form_state['values']);
$redirect .= $import['import_id'];
if (!empty($form_state['values']['import_template_select'])) {
$redirect .= '/' . check_plain($form_state['values']['import_template_select']);
}
$form_state['redirect'] = $redirect;
}
function user_import_edit($form, $form_state, $import_id, $template_id = NULL) {
// load code for supported modules
user_import_load_supported();
$form = array();
$import = _user_import_settings_select($import_id);
$import['template_id'] = $template_id;
$form['ftp'] = array(
'#type' => 'value',
'#value' => $import['options']['ftp'],
);
// add setting template values
if ($import['setting'] == 'file set') {
$import = _user_import_initialise_import($import);
}
$form['import_id'] = array(
'#type' => 'value',
'#value' => $import_id,
);
$form['setting'] = array(
'#type' => 'value',
'#value' => $import['setting'],
);
$form['return_path'] = array(
'#type' => 'value',
'#default_value' => 'admin/people/user_import',
);
$form['og_id'] = array(
'#type' => 'value',
'#default_value' => 0,
);
// don't use hook because these need to be added in this order;
user_import_edit_file_fields($form, $import);
user_import_form_field_match($form, $import);
$collapsed = (empty($import['name'])) ? FALSE : TRUE;
$additional_fieldsets = module_invoke_all('user_import_form_fieldset', $import, $collapsed);
if (is_array($additional_fieldsets)) {
$form = $form + $additional_fieldsets;
}
$update_user = module_invoke_all('user_import_form_update_user');
if (is_array($update_user)) {
$form['update_user'] = array(
'#type' => 'fieldset',
'#title' => t('Update Existing Users'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
foreach ($update_user as $module => $display) {
$options = array(
UPDATE_NONE => t('No Update'),
UPDATE_REPLACE => t('Replace Data'),
UPDATE_ADD => t('Add Data'),
);
if (isset($display['exclude_add']) && $display['exclude_add'] == TRUE) {
unset($options[UPDATE_ADD]);
}
if (isset($display['exclude_replace']) && $display['exclude_replace'] == TRUE) {
unset($options[UPDATE_REPLACE]);
}
$form['update_user'][$module] = array(
'#type' => 'radios',
'#title' => $display['title'],
'#options' => $options,
'#default_value' => empty($import['options']['update_user'][$module]) ? UPDATE_NONE : $import['options']['update_user'][$module],
'#description' => $display['description'],
);
}
}
$form['actions']['#type'] = 'actions';
// don't show test option if import has started
if ($import['setting'] != 'import' && $import['setting'] != 'imported') {
$form['actions']['test'] = array(
'#type' => 'submit',
'#value' => t('Test'),
'#weight' => 100,
'#submit' => array('user_import_test_submit', 'user_import_edit_submit'),
);
}
$form['actions']['import'] = array(
'#type' => 'submit',
'#value' => t('Import'),
'#weight' => 100,
'#submit' => array('user_import_import_submit', 'user_import_edit_submit'),
);
$form['actions']['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#weight' => 100,
'#validate' => array('user_import_edit_cancel_validate'),
);
return $form;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_edit_cancel_validate($form, &$form_state) {
// if import was being added - delete file
if ($form_state['values']['setting'] == 'file set') {
$settings = _user_import_settings_select($form_state['values']['import_id']);
_user_import_settings_deletion($form_state['values']['import_id']);
// _user_import_file_deletion($settings['filepath'], $settings['filename'], $settings['oldfilename'], $settings['options']['ftp']);
file_unmanaged_delete($settings['filepath']);
}
$form_state['redirect'] = 'admin/people/user_import';
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_edit_validate($form, &$form_state) {
$email = FALSE;
$fields = array();
foreach ($form_state['values']['field_match'] as $row => $values) {
// check each field is unique
if ($values['field_match'] != '0' && $values['field_match'] != '-------------' && in_array($values['field_match'], $fields)) {
form_set_error('field_match', t('Database fields can only be matched to one column of the csv file.'));
}
$fields[$values['field_match']] = $values['field_match'];
// check email address has been selected
if ($values['field_match'] == 'user-email') {
$email = TRUE;
}
}
if (!$email) {
form_set_error('email', t('One column of the csv file must be set as the email address.'));
}
if ($form_state['values']['name']) {
$form_state['values']['name'] = rtrim($form_state['values']['name']);
if (backdrop_strlen($form_state['values']['name']) < 1 || backdrop_strlen($form_state['values']['name']) > 25) {
form_set_error('name', t('Name of saved settings must be 25 characters or less.'));
}
}
// Check auto uploads directory is not already in use by another template.
if (!empty($form_state['values']['auto_import_directory'])) {
$auto_import_directory = 'private://user_import/uploads/' . $form_state['values']['auto_import_directory'];
if (file_prepare_directory($auto_import_directory, FILE_MODIFY_PERMISSIONS) || $form_state['values']['auto_import_directory'] == 'selectable') {
form_set_error('auto_import_directory', t("Directory '%directory' already exists and can not be used.", array('%directory' => $form_state['values']['auto_import_directory'])));
}
}
return;
}
/**
* Save a new template.
*/
function user_import_template_new_submit($form, &$form_state) {
// save settings for import
_user_import_settings_save($form_state['values']);
// save settings for template
$import_id = $form_state['values']['import_id'];
$form_state['values']['setting'] = 'template';
unset($form_state['values']['import_id']);
if (isset($form_state['values']['auto_import_directory']) && !empty($form_state['values']['auto_import_directory'])) {
user_import_create_directory($form_state['values']['auto_import_directory']);
}
_user_import_initialise_import($form_state['values']);
backdrop_set_message(t("'%name' was saved as a settings template.", array('%name' => $form_state['values']['name'])));
// reload settings page
$form_state['redirect'] = 'admin/people/user_import/add/' . $import_id;
return;
}
/**
* Update an existing template.
*/
function user_import_template_update_submit($form, &$form_state) {
// save settings for import
$import_id = $form_state['values']['import_id'];
_user_import_settings_save($form_state['values']);
// get template details
$template_id = db_query_range("SELECT import_id from {user_import} where setting = 'template' AND name = :name", 0, 1, array(':name' => $form['#current_template']))->fetchField();
// save settings for template
$form_state['values']['setting'] = 'template';
$form_state['values']['import_id'] = $template_id;
$form_state['values']['name'] = $form['#current_template'];
_user_import_initialise_import($form_state['values']);
backdrop_set_message(t("'%name' settings template was updated.", array('%name' => $form['#current_template'])));
// reload settings page
$form_state['redirect'] = 'admin/people/user_import/add/' . $import_id;
return;
}
/**
*
*/
function user_import_test_submit($form, &$form_state) {
$form_state['values']['setting'] = 'test';
backdrop_set_message(t('Tested'));
}
/**
*
*/
function user_import_import_submit($form, &$form_state) {
$form_state['values']['setting'] = 'import';
backdrop_set_message(t('Imported'));
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_import_move_file_for_processing($source, $file_name) {
// Make sure processing directory exists.
user_import_create_directory(NULL, 'processing');
$destination = 'private://user_import/processing/' . $file_name;
$uri = file_unmanaged_move($source, $destination, FILE_EXISTS_RENAME);
$file_name = substr($uri, 33);
return $file_name;
}
/**
*
*/
function user_import_edit_submit($form, &$form_state) {
// Deal with import being canceled.
if ($form_state['clicked_button']['#value'] == t('Cancel')) {
$form_state['redirect'] = 'admin/people/user_import';
return;
}
// Load import functions.
module_load_include('inc', 'user_import', 'user_import.import');
if ($form_state['values']['setting'] == 'file set') {
// $file = new stdClass();
// $file->uri = $form_state['values']['filepath'];
// $file->filename = $form_state['values']['filename'];
//$filepath = file_move($file, file_directory_path() . '/' . $form_state['values']['filename']);
$filepath = user_import_move_file_for_processing($form_state['values']['filepath']);
}
if (!empty($form_state['values']['og_id'])) {
$form_state['values']['groups'][$form_state['values']['og_id']] = $form_state['values']['og_id'];
}
$form_state['values']['ftp'] = $form_state['values']['ftp'];
$form_state['values'] = _user_import_settings_save($form_state['values']);
$form_state['values']['save']['update'] = NULL;
$form_state['values']['import_template_id'] = NULL;
$form_state['values']['save']['name'] = NULL;
$form_state['redirect'] = $form_state['values']['return_path'];
_user_import_process($form_state['values']);
}
function user_import_edit_file_fields(&$form, $import) {
$form['filename'] = array(
'#type' => 'value',
'#value' => $import['filename'],
);
$form['oldfilename'] = array(
'#type' => 'value',
'#value' => $import['oldfilename'],
);
$form['filepath'] = array(
'#type' => 'value',
'#value' => $import['filepath'],
);
return;
}
// - - - - - - - - FILE HANDLING - - - - - - - -
/**
* open file
*/
function _user_import_file_open($filepath, $filename) {
ini_set('auto_detect_line_endings', TRUE);
$handle = @fopen($filepath, "r");
if (!$handle) {
form_set_error('file', t("Could not find the csv file '%filename'", array('%filename' => $filename)));
return t("Please add your file again.");
}
return $handle;
}
/**
*
* File being used
* $import_id - use file info stored in database
* $ftp_file - chosen from FTP uploaded files
* $uploaded_file - uploaded through browser
*
*/
function _user_import_file($import_id = NULL, $ftp_file_selected = NULL) {
static $file;
if (!empty($file)) {
return $file;
}
// File was uploaded through browser.
if (empty($ftp_file_selected) && empty($import_id)) {
// Delete record from database management so we don't have duplicates problems.
db_delete('file_managed')->condition('uri', 'temporary://' . $_FILES['files']['name']['file_upload'])->execute();
$file = file_save_upload('file_upload', array('file_validate_extensions' => array('csv txt')), FALSE, FILE_EXISTS_RENAME);
if (!empty($file)) {
return $file;
}
}
// File was uploaded by FTP
if (!empty($ftp_file_selected)) {
$ftp_files = file_scan_directory('private://user_import/uploads/selectable', '/.*$/', array('key' => 'filename'));
$file = $ftp_files[$ftp_file_selected];
$file->filepath = $file->uri;
return $file;
}
// Use file info stored in database
if (!empty($import_id)) {
$import = _user_import_settings_select($import_id);
$file->uri = $import['filepath'];
$file->filepath = $import['filepath'];
$file->oldfilename = $import['oldfilename'];
$file->filename = $import['filename'];
return $file;
}
return;
}
/**
* get info on files uploaded via FTP
*/