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