BxDolProfileQuery.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. /**
  11. * All queries related to profiles
  12. */
  13. class BxDolProfileQuery extends BxDolDb implements iBxDolSingleton
  14. {
  15. public function __construct()
  16. {
  17. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  18. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  19. parent::__construct();
  20. }
  21. /**
  22. * Prevent cloning the instance
  23. */
  24. public function __clone()
  25. {
  26. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  27. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  28. }
  29. /**
  30. * Get singleton instance of the class
  31. */
  32. public static function getInstance()
  33. {
  34. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  35. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolProfileQuery();
  36. return $GLOBALS['bxDolClasses'][__CLASS__];
  37. }
  38. /**
  39. * Get all account profiles.
  40. * @param string $iAccountId account id
  41. * @return profile array
  42. */
  43. public function getProfilesByAccount ($iAccountId)
  44. {
  45. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `account_id` = ?", $iAccountId);
  46. return $this->getAllWithKey($sSql, 'id');
  47. }
  48. /**
  49. * Get profile by content id, type and account.
  50. * @param string $iAccountId account id
  51. * @return array if aprofile ids, key is profile id
  52. */
  53. public function getProfileByContentTypeAccount ($iContentId, $sType, $iAccountId)
  54. {
  55. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `account_id` = ? AND `type` = ? AND `content_id` = ?", $iAccountId, $sType, $iContentId);
  56. return $this->getRow($sSql);
  57. }
  58. /**
  59. * Get profile by content id and type.
  60. * @param string $iAccountId account id
  61. * @return array if aprofile ids, key is profile id
  62. */
  63. public function getProfileByContentAndType ($iContentId, $sType)
  64. {
  65. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `content_id` = ? AND `type` = ?", $iContentId, $sType);
  66. return $this->getRow($sSql);
  67. }
  68. /**
  69. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  70. * @param $iAccountId account id
  71. * @param $iContentId content id
  72. * @param $sStatus profile status
  73. * @param $sType profile content type
  74. * @return inserted profile's id
  75. */
  76. public function insertProfile ($iAccountId, $iContentId, $sStatus, $sType = 'system')
  77. {
  78. $sSql = $this->prepare("INSERT INTO `sys_profiles` SET `account_id` = ?, `type` = ?, `content_id` = ?, `status` = ?", $iAccountId, $sType, $iContentId, $sStatus);
  79. if (!$this->query($sSql))
  80. return false;
  81. $iProfileId = $this->lastId();
  82. $this->getCurrentProfileByAccount($iAccountId); // it updates current profile id automatically if it is not defined
  83. return $iProfileId;
  84. }
  85. /**
  86. * Update profile's status
  87. * @param $iProfileId profile id
  88. * @param $sStatus profile status
  89. * @return true on success or false otherwise
  90. */
  91. public function changeStatus ($iProfileId, $sStatus)
  92. {
  93. return $this->_updateField ($iProfileId, 'status', $sStatus);
  94. }
  95. /**
  96. * Get current account profile.
  97. * @param string $iAccountId account id
  98. * @return current account's profile id
  99. */
  100. public function getCurrentProfileByAccount ($iAccountId)
  101. {
  102. $sSql = $this->prepare("SELECT `profile_id` FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iAccountId);
  103. $iProfileId = $this->getOne($sSql);
  104. if (!$iProfileId) {
  105. $sSql = $this->prepare("SELECT `id` FROM `sys_profiles` WHERE `account_id` = ? LIMIT 1", $iAccountId);
  106. $iProfileId = $this->getOne($sSql);
  107. if (!$iProfileId)
  108. return false;
  109. bx_import('BxDolAccountQuery');
  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. * Get profile field by specified field name and value.
  164. * In most cases it is for internal usage only.
  165. * Use other funtions to get profile info, like getIdByEmail, etc.
  166. * @param string $sFieldRequested database field name to return
  167. * @param string $sFieldSearch database field name to search for
  168. * @param mixed $sValue database field value
  169. * @return specified profile field value
  170. */
  171. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue)
  172. {
  173. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_profiles` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  174. return $this->getOne($sSql);
  175. }
  176. /**
  177. * Update some field by profile id
  178. * In most cases it is for internal usage only.
  179. * Use other funtions to get profile info, like updateLogged, etc.
  180. * @param string $sFieldRequested database field name to return
  181. * @param string $sFieldSearch database field name to search for
  182. * @param mixed $sValue database field value
  183. * @return specified profile field value
  184. */
  185. protected function _updateField ($iId, $sFieldForUpdate, $sValue)
  186. {
  187. $sSql = $this->prepare("UPDATE `sys_profiles` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  188. return $this->query($sSql);
  189. }
  190. /**
  191. * Delete profile info by id
  192. * @param int $iID profile id
  193. * @return affected rows
  194. */
  195. public function delete($iID)
  196. {
  197. $sSql = $this->prepare("DELETE FROM `sys_profiles` WHERE `id` = ? LIMIT 1", $iID);
  198. return $this->query($sSql);
  199. }
  200. /**
  201. * Reset deleted profile ids and assign system profile ids.
  202. * Should be called after profiles module deletion.
  203. * It can be called automatically if
  204. * @code
  205. * 'process_deleted_profiles' => 1
  206. * @code
  207. * is specified in module config.php file in 'uninstall' section.
  208. */
  209. public function processDeletedProfiles ()
  210. {
  211. $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
  212. 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
  213. }
  214. }
  215. /** @} */