BxDolProfileQuery.php 9.0 KB

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