Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
NativeImportExportPlugin.inc.php
1 <?php
2 
16 import('classes.plugins.ImportExportPlugin');
17 
23  parent::ImportExportPlugin();
24  }
25 
33  function register($category, $path) {
34  $success = parent::register($category, $path);
35  $this->addLocaleData();
36  $this->import('NativeImportExportDeployment');
37  return $success;
38  }
39 
43  function getTemplatePath($inCore = false) {
44  return parent::getTemplatePath($inCore) . 'templates/';
45  }
46 
52  function getName() {
53  return 'NativeImportExportPlugin';
54  }
55 
60  function getDisplayName() {
61  return __('plugins.importexport.native.displayName');
62  }
63 
68  function getDescription() {
69  return __('plugins.importexport.native.description');
70  }
71 
77  function display($args, $request) {
78  $templateMgr = TemplateManager::getManager($request);
79  $press = $request->getPress();
80 
81  parent::display($args, $request);
82 
83  $templateMgr->assign('plugin', $this);
84 
85  switch (array_shift($args)) {
86  case 'index':
87  case '':
88  $templateMgr->display($this->getTemplatePath() . 'index.tpl');
89  break;
90  case 'uploadImportXML':
91  $user = $request->getUser();
92  import('lib.pkp.classes.file.TemporaryFileManager');
93  $temporaryFileManager = new TemporaryFileManager();
94  $temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId());
95  if ($temporaryFile) {
96  $json = new JSONMessage(true);
97  $json->setAdditionalAttributes(array(
98  'temporaryFileId' => $temporaryFile->getId()
99  ));
100  } else {
101  $json = new JSONMessage(false, __('common.uploadFailed'));
102  }
103 
104  return $json->getString();
105  case 'importBounce':
106  $json = new JSONMessage(true);
107  $json->setEvent('addTab', array(
108  'title' => __('plugins.importexport.native.results'),
109  'url' => $request->url(null, null, null, array('plugin', $this->getName(), 'import'), array('temporaryFileId' => $request->getUserVar('temporaryFileId'))),
110  ));
111  return $json->getString();
112  case 'import':
113  $temporaryFileId = $request->getUserVar('temporaryFileId');
114  $temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO');
115  $user = $request->getUser();
116  $temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $user->getId());
117  if (!$temporaryFile) {
118  $json = new JSONMessage(true, __('plugins.inportexport.native.uploadFile'));
119  return $json->getString();
120  }
121  $temporaryFilePath = $temporaryFile->getFilePath();
122  $submissions = $this->importSubmissions(file_get_contents($temporaryFilePath), $press, $user);
123  $templateMgr->assign('submissions', $submissions);
124  $json = new JSONMessage(true, $templateMgr->fetch($this->getTemplatePath() . 'results.tpl'));
125  return $json->getString();
126  case 'export':
127  $exportXml = $this->exportSubmissions(
128  (array) $request->getUserVar('selectedSubmissions'),
129  $request->getContext(),
130  $request->getUser()
131  );
132  header('Content-type: application/xml');
133  echo $exportXml;
134  break;
135  default:
136  $dispatcher = $request->getDispatcher();
137  $dispatcher->handle404();
138  }
139  }
140 
148  function exportSubmissions($submissionIds, $context, $user) {
149  $submissionDao = Application::getSubmissionDAO();
150  $xml = '';
151  $filterDao = DAORegistry::getDAO('FilterDAO');
152  $nativeExportFilters = $filterDao->getObjectsByGroup('monograph=>native-xml');
153  assert(count($nativeExportFilters) == 1); // Assert only a single serialization filter
154  $exportFilter = array_shift($nativeExportFilters);
155  $exportFilter->setDeployment(new NativeImportExportDeployment($context, $user));
156  $submissions = array();
157  foreach ($submissionIds as $submissionId) {
158  $submission = $submissionDao->getById($submissionId, $context->getId());
159  if ($submission) $submissions[] = $submission;
160  }
161  $submissionXml = $exportFilter->execute($submissions);
162  if ($submissionXml) $xml = $submissionXml->saveXml();
163  else fatalError('Could not convert submissions.');
164  return $xml;
165  }
166 
174  function importSubmissions($importXml, $context, $user) {
175  $filterDao = DAORegistry::getDAO('FilterDAO');
176  $nativeImportFilters = $filterDao->getObjectsByGroup('native-xml=>monograph');
177  assert(count($nativeImportFilters) == 1); // Assert only a single unserialization filter
178  $importFilter = array_shift($nativeImportFilters);
179  $importFilter->setDeployment(new NativeImportExportDeployment($context, $user));
180 
181  return $importFilter->execute($importXml);
182  }
183 }
184 
185 ?>
static & getDAO($name, $dbconn=null)
Class configuring the native import/export process to this application&#39;s specifics.
Class to represent a JSON (Javascript Object Notation) message.
static getSubmissionDAO()
Native XML import/export plugin.
importSubmissions($importXml, $context, $user)
addLocaleData($locale=null)
Definition: Plugin.inc.php:347
exportSubmissions($submissionIds, $context, $user)
Abstract class for import/export plugins.