BxDolProfileQuery.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. 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 profile array
  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 profile by account id.
  45. * @param string $iAccountId account id
  46. * @return array if aprofile ids, key is profile id
  47. */
  48. public function getProfileByContentTypeAccount ($iContentId, $sType, $iAccountId) {
  49. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `account_id` = ? AND `type` = ? AND `content_id` = ?", $iAccountId, $sType, $iContentId);
  50. return $this->getRow($sSql);
  51. }
  52. /**
  53. * Insert account and content id association. Also if currect profile id is not defined - it updates current profile id in account.
  54. * @param $iAccountId account id
  55. * @param $iContentId content id
  56. * @param $sStatus profile status
  57. * @param $sType profile content type
  58. * @return inserted profile's id
  59. */
  60. public function insertProfile ($iAccountId, $iContentId, $sStatus, $sType = 'system') {
  61. $sSql = $this->prepare("INSERT INTO `sys_profiles` SET `account_id` = ?, `type` = ?, `content_id` = ?, `status` = ?", $iAccountId, $sType, $iContentId, $sStatus);
  62. if (!$this->query($sSql))
  63. return false;
  64. $iProfileId = $this->lastId();
  65. $this->getCurrentProfileByAccount($iAccountId); // it updates current profile id automatically if it is not defined
  66. return $iProfileId;
  67. }
  68. /**
  69. * Update profile's status
  70. * @param $iProfileId profile id
  71. * @param $sStatus profile status
  72. * @return true on success or false otherwise
  73. */
  74. public function changeStatus ($iProfileId, $sStatus) {
  75. return $this->_updateField ($iProfileId, 'status', $sStatus);
  76. }
  77. /**
  78. * Get current account profile.
  79. * @param string $iAccountId account id
  80. * @return current account's profile id
  81. */
  82. public function getCurrentProfileByAccount ($iAccountId) {
  83. $sSql = $this->prepare("SELECT `profile_id` FROM `sys_accounts` WHERE `id` = ? LIMIT 1", $iAccountId);
  84. $iProfileId = $this->getOne($sSql);
  85. if (!$iProfileId) {
  86. $sSql = $this->prepare("SELECT `id` FROM `sys_profiles` WHERE `account_id` = ? LIMIT 1", $iAccountId);
  87. $iProfileId = $this->getOne($sSql);
  88. if (!$iProfileId)
  89. return false;
  90. bx_import('BxDolAccountQuery');
  91. if (!BxDolAccountQuery::getInstance()->updateCurrentProfile($iAccountId, $iProfileId))
  92. return false;
  93. }
  94. return $iProfileId;
  95. }
  96. /**
  97. * Get profile by specified field name and value.
  98. * It is for internal usage only.
  99. * Use other funtions to get profile info, like getInfoById, etc.
  100. * @param string $sField database field name
  101. * @param mixed $sValue database field value
  102. * @return array with porfile info
  103. */
  104. protected function _getDataByField ($sField, $sValue) {
  105. $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `$sField` = ? LIMIT 1", $sValue);
  106. return $this->getRow($sSql);
  107. }
  108. /**
  109. * get profile id by id
  110. */
  111. public function getIdById($iId) {
  112. return (int)$this->_getFieldByField('id', 'id', $iId);
  113. }
  114. /**
  115. * get account info by profile id
  116. */
  117. public function getAccountInfoByProfileId($iId) {
  118. $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);
  119. return $this->getRow($sSql);
  120. }
  121. /**
  122. * get account email by profile id
  123. */
  124. public function getEmailById($iId) {
  125. $a = $this->getAccountInfoByProfileId($iId);
  126. if (!$a || empty($a['email']))
  127. return false;
  128. return $a['email'];
  129. }
  130. /**
  131. * Get profile info by id
  132. * @param int $iID profile id
  133. * @return array with profile info
  134. */
  135. public function getInfoById( $iID ) {
  136. return $this->_getDataByField('id', (int)$iID);
  137. }
  138. /**
  139. * Get profile field by specified field name and value.
  140. * In most cases it is for internal usage only.
  141. * Use other funtions to get profile info, like getIdByEmail, etc.
  142. * @param string $sFieldRequested database field name to return
  143. * @param string $sFieldSearch database field name to search for
  144. * @param mixed $sValue database field value
  145. * @return specified profile field value
  146. */
  147. protected function _getFieldByField ($sFieldRequested, $sFieldSearch, $sValue) {
  148. $sSql = $this->prepare("SELECT `$sFieldRequested` FROM `sys_profiles` WHERE `$sFieldSearch` = ? LIMIT 1", $sValue);
  149. return $this->getOne($sSql);
  150. }
  151. /**
  152. * Update some field by profile id
  153. * In most cases it is for internal usage only.
  154. * Use other funtions to get profile info, like updateLogged, etc.
  155. * @param string $sFieldRequested database field name to return
  156. * @param string $sFieldSearch database field name to search for
  157. * @param mixed $sValue database field value
  158. * @return specified profile field value
  159. */
  160. protected function _updateField ($iId, $sFieldForUpdate, $sValue) {
  161. $sSql = $this->prepare("UPDATE `sys_profiles` SET `$sFieldForUpdate` = ? WHERE `id` = ? LIMIT 1", $sValue, $iId);
  162. return $this->query($sSql);
  163. }
  164. /**
  165. * Delete profile info by id
  166. * @param int $iID profile id
  167. * @return affected rows
  168. */
  169. public function delete($iID) {
  170. $sSql = $this->prepare("DELETE FROM `sys_profiles` WHERE `id` = ? LIMIT 1", $iID);
  171. return $this->query($sSql);
  172. }
  173. }
  174. /** @} */