This repository was archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileImportPlugin.inc.php
More file actions
158 lines (132 loc) · 4.03 KB
/
FileImportPlugin.inc.php
File metadata and controls
158 lines (132 loc) · 4.03 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
<?php
/**
* @file plugins/importexport/files/FileImportPlugin.inc.php
*
* Copyright (c) 2016 Language Science Press
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class FileImportPlugin
*/
import('lib.pkp.classes.plugins.ImportExportPlugin');
class FileImportPlugin extends ImportExportPlugin {
/**
* Constructor
*/
function FileImportPlugin() {
parent::ImportExportPlugin();
}
/**
* Called as a plugin is registered to the registry
* @param $category String Name of category plugin was registered to
* @param $path string
* @return boolean True iff plugin initialized successfully; if false,
* the plugin will not be registered.
*/
function register($category, $path) {
$success = parent::register($category, $path);
$this->addLocaleData();
return $success;
}
/**
* @see Plugin::getTemplatePath($inCore)
*/
function getTemplatePath($inCore = false) {
return parent::getTemplatePath($inCore) . 'templates/';
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
function getName() {
return 'FileImportPlugin';
}
/**
* Get the display name.
* @return string
*/
function getDisplayName() {
return __('plugins.importexport.files.displayName');
}
/**
* Get the display description.
* @return string
*/
function getDescription() {
return __('plugins.importexport.files.description');
}
/**
* Display the plugin.
* @param $args array
* @param $request PKPRequest
*/
function display($args, $request) {
$templateMgr = TemplateManager::getManager($request);
$press = $request->getPress();
$context = $request->getContext();
$contextId = $context->getId();
parent::display($args, $request);
$templateMgr->assign('plugin', $this);
switch (array_shift($args)) {
case 'index':
case '':
$files = scandir(getcwd().'/public/importedFiles');
$templateMgr->assign('files',$files);
$templateMgr->assign('baseUrl',$request->getBaseUrl());
$templateMgr->display($this->getTemplatePath() . 'index.tpl');
break;
case 'uploadFile':
$user = $request->getUser();
import('lib.pkp.classes.file.TemporaryFileManager');
$temporaryFileManager = new TemporaryFileManager();
$temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId());
if ($temporaryFile) {
$json = new JSONMessage(true);
$json->setAdditionalAttributes(array(
'temporaryFileId' => $temporaryFile->getId()
));
} else {
$json = new JSONMessage(false, __('common.uploadFailed'));
}
return $json->getString();
case 'import':
// get data from file
$temporaryFileId = $request->getUserVar('temporaryFileId');
$temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO');
$user = $request->getUser();
$temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $user->getId());
if (!$temporaryFile) {
$json = new JSONMessage(true, __('plugins.inportexport.files.noTemporaryFile'));
return $json->getString();
}
$temporaryFilePath = $temporaryFile->getFilePath();
$sucess = false;
if (!file_exists(getcwd().'/public/importedFiles')) { //$request->getBaseUrl().
mkdir(getcwd().'/public/importedFiles', 0777, true);
}
$sucess = rename($temporaryFilePath,
getcwd().'/public/importedFiles/' .$temporaryFile->getOriginalFileName());
// prepare and load template
$templateMgr->assign('success',$sucess);
$templateMgr->assign('originalFileName',$temporaryFile->getOriginalFileName());
$json = new JSONMessage(true, $templateMgr->fetch($this->getTemplatePath() . 'results.tpl'));
return $json->getString();
default:
$dispatcher = $request->getDispatcher();
$dispatcher->handle404();
}
}
/**
* @copydoc ImportExportPlugin::executeCLI
*/
function executeCLI($scriptName, &$args) {
fatalError('Not implemented.');
}
/**
* @copydoc ImportExportPlugin::usage
*/
function usage($scriptName) {
fatalError('Not implemented.');
}
}
?>