PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/submission/SubmissionFileDAODelegate.inc.php

https://github.com/jalilyacob/pkp-lib
PHP | 371 lines | 226 code | 41 blank | 104 comment | 25 complexity | fd4ea1e5947e40823f485e440ae55712 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file classes/submission/SubmissionFileDAODelegate.inc.php
  4. *
  5. * Copyright (c) 2003-2013 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class SubmissionFileDAODelegate
  9. * @ingroup submission
  10. * @see SubmissionFile
  11. *
  12. * @brief Abstract class to support DAO delegates that provide operations
  13. * to retrieve and modify SubmissionFile objects.
  14. */
  15. import('lib.pkp.classes.db.DAO');
  16. class SubmissionFileDAODelegate extends DAO {
  17. /**
  18. * Constructor
  19. */
  20. function SubmissionFileDAODelegate() {
  21. parent::DAO();
  22. }
  23. //
  24. // Abstract public methods to be implemented by subclasses.
  25. //
  26. /**
  27. * Return the name of the base submission entity
  28. * (i.e. 'monograph', 'paper', 'article', etc.)
  29. * @return string
  30. */
  31. function getSubmissionEntityName() {
  32. return 'submission';
  33. }
  34. /**
  35. * Insert a new submission file.
  36. * @param $submissionFile SubmissionFile
  37. * @param $sourceFile string The place where the physical file
  38. * resides right now or the file name in the case of an upload.
  39. * The file will be copied to its canonical target location.
  40. * @param $isUpload boolean set to true if the file has just been
  41. * uploaded.
  42. * @return SubmissionFile the inserted file
  43. */
  44. function insertObject(&$submissionFile, $sourceFile, $isUpload = false) {
  45. $fileId = $submissionFile->getFileId();
  46. if (!is_numeric($submissionFile->getRevision())) {
  47. // Set the initial revision.
  48. $submissionFile->setRevision(1);
  49. }
  50. if (!is_bool($submissionFile->getViewable())) {
  51. // Set the viewable default.
  52. $submissionFile->setViewable(false);
  53. }
  54. $params = array(
  55. (int)$submissionFile->getRevision(),
  56. (int)$submissionFile->getSubmissionId(),
  57. is_null($submissionFile->getSourceFileId()) ? null : (int)$submissionFile->getSourceFileId(),
  58. is_null($submissionFile->getSourceRevision()) ? null : (int)$submissionFile->getSourceRevision(),
  59. $submissionFile->getFileType(),
  60. (int)$submissionFile->getFileSize(),
  61. $submissionFile->getOriginalFileName(),
  62. (int)$submissionFile->getFileStage(),
  63. (boolean)$submissionFile->getViewable() ? 1 : 0,
  64. is_null($submissionFile->getUploaderUserId()) ? null : (int)$submissionFile->getUploaderUserId(),
  65. is_null($submissionFile->getUserGroupId()) ? null : (int)$submissionFile->getUserGroupId(),
  66. is_null($submissionFile->getAssocType()) ? null : (int)$submissionFile->getAssocType(),
  67. is_null($submissionFile->getAssocId()) ? null : (int)$submissionFile->getAssocId(),
  68. is_null($submissionFile->getGenreId()) ? null : (int)$submissionFile->getGenreId(),
  69. $submissionFile->getDirectSalesPrice(),
  70. $submissionFile->getSalesType(),
  71. );
  72. if ($fileId) {
  73. array_unshift($params, $fileId);
  74. }
  75. $this->update(
  76. sprintf('INSERT INTO submission_files
  77. (' . ($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)
  78. VALUES
  79. (' . ($fileId ? '?, ' : '') . '?, ?, ?, ?, ?, ?, ?, ?, %s, %s, ?, ?, ?, ?, ?, ?, ?, ?)',
  80. $this->datetimeToDB($submissionFile->getDateUploaded()), $this->datetimeToDB($submissionFile->getDateModified())),
  81. $params
  82. );
  83. if (!$fileId) {
  84. $submissionFile->setFileId($this->_getInsertId('submission_files', 'file_id'));
  85. }
  86. $this->updateLocaleFields($submissionFile);
  87. // Determine the final destination of the file (requires
  88. // the file id we just generated).
  89. $targetFilePath = $submissionFile->getFilePath();
  90. // Only copy the file if it is not yet in the target position.
  91. if ($isUpload || $sourceFile != $targetFilePath) {
  92. // Copy the file from its current location to the target destination.
  93. import('lib.pkp.classes.file.FileManager');
  94. $fileManager = new FileManager();
  95. if ($isUpload) {
  96. $success = $fileManager->uploadFile($sourceFile, $targetFilePath);
  97. } else {
  98. assert(is_readable($sourceFile));
  99. $success = $fileManager->copyFile($sourceFile, $targetFilePath);
  100. }
  101. if (!$success) {
  102. // If the copy/upload operation fails then remove
  103. // the already inserted meta-data.
  104. $this->deleteObject($submissionFile);
  105. $nullVar = null;
  106. return $nullVar;
  107. }
  108. }
  109. assert(is_readable($targetFilePath));
  110. return $submissionFile;
  111. }
  112. /**
  113. * Update a submission file.
  114. * @param $submissionFile SubmissionFile The target state
  115. * of the updated file.
  116. * @param $previousFile SubmissionFile The current state
  117. * of the updated file.
  118. * @return boolean
  119. */
  120. function updateObject($submissionFile, $previousFile) {
  121. // Update the file in the database.
  122. $this->update(
  123. sprintf('UPDATE submission_files
  124. SET
  125. file_id = ?,
  126. revision = ?,
  127. submission_id = ?,
  128. source_file_id = ?,
  129. source_revision = ?,
  130. file_type = ?,
  131. file_size = ?,
  132. original_file_name = ?,
  133. file_stage = ?,
  134. date_uploaded = %s,
  135. date_modified = %s,
  136. viewable = ?,
  137. uploader_user_id = ?,
  138. user_group_id = ?,
  139. assoc_type = ?,
  140. assoc_id = ?,
  141. genre_id = ?,
  142. direct_sales_price = ?,
  143. sales_type = ?
  144. WHERE file_id = ? AND revision = ?',
  145. $this->datetimeToDB($submissionFile->getDateUploaded()), $this->datetimeToDB($submissionFile->getDateModified())),
  146. array(
  147. (int)$submissionFile->getFileId(),
  148. (int)$submissionFile->getRevision(),
  149. (int)$submissionFile->getSubmissionId(),
  150. is_null($submissionFile->getSourceFileId()) ? null : (int)$submissionFile->getSourceFileId(),
  151. is_null($submissionFile->getSourceRevision()) ? null : (int)$submissionFile->getSourceRevision(),
  152. $submissionFile->getFileType(),
  153. $submissionFile->getFileSize(),
  154. $submissionFile->getOriginalFileName(),
  155. $submissionFile->getFileStage(),
  156. (boolean)$submissionFile->getViewable() ? 1 : 0,
  157. is_null($submissionFile->getUploaderUserId()) ? null : (int)$submissionFile->getUploaderUserId(),
  158. is_null($submissionFile->getUserGroupId()) ? null : (int)$submissionFile->getUserGroupId(),
  159. is_null($submissionFile->getAssocType()) ? null : (int)$submissionFile->getAssocType(),
  160. is_null($submissionFile->getAssocId()) ? null : (int)$submissionFile->getAssocId(),
  161. is_null($submissionFile->getGenreId()) ? null : (int)$submissionFile->getGenreId(),
  162. $submissionFile->getDirectSalesPrice(),
  163. $submissionFile->getSalesType(),
  164. (int)$previousFile->getFileId(),
  165. (int)$previousFile->getRevision(),
  166. )
  167. );
  168. $this->updateLocaleFields($submissionFile);
  169. // Update all dependent objects.
  170. $this->_updateDependentObjects($submissionFile, $previousFile);
  171. // Copy the file from its current location to the target destination
  172. // if necessary.
  173. $previousFilePath = $previousFile->getFilePath();
  174. $targetFilePath = $submissionFile->getFilePath();
  175. if ($previousFilePath != $targetFilePath && is_file($previousFilePath)) {
  176. // The file location changed so let's move the file on
  177. // the file system, too.
  178. assert(is_readable($previousFilePath));
  179. import('lib.pkp.classes.file.FileManager');
  180. $fileManager = new FileManager();
  181. if (!$fileManager->copyFile($previousFilePath, $targetFilePath)) return false;
  182. if (!$fileManager->deleteFile($previousFilePath)) return false;
  183. }
  184. return file_exists($targetFilePath);
  185. }
  186. /**
  187. * Delete a submission file from the database.
  188. * @param $submissionFile SubmissionFile
  189. * @return boolean
  190. */
  191. function deleteObject($submissionFile) {
  192. if (!$this->update(
  193. 'DELETE FROM submission_files
  194. WHERE file_id = ? AND revision = ?',
  195. array(
  196. (int)$submissionFile->getFileId(),
  197. (int)$submissionFile->getRevision()
  198. ))) return false;
  199. // if we've removed the last revision of this file, clean up
  200. // the settings for this file as well.
  201. $result = $this->retrieve(
  202. 'SELECT * FROM submission_files WHERE file_id = ?',
  203. array((int)$submissionFile->getFileId())
  204. );
  205. if ($result->RecordCount() == 0) {
  206. $this->update('DELETE FROM submission_file_settings WHERE file_id = ?',
  207. array((int) $submissionFile->getFileId()));
  208. }
  209. // Delete all dependent objects.
  210. $this->_deleteDependentObjects($submissionFile);
  211. // Delete the file on the file system, too.
  212. $filePath = $submissionFile->getFilePath();
  213. if(!(is_file($filePath) && is_readable($filePath))) return false;
  214. assert(is_writable(dirname($filePath)));
  215. import('lib.pkp.classes.file.FileManager');
  216. $fileManager = new FileManager();
  217. $fileManager->deleteFile($filePath);
  218. return !file_exists($filePath);
  219. }
  220. /**
  221. * Function to return a SubmissionFile object from a row.
  222. * @param $row array
  223. * @return SubmissionFile
  224. */
  225. function fromRow($row) {
  226. $submissionFile = $this->newDataObject();
  227. $submissionFile->setFileId((int)$row['submission_file_id']);
  228. $submissionFile->setRevision((int)$row['submission_revision']);
  229. $submissionFile->setAssocType(is_null($row['assoc_type']) ? null : (int)$row['assoc_type']);
  230. $submissionFile->setAssocId(is_null($row['assoc_id']) ? null : (int)$row['assoc_id']);
  231. $submissionFile->setSourceFileId(is_null($row['source_file_id']) ? null : (int)$row['source_file_id']);
  232. $submissionFile->setSourceRevision(is_null($row['source_revision']) ? null : (int)$row['source_revision']);
  233. $submissionFile->setSubmissionId((int)$row['submission_id']);
  234. $submissionFile->setFileStage((int)$row['file_stage']);
  235. $submissionFile->setOriginalFileName($row['original_file_name']);
  236. $submissionFile->setFileType($row['file_type']);
  237. $submissionFile->setGenreId(is_null($row['genre_id']) ? null : (int)$row['genre_id']);
  238. $submissionFile->setFileSize((int)$row['file_size']);
  239. $submissionFile->setUploaderUserId(is_null($row['uploader_user_id']) ? null : (int)$row['uploader_user_id']);
  240. $submissionFile->setUserGroupId(is_null($row['user_group_id']) ? null : (int)$row['user_group_id']);
  241. $submissionFile->setViewable((boolean)$row['viewable']);
  242. $submissionFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
  243. $submissionFile->setDateModified($this->datetimeFromDB($row['date_modified']));
  244. $submissionFile->setDirectSalesPrice($row['direct_sales_price']);
  245. $submissionFile->setSalesType($row['sales_type']);
  246. $this->getDataObjectSettings('submission_file_settings', 'file_id', $row['submission_file_id'], $submissionFile);
  247. return $submissionFile;
  248. }
  249. /**
  250. * Construct a new data object corresponding to this DAO.
  251. * @return SubmissionFile
  252. */
  253. function newDataObject() {
  254. assert(false);
  255. }
  256. //
  257. // Protected helper methods
  258. //
  259. /**
  260. * Get the list of fields for which data is localized.
  261. * @return array
  262. */
  263. function getLocaleFieldNames() {
  264. $localeFieldNames = parent::getLocaleFieldNames();
  265. $localeFieldNames[] = 'name';
  266. return $localeFieldNames;
  267. }
  268. /**
  269. * Update the localized fields for this submission file.
  270. * @param $submissionFile SubmissionFile
  271. */
  272. function updateLocaleFields(&$submissionFile) {
  273. // Update the locale fields.
  274. $this->updateDataObjectSettings($this->getSubmissionEntityName().'_file_settings', $submissionFile, array(
  275. 'file_id' => $submissionFile->getFileId()
  276. ));
  277. }
  278. //
  279. // Private helper methods
  280. //
  281. /**
  282. * Update all objects that depend on the given file.
  283. * @param $submissionFile SubmissionFile
  284. * @param $previousFile SubmissionFile
  285. */
  286. function _updateDependentObjects($submissionFile, $previousFile) {
  287. // If the file ids didn't change then we do not have to
  288. // do anything.
  289. if (
  290. $previousFile->getFileId() == $submissionFile->getFileId() ||
  291. $previousFile->getRevision() == $submissionFile->getRevision()
  292. ) return;
  293. // Update signoffs that refer to this file.
  294. $signoffDao = DAORegistry::getDAO('SignoffDAO'); /* @var $signoffDao SignoffDAO */
  295. $signoffFactory = $signoffDao->getByFileRevision(
  296. $previousFile->getFileId(), $previousFile->getRevision()
  297. );
  298. while ($signoff = $signoffFactory->next()) { /* @var $signoff Signoff */
  299. $signoff->setFileId($submissionFile->getFileId());
  300. $signoff->setFileRevision($submissionFile->getRevision());
  301. $signoffDao->updateObject($signoff);
  302. }
  303. // Update file views that refer to this file.
  304. $viewsDao = DAORegistry::getDAO('ViewsDAO'); /* @var $viewsDao ViewsDAO */
  305. $viewsDao->moveViews(
  306. ASSOC_TYPE_SUBMISSION_FILE,
  307. $previousFile->getFileIdAndRevision(), $submissionFile->getFileIdAndRevision()
  308. );
  309. }
  310. /**
  311. * Delete all objects that depend on the given file.
  312. * @param $submissionFile SubmissionFile
  313. */
  314. function _deleteDependentObjects($submissionFile) {
  315. // Delete signoffs that refer to this file.
  316. $signoffDao = DAORegistry::getDAO('SignoffDAO'); /* @var $signoffDao SignoffDAO */
  317. $signoffFactory = $signoffDao->getByFileRevision(
  318. $submissionFile->getFileId(), $submissionFile->getRevision()
  319. );
  320. while ($signoff = $signoffFactory->next()) { /* @var $signoff Signoff */
  321. $signoffDao->deleteObject($signoff);
  322. }
  323. // Delete file views that refer to this file.
  324. $viewsDao = DAORegistry::getDAO('ViewsDAO'); /* @var $viewsDao ViewsDAO */
  325. $viewsDao->deleteViews(
  326. ASSOC_TYPE_SUBMISSION_FILE, $submissionFile->getFileIdAndRevision()
  327. );
  328. }
  329. }
  330. ?>