Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
SubmissionFileDAODelegate.inc.php
1 <?php
2 
18 import('lib.pkp.classes.db.DAO');
19 
25  parent::DAO();
26  }
27 
28 
29  //
30  // Abstract public methods to be implemented by subclasses.
31  //
42  function insertObject($submissionFile, $sourceFile, $isUpload = false) {
43  $fileId = $submissionFile->getFileId();
44 
45  if (!is_numeric($submissionFile->getRevision())) {
46  // Set the initial revision.
47  $submissionFile->setRevision(1);
48  }
49 
50  if (!is_bool($submissionFile->getViewable())) {
51  // Set the viewable default.
52  $submissionFile->setViewable(false);
53  }
54 
55  $params = array(
56  (int)$submissionFile->getRevision(),
57  (int)$submissionFile->getSubmissionId(),
58  is_null($submissionFile->getSourceFileId()) ? null : (int)$submissionFile->getSourceFileId(),
59  is_null($submissionFile->getSourceRevision()) ? null : (int)$submissionFile->getSourceRevision(),
60  $submissionFile->getFileType(),
61  (int)$submissionFile->getFileSize(),
62  $submissionFile->getOriginalFileName(),
63  (int)$submissionFile->getFileStage(),
64  (boolean)$submissionFile->getViewable() ? 1 : 0,
65  is_null($submissionFile->getUploaderUserId()) ? null : (int)$submissionFile->getUploaderUserId(),
66  is_null($submissionFile->getUserGroupId()) ? null : (int)$submissionFile->getUserGroupId(),
67  is_null($submissionFile->getAssocType()) ? null : (int)$submissionFile->getAssocType(),
68  is_null($submissionFile->getAssocId()) ? null : (int)$submissionFile->getAssocId(),
69  is_null($submissionFile->getGenreId()) ? null : (int)$submissionFile->getGenreId(),
70  $submissionFile->getDirectSalesPrice(),
71  $submissionFile->getSalesType(),
72  );
73 
74  if ($fileId) {
75  array_unshift($params, $fileId);
76  }
77 
78  $this->update(
79  sprintf('INSERT INTO submission_files
80  (' . ($fileId ? 'file_id, ' : '') . 'revision, submission_id, source_file_id, source_revision, file_type, file_size, original_file_name, file_stage, date_uploaded, date_modified, viewable, uploader_user_id, user_group_id, assoc_type, assoc_id, genre_id, direct_sales_price, sales_type)
81  VALUES
82  (' . ($fileId ? '?, ' : '') . '?, ?, ?, ?, ?, ?, ?, ?, %s, %s, ?, ?, ?, ?, ?, ?, ?, ?)',
83  $this->datetimeToDB($submissionFile->getDateUploaded()), $this->datetimeToDB($submissionFile->getDateModified())),
84  $params
85  );
86 
87  if (!$fileId) {
88  $submissionFile->setFileId($this->_getInsertId('submission_files', 'file_id'));
89  }
90 
91  $this->updateLocaleFields($submissionFile);
92 
93  // Determine the final destination of the file (requires
94  // the file id we just generated).
95  $targetFilePath = $submissionFile->getFilePath();
96 
97  // Only copy the file if it is not yet in the target position.
98  if ($isUpload || $sourceFile != $targetFilePath) {
99  // Copy the file from its current location to the target destination.
100  import('lib.pkp.classes.file.FileManager');
101  $fileManager = new FileManager();
102  if ($isUpload) {
103  $success = $fileManager->uploadFile($sourceFile, $targetFilePath);
104  } else {
105  assert(is_readable($sourceFile));
106  $success = $fileManager->copyFile($sourceFile, $targetFilePath);
107  }
108  if (!$success) {
109  // If the copy/upload operation fails then remove
110  // the already inserted meta-data.
111  $this->deleteObject($submissionFile);
112  $nullVar = null;
113  return $nullVar;
114  }
115  }
116  assert(is_readable($targetFilePath));
117 
118  return $submissionFile;
119  }
120 
129  function updateObject($submissionFile, $previousFile) {
130  // Update the file in the database.
131  $this->update(
132  sprintf('UPDATE submission_files
133  SET
134  file_id = ?,
135  revision = ?,
136  submission_id = ?,
137  source_file_id = ?,
138  source_revision = ?,
139  file_type = ?,
140  file_size = ?,
141  original_file_name = ?,
142  file_stage = ?,
143  date_uploaded = %s,
144  date_modified = %s,
145  viewable = ?,
146  uploader_user_id = ?,
147  user_group_id = ?,
148  assoc_type = ?,
149  assoc_id = ?,
150  genre_id = ?,
151  direct_sales_price = ?,
152  sales_type = ?
153  WHERE file_id = ? AND revision = ?',
154  $this->datetimeToDB($submissionFile->getDateUploaded()), $this->datetimeToDB($submissionFile->getDateModified())),
155  array(
156  (int)$submissionFile->getFileId(),
157  (int)$submissionFile->getRevision(),
158  (int)$submissionFile->getSubmissionId(),
159  is_null($submissionFile->getSourceFileId()) ? null : (int)$submissionFile->getSourceFileId(),
160  is_null($submissionFile->getSourceRevision()) ? null : (int)$submissionFile->getSourceRevision(),
161  $submissionFile->getFileType(),
162  $submissionFile->getFileSize(),
163  $submissionFile->getOriginalFileName(),
164  $submissionFile->getFileStage(),
165  (boolean)$submissionFile->getViewable() ? 1 : 0,
166  is_null($submissionFile->getUploaderUserId()) ? null : (int)$submissionFile->getUploaderUserId(),
167  is_null($submissionFile->getUserGroupId()) ? null : (int)$submissionFile->getUserGroupId(),
168  is_null($submissionFile->getAssocType()) ? null : (int)$submissionFile->getAssocType(),
169  is_null($submissionFile->getAssocId()) ? null : (int)$submissionFile->getAssocId(),
170  is_null($submissionFile->getGenreId()) ? null : (int)$submissionFile->getGenreId(),
171  $submissionFile->getDirectSalesPrice(),
172  $submissionFile->getSalesType(),
173  (int)$previousFile->getFileId(),
174  (int)$previousFile->getRevision(),
175  )
176  );
177 
178  $this->updateLocaleFields($submissionFile);
179 
180  // Update all dependent objects.
181  $this->_updateDependentObjects($submissionFile, $previousFile);
182 
183  // Copy the file from its current location to the target destination
184  // if necessary.
185  $previousFilePath = $previousFile->getFilePath();
186  $targetFilePath = $submissionFile->getFilePath();
187  if ($previousFilePath != $targetFilePath && is_file($previousFilePath)) {
188  // The file location changed so let's move the file on
189  // the file system, too.
190  assert(is_readable($previousFilePath));
191  import('lib.pkp.classes.file.FileManager');
192  $fileManager = new FileManager();
193  if (!$fileManager->copyFile($previousFilePath, $targetFilePath)) return false;
194  if (!$fileManager->deleteFile($previousFilePath)) return false;
195  }
196 
197  return file_exists($targetFilePath);
198  }
199 
205  function deleteObject($submissionFile) {
206  if (!$this->update(
207  'DELETE FROM submission_files
208  WHERE file_id = ? AND revision = ?',
209  array(
210  (int)$submissionFile->getFileId(),
211  (int)$submissionFile->getRevision()
212  ))) return false;
213 
214  // if we've removed the last revision of this file, clean up
215  // the settings for this file as well.
216  $result = $this->retrieve(
217  'SELECT * FROM submission_files WHERE file_id = ?',
218  array((int)$submissionFile->getFileId())
219  );
220 
221  if ($result->RecordCount() == 0) {
222  $this->update('DELETE FROM submission_file_settings WHERE file_id = ?',
223  array((int) $submissionFile->getFileId()));
224  }
225 
226  // Delete all dependent objects.
227  $this->_deleteDependentObjects($submissionFile);
228 
229  // Delete the file on the file system, too.
230  $filePath = $submissionFile->getFilePath();
231  if(!(is_file($filePath) && is_readable($filePath))) return false;
232  assert(is_writable(dirname($filePath)));
233 
234  import('lib.pkp.classes.file.FileManager');
235  $fileManager = new FileManager();
236  $fileManager->deleteFile($filePath);
237 
238  return !file_exists($filePath);
239  }
240 
246  function fromRow($row) {
247  $submissionFile = $this->newDataObject();
248  $submissionFile->setFileId((int)$row['submission_file_id']);
249  $submissionFile->setRevision((int)$row['submission_revision']);
250  $submissionFile->setAssocType(is_null($row['assoc_type']) ? null : (int)$row['assoc_type']);
251  $submissionFile->setAssocId(is_null($row['assoc_id']) ? null : (int)$row['assoc_id']);
252  $submissionFile->setSourceFileId(is_null($row['source_file_id']) ? null : (int)$row['source_file_id']);
253  $submissionFile->setSourceRevision(is_null($row['source_revision']) ? null : (int)$row['source_revision']);
254  $submissionFile->setSubmissionId((int)$row['submission_id']);
255  $submissionFile->setFileStage((int)$row['file_stage']);
256  $submissionFile->setOriginalFileName($row['original_file_name']);
257  $submissionFile->setFileType($row['file_type']);
258  $submissionFile->setGenreId(is_null($row['genre_id']) ? null : (int)$row['genre_id']);
259  $submissionFile->setFileSize((int)$row['file_size']);
260  $submissionFile->setUploaderUserId(is_null($row['uploader_user_id']) ? null : (int)$row['uploader_user_id']);
261  $submissionFile->setUserGroupId(is_null($row['user_group_id']) ? null : (int)$row['user_group_id']);
262  $submissionFile->setViewable((boolean)$row['viewable']);
263  $submissionFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
264  $submissionFile->setDateModified($this->datetimeFromDB($row['date_modified']));
265  $submissionFile->setDirectSalesPrice($row['direct_sales_price']);
266  $submissionFile->setSalesType($row['sales_type']);
267 
268  $this->getDataObjectSettings('submission_file_settings', 'file_id', $row['submission_file_id'], $submissionFile);
269 
270  return $submissionFile;
271  }
272 
277  function newDataObject() {
278  assert(false);
279  }
280 
281 
282  //
283  // Protected helper methods
284  //
289  function getLocaleFieldNames() {
290  $localeFieldNames = parent::getLocaleFieldNames();
291  $localeFieldNames[] = 'name';
292  return $localeFieldNames;
293  }
294 
299  function updateLocaleFields($submissionFile) {
300  // Update the locale fields.
301  $this->updateDataObjectSettings('submission_file_settings', $submissionFile, array(
302  'file_id' => $submissionFile->getFileId()
303  ));
304  }
305 
306  //
307  // Private helper methods
308  //
314  function _updateDependentObjects($submissionFile, $previousFile) {
315  // If the file ids didn't change then we do not have to
316  // do anything.
317  if (
318  $previousFile->getFileId() == $submissionFile->getFileId() ||
319  $previousFile->getRevision() == $submissionFile->getRevision()
320  ) return;
321 
322  // Update signoffs that refer to this file.
323  $signoffDao = DAORegistry::getDAO('SignoffDAO'); /* @var $signoffDao SignoffDAO */
324  $signoffFactory = $signoffDao->getByFileRevision(
325  $previousFile->getFileId(), $previousFile->getRevision()
326  );
327  while ($signoff = $signoffFactory->next()) { /* @var $signoff Signoff */
328  $signoff->setFileId($submissionFile->getFileId());
329  $signoff->setFileRevision($submissionFile->getRevision());
330  $signoffDao->updateObject($signoff);
331  }
332 
333  // Update file views that refer to this file.
334  $viewsDao = DAORegistry::getDAO('ViewsDAO'); /* @var $viewsDao ViewsDAO */
335  $viewsDao->moveViews(
336  ASSOC_TYPE_SUBMISSION_FILE,
337  $previousFile->getFileIdAndRevision(), $submissionFile->getFileIdAndRevision()
338  );
339  }
340 
345  function _deleteDependentObjects($submissionFile) {
346  // Delete signoffs that refer to this file.
347  $signoffDao = DAORegistry::getDAO('SignoffDAO'); /* @var $signoffDao SignoffDAO */
348  $signoffFactory = $signoffDao->getByFileRevision(
349  $submissionFile->getFileId(), $submissionFile->getRevision()
350  );
351  while ($signoff = $signoffFactory->next()) { /* @var $signoff Signoff */
352  $signoffDao->deleteObject($signoff);
353  }
354 
355  // Delete file views that refer to this file.
356  $viewsDao = DAORegistry::getDAO('ViewsDAO'); /* @var $viewsDao ViewsDAO */
357  $viewsDao->deleteViews(
358  ASSOC_TYPE_SUBMISSION_FILE, $submissionFile->getFileIdAndRevision()
359  );
360  }
361 }
362 
363 ?>
static & getDAO($name, $dbconn=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
Abstract class to support DAO delegates that provide operations to retrieve and modify SubmissionFile...
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:457
Class defining basic operations for file management.
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
datetimeFromDB($dt)
Definition: DAO.inc.php:313
updateObject($submissionFile, $previousFile)
_updateDependentObjects($submissionFile, $previousFile)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
insertObject($submissionFile, $sourceFile, $isUpload=false)