PageRenderTime 61ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/ojs/ojs-2.3.1-2/classes/article/SuppFileDAO.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 350 lines | 205 code | 47 blank | 98 comment | 17 complexity | e43af48b656ebb53b874c298aef07acf MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file classes/article/SuppFileDAO.inc.php
  4. *
  5. * Copyright (c) 2003-2009 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class SuppFileDAO
  9. * @ingroup article
  10. * @see SuppFile
  11. *
  12. * @brief Operations for retrieving and modifying SuppFile objects.
  13. */
  14. // $Id: SuppFileDAO.inc.php,v 1.26 2009/05/12 14:34:53 asmecher Exp $
  15. import('article.SuppFile');
  16. class SuppFileDAO extends DAO {
  17. /**
  18. * Retrieve a supplementary file by ID.
  19. * @param $suppFileId int
  20. * @param $articleId int optional
  21. * @return SuppFile
  22. */
  23. function &getSuppFile($suppFileId, $articleId = null) {
  24. $params = array($suppFileId);
  25. if ($articleId) $params[] = $articleId;
  26. $result =& $this->retrieve(
  27. 'SELECT s.*, a.file_name, a.original_file_name, a.file_type, a.file_size, a.date_uploaded, a.date_modified FROM article_supplementary_files s LEFT JOIN article_files a ON (s.file_id = a.file_id) WHERE s.supp_id = ?' . ($articleId?' AND s.article_id = ?':''),
  28. $params
  29. );
  30. $returner = null;
  31. if ($result->RecordCount() != 0) {
  32. $returner =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
  33. }
  34. $result->Close();
  35. unset($result);
  36. return $returner;
  37. }
  38. /**
  39. * Retrieve a supplementary file by public supp file ID.
  40. * @param $publicSuppId string
  41. * @param $articleId int
  42. * @return SuppFile
  43. */
  44. function &getSuppFileByPublicSuppFileId($publicSuppId, $articleId) {
  45. $result =& $this->retrieve(
  46. 'SELECT s.*, a.file_name, a.original_file_name, a.file_type, a.file_size, a.date_uploaded, a.date_modified FROM article_supplementary_files s LEFT JOIN article_files a ON (s.file_id = a.file_id) WHERE s.public_supp_file_id = ? AND s.article_id = ?',
  47. array($publicSuppId, $articleId)
  48. );
  49. $returner = null;
  50. if ($result->RecordCount() != 0) {
  51. $returner =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
  52. }
  53. $result->Close();
  54. unset($result);
  55. return $returner;
  56. }
  57. /**
  58. * Retrieve all supplementary files for an article.
  59. * @param $articleId int
  60. * @return array SuppFiles
  61. */
  62. function &getSuppFilesByArticle($articleId) {
  63. $suppFiles = array();
  64. $result =& $this->retrieve(
  65. 'SELECT s.*, a.file_name, a.original_file_name, a.file_type, a.file_size, a.date_uploaded, a.date_modified FROM article_supplementary_files s LEFT JOIN article_files a ON (s.file_id = a.file_id) WHERE s.article_id = ? ORDER BY s.seq',
  66. $articleId
  67. );
  68. while (!$result->EOF) {
  69. $suppFiles[] =& $this->_returnSuppFileFromRow($result->GetRowAssoc(false));
  70. $result->moveNext();
  71. }
  72. $result->Close();
  73. unset($result);
  74. return $suppFiles;
  75. }
  76. /**
  77. * Get the list of fields for which data is localized.
  78. * @return array
  79. */
  80. function getLocaleFieldNames() {
  81. return array('title', 'creator', 'subject', 'typeOther', 'description', 'publisher', 'sponsor', 'source');
  82. }
  83. /**
  84. * Update the localized fields for this supp file.
  85. * @param $suppFile
  86. */
  87. function updateLocaleFields(&$suppFile) {
  88. $this->updateDataObjectSettings('article_supp_file_settings', $suppFile, array(
  89. 'supp_id' => $suppFile->getSuppFileId()
  90. ));
  91. }
  92. /**
  93. * Internal function to return a SuppFile object from a row.
  94. * @param $row array
  95. * @return SuppFile
  96. */
  97. function &_returnSuppFileFromRow(&$row) {
  98. $suppFile = new SuppFile();
  99. $suppFile->setSuppFileID($row['supp_id']);
  100. $suppFile->setPublicSuppFileID($row['public_supp_file_id']);
  101. $suppFile->setFileId($row['file_id']);
  102. $suppFile->setArticleId($row['article_id']);
  103. $suppFile->setType($row['type']);
  104. $suppFile->setDateCreated($this->dateFromDB($row['date_created']));
  105. $suppFile->setLanguage($row['language']);
  106. $suppFile->setShowReviewers($row['show_reviewers']);
  107. $suppFile->setDateSubmitted($this->datetimeFromDB($row['date_submitted']));
  108. $suppFile->setSequence($row['seq']);
  109. //ArticleFile set methods
  110. $suppFile->setFileName($row['file_name']);
  111. $suppFile->setOriginalFileName($row['original_file_name']);
  112. $suppFile->setFileType($row['file_type']);
  113. $suppFile->setFileSize($row['file_size']);
  114. $suppFile->setDateModified($this->datetimeFromDB($row['date_modified']));
  115. $suppFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
  116. $this->getDataObjectSettings('article_supp_file_settings', 'supp_id', $row['supp_id'], $suppFile);
  117. HookRegistry::call('SuppFileDAO::_returnSuppFileFromRow', array(&$suppFile, &$row));
  118. return $suppFile;
  119. }
  120. /**
  121. * Insert a new SuppFile.
  122. * @param $suppFile SuppFile
  123. */
  124. function insertSuppFile(&$suppFile) {
  125. if ($suppFile->getDateSubmitted() == null) {
  126. $suppFile->setDateSubmitted(Core::getCurrentDate());
  127. }
  128. if ($suppFile->getSequence() == null) {
  129. $suppFile->setSequence($this->getNextSuppFileSequence($suppFile->getArticleID()));
  130. }
  131. $this->update(
  132. sprintf('INSERT INTO article_supplementary_files
  133. (public_supp_file_id, file_id, article_id, type, date_created, language, show_reviewers, date_submitted, seq)
  134. VALUES
  135. (?, ?, ?, ?, %s, ?, ?, %s, ?)',
  136. $this->dateToDB($suppFile->getDateCreated()), $this->datetimeToDB($suppFile->getDateSubmitted())),
  137. array(
  138. $suppFile->getPublicSuppFileId(),
  139. $suppFile->getFileId(),
  140. $suppFile->getArticleId(),
  141. $suppFile->getType(),
  142. $suppFile->getLanguage(),
  143. $suppFile->getShowReviewers(),
  144. $suppFile->getSequence()
  145. )
  146. );
  147. $suppFile->setSuppFileId($this->getInsertSuppFileId());
  148. $this->updateLocaleFields($suppFile);
  149. return $suppFile->getSuppFileId();
  150. }
  151. /**
  152. * Update an existing SuppFile.
  153. * @param $suppFile SuppFile
  154. */
  155. function updateSuppFile(&$suppFile) {
  156. $returner = $this->update(
  157. sprintf('UPDATE article_supplementary_files
  158. SET
  159. public_supp_file_id = ?,
  160. file_id = ?,
  161. type = ?,
  162. date_created = %s,
  163. language = ?,
  164. show_reviewers = ?,
  165. seq = ?
  166. WHERE supp_id = ?',
  167. $this->dateToDB($suppFile->getDateCreated())),
  168. array(
  169. $suppFile->getPublicSuppFileId(),
  170. $suppFile->getFileId(),
  171. $suppFile->getType(),
  172. $suppFile->getLanguage(),
  173. $suppFile->getShowReviewers(),
  174. $suppFile->getSequence(),
  175. $suppFile->getSuppFileId()
  176. )
  177. );
  178. $this->updateLocaleFields($suppFile);
  179. return $returner;
  180. }
  181. /**
  182. * Delete a SuppFile.
  183. * @param $suppFile SuppFile
  184. */
  185. function deleteSuppFile(&$suppFile) {
  186. return $this->deleteSuppFileById($suppFile->getSuppFileId());
  187. }
  188. /**
  189. * Delete a supplementary file by ID.
  190. * @param $suppFileId int
  191. * @param $articleId int optional
  192. */
  193. function deleteSuppFileById($suppFileId, $articleId = null) {
  194. if (isset($articleId)) {
  195. $returner = $this->update('DELETE FROM article_supplementary_files WHERE supp_id = ? AND article_id = ?', array($suppFileId, $articleId));
  196. if ($returner) $this->update('DELETE FROM article_supp_file_settings WHERE supp_id = ?', $suppFileId);
  197. return $returner;
  198. } else {
  199. $this->update('DELETE FROM article_supp_file_settings WHERE supp_id = ?', $suppFileId);
  200. return $this->update(
  201. 'DELETE FROM article_supplementary_files WHERE supp_id = ?', $suppFileId
  202. );
  203. }
  204. }
  205. /**
  206. * Delete supplementary files by article.
  207. * @param $articleId int
  208. */
  209. function deleteSuppFilesByArticle($articleId) {
  210. $suppFiles =& $this->getSuppFilesByArticle($articleId);
  211. foreach ($suppFiles as $suppFile) {
  212. $this->deleteSuppFile($suppFile);
  213. }
  214. }
  215. /**
  216. * Check if a supplementary file exists with the associated file ID.
  217. * @param $articleId int
  218. * @param $fileId int
  219. * @return boolean
  220. */
  221. function suppFileExistsByFileId($articleId, $fileId) {
  222. $result =& $this->retrieve(
  223. 'SELECT COUNT(*) FROM article_supplementary_files
  224. WHERE article_id = ? AND file_id = ?',
  225. array($articleId, $fileId)
  226. );
  227. $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
  228. $result->Close();
  229. unset($result);
  230. return $returner;
  231. }
  232. /**
  233. * Sequentially renumber supplementary files for an article in their sequence order.
  234. * @param $articleId int
  235. */
  236. function resequenceSuppFiles($articleId) {
  237. $result =& $this->retrieve(
  238. 'SELECT supp_id FROM article_supplementary_files WHERE article_id = ? ORDER BY seq',
  239. $articleId
  240. );
  241. for ($i=1; !$result->EOF; $i++) {
  242. list($suppId) = $result->fields;
  243. $this->update(
  244. 'UPDATE article_supplementary_files SET seq = ? WHERE supp_id = ?',
  245. array($i, $suppId)
  246. );
  247. $result->moveNext();
  248. }
  249. $result->close();
  250. unset($result);
  251. }
  252. /**
  253. * Get the the next sequence number for an article's supplementary files (i.e., current max + 1).
  254. * @param $articleId int
  255. * @return int
  256. */
  257. function getNextSuppFileSequence($articleId) {
  258. $result =& $this->retrieve(
  259. 'SELECT MAX(seq) + 1 FROM article_supplementary_files WHERE article_id = ?',
  260. $articleId
  261. );
  262. $returner = floor($result->fields[0]);
  263. $result->Close();
  264. unset($result);
  265. return $returner;
  266. }
  267. /**
  268. * Get the ID of the last inserted supplementary file.
  269. * @return int
  270. */
  271. function getInsertSuppFileId() {
  272. return $this->getInsertId('article_supplementary_files', 'supp_id');
  273. }
  274. /**
  275. * Retrieve supp file by public supp file id or, failing that,
  276. * internal supp file ID; public ID takes precedence.
  277. * @param $articleId int
  278. * @param $suppId string
  279. * @return SuppFile object
  280. */
  281. function &getSuppFileByBestSuppFileId($articleId, $suppId) {
  282. $suppFile =& $this->getSuppFileByPublicSuppFileId($suppId, $articleId);
  283. if (!isset($suppFile)) $suppFile =& $this->getSuppFile((int) $suppId, $articleId);
  284. return $suppFile;
  285. }
  286. /**
  287. * Checks if public identifier exists
  288. * @param $publicSuppFileId string
  289. * @param $suppId int A supplemental file ID to exempt from the test
  290. * @param $journalId int
  291. * @return boolean
  292. */
  293. function suppFileExistsByPublicId($publicSuppFileId, $suppId, $journalId) {
  294. $result =& $this->retrieve(
  295. 'SELECT COUNT(*) FROM article_supplementary_files f, articles a WHERE f.article_id = a.article_id AND f.public_supp_file_id = ? AND f.supp_id <> ? AND a.journal_id = ?', array($publicSuppFileId, $suppId, $journalId)
  296. );
  297. $returner = $result->fields[0] ? true : false;
  298. $result->Close();
  299. unset($result);
  300. return $returner;
  301. }
  302. }
  303. ?>