/classes/gift/PKPGiftDAO.inc.php

https://github.com/davekisly/pkp-lib · PHP · 497 lines · 328 code · 41 blank · 128 comment · 3 complexity · 7b9c8f26d6772d7c3e14c61bcb4e286a MD5 · raw file

  1. <?php
  2. /**
  3. * @file classes/gift/PKPGiftDAO.inc.php
  4. *
  5. * Copyright (c) 2000-2011 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class PKPGiftDAO
  9. * @ingroup gift
  10. * @see Gift, PKPGift
  11. *
  12. * @brief Operations for retrieving and modifying Gift objects.
  13. */
  14. import('lib.pkp.classes.gift.PKPGift');
  15. define('GIFT_REDEEM_STATUS_SUCCESS', 0x01);
  16. define('GIFT_REDEEM_STATUS_ERROR_GIFT_INVALID', 0x2);
  17. define('GIFT_REDEEM_STATUS_ERROR_NO_GIFT_TO_REDEEM', 0x3);
  18. define('GIFT_REDEEM_STATUS_ERROR_GIFT_ALREADY_REDEEMED', 0x4);
  19. class PKPGiftDAO extends DAO {
  20. /**
  21. * Retrieve a gift by gift ID.
  22. * @param $giftId int
  23. * @return Gift object
  24. */
  25. function &getGift($giftId) {
  26. $result =& $this->retrieve(
  27. 'SELECT * FROM gifts WHERE gift_id = ?', $giftId
  28. );
  29. $returner = null;
  30. if ($result->RecordCount() != 0) {
  31. $returner =& $this->_returnGiftFromRow($result->GetRowAssoc(false));
  32. }
  33. $result->Close();
  34. return $returner;
  35. }
  36. /**
  37. * Retrieve gift assoc ID by gift ID.
  38. * @param $giftId int
  39. * @return int
  40. */
  41. function getGiftAssocId($giftId) {
  42. $result =& $this->retrieve(
  43. 'SELECT assoc_id FROM gifts WHERE gift_id = ?', $giftId
  44. );
  45. return isset($result->fields[0]) ? $result->fields[0] : 0;
  46. }
  47. /**
  48. * Retrieve gift assoc type by gift ID.
  49. * @param $giftId int
  50. * @return int
  51. */
  52. function getGiftAssocType($giftId) {
  53. $result =& $this->retrieve(
  54. 'SELECT assoc_type FROM gifts WHERE gift_id = ?', $giftId
  55. );
  56. return isset($result->fields[0]) ? $result->fields[0] : 0;
  57. }
  58. /**
  59. * Internal function to return a Gift object from a row.
  60. * @param $row array
  61. * @return Gift object
  62. */
  63. function &_returnGiftFromRow(&$row) {
  64. $gift = $this->newDataObject();
  65. $gift->setId($row['gift_id']);
  66. $gift->setAssocType($row['assoc_type']);
  67. $gift->setAssocId($row['assoc_id']);
  68. $gift->setStatus($row['status']);
  69. $gift->setGiftType($row['gift_type']);
  70. $gift->setGiftAssocId($row['gift_assoc_id']);
  71. $gift->setBuyerFirstName($row['buyer_first_name']);
  72. $gift->setBuyerMiddleName($row['buyer_middle_name']);
  73. $gift->setBuyerLastName($row['buyer_last_name']);
  74. $gift->setBuyerEmail($row['buyer_email']);
  75. $gift->setBuyerUserId($row['buyer_user_id']);
  76. $gift->setRecipientFirstName($row['recipient_first_name']);
  77. $gift->setRecipientMiddleName($row['recipient_middle_name']);
  78. $gift->setRecipientLastName($row['recipient_last_name']);
  79. $gift->setRecipientEmail($row['recipient_email']);
  80. $gift->setRecipientUserId($row['recipient_user_id']);
  81. $gift->setDatetimeRedeemed($this->datetimeFromDB($row['date_redeemed']));
  82. $gift->setLocale($row['locale']);
  83. $gift->setGiftNoteTitle($row['gift_note_title']);
  84. $gift->setGiftNote($row['gift_note']);
  85. $gift->setNotes($row['notes']);
  86. HookRegistry::call('PKPNoteDAO::_returnGiftFromRow', array(&$gift, &$row));
  87. return $gift;
  88. }
  89. /**
  90. * Insert a new Gift.
  91. * @param $gift Gift object
  92. * @return int
  93. */
  94. function insertObject(&$gift) {
  95. $this->update(
  96. sprintf('INSERT INTO gifts
  97. (assoc_type,
  98. assoc_id,
  99. status,
  100. gift_type,
  101. gift_assoc_id,
  102. buyer_first_name,
  103. buyer_middle_name,
  104. buyer_last_name,
  105. buyer_email,
  106. buyer_user_id,
  107. recipient_first_name,
  108. recipient_middle_name,
  109. recipient_last_name,
  110. recipient_email,
  111. recipient_user_id,
  112. locale,
  113. gift_note_title,
  114. gift_note,
  115. notes,
  116. date_redeemed)
  117. VALUES
  118. (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, %s)',
  119. $this->datetimeToDB($gift->getDatetimeRedeemed())),
  120. array(
  121. $gift->getAssocType(),
  122. $gift->getAssocId(),
  123. $gift->getStatus(),
  124. $gift->getGiftType(),
  125. $gift->getGiftAssocId(),
  126. $gift->getBuyerFirstName(),
  127. $gift->getBuyerMiddleName(),
  128. $gift->getBuyerLastName(),
  129. $gift->getBuyerEmail(),
  130. $gift->getBuyerUserId(),
  131. $gift->getRecipientFirstName(),
  132. $gift->getRecipientMiddleName(),
  133. $gift->getRecipientLastName(),
  134. $gift->getRecipientEmail(),
  135. $gift->getRecipientUserId(),
  136. $gift->getLocale(),
  137. $gift->getGiftNoteTitle(),
  138. $gift->getGiftNote(),
  139. $gift->getNotes()
  140. )
  141. );
  142. $gift->setId($this->getInsertGiftId());
  143. return $gift->getId();
  144. }
  145. /**
  146. * Update an existing gift.
  147. * @param $gift Gift
  148. * @return boolean
  149. */
  150. function updateObject(&$gift) {
  151. $returner = $this->update(
  152. sprintf('UPDATE gifts
  153. SET
  154. assoc_type = ?,
  155. assoc_id = ?,
  156. status = ?,
  157. gift_type = ?,
  158. gift_assoc_id = ?,
  159. buyer_first_name = ?,
  160. buyer_middle_name = ?,
  161. buyer_last_name = ?,
  162. buyer_email = ?,
  163. buyer_user_id = ?,
  164. recipient_first_name = ?,
  165. recipient_middle_name = ?,
  166. recipient_last_name = ?,
  167. recipient_email = ?,
  168. recipient_user_id = ?,
  169. locale = ?,
  170. gift_note_title = ?,
  171. gift_note = ?,
  172. notes = ?,
  173. date_redeemed = %s
  174. WHERE gift_id = ?',
  175. $this->datetimeToDB($gift->getDatetimeRedeemed())),
  176. array(
  177. $gift->getAssocType(),
  178. $gift->getAssocId(),
  179. $gift->getStatus(),
  180. $gift->getGiftType(),
  181. $gift->getGiftAssocId(),
  182. $gift->getBuyerFirstName(),
  183. $gift->getBuyerMiddleName(),
  184. $gift->getBuyerLastName(),
  185. $gift->getBuyerEmail(),
  186. $gift->getBuyerUserId(),
  187. $gift->getRecipientFirstName(),
  188. $gift->getRecipientMiddleName(),
  189. $gift->getRecipientLastName(),
  190. $gift->getRecipientEmail(),
  191. $gift->getRecipientUserId(),
  192. $gift->getLocale(),
  193. $gift->getGiftNoteTitle(),
  194. $gift->getGiftNote(),
  195. $gift->getNotes(),
  196. $gift->getId()
  197. )
  198. );
  199. return $returner;
  200. }
  201. /**
  202. * Delete a gift.
  203. * @param $gift Gift
  204. * @return boolean
  205. */
  206. function deleteObject($gift) {
  207. return $this->deleteGiftById($gift->getId());
  208. }
  209. /**
  210. * Delete a gift by gift ID.
  211. * @param $giftId int
  212. * @return boolean
  213. */
  214. function deleteGiftById($giftId) {
  215. return $this->update('DELETE FROM gifts WHERE gift_id = ?', $giftId);
  216. }
  217. /**
  218. * Delete gifts by assoc ID
  219. * @param $assocType int
  220. * @param $assocId int
  221. */
  222. function deleteGiftsByAssocId($assocType, $assocId) {
  223. $gifts =& $this->getGiftsByAssocId($assocType, $assocId);
  224. while (($gift =& $gifts->next())) {
  225. $this->deleteGiftById($gift->getId());
  226. unset($gift);
  227. }
  228. return true;
  229. }
  230. /**
  231. * Retrieve an array of gifts matching a particular assoc ID.
  232. * @param $assocType int
  233. * @param $assocId int
  234. * @return object DAOResultFactory containing matching Gifts
  235. */
  236. function &getGiftsByAssocId($assocType, $assocId, $rangeInfo = null) {
  237. $result =& $this->retrieveRange(
  238. 'SELECT *
  239. FROM gifts
  240. WHERE assoc_type = ? AND assoc_id = ?
  241. ORDER BY gift_id DESC',
  242. array($assocType, $assocId),
  243. $rangeInfo
  244. );
  245. $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
  246. return $returner;
  247. }
  248. /**
  249. * Check if recipient user has a gift.
  250. * @param $assocType int
  251. * @param $assocId int
  252. * @param $userId int
  253. * @param $giftId int
  254. * @return boolean
  255. */
  256. function recipientHasGift($assocType, $assocId, $userId, $giftId) {
  257. $result =& $this->retrieve(
  258. 'SELECT COUNT(*)
  259. FROM gifts
  260. WHERE gift_id = ?
  261. AND assoc_type = ? AND assoc_id = ?
  262. AND recipient_user_id = ?',
  263. array(
  264. $giftId,
  265. $assocType,
  266. $assocId,
  267. $userId
  268. )
  269. );
  270. $returner = $result->fields[0] ? true : false;
  271. $result->Close();
  272. unset($result);
  273. return $returner;
  274. }
  275. /**
  276. * Check if recipient user has a gift that is unreedemed.
  277. * @param $assocType int
  278. * @param $assocId int
  279. * @param $userId int
  280. * @param $giftId int
  281. * @return boolean
  282. */
  283. function recipientHasNotRedeemedGift($assocType, $assocId, $userId, $giftId) {
  284. $result =& $this->retrieve(
  285. 'SELECT COUNT(*)
  286. FROM gifts
  287. WHERE gift_id = ?
  288. AND assoc_type = ? AND assoc_id = ?
  289. AND recipient_user_id = ?
  290. AND status = ?',
  291. array(
  292. $giftId,
  293. $assocType,
  294. $assocId,
  295. $userId,
  296. GIFT_STATUS_NOT_REDEEMED
  297. )
  298. );
  299. $returner = $result->fields[0] ? true : false;
  300. $result->Close();
  301. unset($result);
  302. return $returner;
  303. }
  304. /**
  305. * Redeem a gift for a recipient user.
  306. * @param $assocType int
  307. * @param $assocId int
  308. * @param $userId int
  309. * @param $giftId int
  310. * @return int Status code indicating whether gift could be redeemed
  311. */
  312. function redeemGift($assocType, $assocId, $userId, $giftId) {
  313. // Must be implemented by sub-classes
  314. assert(false);
  315. }
  316. /**
  317. * Retrieve an array of all gifts for a recipient user.
  318. * @param $assocType int
  319. * @param $userId int
  320. * @return object DAOResultFactory containing matching Gifts
  321. */
  322. function &getAllGiftsByRecipient($assocType, $userId, $rangeInfo = null) {
  323. $result =& $this->retrieveRange(
  324. 'SELECT *
  325. FROM gifts
  326. WHERE assoc_type = ?
  327. AND recipient_user_id = ?
  328. ORDER BY gift_id DESC',
  329. array(
  330. $assocType,
  331. $userId
  332. ),
  333. $rangeInfo
  334. );
  335. $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
  336. return $returner;
  337. }
  338. /**
  339. * Retrieve an array of redeemed and unredeemed gifts for a recipient user.
  340. * @param $assocType int
  341. * @param $assocId int
  342. * @param $userId int
  343. * @return object DAOResultFactory containing matching Gifts
  344. */
  345. function &getGiftsByRecipient($assocType, $assocId, $userId, $rangeInfo = null) {
  346. $result =& $this->retrieveRange(
  347. 'SELECT *
  348. FROM gifts
  349. WHERE assoc_type = ? AND assoc_id = ?
  350. AND recipient_user_id = ?
  351. AND (status = ? OR status = ?)
  352. ORDER BY gift_id DESC',
  353. array(
  354. $assocType,
  355. $assocId,
  356. $userId,
  357. GIFT_STATUS_NOT_REDEEMED,
  358. GIFT_STATUS_REDEEMED
  359. ),
  360. $rangeInfo
  361. );
  362. $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
  363. return $returner;
  364. }
  365. /**
  366. * Retrieve an array of redeemed and unredeemed gifts of a certain type for a recipient user.
  367. * @param $assocType int
  368. * @param $assocId int
  369. * @param $giftType int
  370. * @param $userId int
  371. * @return object DAOResultFactory containing matching Gifts
  372. */
  373. function &getGiftsByTypeAndRecipient($assocType, $assocId, $giftType, $userId, $rangeInfo = null) {
  374. $result =& $this->retrieveRange(
  375. 'SELECT *
  376. FROM gifts
  377. WHERE assoc_type = ? AND assoc_id = ?
  378. AND recipient_user_id = ? AND gift_type = ?
  379. AND (status = ? OR status = ?)
  380. ORDER BY gift_id DESC',
  381. array(
  382. $assocType,
  383. $assocId,
  384. $userId,
  385. $giftType,
  386. GIFT_STATUS_NOT_REDEEMED,
  387. GIFT_STATUS_REDEEMED
  388. ),
  389. $rangeInfo
  390. );
  391. $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
  392. return $returner;
  393. }
  394. /**
  395. * Retrieve an array of unredeemed gifts for a recipient user.
  396. * @param $assocType int
  397. * @param $assocId int
  398. * @param $userId int
  399. * @return object DAOResultFactory containing matching Gifts
  400. */
  401. function &getNotRedeemedGiftsByRecipient($assocType, $assocId, $userId, $rangeInfo = null) {
  402. $result =& $this->retrieveRange(
  403. 'SELECT *
  404. FROM gifts
  405. WHERE assoc_type = ? AND assoc_id = ?
  406. AND recipient_user_id = ?
  407. AND status = ?
  408. ORDER BY gift_id DESC',
  409. array(
  410. $assocType,
  411. $assocId,
  412. $userId,
  413. GIFT_STATUS_NOT_REDEEMED
  414. ),
  415. $rangeInfo
  416. );
  417. $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
  418. return $returner;
  419. }
  420. /**
  421. * Retrieve an array of unredeemed gifts of a certain type for a recipient user.
  422. * @param $assocType int
  423. * @param $assocId int
  424. * @param $giftType int
  425. * @param $userId int
  426. * @return object DAOResultFactory containing matching Gifts
  427. */
  428. function &getNotRedeemedGiftsByTypeAndRecipient($assocType, $assocId, $giftType, $userId, $rangeInfo = null) {
  429. $result =& $this->retrieveRange(
  430. 'SELECT *
  431. FROM gifts
  432. WHERE assoc_type = ? AND assoc_id = ?
  433. AND recipient_user_id = ? AND gift_type = ?
  434. AND status = ?
  435. ORDER BY gift_id DESC',
  436. array(
  437. $assocType,
  438. $assocId,
  439. $userId,
  440. $giftType,
  441. GIFT_STATUS_NOT_REDEEMED
  442. ),
  443. $rangeInfo
  444. );
  445. $returner = new DAOResultFactory($result, $this, '_returnGiftFromRow');
  446. return $returner;
  447. }
  448. /**
  449. * Get the ID of the last inserted gift.
  450. * @return int
  451. */
  452. function getInsertGiftId() {
  453. return $this->getInsertId('gifts', 'gift_id');
  454. }
  455. }
  456. ?>