BxInvDb.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 Invites Invites
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxInvDb extends BxDolModuleDb
  12. {
  13. protected $_oConfig;
  14. protected $_sTableInvites;
  15. protected $_sTableRequests;
  16. /*
  17. * Constructor.
  18. */
  19. function __construct(&$oConfig)
  20. {
  21. parent::__construct($oConfig);
  22. $this->_oConfig = $oConfig;
  23. $CNF = $oConfig->CNF;
  24. $this->_sTableInvites = $CNF['TABLE_INVITES'];
  25. $this->_sTableRequests = $CNF['TABLE_REQUESTS'];
  26. }
  27. public function getInvites($aParams, $bReturnCount = false)
  28. {
  29. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  30. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  31. $sSelectClause = "`{$this->_sTableInvites}`.*";
  32. switch($aParams['type']) {
  33. case 'count_by_account':
  34. $aMethod['name'] = 'getOne';
  35. $aMethod['params'][1] = array(
  36. 'account_id' => $aParams['value']
  37. );
  38. $sSelectClause = "COUNT(`{$this->_sTableInvites}`.`id`) AS `count`";
  39. $sWhereClause = "AND `{$this->_sTableInvites}`.`account_id`=:account_id ";
  40. $sLimitClause = "LIMIT 1";
  41. break;
  42. case 'count_by_request':
  43. $aMethod['name'] = 'getOne';
  44. $aMethod['params'][1] = array(
  45. 'request_id' => $aParams['value']
  46. );
  47. $sSelectClause = "COUNT(`{$this->_sTableInvites}`.`id`) AS `count`";
  48. $sWhereClause = "AND `{$this->_sTableInvites}`.`request_id`=:request_id ";
  49. $sLimitClause = "LIMIT 1";
  50. break;
  51. case 'account_by_request':
  52. $aMethod['name'] = 'getOne';
  53. $aMethod['params'][1] = array(
  54. 'request_id' => $aParams['value']
  55. );
  56. $sSelectClause = "MAX(`{$this->_sTableInvites}`.`joined_account_id`)";
  57. $sWhereClause = "AND joined_account_id IS NOT NULL AND `{$this->_sTableInvites}`.`request_id`=:request_id ";
  58. $sLimitClause = "LIMIT 1";
  59. break;
  60. case 'date_joined_by_request':
  61. $aMethod['name'] = 'getOne';
  62. $aMethod['params'][1] = array(
  63. 'request_id' => $aParams['value']
  64. );
  65. $sSelectClause = "MAX(`{$this->_sTableInvites}`.`date_joined`)";
  66. $sWhereClause = "AND joined_account_id IS NOT NULL AND `{$this->_sTableInvites}`.`request_id`=:request_id ";
  67. $sLimitClause = "LIMIT 1";
  68. break;
  69. case 'invites_code_by_single':
  70. $aMethod['name'] = 'getOne';
  71. $aMethod['params'][1] = array(
  72. 'keyvalue' => $aParams['value']
  73. );
  74. $sSelectClause = "GROUP_CONCAT(`{$this->_sTableInvites}`.`key` SEPARATOR ',')";
  75. $sWhereClause = "AND `{$this->_sTableInvites}`.`request_id` IN (SELECT `request_id` FROM `{$this->_sTableInvites}` WHERE `key`=:keyvalue ) ";
  76. $sLimitClause = "";
  77. break;
  78. case 'all':
  79. $aMethod['name'] = 'getAll';
  80. $aMethod['params'][1] = array(
  81. 'request_id' => $aParams['value']
  82. );
  83. $sWhereClause = "AND `{$this->_sTableInvites}`.`request_id`=:request_id ";
  84. break;
  85. }
  86. $sSql = "SELECT {select} FROM `{$this->_sTableInvites}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  87. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  88. $aEntries = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  89. if(!$bReturnCount)
  90. return $aEntries;
  91. $aMethod['name'] = 'getOne';
  92. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  93. return array($aEntries, (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params']));
  94. }
  95. public function deleteInvites($aParams)
  96. {
  97. $sSql = "DELETE FROM `{$this->_sTableInvites}` WHERE " . $this->arrayToSQL($aParams, " AND ");
  98. return $this->query($sSql);
  99. }
  100. public function attachInviteToRequest($iReqestId, $iInviteId)
  101. {
  102. $aBindings = array(
  103. 'request_id' => $iReqestId,
  104. 'id' => $iInviteId
  105. );
  106. $this->query("UPDATE `{$this->_sTableInvites}` SET request_id=:request_id WHERE `id`=:id", $aBindings);
  107. $this->updateRequestStatusByRequestId(1, $iReqestId);
  108. }
  109. public function attachAccountIdToInvite($iAccountId, $sKey)
  110. {
  111. $aBindings = array(
  112. 'joined_account_id' => $iAccountId,
  113. 'keyvalue' => $sKey,
  114. 'date_joined' => time(),
  115. );
  116. $this->query("UPDATE `{$this->_sTableInvites}` SET `joined_account_id`=:joined_account_id, `date_joined`=:date_joined WHERE `key`=:keyvalue", $aBindings);
  117. $this->updateRequestStatusByInviteCode(3, $sKey);
  118. }
  119. public function updateDateSeenForInvite($sKey)
  120. {
  121. $aBindings = array(
  122. 'date_seen' => time(),
  123. 'keyvalue' => $sKey
  124. );
  125. $this->query("UPDATE `{$this->_sTableInvites}` SET `date_seen`=:date_seen WHERE `key`=:keyvalue", $aBindings);
  126. $this->updateRequestStatusByInviteCode(2, $sKey);
  127. }
  128. public function insertInvite($iAccountId, $iProfileId, $sKey, $sEmail, $iDate)
  129. {
  130. $aBindings = array(
  131. 'account_id' => $iAccountId,
  132. 'profile_id' => $iProfileId,
  133. 'keyvalue' => $sKey,
  134. 'email' => $sEmail,
  135. 'date' => $iDate
  136. );
  137. $this->query("INSERT `{$this->_sTableInvites}` (account_id, profile_id, `key`, email, date) VALUES (:account_id, :profile_id, :keyvalue, :email, :date)", $aBindings);
  138. return (int)$this->lastId();
  139. }
  140. public function getRequests($aParams, $bReturnCount = false)
  141. {
  142. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  143. $sSelectClause = $sWhereClause = $sJoinClause = $sOrderClause = $sLimitClause = "";
  144. $sSelectClause = "`{$this->_sTableRequests}`.* ";
  145. switch($aParams['type']) {
  146. case 'by_id':
  147. $aMethod['name'] = 'getRow';
  148. $aMethod['params'][1] = array(
  149. 'id' => $aParams['value']
  150. );
  151. $sWhereClause = "AND `{$this->_sTableRequests}`.`id`=:id ";
  152. $sLimitClause = "LIMIT 1";
  153. break;
  154. case 'count_all':
  155. $aMethod['name'] = 'getOne';
  156. $sSelectClause = "COUNT(`{$this->_sTableRequests}`.`id`) AS `count`";
  157. $sLimitClause = "LIMIT 1";
  158. break;
  159. }
  160. $sSql = "SELECT {select} FROM `{$this->_sTableRequests}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  161. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  162. $aEntries = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  163. if(!$bReturnCount)
  164. return $aEntries;
  165. $aMethod['name'] = 'getOne';
  166. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  167. return array($aEntries, (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params']));
  168. }
  169. private function updateRequestStatusByRequestId($iStatus, $iReqestId)
  170. {
  171. $aBindings = array(
  172. 'request_id' => $iReqestId,
  173. 'status' => $iStatus
  174. );
  175. $this->query("UPDATE `{$this->_sTableRequests}` SET `status`=:status WHERE `id`=:request_id", $aBindings);
  176. }
  177. private function updateRequestStatusByInviteCode($iStatus, $sKey)
  178. {
  179. $iReqestId = $this->getOne("SELECT request_id FROM `{$this->_sTableInvites}` WHERE `key`=:keyvalue", array('keyvalue' => $sKey));
  180. if ($iReqestId != "")
  181. $this->updateRequestStatusByRequestId($iStatus, $iReqestId);
  182. }
  183. }
  184. /** @} */