1
0

BxBaseServiceRecommendations.php 5.8 KB

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