Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
PKPGiftDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.gift.PKPGift');
18 
19 define('GIFT_REDEEM_STATUS_SUCCESS', 0x01);
20 define('GIFT_REDEEM_STATUS_ERROR_GIFT_INVALID', 0x2);
21 define('GIFT_REDEEM_STATUS_ERROR_NO_GIFT_TO_REDEEM', 0x3);
22 define('GIFT_REDEEM_STATUS_ERROR_GIFT_ALREADY_REDEEMED', 0x4);
23 
24 
25 class PKPGiftDAO extends DAO {
29  function PKPGiftDAO() {
30  parent::DAO();
31  }
32 
38  function &getGift($giftId) {
39  $result = $this->retrieve(
40  'SELECT * FROM gifts WHERE gift_id = ?', $giftId
41  );
42 
43  $returner = null;
44  if ($result->RecordCount() != 0) {
45  $returner =& $this->_returnGiftFromRow($result->GetRowAssoc(false));
46  }
47  $result->Close();
48  return $returner;
49  }
50 
56  function getGiftAssocId($giftId) {
57  $result = $this->retrieve(
58  'SELECT assoc_id FROM gifts WHERE gift_id = ?', $giftId
59  );
60 
61  return isset($result->fields[0]) ? $result->fields[0] : 0;
62  }
63 
69  function getGiftAssocType($giftId) {
70  $result = $this->retrieve(
71  'SELECT assoc_type FROM gifts WHERE gift_id = ?', $giftId
72  );
73 
74  return isset($result->fields[0]) ? $result->fields[0] : 0;
75  }
76 
82  function &_returnGiftFromRow($row) {
83  $gift = $this->newDataObject();
84  $gift->setId($row['gift_id']);
85  $gift->setAssocType($row['assoc_type']);
86  $gift->setAssocId($row['assoc_id']);
87  $gift->setStatus($row['status']);
88  $gift->setGiftType($row['gift_type']);
89  $gift->setGiftAssocId($row['gift_assoc_id']);
90  $gift->setBuyerFirstName($row['buyer_first_name']);
91  $gift->setBuyerMiddleName($row['buyer_middle_name']);
92  $gift->setBuyerLastName($row['buyer_last_name']);
93  $gift->setBuyerEmail($row['buyer_email']);
94  $gift->setBuyerUserId($row['buyer_user_id']);
95  $gift->setRecipientFirstName($row['recipient_first_name']);
96  $gift->setRecipientMiddleName($row['recipient_middle_name']);
97  $gift->setRecipientLastName($row['recipient_last_name']);
98  $gift->setRecipientEmail($row['recipient_email']);
99  $gift->setRecipientUserId($row['recipient_user_id']);
100  $gift->setDatetimeRedeemed($this->datetimeFromDB($row['date_redeemed']));
101  $gift->setLocale($row['locale']);
102  $gift->setGiftNoteTitle($row['gift_note_title']);
103  $gift->setGiftNote($row['gift_note']);
104  $gift->setNotes($row['notes']);
105 
106  HookRegistry::call('PKPGiftDAO::_returnGiftFromRow', array(&$gift, &$row));
107 
108  return $gift;
109  }
110 
116  function insertObject(&$gift) {
117  $this->update(
118  sprintf('INSERT INTO gifts
119  (assoc_type,
120  assoc_id,
121  status,
122  gift_type,
123  gift_assoc_id,
124  buyer_first_name,
125  buyer_middle_name,
126  buyer_last_name,
127  buyer_email,
128  buyer_user_id,
129  recipient_first_name,
130  recipient_middle_name,
131  recipient_last_name,
132  recipient_email,
133  recipient_user_id,
134  locale,
135  gift_note_title,
136  gift_note,
137  notes,
138  date_redeemed)
139  VALUES
140  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, %s)',
141  $this->datetimeToDB($gift->getDatetimeRedeemed())),
142  array(
143  $gift->getAssocType(),
144  $gift->getAssocId(),
145  $gift->getStatus(),
146  $gift->getGiftType(),
147  $gift->getGiftAssocId(),
148  $gift->getBuyerFirstName(),
149  $gift->getBuyerMiddleName(),
150  $gift->getBuyerLastName(),
151  $gift->getBuyerEmail(),
152  $gift->getBuyerUserId(),
153  $gift->getRecipientFirstName(),
154  $gift->getRecipientMiddleName(),
155  $gift->getRecipientLastName(),
156  $gift->getRecipientEmail(),
157  $gift->getRecipientUserId(),
158  $gift->getLocale(),
159  $gift->getGiftNoteTitle(),
160  $gift->getGiftNote(),
161  $gift->getNotes()
162  )
163  );
164  $gift->setId($this->getInsertId());
165  return $gift->getId();
166  }
167 
173  function updateObject(&$gift) {
174  $returner = $this->update(
175  sprintf('UPDATE gifts
176  SET
177  assoc_type = ?,
178  assoc_id = ?,
179  status = ?,
180  gift_type = ?,
181  gift_assoc_id = ?,
182  buyer_first_name = ?,
183  buyer_middle_name = ?,
184  buyer_last_name = ?,
185  buyer_email = ?,
186  buyer_user_id = ?,
187  recipient_first_name = ?,
188  recipient_middle_name = ?,
189  recipient_last_name = ?,
190  recipient_email = ?,
191  recipient_user_id = ?,
192  locale = ?,
193  gift_note_title = ?,
194  gift_note = ?,
195  notes = ?,
196  date_redeemed = %s
197  WHERE gift_id = ?',
198  $this->datetimeToDB($gift->getDatetimeRedeemed())),
199  array(
200  $gift->getAssocType(),
201  $gift->getAssocId(),
202  $gift->getStatus(),
203  $gift->getGiftType(),
204  $gift->getGiftAssocId(),
205  $gift->getBuyerFirstName(),
206  $gift->getBuyerMiddleName(),
207  $gift->getBuyerLastName(),
208  $gift->getBuyerEmail(),
209  $gift->getBuyerUserId(),
210  $gift->getRecipientFirstName(),
211  $gift->getRecipientMiddleName(),
212  $gift->getRecipientLastName(),
213  $gift->getRecipientEmail(),
214  $gift->getRecipientUserId(),
215  $gift->getLocale(),
216  $gift->getGiftNoteTitle(),
217  $gift->getGiftNote(),
218  $gift->getNotes(),
219  $gift->getId()
220  )
221  );
222  return $returner;
223  }
224 
230  function deleteObject($gift) {
231  return $this->deleteGiftById($gift->getId());
232  }
233 
239  function deleteGiftById($giftId) {
240  return $this->update('DELETE FROM gifts WHERE gift_id = ?', $giftId);
241  }
242 
248  function deleteGiftsByAssocId($assocType, $assocId) {
249  $gifts = $this->getGiftsByAssocId($assocType, $assocId);
250  while ($gift = $gifts->next()) {
251  $this->deleteGiftById($gift->getId());
252  }
253  return true;
254  }
255 
262  function &getGiftsByAssocId($assocType, $assocId, $rangeInfo = null) {
263  $result = $this->retrieveRange(
264  'SELECT *
265  FROM gifts
266  WHERE assoc_type = ? AND assoc_id = ?
267  ORDER BY gift_id DESC',
268  array($assocType, $assocId),
269  $rangeInfo
270  );
271 
272  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
273  return $returner;
274  }
275 
284  function recipientHasGift($assocType, $assocId, $userId, $giftId) {
285  $result = $this->retrieve(
286  'SELECT COUNT(*)
287  FROM gifts
288  WHERE gift_id = ?
289  AND assoc_type = ? AND assoc_id = ?
290  AND recipient_user_id = ?',
291  array(
292  $giftId,
293  $assocType,
294  $assocId,
295  $userId
296  )
297  );
298 
299  $returner = $result->fields[0] ? true : false;
300 
301  $result->Close();
302  return $returner;
303  }
304 
313  function recipientHasNotRedeemedGift($assocType, $assocId, $userId, $giftId) {
314  $result = $this->retrieve(
315  'SELECT COUNT(*)
316  FROM gifts
317  WHERE gift_id = ?
318  AND assoc_type = ? AND assoc_id = ?
319  AND recipient_user_id = ?
320  AND status = ?',
321  array(
322  $giftId,
323  $assocType,
324  $assocId,
325  $userId,
326  GIFT_STATUS_NOT_REDEEMED
327  )
328  );
329 
330  $returner = $result->fields[0] ? true : false;
331 
332  $result->Close();
333  return $returner;
334  }
335 
344  function redeemGift($assocType, $assocId, $userId, $giftId) {
345  // Must be implemented by sub-classes
346  assert(false);
347  }
348 
355  function &getAllGiftsByRecipient($assocType, $userId, $rangeInfo = null) {
356  $result = $this->retrieveRange(
357  'SELECT *
358  FROM gifts
359  WHERE assoc_type = ?
360  AND recipient_user_id = ?
361  ORDER BY gift_id DESC',
362  array(
363  $assocType,
364  $userId
365  ),
366  $rangeInfo
367  );
368 
369  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
370  return $returner;
371  }
372 
380  function &getGiftsByRecipient($assocType, $assocId, $userId, $rangeInfo = null) {
381  $result = $this->retrieveRange(
382  'SELECT *
383  FROM gifts
384  WHERE assoc_type = ? AND assoc_id = ?
385  AND recipient_user_id = ?
386  AND (status = ? OR status = ?)
387  ORDER BY gift_id DESC',
388  array(
389  $assocType,
390  $assocId,
391  $userId,
392  GIFT_STATUS_NOT_REDEEMED,
393  GIFT_STATUS_REDEEMED
394  ),
395  $rangeInfo
396  );
397 
398  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
399  return $returner;
400  }
401 
410  function &getGiftsByTypeAndRecipient($assocType, $assocId, $giftType, $userId, $rangeInfo = null) {
411  $result = $this->retrieveRange(
412  'SELECT *
413  FROM gifts
414  WHERE assoc_type = ? AND assoc_id = ?
415  AND recipient_user_id = ? AND gift_type = ?
416  AND (status = ? OR status = ?)
417  ORDER BY gift_id DESC',
418  array(
419  $assocType,
420  $assocId,
421  $userId,
422  $giftType,
423  GIFT_STATUS_NOT_REDEEMED,
424  GIFT_STATUS_REDEEMED
425  ),
426  $rangeInfo
427  );
428 
429  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
430  return $returner;
431  }
432 
440  function &getNotRedeemedGiftsByRecipient($assocType, $assocId, $userId, $rangeInfo = null) {
441  $result = $this->retrieveRange(
442  'SELECT *
443  FROM gifts
444  WHERE assoc_type = ? AND assoc_id = ?
445  AND recipient_user_id = ?
446  AND status = ?
447  ORDER BY gift_id DESC',
448  array(
449  $assocType,
450  $assocId,
451  $userId,
452  GIFT_STATUS_NOT_REDEEMED
453  ),
454  $rangeInfo
455  );
456 
457  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
458  return $returner;
459  }
460 
469  function &getNotRedeemedGiftsByTypeAndRecipient($assocType, $assocId, $giftType, $userId, $rangeInfo = null) {
470  $result = $this->retrieveRange(
471  'SELECT *
472  FROM gifts
473  WHERE assoc_type = ? AND assoc_id = ?
474  AND recipient_user_id = ? AND gift_type = ?
475  AND status = ?
476  ORDER BY gift_id DESC',
477  array(
478  $assocType,
479  $assocId,
480  $userId,
481  $giftType,
482  GIFT_STATUS_NOT_REDEEMED
483  ),
484  $rangeInfo
485  );
486 
487  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
488  return $returner;
489  }
490 
495  function getInsertId() {
496  return $this->_getInsertId('gifts', 'gift_id');
497  }
498 }
499 
500 ?>
recipientHasGift($assocType, $assocId, $userId, $giftId)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
updateObject(&$gift)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
& getNotRedeemedGiftsByTypeAndRecipient($assocType, $assocId, $giftType, $userId, $rangeInfo=null)
insertObject(&$gift)
& getGiftsByRecipient($assocType, $assocId, $userId, $rangeInfo=null)
getGiftAssocType($giftId)
deleteObject($gift)
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
redeemGift($assocType, $assocId, $userId, $giftId)
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
datetimeFromDB($dt)
Definition: DAO.inc.php:313
static call($hookName, $args=null)
& getGift($giftId)
& getGiftsByAssocId($assocType, $assocId, $rangeInfo=null)
deleteGiftById($giftId)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
recipientHasNotRedeemedGift($assocType, $assocId, $userId, $giftId)
& getNotRedeemedGiftsByRecipient($assocType, $assocId, $userId, $rangeInfo=null)
& getAllGiftsByRecipient($assocType, $userId, $rangeInfo=null)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
deleteGiftsByAssocId($assocType, $assocId)
Operations for retrieving and modifying Gift objects.
& _returnGiftFromRow($row)
& getGiftsByTypeAndRecipient($assocType, $assocId, $giftType, $userId, $rangeInfo=null)
getGiftAssocId($giftId)