BxBaseModProfileDb.php 8.7 KB

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