BxBaseModProfileDb.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. ]);
  48. return $aInfo;
  49. }
  50. public function getContentInfoByProfileId ($iProfileId)
  51. {
  52. $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", [
  53. 'type' => $this->_oConfig->getName(),
  54. 'profile_id' => $iProfileId
  55. ]);
  56. /**
  57. * @hooks
  58. * @hookdef hook-profile-content_info_by_profile_id 'profile', 'content_info_by_profile_id' - hook to modify profile info retrieved by profile id
  59. * - $unit_name - equals `profile`
  60. * - $action - equals `content_info_by_profile_id`
  61. * - $object_id - profile id
  62. * - $sender_id - not used
  63. * - $extra_params - array of additional params with the following array keys:
  64. * - `module` - [string] profile module name
  65. * - `info` - [array] by ref, profile info array as key&value pairs, can be overridden in hook processing
  66. * @hook @ref hook-profile-content_info_by_profile_id
  67. */
  68. bx_alert('profile', 'content_info_by_profile_id', $iProfileId, 0, [
  69. 'module' => $this->_oConfig->getName(),
  70. 'info' => &$aInfo
  71. ]);
  72. return $aInfo;
  73. }
  74. public function resetContentPictureByFileId($iFileId, $sFieldPicture)
  75. {
  76. return $this->query("UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $sFieldPicture . "` = 0 WHERE `" . $sFieldPicture . "` = :file", [
  77. 'file' => $iFileId,
  78. ]);
  79. }
  80. public function updateContentPictureById($iContentId, $iProfileId, $iPictureId, $sFieldPicture)
  81. {
  82. $aBindings = array(
  83. 'pic' => $iPictureId,
  84. 'id' => $iContentId
  85. );
  86. $sWhere = '';
  87. if ($iProfileId) {
  88. $aBindings['author'] = $iProfileId;
  89. $sWhere = " AND `author` = :author ";
  90. }
  91. $sQuery = "UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $sFieldPicture . "` = :pic WHERE `id` = :id" . $sWhere;
  92. return $this->query($sQuery, $aBindings);
  93. }
  94. public function searchByTerm($sTerm, $mixedParams)
  95. {
  96. if(!$this->_oConfig->CNF['FIELDS_QUICK_SEARCH'])
  97. return [];
  98. $aBindings = [
  99. 'type' => $this->_oConfig->getName(),
  100. 'status' => BX_PROFILE_STATUS_ACTIVE
  101. ];
  102. $sSelect = "`c`.`id` AS `content_id`, `p`.`account_id`, `p`.`id` AS `profile_id`, `p`.`status` AS `profile_status` ";
  103. $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`)";
  104. $sWhere = '';
  105. foreach ($this->_oConfig->CNF['FIELDS_QUICK_SEARCH'] as $sField) {
  106. $aBindings[$sField] = '%' . $sTerm . '%';
  107. $sWhere .= " OR `c`.`$sField` LIKE :" . $sField;
  108. }
  109. $sWhere = "`p`.`status` = :status AND (0 $sWhere) ";
  110. $sOrderBy = " ORDER BY `a`.`logged` DESC";
  111. $sLimit = "";
  112. if((is_numeric($mixedParams) && ($iLimit = (int)$mixedParams)) || (is_array($mixedParams) && isset($mixedParams['limit']) && ($iLimit = (int)$mixedParams['limit'])))
  113. $sLimit = $this->prepareAsString(" LIMIT ?", $iLimit);
  114. /**
  115. * @hooks
  116. * @hookdef hook-profile-search_by_term 'profile', 'search_by_term' - hook to modify a list of profiles found by term
  117. * - $unit_name - equals `profile`
  118. * - $action - equals `search_by_term`
  119. * - $object_id - not used
  120. * - $sender_id - not used
  121. * - $extra_params - array of additional params with the following array keys:
  122. * - `module` - [string] profile module name
  123. * - `table` - [string] db table name
  124. * - `select` - [string] by ref, 'select' part of SQL query, can be overridden in hook processing
  125. * - `join` - [string] by ref, 'join' part of SQL query, can be overridden in hook processing
  126. * - `where` - [string] by ref, 'where' part of SQL query, can be overridden in hook processing
  127. * - `order_by` - [string] by ref, 'order' part of SQL query, can be overridden in hook processing
  128. * - `limit` - [string] by ref, 'limit' part of SQL query, can be overridden in hook processing
  129. * @hook @ref hook-profile-search_by_term
  130. */
  131. bx_alert('profile', 'search_by_term', 0, 0, [
  132. 'module' => $this->_oConfig->getName(),
  133. 'params' => $mixedParams['search_params'] ?? '',
  134. 'table' => $this->_oConfig->CNF['TABLE_ENTRIES'],
  135. 'select' => &$sSelect,
  136. 'join' => &$sJoin,
  137. 'where' => &$sWhere,
  138. 'order_by' => &$sOrderBy,
  139. 'limit' => &$sLimit
  140. ]);
  141. return $this->getAll("SELECT " . $sSelect . " FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c` " . $sJoin . " WHERE " . $sWhere . $sOrderBy . $sLimit, $aBindings);
  142. }
  143. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  144. {
  145. $CNF = &$this->_oConfig->CNF;
  146. $aMethod['params'][1] = array_merge($aMethod['params'][1], array(
  147. 'profile_type' => $this->_oConfig->getName()
  148. ));
  149. $sJoinClause .= " LEFT JOIN `sys_profiles` AS `tp` ON `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`=`tp`.`content_id` AND `tp`.`type`=:profile_type ";
  150. $sWhereClause .= " AND `tp`.`status`='active'";
  151. if(isset($aParams['search_params']['online'])) {
  152. $aMethod['params'][1] = array_merge($aMethod['params'][1], array(
  153. 'online_time' => (int)getParam('sys_account_online_time')
  154. ));
  155. $sJoinClause .= "
  156. INNER JOIN `sys_accounts` AS `ta` ON `tp`.`account_id`=`ta`.`id`
  157. INNER JOIN `sys_sessions` AS `ts` ON `tp`.`account_id`=`ts`.`user_id`
  158. ";
  159. $sWhereClause .= " AND `ta`.`profile_id`=`tp`.`id` AND `ts`.`date` > (UNIX_TIMESTAMP() - 60 * :online_time)";
  160. unset($aParams['search_params']['online']);
  161. }
  162. parent::_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  163. }
  164. public function getEntriesNumByParams ($aParams = [])
  165. {
  166. $CNF = &$this->_oConfig->CNF;
  167. $sSql = "SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "`
  168. INNER JOIN `sys_profiles` ON `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`=`sys_profiles`.`content_id` AND `sys_profiles`.`type`=?
  169. WHERE 1";
  170. foreach($aParams as $aValue){
  171. $sSql .= ' AND ' . (isset($aValue['table'])? '`' . $aValue['table'] .'`.' : '') . '`' . $aValue['key'] ."` " . $aValue['operator'] . " '" . $aValue['value'] . "'";
  172. }
  173. $sQuery = $this->prepare($sSql, $this->_oConfig->getName());
  174. return $this->getOne($sQuery);
  175. }
  176. }
  177. /** @} */