Open Journal Systems  2.4.4
 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('PKPNoteDAO::_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->getInsertGiftId());
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  unset($gift);
253  }
254  return true;
255  }
256 
263  function &getGiftsByAssocId($assocType, $assocId, $rangeInfo = null) {
264  $result =& $this->retrieveRange(
265  'SELECT *
266  FROM gifts
267  WHERE assoc_type = ? AND assoc_id = ?
268  ORDER BY gift_id DESC',
269  array($assocType, $assocId),
270  $rangeInfo
271  );
272 
273  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
274  return $returner;
275  }
276 
285  function recipientHasGift($assocType, $assocId, $userId, $giftId) {
286  $result =& $this->retrieve(
287  'SELECT COUNT(*)
288  FROM gifts
289  WHERE gift_id = ?
290  AND assoc_type = ? AND assoc_id = ?
291  AND recipient_user_id = ?',
292  array(
293  $giftId,
294  $assocType,
295  $assocId,
296  $userId
297  )
298  );
299 
300  $returner = $result->fields[0] ? true : false;
301 
302  $result->Close();
303  unset($result);
304 
305  return $returner;
306  }
307 
316  function recipientHasNotRedeemedGift($assocType, $assocId, $userId, $giftId) {
317  $result =& $this->retrieve(
318  'SELECT COUNT(*)
319  FROM gifts
320  WHERE gift_id = ?
321  AND assoc_type = ? AND assoc_id = ?
322  AND recipient_user_id = ?
323  AND status = ?',
324  array(
325  $giftId,
326  $assocType,
327  $assocId,
328  $userId,
329  GIFT_STATUS_NOT_REDEEMED
330  )
331  );
332 
333  $returner = $result->fields[0] ? true : false;
334 
335  $result->Close();
336  unset($result);
337 
338  return $returner;
339  }
340 
349  function redeemGift($assocType, $assocId, $userId, $giftId) {
350  // Must be implemented by sub-classes
351  assert(false);
352  }
353 
360  function &getAllGiftsByRecipient($assocType, $userId, $rangeInfo = null) {
361  $result =& $this->retrieveRange(
362  'SELECT *
363  FROM gifts
364  WHERE assoc_type = ?
365  AND recipient_user_id = ?
366  ORDER BY gift_id DESC',
367  array(
368  $assocType,
369  $userId
370  ),
371  $rangeInfo
372  );
373 
374  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
375  return $returner;
376  }
377 
385  function &getGiftsByRecipient($assocType, $assocId, $userId, $rangeInfo = null) {
386  $result =& $this->retrieveRange(
387  'SELECT *
388  FROM gifts
389  WHERE assoc_type = ? AND assoc_id = ?
390  AND recipient_user_id = ?
391  AND (status = ? OR status = ?)
392  ORDER BY gift_id DESC',
393  array(
394  $assocType,
395  $assocId,
396  $userId,
397  GIFT_STATUS_NOT_REDEEMED,
398  GIFT_STATUS_REDEEMED
399  ),
400  $rangeInfo
401  );
402 
403  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
404  return $returner;
405  }
406 
415  function &getGiftsByTypeAndRecipient($assocType, $assocId, $giftType, $userId, $rangeInfo = null) {
416  $result =& $this->retrieveRange(
417  'SELECT *
418  FROM gifts
419  WHERE assoc_type = ? AND assoc_id = ?
420  AND recipient_user_id = ? AND gift_type = ?
421  AND (status = ? OR status = ?)
422  ORDER BY gift_id DESC',
423  array(
424  $assocType,
425  $assocId,
426  $userId,
427  $giftType,
428  GIFT_STATUS_NOT_REDEEMED,
429  GIFT_STATUS_REDEEMED
430  ),
431  $rangeInfo
432  );
433 
434  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
435  return $returner;
436  }
437 
445  function &getNotRedeemedGiftsByRecipient($assocType, $assocId, $userId, $rangeInfo = null) {
446  $result =& $this->retrieveRange(
447  'SELECT *
448  FROM gifts
449  WHERE assoc_type = ? AND assoc_id = ?
450  AND recipient_user_id = ?
451  AND status = ?
452  ORDER BY gift_id DESC',
453  array(
454  $assocType,
455  $assocId,
456  $userId,
457  GIFT_STATUS_NOT_REDEEMED
458  ),
459  $rangeInfo
460  );
461 
462  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
463  return $returner;
464  }
465 
474  function &getNotRedeemedGiftsByTypeAndRecipient($assocType, $assocId, $giftType, $userId, $rangeInfo = null) {
475  $result =& $this->retrieveRange(
476  'SELECT *
477  FROM gifts
478  WHERE assoc_type = ? AND assoc_id = ?
479  AND recipient_user_id = ? AND gift_type = ?
480  AND status = ?
481  ORDER BY gift_id DESC',
482  array(
483  $assocType,
484  $assocId,
485  $userId,
486  $giftType,
487  GIFT_STATUS_NOT_REDEEMED
488  ),
489  $rangeInfo
490  );
491 
492  $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
493  return $returner;
494  }
495 
500  function getInsertGiftId() {
501  return $this->getInsertId('gifts', 'gift_id');
502  }
503 }
504 
505 ?>
recipientHasGift($assocType, $assocId, $userId, $giftId)
& _returnGiftFromRow(&$row)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
updateObject(&$gift)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
& 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:176
redeemGift($assocType, $assocId, $userId, $giftId)
datetimeFromDB($dt)
Definition: DAO.inc.php:316
& 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)
call($hookName, $args=null)
& getNotRedeemedGiftsByRecipient($assocType, $assocId, $userId, $rangeInfo=null)
& getAllGiftsByRecipient($assocType, $userId, $rangeInfo=null)
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
deleteGiftsByAssocId($assocType, $assocId)
Operations for retrieving and modifying Gift objects.
& getGiftsByTypeAndRecipient($assocType, $assocId, $giftType, $userId, $rangeInfo=null)
getGiftAssocId($giftId)