BxDolProfileQuery.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 {
  14. public function __construct() {
  15. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  16. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  17. parent::__construct();
  18. }
  19. /**
  20. * Prevent cloning the instance
  21. */
  22. public function __clone() {
  23. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  24. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  25. }
  26. /**
  27. * Get singleton instance of the class
  28. */
  29. public static function getInstance() {
  30. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  31. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolProfileQuery();
  32. return $GLOBALS['bxDolClasses'][__CLASS__];
  33. }
  34. /**
  35. * Get all account profiles.
  36. * @param string $iAccountId account id
  37. * @return array if aprofile ids, key is profile id
  38. */
  39. public function getProfilesByAccount ($iAccountId) {
  40. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `account_id` = ?", $iAccountId);
  41. return $this->getAllWithKey($sSql, 'id');
  42. }
  43. /**
  44. * Get account profile.
  45. * @param string $iAccountId account id
  46. * @return array if aprofile ids, key is profile id
  47. */
  48. public function getAccountProfile ($iAccountId) {
  49. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `account_id` = ? AND `content_id` = ?", $iAccountId, $iAccountId);
  50. return $this->getRow($sSql);
  51. }
  52. /**
  53. * Get profile by account id.
  54. * @param string $iAccountId account id
  55. * @return array if aprofile ids, key is profile id
  56. */
  57. public function getProfileByContentTypeAccount ($iContentId, $sType, $iAccountId) {
  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. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  63. * @param $iAccountId account id
  64. * @param $iContentId content id
  65. * @param $sStatus profile status
  66. * @param $sType profile content type
  67. * @return inserted profile's id
  68. */
  69. public function insertProfile ($iAccountId, $iContentId, $sStatus, $sType = 'system') {
  70. $sSql = $this->prepare("INSERT INTO `sys_profiles` SET `account_id` = ?, `type` = ?, `content_id` = ?, `status` = ?", $iAccountId, $sType, $iContentId, $sStatus);
  71. if (!$this->query($sSql))
  72. return false;
  73. $iProfileId = $this->lastId();
  74. $this->getCurrentProfileByAccount($iAccountId); // it updates current profile id automatically if it is not defined
  75. return $iProfileId;
  76. }
  77. /**
  78. * Update profile's status
  79. * @param $iProfileId profile id
  80. * @param $sStatus profile status
  81. * @return true on success or false otherwise
  82. */
  83. public function changeStatus ($iProfileId, $sStatus) {
  84. return $this->_updateField ($iProfileId, 'status', $sStatus);
  85. }
  86. /**
  87. * Get current account profile.
  88. * @param string $iAccountId account id
  89. * @return current account's profile id
  90. */
  91. public function getCurrentProfileByAccount ($iAccountId) {
  92. $sSql = $this->prepare("SELECT `profile_id` FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iAccountId);
  93. $iProfileId = $this->getOne($sSql);
  94. if (!$iProfileId) {
  95. $sSql = $this->prepare("SELECT `id` FROM `sys_profiles` WHERE `account_id` = ? LIMIT 1", $iAccountId);
  96. $iProfileId = $this->getOne($sSql);
  97. if (!$iProfileId)
  98. return false;
  99. bx_import('BxDolAccountQuery');
  100. if (!BxDolAccountQuery::getInstance()->updateCurrentProfile($iAccountId, $iProfileId))
  101. return false;
  102. }
  103. return $iProfileId;
  104. }
  105. /**
  106. * Get profile by specified field name and value.
  107. * It is for internal usage only.
  108. * Use other funtions to get profile info, like getInfoById, etc.
  109. * @param string $sField database field name
  110. * @param mixed $sValue database field value
  111. * @return array with porfile info
  112. */
  113. protected function _getDataByField ($sField, $sValue) {
  114. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `$sField` = ? LIMIT 1", $sValue);
  115. return $this->getRow($sSql);
  116. }
  117. /**
  118. * get profile id by id
  119. */
  120. public function getIdById($iId) {
  121. return (int)$this->_getFieldByField('id', 'id', $iId);
  122. }
  123. /**
  124. * get account info by profile id
  125. */
  126. public function getAccountInfoByProfileId($iId) {
  127. $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);
  128. return $this->getRow($sSql);
  129. }
  130. /**
  131. * get account email by profile id
  132. */
  133. public function getEmailById($iId) {
  134. $a = $this->getAccountInfoByProfileId($iId);
  135. if (!$a || empty($a['email']))
  136. return false;
  137. return $a['email'];
  138. }
  139. /**
  140. * Get profile info by id
  141. * @param int $iID profile id
  142. * @return array with profile info
  143. */
  144. public function getInfoById( $iID ) {
  145. return $this->_getDataByField('id', (int)$iID);
  146. }
  147. /**
  148. * Get profile field by specified field name and value.
  149. * In most cases it is for internal usage only.
  150. * Use other funtions to get profile info, like getIdByEmail, etc.
  151. * @param string $sFieldRequested database field name to return
  152. * @param string $sFieldSearch database field name to search for
  153. * @param mixed $sValue database field value
  154. * @return specified profile field value
  155. */
  156. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue) {
  157. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_profiles` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  158. return $this->getOne($sSql);
  159. }
  160. /**
  161. * Update some field by profile id
  162. * In most cases it is for internal usage only.
  163. * Use other funtions to get profile info, like updateLogged, etc.
  164. * @param string $sFieldRequested database field name to return
  165. * @param string $sFieldSearch database field name to search for
  166. * @param mixed $sValue database field value
  167. * @return specified profile field value
  168. */
  169. protected function _updateField ($iId, $sFieldForUpdate, $sValue) {
  170. $sSql = $this->prepare("UPDATE `sys_profiles` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  171. return $this->query($sSql);
  172. }
  173. /**
  174. * Delete profile info by id
  175. * @param int $iID profile id
  176. * @return affected rows
  177. */
  178. public function delete($iID) {
  179. $sSql = $this->prepare("DELETE FROM `sys_profiles` WHERE `id` = ? LIMIT 1", $iID);
  180. return $this->query($sSql);
  181. }
  182. }
  183. /** @} */