1
0

BxDonationsDb.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Donations Donations
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxDonationsDb extends BxDolModuleDb
  12. {
  13. protected $_oConfig;
  14. public function __construct(&$oConfig)
  15. {
  16. parent::__construct($oConfig);
  17. $this->_oConfig = $oConfig;
  18. }
  19. public function getTypeById($iId)
  20. {
  21. return $this->getTypes(array('type' => 'by_id', 'value' => $iId));
  22. }
  23. public function getTypes($aParams)
  24. {
  25. $CNF = &$this->_oConfig->CNF;
  26. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  27. $sSelectClause = "`tt`.*";
  28. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  29. switch($aParams['type']) {
  30. case 'by_id':
  31. $aMethod['name'] = 'getRow';
  32. $aMethod['params'][1] = array(
  33. 'id' => $aParams['value']
  34. );
  35. $sWhereClause .= "AND `tt`.`id`=:id";
  36. break;
  37. case 'by_name':
  38. $aMethod['name'] = 'getRow';
  39. $aMethod['params'][1] = array(
  40. 'name' => $aParams['value']
  41. );
  42. $sWhereClause .= "AND `tt`.`name`=:name";
  43. break;
  44. case 'by_duration_amount':
  45. $aMethod['name'] = 'getRow';
  46. $aMethod['params'][1] = array(
  47. 'period' => $aParams['period'],
  48. 'period_unit' => $aParams['period_unit'],
  49. 'amount' => $aParams['amount'],
  50. );
  51. $sWhereClause .= "AND `tt`.`period`=:period AND `tt`.`period_unit`=:period_unit AND `tt`.`amount`=:amount";
  52. break;
  53. case 'by_btype_single':
  54. case 'by_btype_recurring':
  55. $aMethod['params'][1] = array(
  56. 'period' => 0,
  57. );
  58. $sWhereClause .= "AND `tt`.`period`" . ($aParams['type'] == 'by_btype_single' ? '=' : '<>') . ":period AND `tt`.`custom`='0'";
  59. if(isset($aParams['active'])) {
  60. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  61. $sWhereClause .= " AND `tt`.`active`=:active";
  62. }
  63. $sOrderClause = "`tt`.`order` ASC";
  64. break;
  65. case 'conditions':
  66. if(empty($aParams['conditions']))
  67. break;
  68. if(is_array($aParams['conditions']))
  69. $sWhereClause .= $this->arrayToSQL($aParams['conditions'], ' AND ');
  70. else if(is_string($aParams['conditions']) && !empty($aParams['bindings']) && is_array($aParams['bindings'])) {
  71. $sWhereClause .= $aParams['conditions'];
  72. if(!is_array($aMethod['params'][1]))
  73. $aMethod['params'][1] = array();
  74. $aMethod['params'][1] = array_merge($aMethod['params'][1], $aParams['bindings']);
  75. }
  76. break;
  77. case 'all':
  78. break;
  79. }
  80. if(!empty($sOrderClause))
  81. $sOrderClause = ' ORDER BY ' . $sOrderClause;
  82. if(!empty($sLimitClause))
  83. $sLimitClause = ' LIMIT ' . $sLimitClause;
  84. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_TYPES'] . "` AS `tt` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sOrderClause . $sLimitClause;
  85. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  86. }
  87. public function getTypeOrderMax()
  88. {
  89. $CNF = &$this->_oConfig->CNF;
  90. return (int)$this->getOne("SELECT MAX(`order`) FROM `" . $CNF['TABLE_TYPES'] . "` WHERE 1");
  91. }
  92. public function insertType($aSet)
  93. {
  94. $CNF = &$this->_oConfig->CNF;
  95. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_TYPES'] . "` SET " . $this->arrayToSQL($aSet)) > 0 ? $this->lastId() : false;
  96. }
  97. public function deleteTypes($aWhere)
  98. {
  99. $CNF = &$this->_oConfig->CNF;
  100. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_TYPES'] . "` WHERE " . $this->arrayToSQL($aWhere, " AND ")) > 0;
  101. }
  102. public function registerEntry($iProfileId, $aTypeId, $iCount, $sOrder, $sLicense)
  103. {
  104. $CNF = &$this->_oConfig->CNF;
  105. $iProcessed = 0;
  106. for($i = 0; $i < $iCount; $i++) {
  107. $aQueryParams = array(
  108. 'profile_id' => $iProfileId,
  109. 'type_id' => $aTypeId,
  110. 'order' => $sOrder,
  111. 'license' => $sLicense
  112. );
  113. if((int)$this->query("INSERT INTO `" . $CNF['TABLE_ENTRIES'] . "` SET " . $this->arrayToSQL($aQueryParams) . ", `added`=UNIX_TIMESTAMP()") == 0)
  114. continue;
  115. $iProcessed += 1;
  116. }
  117. return $iCount == $iProcessed;
  118. }
  119. public function unregisterEntry($iProfileId, $aTypeId, $sOrder, $sLicense)
  120. {
  121. $CNF = &$this->_oConfig->CNF;
  122. $aBindings = array(
  123. 'profile_id' => $iProfileId,
  124. 'type_id' => $aTypeId,
  125. 'order' => $sOrder,
  126. 'license' => $sLicense
  127. );
  128. $sWhereClause = "`profile_id`=:profile_id AND `type_id`=:type_id AND `order`=:order AND `license`=:license";
  129. //--- Move to deleted entries table with 'refund' as reason.
  130. $sQuery = "INSERT IGNORE INTO `" . $CNF['TABLE_ENTRIES_DELETED'] . "` SELECT *, 'refund' AS `reason`, UNIX_TIMESTAMP() AS `deleted` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE " . $sWhereClause;
  131. $this->query($sQuery, $aBindings);
  132. $sQuery = "DELETE FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE " . $sWhereClause;
  133. return $this->query($sQuery, $aBindings) !== false;
  134. }
  135. }
  136. /** @} */