Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
LibraryFileDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.context.LibraryFile');
18 
19 class LibraryFileDAO extends DAO {
23  function LibraryFileDAO() {
24  parent::DAO();
25  }
26 
34  function getById($fileId) {
35  $result = $this->retrieve(
36  'SELECT file_id, context_id, file_name, original_file_name, file_type, file_size, type, date_uploaded, submission_id FROM library_files WHERE file_id = ?',
37  array((int) $fileId)
38  );
39 
40  $returner = null;
41  if (isset($result) && $result->RecordCount() != 0) {
42  $returner = $this->_fromRow($result->GetRowAssoc(false));
43  }
44 
45  $result->Close();
46  return $returner;
47  }
48 
55  function getByContextId($contextId, $type = null) {
56  $params = array((int) $contextId);
57  if (isset($type)) $params[] = (int) $type;
58 
59  $result = $this->retrieve(
60  'SELECT *
61  FROM library_files
62  WHERE context_id = ? AND submission_id = 0 ' . (isset($type)?' AND type = ?' : ''),
63  $params
64  );
65  return new DAOResultFactory($result, $this, '_fromRow', array('id'));
66  }
67 
75  function getBySubmissionId($submissionId, $type = null, $contextId = null) {
76  $params = array((int) $submissionId);
77  if (isset($type)) $params[] = (int) $type;
78  if (isset($contextId)) $params[] = (int) $contextId;
79 
80  $result = $this->retrieve(
81  'SELECT *
82  FROM library_files
83  WHERE submission_id = ? ' . (isset($contextId)?' AND context_id = ?' : '') . (isset($type)?' AND type = ?' : ''),
84  $params
85  );
86  return new DAOResultFactory($result, $this, '_fromRow', array('id'));
87  }
88 
93  function newDataObject() {
94  return new LibraryFile();
95  }
96 
97 
102  function getLocaleFieldNames() {
103  return array('name');
104  }
105 
110  function updateLocaleFields(&$libraryFile) {
112  'library_file_settings',
113  $libraryFile,
114  array('file_id' => $libraryFile->getId())
115  );
116  }
117 
123  function _fromRow($row) {
124  $libraryFile = $this->newDataObject();
125 
126  $libraryFile->setId($row['file_id']);
127  $libraryFile->setContextId($row['context_id']);
128  $libraryFile->setServerFileName($row['file_name']);
129  $libraryFile->setOriginalFileName($row['original_file_name']);
130  $libraryFile->setFileType($row['file_type']);
131  $libraryFile->setFileSize($row['file_size']);
132  $libraryFile->setType($row['type']);
133  $libraryFile->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
134  $libraryFile->setSubmissionId($row['submission_id']);
135 
136  $this->getDataObjectSettings('library_file_settings', 'file_id', $row['file_id'], $libraryFile);
137 
138  HookRegistry::call('LibraryFileDAO::_fromRow', array(&$libraryFile, &$row));
139 
140  return $libraryFile;
141  }
142 
148  function insertObject($libraryFile) {
149  $params = array(
150  (int) $libraryFile->getContextId(),
151  $libraryFile->getServerFileName(),
152  $libraryFile->getOriginalFileName(),
153  $libraryFile->getFileType(),
154  (int) $libraryFile->getFileSize(),
155  (int) $libraryFile->getType(),
156  (int) $libraryFile->getSubmissionId(),
157  );
158 
159  if ($libraryFile->getId()) $params[] = (int) $libraryFile->getId();
160 
161  $this->update(
162  sprintf('INSERT INTO library_files
163  (context_id, file_name, original_file_name, file_type, file_size, type, submission_id, date_uploaded, date_modified' . ($libraryFile->getId()?', file_id':'') . ')
164  VALUES
165  (?, ?, ?, ?, ?, ?, ?, %s, %s' . ($libraryFile->getId()?', ?':'') . ')',
166  $this->datetimeToDB($libraryFile->getDateUploaded()),
167  $this->datetimeToDB($libraryFile->getDateModified())
168  ),
169  $params
170  );
171 
172  if (!$libraryFile->getId()) $libraryFile->setId($this->getInsertId());
173 
174  $this->updateLocaleFields($libraryFile);
175  return $libraryFile->getId();
176  }
177 
183  function updateObject($libraryFile) {
184  $this->update(
185  sprintf('UPDATE library_files
186  SET context_id = ?,
187  file_name = ?,
188  original_file_name = ?,
189  file_type = ?,
190  file_size = ?,
191  type = ?,
192  submission_id = ?,
193  date_uploaded = %s
194  WHERE file_id = ?',
195  $this->datetimeToDB($libraryFile->getDateUploaded())
196  ), array(
197  (int) $libraryFile->getContextId(),
198  $libraryFile->getServerFileName(),
199  $libraryFile->getOriginalFileName(),
200  $libraryFile->getFileType(),
201  (int) $libraryFile->getFileSize(),
202  (int) $libraryFile->getType(),
203  (int) $libraryFile->getSubmissionId(),
204  (int) $libraryFile->getId()
205  )
206  );
207 
208  $this->updateLocaleFields($libraryFile);
209  return $libraryFile->getId();
210  }
211 
217  function deleteById($fileId, $revision = null) {
218  $this->update(
219  'DELETE FROM library_files WHERE file_id = ?',
220  (int) $fileId
221  );
222  $this->update(
223  'DELETE FROM library_file_settings WHERE file_id = ?',
224  (int) $fileId
225  );
226  }
227 
234  function filenameExists($contextId, $fileName) {
235  $result = $this->retrieve(
236  'SELECT COUNT(*) FROM library_files WHERE context_id = ? AND file_name = ?',
237  array((int) $contextId, $fileName)
238  );
239 
240  $returner = (isset($result->fields[0]) && $result->fields[0] > 0) ? true : false;
241  $result->Close();
242  return $returner;
243  }
244 
249  function getInsertId() {
250  return $this->_getInsertId('library_files', 'file_id');
251  }
252 }
253 
254 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
updateLocaleFields(&$libraryFile)
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:457
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
datetimeFromDB($dt)
Definition: DAO.inc.php:313
static call($hookName, $args=null)
Operations for retrieving and modifying LibraryFile objects.
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
insertObject($libraryFile)
deleteById($fileId, $revision=null)
getByContextId($contextId, $type=null)
filenameExists($contextId, $fileName)
getBySubmissionId($submissionId, $type=null, $contextId=null)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
updateObject($libraryFile)
Library file class.