BxBaseServiceRecommendations.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * System services for recommendations.
  11. */
  12. class BxBaseServiceRecommendations extends BxDol
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. public function serviceUpdateData($iProfileId)
  19. {
  20. $aObjects = BxDolRecommendationQuery::getObjects();
  21. foreach($aObjects as $aObject)
  22. if(($oRecommendation = BxDolRecommendation::getObjectInstance($aObject['name'])) !== false)
  23. $oRecommendation->processCriteria($iProfileId);
  24. }
  25. /**
  26. * @page service Service Calls
  27. * @section bx_system_general System Services
  28. * @subsection bx_system_general-recommendations Recommendations
  29. * @subsubsection bx_system_general-perform perform
  30. *
  31. * @code bx_srv('system', 'perform', [[...]], 'TemplServiceRecommendations'); @endcode
  32. * @code {{~system:perform:TemplServiceRecommendations[[...]]~}} @endcode
  33. *
  34. * Performs an action (add, remove, etc) with recommendations object.
  35. * @param $aParams an array with necessary parameters
  36. *
  37. * @see BxBaseServiceRecommendations::servicePerform
  38. */
  39. /**
  40. * @ref bx_system_general-perform "perform"
  41. * @api @ref bx_system_general-perform "perform"
  42. */
  43. public function servicePerform($aParams)
  44. {
  45. if(is_string($aParams))
  46. $aParams = json_decode($aParams, true);
  47. if(!$aParams['o'] || !$aParams['a'] || !$aParams['iid'] || !$aParams['cid'])
  48. return ['code' => 1];
  49. $oRecommendation = BxDolRecommendation::getObjectInstance($aParams['o']);
  50. if(!$oRecommendation)
  51. return ['code' => 2];
  52. $sMethod = 'action' . bx_gen_method_name($aParams['a']);
  53. if(!method_exists($oRecommendation, $sMethod))
  54. return ['code' => 2];
  55. $aResult = $oRecommendation->$sMethod($aParams['iid'], $aParams['cid']);
  56. if($aResult['code'] != 0)
  57. return ['code' => 3, 'message' => $aResult['msg']];
  58. $aFlip = [
  59. 'add' => '',
  60. 'ignore' => ''
  61. ];
  62. $sFlipped = $aFlip[$aParams['a']];
  63. return [
  64. 'a' => $sFlipped,
  65. 'title' => !empty($sFlipped) ? _t('_sys_menu_item_title_sm_' . $sFlipped) : '',
  66. ];
  67. }
  68. public function serviceGetFriendRecommendationsBySharedContext($iProfileId, $sConnection, $iPoints)
  69. {
  70. $oProfile = BxDolProfile::getInstance($iProfileId);
  71. if(!$oProfile)
  72. return [];
  73. $oConnection = BxDolConnection::getObjectInstance($sConnection);
  74. if(!$oConnection)
  75. return [];
  76. $sQuery = "SELECT `tm`.`initiator`AS `id`, SUM({points}) AS `value` FROM `{connection}` AS `tg` INNER JOIN `{connection}` AS `tm` ON `tg`.`content`=`tm`.`content` AND `tm`.`initiator`<>{profile_id} AND `tm`.`initiator` NOT IN (SELECT `content` FROM `sys_profiles_conn_friends` WHERE `initiator`={profile_id} AND `mutual`='1') AND `tm`.`mutual`='1' WHERE `tg`.`initiator`={profile_id} AND `tg`.`mutual`='1' GROUP BY `id`";
  77. $sQuery = bx_replace_markers($sQuery, [
  78. 'profile_id' => $iProfileId,
  79. 'connection' => $oConnection->getTable(),
  80. 'points' => $iPoints
  81. ]);
  82. return BxDolDb::getInstance()->getPairs($sQuery, 'id', 'value');
  83. }
  84. public function serviceGetFriendRecommendationsBySharedLocation($iProfileId, $iRadius, $iPoints)
  85. {
  86. $oProfile = BxDolProfile::getInstance($iProfileId);
  87. if(!$oProfile)
  88. return [];
  89. $sProfileModule = $oProfile->getModule();
  90. $iProfileContentId = $oProfile->getContentId();
  91. $aLocation = bx_srv($sProfileModule, 'get_location', [$iProfileContentId]);
  92. if(empty($aLocation) || !is_array($aLocation))
  93. return [];
  94. $aIds = bx_srv('system', 'profiles_search_by_location', [$aLocation, $iRadius], 'TemplServiceProfiles');
  95. if(empty($aIds) || !is_array($aIds))
  96. return [];
  97. /**
  98. * Exclude friends and oneself
  99. */
  100. $aIdsExclude = BxDolConnection::getObjectInstance('sys_profiles_friends')->getConnectedContent($iProfileId);
  101. $aIdsExclude[] = $iProfileId;
  102. $aIds = array_diff($aIds, $aIdsExclude);
  103. return array_combine($aIds, array_fill(0, count($aIds), $iPoints));
  104. }
  105. public function serviceGetSubscriptionRecommendationsBySharedContext($iProfileId, $sConnection, $iPoints)
  106. {
  107. $oProfile = BxDolProfile::getInstance($iProfileId);
  108. if(!$oProfile)
  109. return [];
  110. $oConnection = BxDolConnection::getObjectInstance($sConnection);
  111. if(!$oConnection)
  112. return [];
  113. $sQuery = "SELECT `tm`.`initiator`AS `id`, SUM({points}) AS `value` FROM `{connection}` AS `tg` INNER JOIN `{connection}` AS `tm` ON `tg`.`content`=`tm`.`content` AND `tm`.`initiator`<>{profile_id} AND `tm`.`initiator` NOT IN (SELECT `content` FROM `sys_profiles_conn_subscriptions` WHERE `initiator`={profile_id}) AND `tm`.`mutual`='1' WHERE `tg`.`initiator`={profile_id} AND `tg`.`mutual`='1' GROUP BY `id`";
  114. $sQuery = bx_replace_markers($sQuery, [
  115. 'profile_id' => $iProfileId,
  116. 'connection' => $oConnection->getTable(),
  117. 'points' => $iPoints
  118. ]);
  119. return BxDolDb::getInstance()->getPairs($sQuery, 'id', 'value');
  120. }
  121. }
  122. /** @} */