BxBaseModProfileDb.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. $sQuery = $this->prepare ("SELECT `c`.*, `p`.`account_id`, `p`.`id` AS `profile_id`, `a`.`email` AS `profile_email`, `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` = ?) INNER JOIN `sys_accounts` AS `a` ON (`p`.`account_id` = `a`.`id`) WHERE `c`.`id` = ?", $this->_oConfig->getName(), $iContentId);
  23. $aInfo = $this->getRow($sQuery);
  24. bx_alert('profile', 'content_info_by_id', $iContentId, 0, array('module' => $this->_oConfig->getName(), 'info' => &$aInfo));
  25. return $aInfo;
  26. }
  27. public function resetContentPictureByFileId($iFileId, $sFieldPicture)
  28. {
  29. return $this->query("UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $sFieldPicture . "` = 0 WHERE `" . $sFieldPicture . "` = :file", [
  30. 'file' => $iFileId,
  31. ]);
  32. }
  33. public function updateContentPictureById($iContentId, $iProfileId, $iPictureId, $sFieldPicture)
  34. {
  35. $aBindings = array(
  36. 'pic' => $iPictureId,
  37. 'id' => $iContentId
  38. );
  39. $sWhere = '';
  40. if ($iProfileId) {
  41. $aBindings['author'] = $iProfileId;
  42. $sWhere = " AND `author` = :author ";
  43. }
  44. $sQuery = "UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $sFieldPicture . "` = :pic WHERE `id` = :id" . $sWhere;
  45. return $this->query($sQuery, $aBindings);
  46. }
  47. public function searchByTerm($sTerm, $iLimit)
  48. {
  49. if (!$this->_oConfig->CNF['FIELDS_QUICK_SEARCH'])
  50. return array();
  51. $aBindings = array(
  52. 'type' => $this->_oConfig->getName(),
  53. 'status' => BX_PROFILE_STATUS_ACTIVE
  54. );
  55. $sWhere = '';
  56. foreach ($this->_oConfig->CNF['FIELDS_QUICK_SEARCH'] as $sField) {
  57. $aBindings[$sField] = $sTerm . '%';
  58. $sWhere .= " OR `c`.`$sField` LIKE :" . $sField;
  59. }
  60. $sOrderBy = $this->prepareAsString(" ORDER BY `a`.`logged` DESC LIMIT ?", (int)$iLimit);
  61. $sQuery = "SELECT `c`.`id` AS `content_id`, `p`.`account_id`, `p`.`id` AS `profile_id`, `p`.`status` AS `profile_status` FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c`
  62. INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `c`.`id` AND `p`.`type` = :type)
  63. INNER JOIN `sys_accounts` AS `a` ON (`a`.`id` = `p`.`account_id`)
  64. WHERE `p`.`status` = :status AND (0 $sWhere)" . $sOrderBy;
  65. return $this->getAll($sQuery, $aBindings);
  66. }
  67. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  68. {
  69. $CNF = &$this->_oConfig->CNF;
  70. $aMethod['params'][1] = array_merge($aMethod['params'][1], array(
  71. 'profile_type' => $this->_oConfig->getName()
  72. ));
  73. $sJoinClause .= " LEFT JOIN `sys_profiles` AS `tp` ON `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`=`tp`.`content_id` AND `tp`.`type`=:profile_type ";
  74. $sWhereClause .= " AND `tp`.`status`='active'";
  75. if(isset($aParams['search_params']['online'])) {
  76. $aMethod['params'][1] = array_merge($aMethod['params'][1], array(
  77. 'online_time' => (int)getParam('sys_account_online_time')
  78. ));
  79. $sJoinClause .= "
  80. INNER JOIN `sys_accounts` AS `ta` ON `tp`.`account_id`=`ta`.`id`
  81. INNER JOIN `sys_sessions` AS `ts` ON `tp`.`account_id`=`ts`.`user_id`
  82. ";
  83. $sWhereClause .= " AND `ta`.`profile_id`=`tp`.`id` AND `ts`.`date` > (UNIX_TIMESTAMP() - 60 * :online_time)";
  84. unset($aParams['search_params']['online']);
  85. }
  86. parent::_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  87. }
  88. }
  89. /** @} */