BxInvDb.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. }
  43. $sSql = "SELECT {select} FROM `{$this->_sTableInvites}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  44. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  45. $aEntries = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  46. if(!$bReturnCount)
  47. return $aEntries;
  48. $aMethod['name'] = 'getOne';
  49. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  50. return array($aEntries, (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params']));
  51. }
  52. public function deleteInvites($aParams)
  53. {
  54. $sSql = "DELETE FROM `{$this->_sTableInvites}` WHERE " . $this->arrayToSQL($aParams, " AND ");
  55. return $this->query($sSql);
  56. }
  57. public function attachInviteToRequest($iReqestId, $iInviteId)
  58. {
  59. $aBindings = array(
  60. 'id' => $iReqestId,
  61. 'invite_id' => $iInviteId
  62. );
  63. $this->query("UPDATE `{$this->_sTableRequests}` SET invite_id=:invite_id WHERE id=:id", $aBindings);
  64. }
  65. public function attachAccountIdToInvite($iAccountId, $sKey)
  66. {
  67. $aBindings = array(
  68. 'joined_account_id' => $iAccountId,
  69. 'keyvalue' => $sKey,
  70. 'date_joined' => time(),
  71. );
  72. $this->query("UPDATE `{$this->_sTableInvites}` SET `joined_account_id`=:joined_account_id, `date_joined`=:date_joined WHERE `key`=:keyvalue", $aBindings);
  73. }
  74. public function updateDateSeenForInvite($sKey)
  75. {
  76. $aBindings = array(
  77. 'date_seen' => time(),
  78. 'keyvalue' => $sKey
  79. );
  80. $this->query("UPDATE `{$this->_sTableInvites}` SET `date_seen`=:date_seen WHERE `key`=:keyvalue", $aBindings);
  81. }
  82. public function insertInvite($iAccountId, $iProfileId, $sKey, $sEmail, $iDate)
  83. {
  84. $aBindings = array(
  85. 'account_id' => $iAccountId,
  86. 'profile_id' => $iProfileId,
  87. 'keyvalue' => $sKey,
  88. 'email' => $sEmail,
  89. 'date' => $iDate
  90. );
  91. $this->query("INSERT `{$this->_sTableInvites}` (account_id, profile_id, `key`, email, date) VALUES (:account_id, :profile_id, :keyvalue, :email, :date)", $aBindings);
  92. return (int)$this->lastId();
  93. }
  94. public function getRequests($aParams, $bReturnCount = false)
  95. {
  96. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  97. $sSelectClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  98. $sJoinClause = " LEFT JOIN `{$this->_sTableInvites}` ON `bx_inv_invites`.`id` = `bx_inv_requests`.`invite_id` ";
  99. $sSelectClause = "`{$this->_sTableRequests}`.*, `{$this->_sTableInvites}`.`date_seen`, `{$this->_sTableInvites}`.`date_joined`, `{$this->_sTableInvites}`.`joined_account_id` AS joined_account, `{$this->_sTableInvites}`.`date` AS `date_invite` ";
  100. switch($aParams['type']) {
  101. case 'by_id':
  102. $aMethod['name'] = 'getRow';
  103. $aMethod['params'][1] = array(
  104. 'id' => $aParams['value']
  105. );
  106. $sWhereClause = "AND `{$this->_sTableRequests}`.`id`=:id ";
  107. $sLimitClause = "LIMIT 1";
  108. break;
  109. case 'count_all':
  110. $aMethod['name'] = 'getOne';
  111. $sSelectClause = "COUNT(`{$this->_sTableRequests}`.`id`) AS `count`";
  112. $sLimitClause = "LIMIT 1";
  113. break;
  114. }
  115. $sSql = "SELECT {select} FROM `{$this->_sTableRequests}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  116. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  117. $aEntries = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  118. if(!$bReturnCount)
  119. return $aEntries;
  120. $aMethod['name'] = 'getOne';
  121. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  122. return array($aEntries, (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params']));
  123. }
  124. }
  125. /** @} */