pkp-lib /classes/submission/SubmissionFileDAODelegate.inc.php

Language PHP Lines 372
MD5 Hash fd4ea1e5947e40823f485e440ae55712
Repository https://github.com/jalilyacob/pkp-lib.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php

/**
 * @file classes/submission/SubmissionFileDAODelegate.inc.php
 *
 * Copyright (c) 2003-2013 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class SubmissionFileDAODelegate
 * @ingroup submission
 * @see SubmissionFile
 *
 * @brief Abstract class to support DAO delegates that provide operations
 *  to retrieve and modify SubmissionFile objects.
 */

import('lib.pkp.classes.db.DAO');

class SubmissionFileDAODelegate extends DAO {
	/**
	 * Constructor
	 */
	function SubmissionFileDAODelegate() {
		parent::DAO();
	}


	//
	// Abstract public methods to be implemented by subclasses.
	//
	/**
	 * Return the name of the base submission entity
	 * (i.e. 'monograph', 'paper', 'article', etc.)
	 * @return string
	 */
	function getSubmissionEntityName() {
		return 'submission';
	}

	/**
	 * Insert a new submission file.
	 * @param $submissionFile SubmissionFile
	 * @param $sourceFile string The place where the physical file
	 *  resides right now or the file name in the case of an upload.
	 *  The file will be copied to its canonical target location.
	 * @param $isUpload boolean set to true if the file has just been
	 *  uploaded.
	 * @return SubmissionFile the inserted file
	 */
	function insertObject(&$submissionFile, $sourceFile, $isUpload = false) {
		$fileId = $submissionFile->getFileId();

		if (!is_numeric($submissionFile->getRevision())) {
			// Set the initial revision.
			$submissionFile->setRevision(1);
		}

		if (!is_bool($submissionFile->getViewable())) {
			// Set the viewable default.
			$submissionFile->setViewable(false);
		}

		$params = array(
			(int)$submissionFile->getRevision(),
			(int)$submissionFile->getSubmissionId(),
			is_null($submissionFile->getSourceFileId()) ? null : (int)$submissionFile->getSourceFileId(),
			is_null($submissionFile->getSourceRevision()) ? null : (int)$submissionFile->getSourceRevision(),
			$submissionFile->getFileType(),
			(int)$submissionFile->getFileSize(),
			$submissionFile->getOriginalFileName(),
			(int)$submissionFile->getFileStage(),
			(boolean)$submissionFile->getViewable() ? 1 : 0,
			is_null($submissionFile->getUploaderUserId()) ? null : (int)$submissionFile->getUploaderUserId(),
			is_null($submissionFile->getUserGroupId()) ? null : (int)$submissionFile->getUserGroupId(),
			is_null($submissionFile->getAssocType()) ? null : (int)$submissionFile->getAssocType(),
			is_null($submissionFile->getAssocId()) ? null : (int)$submissionFile->getAssocId(),
			is_null($submissionFile->getGenreId()) ? null : (int)$submissionFile->getGenreId(),
			$submissionFile->getDirectSalesPrice(),
			$submissionFile->getSalesType(),
		);

		if ($fileId) {
			array_unshift($params, $fileId);
		}

		$this->update(
			sprintf('INSERT INTO submission_files
				(' . ($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)
				VALUES
				(' . ($fileId ? '?, ' : '') . '?, ?, ?, ?, ?, ?, ?, ?, %s, %s, ?, ?, ?, ?, ?, ?, ?, ?)',
				$this->datetimeToDB($submissionFile->getDateUploaded()), $this->datetimeToDB($submissionFile->getDateModified())),
			$params
		);

		if (!$fileId) {
			$submissionFile->setFileId($this->_getInsertId('submission_files', 'file_id'));
		}

		$this->updateLocaleFields($submissionFile);

		// Determine the final destination of the file (requires
		// the file id we just generated).
		$targetFilePath = $submissionFile->getFilePath();

		// Only copy the file if it is not yet in the target position.
		if ($isUpload || $sourceFile != $targetFilePath) {
			// Copy the file from its current location to the target destination.
			import('lib.pkp.classes.file.FileManager');
			$fileManager = new FileManager();
			if ($isUpload) {
				$success = $fileManager->uploadFile($sourceFile, $targetFilePath);
			} else {
				assert(is_readable($sourceFile));
				$success = $fileManager->copyFile($sourceFile, $targetFilePath);
			}
			if (!$success) {
				// If the copy/upload operation fails then remove
				// the already inserted meta-data.
				$this->deleteObject($submissionFile);
				$nullVar = null;
				return $nullVar;
			}
		}
		assert(is_readable($targetFilePath));

		return $submissionFile;
	}

	/**
	 * Update a submission file.
	 * @param $submissionFile SubmissionFile The target state
	 *  of the updated file.
	 * @param $previousFile SubmissionFile The current state
	 *  of the updated file.
	 * @return boolean
	 */
	function updateObject($submissionFile, $previousFile) {
		// Update the file in the database.
		$this->update(
			sprintf('UPDATE submission_files
				SET
					file_id = ?,
					revision = ?,
					submission_id = ?,
					source_file_id = ?,
					source_revision = ?,
					file_type = ?,
					file_size = ?,
					original_file_name = ?,
					file_stage = ?,
					date_uploaded = %s,
					date_modified = %s,
					viewable = ?,
					uploader_user_id = ?,
					user_group_id = ?,
					assoc_type = ?,
					assoc_id = ?,
					genre_id = ?,
					direct_sales_price = ?,
					sales_type = ?
				WHERE file_id = ? AND revision = ?',
				$this->datetimeToDB($submissionFile->getDateUploaded()), $this->datetimeToDB($submissionFile->getDateModified())),
			array(
				(int)$submissionFile->getFileId(),
				(int)$submissionFile->getRevision(),
				(int)$submissionFile->getSubmissionId(),
				is_null($submissionFile->getSourceFileId()) ? null : (int)$submissionFile->getSourceFileId(),
				is_null($submissionFile->getSourceRevision()) ? null : (int)$submissionFile->getSourceRevision(),
				$submissionFile->getFileType(),
				$submissionFile->getFileSize(),
				$submissionFile->getOriginalFileName(),
				$submissionFile->getFileStage(),
				(boolean)$submissionFile->getViewable() ? 1 : 0,
				is_null($submissionFile->getUploaderUserId()) ? null : (int)$submissionFile->getUploaderUserId(),
				is_null($submissionFile->getUserGroupId()) ? null : (int)$submissionFile->getUserGroupId(),
				is_null($submissionFile->getAssocType()) ? null : (int)$submissionFile->getAssocType(),
				is_null($submissionFile->getAssocId()) ? null : (int)$submissionFile->getAssocId(),
				is_null($submissionFile->getGenreId()) ? null : (int)$submissionFile->getGenreId(),
				$submissionFile->getDirectSalesPrice(),
				$submissionFile->getSalesType(),
				(int)$previousFile->getFileId(),
				(int)$previousFile->getRevision(),
			)
		);

		$this->updateLocaleFields($submissionFile);

		// Update all dependent objects.
		$this->_updateDependentObjects($submissionFile, $previousFile);

		// Copy the file from its current location to the target destination
		// if necessary.
		$previousFilePath = $previousFile->getFilePath();
		$targetFilePath = $submissionFile->getFilePath();
		if ($previousFilePath != $targetFilePath && is_file($previousFilePath)) {
			// The file location changed so let's move the file on
			// the file system, too.
			assert(is_readable($previousFilePath));
			import('lib.pkp.classes.file.FileManager');
			$fileManager = new FileManager();
			if (!$fileManager->copyFile($previousFilePath, $targetFilePath)) return false;
			if (!$fileManager->deleteFile($previousFilePath)) return false;
		}

		return file_exists($targetFilePath);
	}

	/**
	 * Delete a submission file from the database.
	 * @param $submissionFile SubmissionFile
	 * @return boolean
	 */
	function deleteObject($submissionFile) {
		if (!$this->update(
			'DELETE FROM submission_files
			 WHERE file_id = ? AND revision = ?',
			array(
				(int)$submissionFile->getFileId(),
				(int)$submissionFile->getRevision()
			))) return false;

		// if we've removed the last revision of this file, clean up
		// the settings for this file as well.
		$result = $this->retrieve(
			'SELECT * FROM submission_files WHERE file_id = ?',
			array((int)$submissionFile->getFileId())
		);

		if ($result->RecordCount() == 0) {
			$this->update('DELETE FROM submission_file_settings WHERE file_id = ?',
			array((int) $submissionFile->getFileId()));
		}

		// Delete all dependent objects.
		$this->_deleteDependentObjects($submissionFile);

		// Delete the file on the file system, too.
		$filePath = $submissionFile->getFilePath();
		if(!(is_file($filePath) && is_readable($filePath))) return false;
		assert(is_writable(dirname($filePath)));

		import('lib.pkp.classes.file.FileManager');
		$fileManager = new FileManager();
		$fileManager->deleteFile($filePath);

		return !file_exists($filePath);
	}

	/**
	 * Function to return a SubmissionFile object from a row.
	 * @param $row array
	 * @return SubmissionFile
	 */
	function fromRow($row) {
		$submissionFile = $this->newDataObject();
		$submissionFile->setFileId((int)$row['submission_file_id']);
		$submissionFile->setRevision((int)$row['submission_revision']);
		$submissionFile->setAssocType(is_null($row['assoc_type']) ? null : (int)$row['assoc_type']);
		$submissionFile->setAssocId(is_null($row['assoc_id']) ? null : (int)$row['assoc_id']);
		$submissionFile->setSourceFileId(is_null($row['source_file_id']) ? null : (int)$row['source_file_id']);
		$submissionFile->setSourceRevision(is_null($row['source_revision']) ? null : (int)$row['source_revision']);
		$submissionFile->setSubmissionId((int)$row['submission_id']);
		$submissionFile->setFileStage((int)$row['file_stage']);
		$submissionFile->setOriginalFileName($row['original_file_name']);
		$submissionFile->setFileType($row['file_type']);
		$submissionFile->setGenreId(is_null($row['genre_id']) ? null : (int)$row['genre_id']);
		$submissionFile->setFileSize((int)$row['file_size']);
		$submissionFile->setUploaderUserId(is_null($row['uploader_user_id']) ? null : (int)$row['uploader_user_id']);
		$submissionFile->setUserGroupId(is_null($row['user_group_id']) ? null : (int)$row['user_group_id']);
		$submissionFile->setViewable((boolean)$row['viewable']);
		$submissionFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
		$submissionFile->setDateModified($this->datetimeFromDB($row['date_modified']));
		$submissionFile->setDirectSalesPrice($row['direct_sales_price']);
		$submissionFile->setSalesType($row['sales_type']);

		$this->getDataObjectSettings('submission_file_settings', 'file_id', $row['submission_file_id'], $submissionFile);

		return $submissionFile;
	}

	/**
	 * Construct a new data object corresponding to this DAO.
	 * @return SubmissionFile
	 */
	function newDataObject() {
		assert(false);
	}


	//
	// Protected helper methods
	//
	/**
	 * Get the list of fields for which data is localized.
	 * @return array
	 */
	function getLocaleFieldNames() {
		$localeFieldNames = parent::getLocaleFieldNames();
		$localeFieldNames[] = 'name';
		return $localeFieldNames;
	}

	/**
	 * Update the localized fields for this submission file.
	 * @param $submissionFile SubmissionFile
	 */
	function updateLocaleFields(&$submissionFile) {
		// Update the locale fields.
		$this->updateDataObjectSettings($this->getSubmissionEntityName().'_file_settings', $submissionFile, array(
			'file_id' => $submissionFile->getFileId()
		));
	}

	//
	// Private helper methods
	//
	/**
	 * Update all objects that depend on the given file.
	 * @param $submissionFile SubmissionFile
	 * @param $previousFile SubmissionFile
	 */
	function _updateDependentObjects($submissionFile, $previousFile) {
		// If the file ids didn't change then we do not have to
		// do anything.
		if (
				$previousFile->getFileId() == $submissionFile->getFileId() ||
				$previousFile->getRevision() == $submissionFile->getRevision()
		) return;

		// Update signoffs that refer to this file.
		$signoffDao = DAORegistry::getDAO('SignoffDAO'); /* @var $signoffDao SignoffDAO */
		$signoffFactory = $signoffDao->getByFileRevision(
				$previousFile->getFileId(), $previousFile->getRevision()
		);
		while ($signoff = $signoffFactory->next()) { /* @var $signoff Signoff */
			$signoff->setFileId($submissionFile->getFileId());
			$signoff->setFileRevision($submissionFile->getRevision());
			$signoffDao->updateObject($signoff);
		}

		// Update file views that refer to this file.
		$viewsDao = DAORegistry::getDAO('ViewsDAO'); /* @var $viewsDao ViewsDAO */
		$viewsDao->moveViews(
				ASSOC_TYPE_SUBMISSION_FILE,
				$previousFile->getFileIdAndRevision(), $submissionFile->getFileIdAndRevision()
		);
	}

	/**
	 * Delete all objects that depend on the given file.
	 * @param $submissionFile SubmissionFile
	 */
	function _deleteDependentObjects($submissionFile) {
		// Delete signoffs that refer to this file.
		$signoffDao = DAORegistry::getDAO('SignoffDAO'); /* @var $signoffDao SignoffDAO */
		$signoffFactory = $signoffDao->getByFileRevision(
				$submissionFile->getFileId(), $submissionFile->getRevision()
		);
		while ($signoff = $signoffFactory->next()) { /* @var $signoff Signoff */
			$signoffDao->deleteObject($signoff);
		}

		// Delete file views that refer to this file.
		$viewsDao = DAORegistry::getDAO('ViewsDAO'); /* @var $viewsDao ViewsDAO */
		$viewsDao->deleteViews(
				ASSOC_TYPE_SUBMISSION_FILE, $submissionFile->getFileIdAndRevision()
		);
	}
}

?>
Back to Top