BxBaseServiceRecommendations.php 5.7 KB

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