-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workbook.php
142 lines (125 loc) · 4.42 KB
/
Workbook.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
<?php
class WMN_Form_Workbook extends WMN_Form_Admin {
protected $capability = 'import';
protected $slug = 'workbook';
public function __construct() {
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
add_action( 'admin_menu', [ $this, 'add_menu_option' ] );
add_action( 'wp_ajax_wmn_import_nodelist', [ $this, 'import_nodelist' ] );
add_action( 'wp_ajax_wmn_reset_nodelist', [ $this, 'reset_nodelist' ] );
}
public function add_menu_option() {
if ( current_user_can( $this->capability ) ) {
$page = __( 'Import Nodelist', 'wmn-workbook' );
$menu = __( 'Import Nodelist', 'wmn-workbook' );
$func = array( $this, 'show_import_form' );
$this->hook_suffix = add_management_page( $page, $menu, $this->capability, $this->slug, $func );
}
}
public function admin_enqueue_scripts( $hook ) {
$paths = wmn_paths();
wp_enqueue_media();
wp_enqueue_style( 'wmn-form-workbook.css', $paths->get_plugin_file_uri( 'css/form-workbook.css' ), null, $paths->version );
wp_enqueue_script( 'wmn-form-workbook.js', $paths->get_plugin_file_uri( 'js/form-workbook.js' ), [ 'jquery' ], $paths->version, true );
}
protected function form_layout( $form = array() ) {
return $form;
}
public function show_import_form() { ?>
<h1 class="centered">
<?php esc_html_e( 'Import Master Node List', 'wmn-workbook' ); ?>
</h1>
<form method='post'>
<p id="file_status" class="centered">No file selected</p>
<div id="file_log" class="centered">
</div>
<div>
<div class="centered">
<input id="upload_nodelist_button" type="button" class="button" value="<?php _e( 'Choose file to import', 'wmn-workbook' ); ?>" />
</div>
<div class="pull-right">
<input id="reset_nodelist_button" type="button" class="button" value="<?php _e( 'Reset nodelist', 'wmn-workbook' ); ?>" />
</div>
</div>
</form>
<div>
<?php #phpinfo(); ?>
</div><?php
}
public function import_nodelist() {
session_start();
require_once( wmn_paths()->dir . 'vendor/autoload.php' );
if ( isset( $_SESSION['import_nodelist'] ) ) {
$data = $_SESSION['import_nodelist'];
$data['index'] = $_POST['start_index'];
} else {
$data = array(
'count' => 0,
'file' => get_attached_file( $_POST['attachment_id'] ), // full path
'index' => 0,
'names' => array(),
);
}
$import = new WMN_Query_Nodelist;
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$reader->setReadDataOnly( true );
if ( empty( $data['names'] ) ) {
$data['names'] = $reader->listWorksheetNames( $data['file'] );
$data['count'] = count( $data['names'] );
}
$had_error = false;
$skipped = false;
$sheet_name = $data['names'][$data['index']];
$worksheets = $reader->listWorksheetInfo( $data['file'] );
if ( $worksheets[ $data['index'] ]['totalRows'] === 0 ) {
$skipped = true;
} else {
$reader->setLoadSheetsOnly( $sheet_name );
$spreadsheet = $reader->load( $data['file'] );
$sheet_data = $spreadsheet->getActiveSheet()->toArray( null, false, false, false );
$results = $import->import( $sheet_data );
if ( empty( $results ) ) {
$had_error = true;
}
}
$_SESSION['import_nodelist'] = $data;
$response = array(
'status' => 'success',
'index' => $data['index'],
'type' => 'complete',
'message' => '<p>Master Nodelist successfully imported.</p>',
);
if ( $had_error ) {
$response['status'] = 'error';
$response['message'] = "ERROR: Worksheet $sheet_name was not imported. Operation aborted.";
unset( $_SESSION['import_nodelist'] );
} else if ( $skipped ) {
$response['type'] = 'incomplete';
$response['message'] = "Worksheet $sheet_name skipped.";
} else if ( ( $data['index'] + 1 ) < $data['count'] ) {
$response['type'] = 'incomplete';
$response['message'] = "Worksheet $sheet_name imported.";
if ( $results['dups'] > 0 ) {
$response['message'] .= " {$results['dups']} records skipped.";
}
if ( $results['new'] > 0 ) {
$response['message'] .= " {$results['new']} records imported.";
}
} else {
unset( $_SESSION['import_nodelist'] );
}
echo json_encode( $response );
wp_die();
}
public function reset_nodelist() {
$import = new WMN_Query_Nodelist;
$import->destroy();
$import->create();
$response = array(
'status' => 'success',
'message' => __( 'Master nodelist has been reset.', 'wmn-workbook' )
);
echo json_encode( $response );
wp_die();
}
}