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  //
38  return 'submission';
39  }
40 
51  function insertObject(&$submissionFile, $sourceFile, $isUpload = false) {
52  $fileId = $submissionFile->getFileId();
53 
54  if (!is_numeric($submissionFile->getRevision())) {
55  // Set the initial revision.
56  $submissionFile->setRevision(1);
57  }
58 
59  if (!is_bool($submissionFile->getViewable())) {
60  // Set the viewable default.
61  $submissionFile->setViewable(false);
62  }
63 
64  $params = array(
65  (int)$submissionFile->getRevision(),
66  (int)$submissionFile->getSubmissionId(),
67  is_null($submissionFile->getSourceFileId()) ? null : (int)$submissionFile->getSourceFileId(),
68  is_null($submissionFile->getSourceRevision()) ? null : (int)$submissionFile->getSourceRevision(),
69  $submissionFile->getFileType(),
70  (int)$submissionFile->getFileSize(),
71  $submissionFile->getOriginalFileName(),
72  (int)$submissionFile->getFileStage(),
73  (boolean)$submissionFile->getViewable() ? 1 : 0,
74  is_null($submissionFile->getUploaderUserId()) ? null : (int)$submissionFile->getUploaderUserId(),
75  is_null($submissionFile->getUserGroupId()) ? null : (int)$submissionFile->getUserGroupId(),
76  is_null($submissionFile->getAssocType()) ? null : (int)$submissionFile->getAssocType(),
77  is_null($submissionFile->getAssocId()) ? null : (int)$submissionFile->getAssocId(),
78  is_null($submissionFile->getGenreId()) ? null : (int)$submissionFile->getGenreId(),
79  $submissionFile->getDirectSalesPrice(),
80  $submissionFile->getSalesType(),
81  );
82 
83  if ($fileId) {
84  array_unshift($params, $fileId);
85  }
86 
87  $this->update(
88  sprintf('INSERT INTO submission_files
89  (' . ($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)
90  VALUES
91  (' . ($fileId ? '?, ' : '') . '?, ?, ?, ?, ?, ?, ?, ?, %s, %s, ?, ?, ?, ?, ?, ?, ?, ?)',
92  $this->datetimeToDB($submissionFile->getDateUploaded()), $this->datetimeToDB($submissionFile->getDateModified())),
93  $params
94  );
95 
96  if (!$fileId) {
97  $submissionFile->setFileId($this->_getInsertId('submission_files', 'file_id'));
98  }
99 
100  $this->updateLocaleFields($submissionFile);
101 
102  // Determine the final destination of the file (requires
103  // the file id we just generated).
104  $targetFilePath = $submissionFile->getFilePath();
105 
106  // Only copy the file if it is not yet in the target position.
107  if ($isUpload || $sourceFile != $targetFilePath) {
108  // Copy the file from its current location to the target destination.
109  import('lib.pkp.classes.file.FileManager');
110  $fileManager = new FileManager();
111  if ($isUpload) {
112  $success = $fileManager->uploadFile($sourceFile, $targetFilePath);
113  } else {
114  assert(is_readable($sourceFile));
115  $success = $fileManager->copyFile($sourceFile, $targetFilePath);
116  }
117  if (!$success) {
118  // If the copy/upload operation fails then remove
119  // the already inserted meta-data.
120  $this->deleteObject($submissionFile);
121  $nullVar = null;
122  return $nullVar;
123  }
124  }
125  assert(is_readable($targetFilePath));
126 
127  return $submissionFile;
128  }
129 
138  function updateObject($submissionFile, $previousFile) {
139  // Update the file in the database.
140  $this->update(
141  sprintf('UPDATE submission_files
142  SET
143  file_id = ?,
144  revision = ?,
145  submission_id = ?,
146  source_file_id = ?,
147  source_revision = ?,
148  file_type = ?,
149  file_size = ?,
150  original_file_name = ?,
151  file_stage = ?,
152  date_uploaded = %s,
153  date_modified = %s,
154  viewable = ?,
155  uploader_user_id = ?,
156  user_group_id = ?,
157  assoc_type = ?,
158  assoc_id = ?,
159  genre_id = ?,
160  direct_sales_price = ?,
161  sales_type = ?
162  WHERE file_id = ? AND revision = ?',
163  $this->datetimeToDB($submissionFile->getDateUploaded()), $this->datetimeToDB($submissionFile->getDateModified())),
164  array(
165  (int)$submissionFile->getFileId(),
166  (int)$submissionFile->getRevision(),
167  (int)$submissionFile->getSubmissionId(),
168  is_null($submissionFile->getSourceFileId()) ? null : (int)$submissionFile->getSourceFileId(),
169  is_null($submissionFile->getSourceRevision()) ? null : (int)$submissionFile->getSourceRevision(),
170  $submissionFile->getFileType(),
171  $submissionFile->getFileSize(),
172  $submissionFile->getOriginalFileName(),
173  $submissionFile->getFileStage(),
174  (boolean)$submissionFile->getViewable() ? 1 : 0,
175  is_null($submissionFile->getUploaderUserId()) ? null : (int)$submissionFile->getUploaderUserId(),
176  is_null($submissionFile->getUserGroupId()) ? null : (int)$submissionFile->getUserGroupId(),
177  is_null($submissionFile->getAssocType()) ? null : (int)$submissionFile->getAssocType(),
178  is_null($submissionFile->getAssocId()) ? null : (int)$submissionFile->getAssocId(),
179  is_null($submissionFile->getGenreId()) ? null : (int)$submissionFile->getGenreId(),
180  $submissionFile->getDirectSalesPrice(),
181  $submissionFile->getSalesType(),
182  (int)$previousFile->getFileId(),
183  (int)$previousFile->getRevision(),
184  )
185  );
186 
187  $this->updateLocaleFields($submissionFile);
188 
189  // Update all dependent objects.
190  $this->_updateDependentObjects($submissionFile, $previousFile);
191 
192  // Copy the file from its current location to the target destination
193  // if necessary.
194  $previousFilePath = $previousFile->getFilePath();
195  $targetFilePath = $submissionFile->getFilePath();
196  if ($previousFilePath != $targetFilePath && is_file($previousFilePath)) {
197  // The file location changed so let's move the file on
198  // the file system, too.
199  assert(is_readable($previousFilePath));
200  import('lib.pkp.classes.file.FileManager');
201  $fileManager = new FileManager();
202  if (!$fileManager->copyFile($previousFilePath, $targetFilePath)) return false;
203  if (!$fileManager->deleteFile($previousFilePath)) return false;
204  }
205 
206  return file_exists($targetFilePath);
207  }
208 
214  function deleteObject($submissionFile) {
215  if (!$this->update(
216  'DELETE FROM submission_files
217  WHERE file_id = ? AND revision = ?',
218  array(
219  (int)$submissionFile->getFileId(),
220  (int)$submissionFile->getRevision()
221  ))) return false;
222 
223  // if we've removed the last revision of this file, clean up
224  // the settings for this file as well.
225  $result = $this->retrieve(
226  'SELECT * FROM submission_files WHERE file_id = ?',
227  array((int)$submissionFile->getFileId())
228  );
229 
230  if ($result->RecordCount() == 0) {
231  $this->update('DELETE FROM submission_file_settings WHERE file_id = ?',
232  array((int) $submissionFile->getFileId()));
233  }
234 
235  // Delete all dependent objects.
236  $this->_deleteDependentObjects($submissionFile);
237 
238  // Delete the file on the file system, too.
239  $filePath = $submissionFile->getFilePath();
240  if(!(is_file($filePath) && is_readable($filePath))) return false;
241  assert(is_writable(dirname($filePath)));
242 
243  import('lib.pkp.classes.file.FileManager');
244  $fileManager = new FileManager();
245  $fileManager->deleteFile($filePath);
246 
247  return !file_exists($filePath);
248  }
249 
255  function fromRow($row) {
256  $submissionFile = $this->newDataObject();
257  $submissionFile->setFileId((int)$row['submission_file_id']);
258  $submissionFile->setRevision((int)$row['submission_revision']);
259  $submissionFile->setAssocType(is_null($row['assoc_type']) ? null : (int)$row['assoc_type']);
260  $submissionFile->setAssocId(is_null($row['assoc_id']) ? null : (int)$row['assoc_id']);
261  $submissionFile->setSourceFileId(is_null($row['source_file_id']) ? null : (int)$row['source_file_id']);
262  $submissionFile->setSourceRevision(is_null($row['source_revision']) ? null : (int)$row['source_revision']);
263  $submissionFile->setSubmissionId((int)$row['submission_id']);
264  $submissionFile->setFileStage((int)$row['file_stage']);
265  $submissionFile->setOriginalFileName($row['original_file_name']);
266  $submissionFile->setFileType($row['file_type']);
267  $submissionFile->setGenreId(is_null($row['genre_id']) ? null : (int)$row['genre_id']);
268  $submissionFile->setFileSize((int)$row['file_size']);
269  $submissionFile->setUploaderUserId(is_null($row['uploader_user_id']) ? null : (int)$row['uploader_user_id']);
270  $submissionFile->setUserGroupId(is_null($row['user_group_id']) ? null : (int)$row['user_group_id']);
271  $submissionFile->setViewable((boolean)$row['viewable']);
272  $submissionFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
273  $submissionFile->setDateModified($this->datetimeFromDB($row['date_modified']));
274  $submissionFile->setDirectSalesPrice($row['direct_sales_price']);
275  $submissionFile->setSalesType($row['sales_type']);
276 
277  $this->getDataObjectSettings('submission_file_settings', 'file_id', $row['submission_file_id'], $submissionFile);
278 
279  return $submissionFile;
280  }
281 
286  function newDataObject() {
287  assert(false);
288  }
289 
290 
291  //
292  // Protected helper methods
293  //
298  function getLocaleFieldNames() {
299  $localeFieldNames = parent::getLocaleFieldNames();
300  $localeFieldNames[] = 'name';
301  return $localeFieldNames;
302  }
303 
308  function updateLocaleFields(&$submissionFile) {
309  // Update the locale fields.
310  $this->updateDataObjectSettings($this->getSubmissionEntityName().'_file_settings', $submissionFile, array(
311  'file_id' => $submissionFile->getFileId()
312  ));
313  }
314 
315  //
316  // Private helper methods
317  //
323  function _updateDependentObjects($submissionFile, $previousFile) {
324  // If the file ids didn't change then we do not have to
325  // do anything.
326  if (
327  $previousFile->getFileId() == $submissionFile->getFileId() ||
328  $previousFile->getRevision() == $submissionFile->getRevision()
329  ) return;
330 
331  // Update signoffs that refer to this file.
332  $signoffDao = DAORegistry::getDAO('SignoffDAO'); /* @var $signoffDao SignoffDAO */
333  $signoffFactory = $signoffDao->getByFileRevision(
334  $previousFile->getFileId(), $previousFile->getRevision()
335  );
336  while ($signoff = $signoffFactory->next()) { /* @var $signoff Signoff */
337  $signoff->setFileId($submissionFile->getFileId());
338  $signoff->setFileRevision($submissionFile->getRevision());
339  $signoffDao->updateObject($signoff);
340  }
341 
342  // Update file views that refer to this file.
343  $viewsDao = DAORegistry::getDAO('ViewsDAO'); /* @var $viewsDao ViewsDAO */
344  $viewsDao->moveViews(
345  ASSOC_TYPE_SUBMISSION_FILE,
346  $previousFile->getFileIdAndRevision(), $submissionFile->getFileIdAndRevision()
347  );
348  }
349 
354  function _deleteDependentObjects($submissionFile) {
355  // Delete signoffs that refer to this file.
356  $signoffDao = DAORegistry::getDAO('SignoffDAO'); /* @var $signoffDao SignoffDAO */
357  $signoffFactory = $signoffDao->getByFileRevision(
358  $submissionFile->getFileId(), $submissionFile->getRevision()
359  );
360  while ($signoff = $signoffFactory->next()) { /* @var $signoff Signoff */
361  $signoffDao->deleteObject($signoff);
362  }
363 
364  // Delete file views that refer to this file.
365  $viewsDao = DAORegistry::getDAO('ViewsDAO'); /* @var $viewsDao ViewsDAO */
366  $viewsDao->deleteViews(
367  ASSOC_TYPE_SUBMISSION_FILE, $submissionFile->getFileIdAndRevision()
368  );
369  }
370 }
371 
372 ?>
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...
insertObject(&$submissionFile, $sourceFile, $isUpload=false)
& 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