BxInvDb.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 'profile_id_by_joined_account_id':
  61. $aMethod['name'] = 'getOne';
  62. $aMethod['params'][1] = array(
  63. 'joined_account_id' => $aParams['value']
  64. );
  65. $sSelectClause = "MAX(`{$this->_sTableInvites}`.`profile_id`)";
  66. $sWhereClause = "AND `{$this->_sTableInvites}`.`joined_account_id`=:joined_account_id ";
  67. $sLimitClause = "LIMIT 1";
  68. break;
  69. case 'date_joined_by_request':
  70. $aMethod['name'] = 'getOne';
  71. $aMethod['params'][1] = array(
  72. 'request_id' => $aParams['value']
  73. );
  74. $sSelectClause = "MAX(`{$this->_sTableInvites}`.`date_joined`)";
  75. $sWhereClause = "AND joined_account_id IS NOT NULL AND `{$this->_sTableInvites}`.`request_id`=:request_id ";
  76. $sLimitClause = "LIMIT 1";
  77. break;
  78. case 'invites_code_by_single':
  79. $aMethod['name'] = 'getOne';
  80. $aMethod['params'][1] = array(
  81. 'keyvalue' => $aParams['value']
  82. );
  83. $sSelectClause = "GROUP_CONCAT(`{$this->_sTableInvites}`.`key` SEPARATOR ',')";
  84. $sWhereClause = "AND `{$this->_sTableInvites}`.`request_id` IN (SELECT `request_id` FROM `{$this->_sTableInvites}` WHERE `key`=:keyvalue ) ";
  85. $sLimitClause = "";
  86. break;
  87. case 'all':
  88. $aMethod['name'] = 'getAll';
  89. $aMethod['params'][1] = array(
  90. 'request_id' => $aParams['value']
  91. );
  92. $sWhereClause = "AND `{$this->_sTableInvites}`.`request_id`=:request_id ";
  93. break;
  94. }
  95. $sSql = "SELECT {select} FROM `{$this->_sTableInvites}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  96. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  97. $aEntries = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  98. if(!$bReturnCount)
  99. return $aEntries;
  100. $aMethod['name'] = 'getOne';
  101. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  102. return array($aEntries, (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params']));
  103. }
  104. public function deleteInvites($aParams)
  105. {
  106. $sSql = "DELETE FROM `{$this->_sTableInvites}` WHERE " . $this->arrayToSQL($aParams, " AND ");
  107. return $this->query($sSql);
  108. }
  109. public function deleteInvitesByAccount($aParams)
  110. {
  111. $sSql = "DELETE FROM `{$this->_sTableInvites}` WHERE " . $this->arrayToSQL($aParams, " AND ");
  112. return $this->query($sSql);
  113. }
  114. public function attachInviteToRequest($iReqestId, $iInviteId)
  115. {
  116. $aBindings = array(
  117. 'request_id' => $iReqestId,
  118. 'id' => $iInviteId
  119. );
  120. $this->query("UPDATE `{$this->_sTableInvites}` SET request_id=:request_id WHERE `id`=:id", $aBindings);
  121. $this->updateRequestStatusByRequestId(1, $iReqestId);
  122. }
  123. public function attachAccountIdToInvite($iAccountId, $sKey)
  124. {
  125. $aBindings = array(
  126. 'joined_account_id' => $iAccountId,
  127. 'keyvalue' => $sKey,
  128. 'date_joined' => time(),
  129. );
  130. $this->query("UPDATE `{$this->_sTableInvites}` SET `joined_account_id`=:joined_account_id, `date_joined`=:date_joined WHERE `key`=:keyvalue", $aBindings);
  131. $this->updateRequestStatusByInviteCode(3, $sKey);
  132. }
  133. public function updateDateSeenForInvite($sKey)
  134. {
  135. $aBindings = array(
  136. 'date_seen' => time(),
  137. 'keyvalue' => $sKey
  138. );
  139. $this->query("UPDATE `{$this->_sTableInvites}` SET `date_seen`=:date_seen WHERE `key`=:keyvalue", $aBindings);
  140. $this->updateRequestStatusByInviteCode(2, $sKey);
  141. }
  142. public function insertInvite($iAccountId, $iProfileId, $sKey, $sEmail, $iDate)
  143. {
  144. $aBindings = array(
  145. 'account_id' => $iAccountId,
  146. 'profile_id' => $iProfileId,
  147. 'keyvalue' => $sKey,
  148. 'email' => $sEmail,
  149. 'date' => $iDate
  150. );
  151. $this->query("INSERT `{$this->_sTableInvites}` (account_id, profile_id, `key`, email, date) VALUES (:account_id, :profile_id, :keyvalue, :email, :date)", $aBindings);
  152. return (int)$this->lastId();
  153. }
  154. public function getRequests($aParams, $bReturnCount = false)
  155. {
  156. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  157. $sSelectClause = $sWhereClause = $sJoinClause = $sOrderClause = $sLimitClause = "";
  158. $sSelectClause = "`{$this->_sTableRequests}`.* ";
  159. switch($aParams['type']) {
  160. case 'by_id':
  161. $aMethod['name'] = 'getRow';
  162. $aMethod['params'][1] = array(
  163. 'id' => $aParams['value']
  164. );
  165. $sWhereClause = "AND `{$this->_sTableRequests}`.`id`=:id ";
  166. $sLimitClause = "LIMIT 1";
  167. break;
  168. case 'count_all':
  169. $aMethod['name'] = 'getOne';
  170. $sSelectClause = "COUNT(`{$this->_sTableRequests}`.`id`) AS `count`";
  171. break;
  172. case 'count_by_email':
  173. $aMethod['name'] = 'getOne';
  174. $sSelectClause = "COUNT(`{$this->_sTableRequests}`.`id`) AS `count`";
  175. $aMethod['params'][1] = array(
  176. 'email' => $aParams['value']
  177. );
  178. $sWhereClause = "AND `{$this->_sTableRequests}`.`email`=:email ";
  179. break;
  180. }
  181. $sSql = "SELECT {select} FROM `{$this->_sTableRequests}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  182. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  183. $aEntries = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  184. if(!$bReturnCount)
  185. return $aEntries;
  186. $aMethod['name'] = 'getOne';
  187. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  188. return array($aEntries, (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params']));
  189. }
  190. public function getDataForCharts($iDateFrom, $iDateTo, $isInvited = false)
  191. {
  192. $aBindings = array(
  193. 'datefrom' => $iDateFrom,
  194. 'dateto' => $iDateTo
  195. );
  196. $sQuery = "SELECT DATE(FROM_UNIXTIME(`date`)) AS `period`, YEAR(FROM_UNIXTIME(`date`)) AS `year`, COUNT(*) AS `count` FROM `" . $this->_sTableInvites . "` WHERE `date` >= :datefrom AND `date` <= :dateto " . $this->getAddSqlForCharts($isInvited) . " GROUP BY `period`, `year` ORDER BY `year`, `period` ASC";
  197. return $this->getAll($sQuery, $aBindings);
  198. }
  199. public function getInitValueForCharts($iDateFrom, $isInvited = false)
  200. {
  201. $aBindings = array(
  202. 'datefrom' => $iDateFrom
  203. );
  204. $sQuery = "SELECT COUNT(*) AS `count` FROM " . $this->_sTableInvites . " WHERE `date` < :datefrom " . $this->getAddSqlForCharts($isInvited);
  205. return $this->getOne($sQuery, $aBindings);
  206. }
  207. private function getAddSqlForCharts($isInvited)
  208. {
  209. $sSqlAdd = "";
  210. if ($isInvited){
  211. $sSqlAdd = " AND `joined_account_id` IS NOT NULL ";
  212. }
  213. return $sSqlAdd;
  214. }
  215. private function updateRequestStatusByRequestId($iStatus, $iReqestId)
  216. {
  217. $aBindings = array(
  218. 'request_id' => $iReqestId,
  219. 'status' => $iStatus
  220. );
  221. $this->query("UPDATE `{$this->_sTableRequests}` SET `status`=:status WHERE `id`=:request_id AND `status` < :status", $aBindings);
  222. }
  223. private function updateRequestStatusByInviteCode($iStatus, $sKey)
  224. {
  225. $iReqestId = $this->getOne("SELECT request_id FROM `{$this->_sTableInvites}` WHERE `key`=:keyvalue", array('keyvalue' => $sKey));
  226. if ($iReqestId != "")
  227. $this->updateRequestStatusByRequestId($iStatus, $iReqestId);
  228. }
  229. }
  230. /** @} */