BxBaseModProfileDb.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 BaseProfile Base classes for profile modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Profile based module database queries
  13. */
  14. class BxBaseModProfileDb extends BxBaseModGeneralDb
  15. {
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function getContentInfoById ($iContentId, $bForceGet = false)
  21. {
  22. $sQuery = "SELECT `c`.*, `p`.`account_id`, `p`.`id` AS `profile_id`, `a`.`email` AS `profile_email`, `a`.`active` AS `profile_last_active`, `a`.`ip` AS `profile_ip`, `p`.`status` AS `profile_status` FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c` INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `c`.`id` AND `p`.`type` = :type) INNER JOIN `sys_accounts` AS `a` ON (`p`.`account_id` = `a`.`id`) WHERE `c`.`id` = :id";
  23. $aBindings = [
  24. 'type' => $this->_oConfig->getName(),
  25. 'id' => $iContentId
  26. ];
  27. $aInfo = [];
  28. if($bForceGet)
  29. $aInfo = $this->getRow($sQuery, $aBindings);
  30. else
  31. $aInfo = $this->fromMemory("profile_content_info_" . $this->_oConfig->getName() . $iContentId, 'getRow', $sQuery, $aBindings);
  32. /**
  33. * @hooks
  34. * @hookdef hook-profile-content_info_by_id 'profile', 'content_info_by_id' - hook to modify profile info retrieved by content id
  35. * - $unit_name - equals `profile`
  36. * - $action - equals `content_info_by_id`
  37. * - $object_id - content id
  38. * - $sender_id - not used
  39. * - $extra_params - array of additional params with the following array keys:
  40. * - `module` - [string] profile module name
  41. * - `info` - [array] by ref, profile info array as key&value pairs, can be overridden in hook processing
  42. * @hook @ref hook-profile-content_info_by_id
  43. */
  44. bx_alert('profile', 'content_info_by_id', $iContentId, 0, [
  45. 'module' => $this->_oConfig->getName(),
  46. 'info' => &$aInfo,
  47. 'info_ref' => &$aInfo
  48. ]);
  49. return $aInfo;
  50. }
  51. public function getContentInfoByProfileId ($iProfileId)
  52. {
  53. $aInfo = $this->getRow("SELECT `c`.*, `p`.`account_id`, `p`.`id` AS `profile_id`, `a`.`email` AS `profile_email`, `a`.`active` AS `profile_last_active`, `a`.`ip` AS `profile_ip`, `p`.`status` AS `profile_status` FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c` INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `c`.`id` AND `p`.`type` = :type) INNER JOIN `sys_accounts` AS `a` ON (`p`.`account_id` = `a`.`id`) WHERE `p`.`id` = :profile_id", [
  54. 'type' => $this->_oConfig->getName(),
  55. 'profile_id' => $iProfileId
  56. ]);
  57. /**
  58. * @hooks
  59. * @hookdef hook-profile-content_info_by_profile_id 'profile', 'content_info_by_profile_id' - hook to modify profile info retrieved by profile id
  60. * - $unit_name - equals `profile`
  61. * - $action - equals `content_info_by_profile_id`
  62. * - $object_id - profile id
  63. * - $sender_id - not used
  64. * - $extra_params - array of additional params with the following array keys:
  65. * - `module` - [string] profile module name
  66. * - `info` - [array] by ref, profile info array as key&value pairs, can be overridden in hook processing
  67. * @hook @ref hook-profile-content_info_by_profile_id
  68. */
  69. bx_alert('profile', 'content_info_by_profile_id', $iProfileId, 0, [
  70. 'module' => $this->_oConfig->getName(),
  71. 'info' => &$aInfo,
  72. 'info_ref' => &$aInfo
  73. ]);
  74. return $aInfo;
  75. }
  76. public function resetContentPictureByFileId($iFileId, $sFieldPicture)
  77. {
  78. return $this->query("UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $sFieldPicture . "` = 0 WHERE `" . $sFieldPicture . "` = :file", [
  79. 'file' => $iFileId,
  80. ]);
  81. }
  82. public function updateContentPictureById($iContentId, $iProfileId, $iPictureId, $sFieldPicture)
  83. {
  84. $aBindings = array(
  85. 'pic' => $iPictureId,
  86. 'id' => $iContentId
  87. );
  88. $sWhere = '';
  89. if ($iProfileId) {
  90. $aBindings['author'] = $iProfileId;
  91. $sWhere = " AND `author` = :author ";
  92. }
  93. $sQuery = "UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $sFieldPicture . "` = :pic WHERE `id` = :id" . $sWhere;
  94. return $this->query($sQuery, $aBindings);
  95. }
  96. public function searchByTerm($sTerm, $mixedParams)
  97. {
  98. if(!$this->_oConfig->CNF['FIELDS_QUICK_SEARCH'])
  99. return [];
  100. $aBindings = [
  101. 'type' => $this->_oConfig->getName(),
  102. 'status' => BX_PROFILE_STATUS_ACTIVE
  103. ];
  104. $sSelect = "`c`.`id` AS `content_id`, `p`.`account_id`, `p`.`id` AS `profile_id`, `p`.`status` AS `profile_status` ";
  105. $sJoin = "INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `c`.`id` AND `p`.`type` = :type) INNER JOIN `sys_accounts` AS `a` ON (`a`.`id` = `p`.`account_id`)";
  106. $sWhere = '';
  107. foreach ($this->_oConfig->CNF['FIELDS_QUICK_SEARCH'] as $sField) {
  108. $aBindings[$sField] = '%' . $sTerm . '%';
  109. $sWhere .= " OR `c`.`$sField` LIKE :" . $sField;
  110. }
  111. $sWhere = "`p`.`status` = :status AND (0 $sWhere) ";
  112. $sOrderBy = " ORDER BY `a`.`logged` DESC";
  113. $sLimit = "";
  114. if((is_numeric($mixedParams) && ($iLimit = (int)$mixedParams)) || (is_array($mixedParams) && isset($mixedParams['limit']) && ($iLimit = (int)$mixedParams['limit'])))
  115. $sLimit = $this->prepareAsString(" LIMIT ?", $iLimit);
  116. $aPreferred = [];
  117. if(($iContext = (int)($mixedParams['search_params']['cp'] ?? 0)) < 0 && ($oContext = BxDolProfile::getInstance(abs($iContext))) !== false) {
  118. $aPreferred = bx_srv($oContext->getModule(), 'fans', [$oContext->getContentId(), true]);
  119. }
  120. else if(($iCntId = $mixedParams['search_params']['ci'] ?? false) && ($aCntModule = $mixedParams['search_params']['cm'] ?? false)) {
  121. $aContent = bx_srv($aCntModule, 'get_info', [$iCntId, false]);
  122. if(!bx_srv('system', 'is_module_context', [$aCntModule])) {
  123. if(($sK = 'allow_view_to') && ($iContext = $aContent[$sK] ?? 0) < 0 && ($oContext = BxDolProfile::getInstance(abs($iContext))) !== false)
  124. $aPreferred = bx_srv($oContext->getModule(), 'fans', [$oContext->getContentId(), true]);
  125. else
  126. $aPreferred = BxDolConnection::getObjectInstance('sys_profiles_friends')->getConnectedContentByType(bx_get_logged_profile_id(), $aBindings['type'], true);
  127. }
  128. else
  129. $aPreferred = bx_srv($aCntModule, 'fans', [$iCntId, true]);
  130. }
  131. if($aPreferred) {
  132. $sSelect .= ", IF(`p`.`id` IN (" . $this->implode_escape($aPreferred) . "), 1, 0) AS `profile_weight`";
  133. $sOrderBy = " ORDER BY `profile_weight` DESC, `a`.`logged` DESC";
  134. }
  135. /**
  136. * @hooks
  137. * @hookdef hook-profile-search_by_term 'profile', 'search_by_term' - hook to modify a list of profiles found by term
  138. * - $unit_name - equals `profile`
  139. * - $action - equals `search_by_term`
  140. * - $object_id - not used
  141. * - $sender_id - not used
  142. * - $extra_params - array of additional params with the following array keys:
  143. * - `module` - [string] profile module name
  144. * - `table` - [string] db table name
  145. * - `select` - [string] by ref, 'select' part of SQL query, can be overridden in hook processing
  146. * - `join` - [string] by ref, 'join' part of SQL query, can be overridden in hook processing
  147. * - `where` - [string] by ref, 'where' part of SQL query, can be overridden in hook processing
  148. * - `order_by` - [string] by ref, 'order' part of SQL query, can be overridden in hook processing
  149. * - `limit` - [string] by ref, 'limit' part of SQL query, can be overridden in hook processing
  150. * @hook @ref hook-profile-search_by_term
  151. */
  152. bx_alert('profile', 'search_by_term', 0, 0, [
  153. 'module' => $this->_oConfig->getName(),
  154. 'params' => $mixedParams['search_params'] ?? '',
  155. 'table' => $this->_oConfig->CNF['TABLE_ENTRIES'],
  156. 'select' => &$sSelect,
  157. 'join' => &$sJoin,
  158. 'where' => &$sWhere,
  159. 'order_by' => &$sOrderBy,
  160. 'limit' => &$sLimit,
  161. 'select_ref' => &$sSelect,
  162. 'join_ref' => &$sJoin,
  163. 'where_ref' => &$sWhere,
  164. 'order_by_ref' => &$sOrderBy,
  165. 'limit_ref' => &$sLimit
  166. ]);
  167. return $this->getAll("SELECT " . $sSelect . " FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c` " . $sJoin . " WHERE " . $sWhere . $sOrderBy . $sLimit, $aBindings);
  168. }
  169. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  170. {
  171. $CNF = &$this->_oConfig->CNF;
  172. $aMethod['params'][1] = array_merge($aMethod['params'][1], array(
  173. 'profile_type' => $this->_oConfig->getName()
  174. ));
  175. $sJoinClause .= " LEFT JOIN `sys_profiles` AS `tp` ON `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`=`tp`.`content_id` AND `tp`.`type`=:profile_type ";
  176. $sWhereClause .= " AND `tp`.`status`='active'";
  177. if(isset($aParams['search_params']['online'])) {
  178. $aMethod['params'][1] = array_merge($aMethod['params'][1], array(
  179. 'online_time' => (int)getParam('sys_account_online_time')
  180. ));
  181. $sJoinClause .= "
  182. INNER JOIN `sys_accounts` AS `ta` ON `tp`.`account_id`=`ta`.`id`
  183. INNER JOIN `sys_sessions` AS `ts` ON `tp`.`account_id`=`ts`.`user_id`
  184. ";
  185. $sWhereClause .= " AND `ta`.`profile_id`=`tp`.`id` AND `ts`.`date` > (UNIX_TIMESTAMP() - 60 * :online_time)";
  186. unset($aParams['search_params']['online']);
  187. }
  188. parent::_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  189. }
  190. public function getEntriesNumByParams ($aParams = [])
  191. {
  192. $CNF = &$this->_oConfig->CNF;
  193. $sSql = "SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "`
  194. INNER JOIN `sys_profiles` ON `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`=`sys_profiles`.`content_id` AND `sys_profiles`.`type`=?
  195. WHERE 1";
  196. foreach($aParams as $aValue){
  197. if (!$this->isValidOperator($aValue['operator'])) {
  198. throw new Exception('Invalid operator in getEntriesNumByParams method');
  199. }
  200. $sSql .= ' AND ' . (isset($aValue['table'])? '`' . $aValue['table'] .'`.' : '') . '`' . $aValue['key'] ."` " . $aValue['operator'] . " '" . $aValue['value'] . "'";
  201. }
  202. $sQuery = $this->prepare($sSql, $this->_oConfig->getName());
  203. return $this->getOne($sQuery);
  204. }
  205. }
  206. /** @} */