BxDolProfileQuery.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * All queries related to profiles
  11. */
  12. class BxDolProfileQuery extends BxDolDb implements iBxDolSingleton
  13. {
  14. public function __construct()
  15. {
  16. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  17. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  18. parent::__construct();
  19. }
  20. /**
  21. * Prevent cloning the instance
  22. */
  23. public function __clone()
  24. {
  25. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  26. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  27. }
  28. /**
  29. * Get singleton instance of the class
  30. */
  31. public static function getInstance()
  32. {
  33. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  34. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolProfileQuery();
  35. return $GLOBALS['bxDolClasses'][__CLASS__];
  36. }
  37. /**
  38. * Get all account profiles.
  39. * @param string $iAccountId account id
  40. * @return profile array
  41. */
  42. public function getProfilesByAccount ($iAccountId)
  43. {
  44. $oAccountQuery = BxDolAccountQuery::getInstance();
  45. $aAccountInfo = $oAccountQuery->getInfoById($iAccountId);
  46. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `account_id` = ? ORDER BY `id` = ? DESC", $iAccountId, $aAccountInfo['profile_id']);
  47. return $this->getAllWithKey($sSql, 'id');
  48. }
  49. /**
  50. * Get profile by content id, type and account.
  51. * @param string $iAccountId account id
  52. * @return array if aprofile ids, key is profile id
  53. */
  54. public function getProfileByContentTypeAccount ($iContentId, $sType, $iAccountId)
  55. {
  56. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `account_id` = ? AND `type` = ? AND `content_id` = ?", $iAccountId, $sType, $iContentId);
  57. return $this->getRow($sSql);
  58. }
  59. /**
  60. * Get profile by content id and type.
  61. * @param string $iAccountId account id
  62. * @return array if aprofile ids, key is profile id
  63. */
  64. public function getProfileByContentAndType ($iContentId, $sType)
  65. {
  66. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `content_id` = ? AND `type` = ?", $iContentId, $sType);
  67. return $this->getRow($sSql);
  68. }
  69. /**
  70. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  71. * @param $iAccountId account id
  72. * @param $iContentId content id
  73. * @param $sStatus profile status
  74. * @param $sType profile content type
  75. * @return inserted profile's id
  76. */
  77. public function insertProfile ($iAccountId, $iContentId, $sStatus, $sType = 'system')
  78. {
  79. $sSql = $this->prepare("INSERT INTO `sys_profiles` SET `account_id` = ?, `type` = ?, `content_id` = ?, `status` = ?", $iAccountId, $sType, $iContentId, $sStatus);
  80. if (!$this->query($sSql))
  81. return false;
  82. $iProfileId = $this->lastId();
  83. $this->getCurrentProfileByAccount($iAccountId); // it updates current profile id automatically if it is not defined
  84. return $iProfileId;
  85. }
  86. /**
  87. * Update profile's status
  88. * @param $iProfileId profile id
  89. * @param $sStatus profile status
  90. * @return true on success or false otherwise
  91. */
  92. public function changeStatus ($iProfileId, $sStatus)
  93. {
  94. return $this->_updateField ($iProfileId, 'status', $sStatus);
  95. }
  96. /**
  97. * Get current account profile.
  98. * @param string $iAccountId account id
  99. * @return current account's profile id
  100. */
  101. public function getCurrentProfileByAccount ($iAccountId)
  102. {
  103. $sSql = $this->prepare("SELECT `profile_id` FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iAccountId);
  104. $iProfileId = $this->getOne($sSql);
  105. if (!$iProfileId) {
  106. $sSql = $this->prepare("SELECT `id` FROM `sys_profiles` WHERE `account_id` = ? LIMIT 1", $iAccountId);
  107. $iProfileId = $this->getOne($sSql);
  108. if (!$iProfileId)
  109. return false;
  110. if (!BxDolAccountQuery::getInstance()->updateCurrentProfile($iAccountId, $iProfileId))
  111. return false;
  112. }
  113. return $iProfileId;
  114. }
  115. /**
  116. * Get profile by specified field name and value.
  117. * It is for internal usage only.
  118. * Use other funtions to get profile info, like getInfoById, etc.
  119. * @param string $sField database field name
  120. * @param mixed $sValue database field value
  121. * @return array with porfile info
  122. */
  123. protected function _getDataByField ($sField, $sValue)
  124. {
  125. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `$sField` = ? LIMIT 1", $sValue);
  126. return $this->getRow($sSql);
  127. }
  128. /**
  129. * get profile id by id
  130. */
  131. public function getIdById($iId)
  132. {
  133. return (int)$this->_getFieldByField('id', 'id', $iId);
  134. }
  135. /**
  136. * get account info by profile id
  137. */
  138. public function getAccountInfoByProfileId($iId)
  139. {
  140. $sSql = $this->prepare("SELECT `a`.* FROM `sys_accounts` AS `a` INNER JOIN `sys_profiles` AS `p` ON (`p`.`account_id` = `a`.`id`) WHERE `p`.`id` = ? LIMIT 1", $iId);
  141. return $this->getRow($sSql);
  142. }
  143. /**
  144. * get account email by profile id
  145. */
  146. public function getEmailById($iId)
  147. {
  148. $a = $this->getAccountInfoByProfileId($iId);
  149. if (!$a || empty($a['email']))
  150. return false;
  151. return $a['email'];
  152. }
  153. /**
  154. * Get profile info by id
  155. * @param int $iID profile id
  156. * @return array with profile info
  157. */
  158. public function getInfoById( $iID )
  159. {
  160. return $this->_getDataByField('id', (int)$iID);
  161. }
  162. /**
  163. * Is profile online by id
  164. * @param int $iId profile id
  165. * @return profile online status
  166. */
  167. public function isOnline($iId)
  168. {
  169. $sSql = $this->prepare("SELECT
  170. `tp`.`id`
  171. FROM `sys_profiles` AS `tp`
  172. INNER JOIN `sys_accounts` AS `ta` ON `tp`.`account_id`=`ta`.`id`
  173. INNER JOIN `sys_sessions` AS `ts` ON `tp`.`account_id`=`ts`.`user_id`
  174. WHERE
  175. `tp`.`id` = ? AND
  176. `ta`.`profile_id`=`tp`.`id` AND
  177. `ts`.`date` > (UNIX_TIMESTAMP() - 60 * ?)
  178. LIMIT 1", $iId, (int)getParam('sys_account_online_time'));
  179. return (int)$this->getOne($sSql) > 0;
  180. }
  181. /**
  182. * Get profile field by specified field name and value.
  183. * In most cases it is for internal usage only.
  184. * Use other funtions to get profile info, like getIdByEmail, etc.
  185. * @param string $sFieldRequested database field name to return
  186. * @param string $sFieldSearch database field name to search for
  187. * @param mixed $sValue database field value
  188. * @return specified profile field value
  189. */
  190. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue)
  191. {
  192. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_profiles` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  193. return $this->getOne($sSql);
  194. }
  195. /**
  196. * Update some field by profile id
  197. * In most cases it is for internal usage only.
  198. * Use other funtions to get profile info, like updateLogged, etc.
  199. * @param string $sFieldRequested database field name to return
  200. * @param string $sFieldSearch database field name to search for
  201. * @param mixed $sValue database field value
  202. * @return specified profile field value
  203. */
  204. protected function _updateField ($iId, $sFieldForUpdate, $sValue)
  205. {
  206. $sSql = $this->prepare("UPDATE `sys_profiles` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  207. return $this->query($sSql);
  208. }
  209. /**
  210. * Delete profile info by id
  211. * @param int $iID profile id
  212. * @return affected rows
  213. */
  214. public function delete($iID)
  215. {
  216. $sSql = $this->prepare("DELETE FROM `sys_profiles` WHERE `id` = ? LIMIT 1", $iID);
  217. return $this->query($sSql);
  218. }
  219. /**
  220. * Reset deleted profile ids and assign system profile ids.
  221. * Should be called after profiles module deletion.
  222. * It can be called automatically if
  223. * @code
  224. * 'process_deleted_profiles' => 1
  225. * @code
  226. * is specified in module config.php file in 'uninstall' section.
  227. */
  228. public function processDeletedProfiles ()
  229. {
  230. $this->query("UPDATE `sys_accounts` AS `a` LEFT OUTER JOIN `sys_profiles` AS `p` ON `a`.`profile_id` = `p`.`id` SET `a`.`profile_id` =0 WHERE `p`.`id` IS NULL"); // reset deleted profiles
  231. return $this->query("UPDATE `sys_accounts` AS `a` INNER JOIN `sys_profiles` AS `p` ON `a`.`id` = `p`.`account_id` AND `p`.`type` = 'system' SET `a`.`profile_id` = `p`.`id`"); // assign system profile to reset accounts
  232. }
  233. }
  234. /** @} */