Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
NativeXmlSubmissionFileFilter.inc.php
1 <?php
2 
16 import('lib.pkp.plugins.importexport.native.filter.NativeImportFilter');
17 
23  function NativeXmlSubmissionFileFilter($filterGroup) {
24  $this->setDisplayName('Native XML submission file import');
25  parent::NativeImportFilter($filterGroup);
26  }
27 
28  //
29  // Implement template methods from NativeImportFilter
30  //
35  function getPluralElementName() {
36  return 'submission_files';
37  }
38 
44  return 'submission_file';
45  }
46 
47  //
48  // Implement template methods from PersistableFilter
49  //
53  function getClassName() {
54  return 'lib.pkp.plugins.importexport.native.filter.NativeXmlSubmissionFileFilter';
55  }
56 
57 
63  function handleElement($node) {
64  $deployment = $this->getDeployment();
65  $stageName = $node->getAttribute('stage');
66  $stageNameIdMapping = $deployment->getStageNameStageIdMapping();
67  assert(isset($stageNameIdMapping[$stageName]));
68  $stageId = $stageNameIdMapping[$stageName];
69 
70  $submissionFiles = array();
71  // Handle metadata in subelements
72  for ($n = $node->firstChild; $n !== null; $n=$n->nextSibling) {
73  if (is_a($n, 'DOMElement')) {
74  $this->handleChildElement($n, $stageId, $submissionFiles);
75  }
76  }
77  return $submissionFiles;
78  }
79 
87  function handleChildElement($node, $stageId, &$submissionFiles) {
88  switch ($node->tagName) {
89  case 'revision':
90  $submissionFiles[] = $this->handleRevisionElement($node, $stageId);
91  break;
92  default:
93  fatalError('Unknown element ' . $node->tagName);
94  }
95  }
96 
102  function handleRevisionElement($node, $stageId) {
103  static $genresByContextId = array();
104 
105  $deployment = $this->getDeployment();
106  $submission = $deployment->getSubmission();
107  $context = $deployment->getContext();
108 
109  $genreName = $node->getAttribute('genre');
110  // Build a cached list of genres by context ID by name
111  if (!isset($genresByContextId[$context->getId()])) {
112  $genreDao = DAORegistry::getDAO('GenreDAO');
113  $genres = $genreDao->getByContextId($context->getId());
114  while ($genre = $genres->next()) {
115  foreach ($genre->getName(null) as $locale => $name) {
116  $genresByContextId[$context->getId()][$name] = $genre;
117  }
118  }
119  }
120  if (!isset($genresByContextId[$context->getId()][$genreName])) {
121  fatalError('Unknown genre "' . $genreName . '"!');
122  }
123  $genre = $genresByContextId[$context->getId()][$genreName];
124 
125  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
126  $submissionFile = $submissionFileDao->newDataObjectByGenreId($genre->getId());
127  $submissionFile->setSubmissionId($submission->getId());
128  $submissionFile->setGenreId($genre->getId());
129  $submissionFile->setFileStage($stageId);
130  $submissionFile->setDateUploaded(Core::getCurrentDate());
131  $submissionFile->setDateModified(Core::getCurrentDate());
132 
133  $submissionFile->setOriginalFileName($filename = $node->getAttribute('filename'));
134  for ($n = $node->firstChild; $n !== null; $n=$n->nextSibling) {
135  if (is_a($n, 'DOMElement')) {
136  $filename = $this->handleRevisionChildElement($n, $submission, $submissionFile);
137  }
138  }
139 
140  $uploaderUsername = $node->getAttribute('uploader');
141  $uploaderUserGroup = $node->getAttribute('user_group_ref');
142 
143  // Determine the user group based on the user_group_ref element.
144  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
145  $userGroups = $userGroupDao->getByContextId($context->getId());
146  while ($userGroup = $userGroups->next()) {
147  if (in_array($uploaderUserGroup, $userGroup->getName(null))) {
148  $submissionFile->setUserGroupId($userGroup->getId());
149  }
150  }
151 
152  // Do the same for the user.
153  $userDao = DAORegistry::getDAO('UserDAO');
154  $user = $userDao->getByUsername($uploaderUsername);
155  if ($user) {
156  $submissionFile->setUploaderUserId($user->getId());
157  }
158 
159  $fileSize = $node->getAttribute('filesize');
160  $submissionFile->setFileSize($fileSize);
161 
162  $fileType = $node->getAttribute('filetype');
163  $submissionFile->setFileType($fileType);
164 
165  $submissionFileDao->insertObject($submissionFile, $filename, false);
166  return $submissionFile;
167  }
168 
176  function handleRevisionChildElement($node, $submission, $submissionFile) {
177  switch ($node->tagName) {
178  case 'name':
179  $submissionFile->setName($node->textContent, $node->getAttribute('locale'));
180  break;
181  case 'remote':
182  fatalError('UNIMPLEMENTED');
183  break;
184  case 'href':
185  $submissionFile->setFileType($node->getAttribute('mime_type'));
186  // Allow wrappers to handle URLs
187  return $node->getAttribute('src');
188  break;
189  case 'embed':
190  $submissionFile->setFileType($node->getAttribute('mime_type'));
191  if (($e = $node->getAttribute('encoding')) != 'base64') {
192  fatalError('Unknown encoding "' . $e . '"!');
193  }
194  $temporaryFileManager = new TemporaryFileManager();
195  $temporaryFilename = tempnam($temporaryFileManager->getBasePath(), 'embed');
196  file_put_contents($temporaryFilename, base64_decode($node->textContent));
197  return $temporaryFilename;
198  break;
199  }
200  }
201 
207  function instantiateSubmissionFile($tagName) {
208  assert(false); // Subclasses should override
209  }
210 }
211 
212 ?>
static & getDAO($name, $dbconn=null)
Base class that converts a Native XML document to a submission file.
setDisplayName($displayName)
Definition: Filter.inc.php:140
handleChildElement($node, $stageId, &$submissionFiles)
handleRevisionChildElement($node, $submission, $submissionFile)
Base class that converts a Native XML document to a DataObject.
static getCurrentDate($ts=null)
Definition: Core.inc.php:95