Open Journal Systems  3.3.0
NoteDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.note.Note');
18 
19 define('NOTE_ORDER_DATE_CREATED', 0x0001);
20 define('NOTE_ORDER_ID', 0x0002);
21 
22 class NoteDAO extends DAO {
23 
28  function newDataObject() {
29  return new Note();
30  }
31 
37  function getById($noteId) {
38  $result = $this->retrieve(
39  'SELECT * FROM notes WHERE note_id = ?', (int) $noteId
40  );
41 
42  $note = $this->_fromRow($result->GetRowAssoc(false));
43 
44  $result->Close();
45  return $note;
46  }
47 
54  function getByUserId($userId, $rangeInfo = null) {
55  $result = $this->retrieveRange(
56  'SELECT * FROM notes WHERE user_id = ? ORDER BY date_created DESC',
57  array((int) $userId),
58  $rangeInfo
59  );
60 
61  return new DAOResultFactory($result, $this, '_fromRow');
62  }
63 
73  function getByAssoc($assocType, $assocId, $userId = null, $orderBy = NOTE_ORDER_DATE_CREATED, $sortDirection = SORT_DIRECTION_DESC, $isAdmin = false) {
74  $params = array((int) $assocId, (int) $assocType);
75  if ($userId) $params[] = (int) $userId;
76 
77  // Sanitize sort ordering
78  switch ($orderBy) {
79  case NOTE_ORDER_ID:
80  $orderSanitized = 'note_id';
81  break;
82  case NOTE_ORDER_DATE_CREATED:
83  default:
84  $orderSanitized = 'date_created';
85  }
86  switch ($sortDirection) {
87  case SORT_DIRECTION_ASC:
88  $directionSanitized = 'ASC';
89  break;
90  case SORT_DIRECTION_DESC:
91  default:
92  $directionSanitized = 'DESC';
93  }
94 
95  $result = $this->retrieve(
96  'SELECT *
97  FROM notes
98  WHERE assoc_id = ?
99  AND assoc_type = ?
100  ' . ($userId?' AND user_id = ?':'') .
101  ($isAdmin?'':'
102  AND (title IS NOT NULL OR contents IS NOT NULL)') . '
103  ORDER BY ' . $orderSanitized . ' ' . $directionSanitized,
104  $params
105  );
106  return new DAOResultFactory($result, $this, '_fromRow');
107  }
108 
116  function notesExistByAssoc($assocType, $assocId, $userId = null) {
117  $params = array((int) $assocId, (int) $assocType);
118  if ($userId) $params[] = (int) $userId;
119 
120  $result = $this->retrieve(
121  'SELECT COUNT(*)
122  FROM notes
123  WHERE assoc_id = ? AND assoc_type = ?
124  ' . ($userId?' AND user_id = ?':''),
125  $params
126  );
127  $returner = isset($result->fields[0]) && $result->fields[0] == 0 ? false : true;
128  $result->Close();
129 
130  return $returner;
131  }
132 
139  function unreadNotesExistByAssoc($assocType, $assocId, $userId) {
140  $params = array((int) $assocId, (int) $assocType, (int) $userId);
141 
142  $result = $this->retrieve(
143  'SELECT COUNT(*)
144  FROM notes n
145  JOIN item_views v ON (v.assoc_type = ? AND v.assoc_id = CAST(n.note_id AS CHAR) AND v.user_id = ?)
146  WHERE n.assoc_type = ? AND
147  n.assoc_id = ? AND
148  v.assoc_id IS NULL',
149  array(
150  (int) ASSOC_TYPE_NOTE,
151  (int) $userId,
152  (int) $assocType,
153  (int) $assocId
154  )
155  );
156 
157  $returner = isset($result->fields[0]) && $result->fields[0] == 0 ? false : true;
158  $result->Close();
159 
160  return $returner;
161  }
162 
168  function _fromRow($row) {
169  $note = $this->newDataObject();
170  $note->setId($row['note_id']);
171  $note->setUserId($row['user_id']);
172  $note->setDateCreated($this->datetimeFromDB($row['date_created']));
173  $note->setDateModified($this->datetimeFromDB($row['date_modified']));
174  $note->setContents($row['contents']);
175  $note->setTitle($row['title']);
176  $note->setAssocType($row['assoc_type']);
177  $note->setAssocId($row['assoc_id']);
178 
179  HookRegistry::call('NoteDAO::_fromRow', array(&$note, &$row));
180 
181  return $note;
182  }
183 
189  function insertObject($note) {
190  if (!$note->getDateCreated()) $note->setDateCreated(Core::getCurrentDate());
191  $this->update(
192  sprintf('INSERT INTO notes
193  (user_id, date_created, date_modified, title, contents, assoc_type, assoc_id)
194  VALUES
195  (?, %s, %s, ?, ?, ?, ?)',
196  $this->datetimeToDB($note->getDateCreated()),
197  $this->datetimeToDB(Core::getCurrentDate())
198  ),
199  array(
200  (int) $note->getUserId(),
201  $note->getTitle(),
202  $note->getContents(),
203  (int) $note->getAssocType(),
204  (int) $note->getAssocId()
205  )
206  );
207 
208  $note->setId($this->getInsertId());
209  return $note->getId();
210  }
211 
217  function updateObject($note) {
218  return $this->update(
219  sprintf('UPDATE notes SET
220  user_id = ?,
221  date_created = %s,
222  date_modified = %s,
223  title = ?,
224  contents = ?,
225  assoc_type = ?,
226  assoc_id = ?
227  WHERE note_id = ?',
228  $this->datetimeToDB($note->getDateCreated()),
229  $this->datetimeToDB(Core::getCurrentDate())
230  ),
231  array(
232  (int) $note->getUserId(),
233  $note->getTitle(),
234  $note->getContents(),
235  (int) $note->getAssocType(),
236  (int) $note->getAssocId(),
237  (int) $note->getId()
238  )
239  );
240  }
241 
246  function deleteObject($note) {
247  $this->deleteById($note->getId());
248  }
249 
255  function deleteById($noteId, $userId = null) {
256  $params = array((int) $noteId);
257  if ($userId) $params[] = (int) $userId;
258 
259  $this->update(
260  'DELETE FROM notes WHERE note_id = ?' .
261  ($userId?' AND user_id = ?':''),
262  $params
263  );
264  }
265 
271  function deleteByAssoc($assocType, $assocId) {
272  $notes = $this->getByAssoc($assocType, $assocId);
273  while ($note = $notes->next()) {
274  $this->deleteObject($note);
275  }
276  }
277 
282  function getInsertId() {
283  return $this->_getInsertId('notes', 'note_id');
284  }
285 }
286 
287 
NoteDAO\deleteByAssoc
deleteByAssoc($assocType, $assocId)
Definition: NoteDAO.inc.php:271
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
NoteDAO\newDataObject
newDataObject()
Definition: NoteDAO.inc.php:28
NoteDAO\insertObject
insertObject($note)
Definition: NoteDAO.inc.php:189
DAO\retrieveRange
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
NoteDAO\getByUserId
getByUserId($userId, $rangeInfo=null)
Definition: NoteDAO.inc.php:54
NoteDAO
Operations for retrieving and modifying Note objects.
Definition: NoteDAO.inc.php:22
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
NoteDAO\getById
getById($noteId)
Definition: NoteDAO.inc.php:37
DAO\datetimeFromDB
datetimeFromDB($dt)
Definition: DAO.inc.php:319
NoteDAO\deleteObject
deleteObject($note)
Definition: NoteDAO.inc.php:246
NoteDAO\updateObject
updateObject($note)
Definition: NoteDAO.inc.php:217
NoteDAO\unreadNotesExistByAssoc
unreadNotesExistByAssoc($assocType, $assocId, $userId)
Definition: NoteDAO.inc.php:139
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
DAO\datetimeToDB
datetimeToDB($dt)
Definition: DAO.inc.php:299
DAO\_getInsertId
_getInsertId($table='', $id='')
Definition: DAO.inc.php:255
NoteDAO\deleteById
deleteById($noteId, $userId=null)
Definition: NoteDAO.inc.php:255
Note
Class for Note.
Definition: Note.inc.php:17
Core\getCurrentDate
static getCurrentDate($ts=null)
Definition: Core.inc.php:63
NoteDAO\getByAssoc
getByAssoc($assocType, $assocId, $userId=null, $orderBy=NOTE_ORDER_DATE_CREATED, $sortDirection=SORT_DIRECTION_DESC, $isAdmin=false)
Definition: NoteDAO.inc.php:73
NoteDAO\getInsertId
getInsertId()
Definition: NoteDAO.inc.php:282
NoteDAO\notesExistByAssoc
notesExistByAssoc($assocType, $assocId, $userId=null)
Definition: NoteDAO.inc.php:116
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
NoteDAO\_fromRow
_fromRow($row)
Definition: NoteDAO.inc.php:168