1
0

BxBaseServiceConnections.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 connections.
  11. */
  12. class BxBaseServiceConnections extends BxDol
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. /**
  19. * @page service Service Calls
  20. * @section bx_system_general System Services
  21. * @subsection bx_system_general-connections Connections
  22. * @subsubsection bx_system_general-perform perform
  23. *
  24. * @code bx_srv('system', 'perform', [[...]], 'TemplVoteServices'); @endcode
  25. * @code {{~system:perform:TemplVoteServices[[...]]~}} @endcode
  26. *
  27. * Performs Perform an action (add, remove, etc) with connection object.
  28. * @param $aParams an array with necessary parameters
  29. *
  30. * @see BxBaseServiceConnections::servicePerform
  31. */
  32. /**
  33. * @ref bx_system_general-perform "perform"
  34. * @api @ref bx_system_general-perform "perform"
  35. */
  36. public function servicePerform($aParams)
  37. {
  38. if(is_string($aParams))
  39. $aParams = json_decode($aParams, true);
  40. if(!$aParams['o'] || !$aParams['a'] || !$aParams['iid'] || !$aParams['cid'])
  41. return ['code' => 1];
  42. $oConnection = BxDolConnection::getObjectInstance($aParams['o']);
  43. if(!$oConnection)
  44. return ['code' => 2];
  45. $sMethodHas = 'hasQuestionnaire';
  46. $sMethodIsAnswered = 'isQuestionnaireAnswered';
  47. if($aParams['a'] == 'add' && method_exists($oConnection, $sMethodHas) && $oConnection->$sMethodHas($aParams['cid']) && method_exists($oConnection, $sMethodIsAnswered) && !$oConnection->$sMethodIsAnswered($aParams['cid'], $aParams['iid'])) {
  48. $sModule = $oConnection->getModule();
  49. return [
  50. 'a' => 'questionnaire',
  51. 'data' => [
  52. bx_api_get_block('form', $oConnection->getQuestionnaireForm('add', $aParams['cid'])->getCodeAPI(), ['ext' => [
  53. 'name' => $sModule,
  54. 'request' => ['url' => '/api.php?r=' . $sModule . '/get_questionnaire&params[]=main&params[]=' . $aParams['o'] . '&params[]=add&params[]=' . $aParams['cid'], 'immutable' => true]
  55. ]])
  56. ]
  57. ];
  58. }
  59. $sMethod = 'action' . bx_gen_method_name($aParams['a']);
  60. if(!method_exists($oConnection, $sMethod))
  61. return ['code' => 2];
  62. $aResult = $oConnection->$sMethod($aParams['cid'], $aParams['iid']);
  63. if($aResult['err'] !== false)
  64. return ['code' => 3, 'message' => $aResult['msg']];
  65. if(($aParams['r'] ?? '') == 'object')
  66. return $oConnection->getElementAPI($aParams['cid'], $aParams['iid']);
  67. $aFlip = ['add' => 'remove', 'remove' => 'add'];
  68. return [
  69. 'a' => $aFlip[$aParams['a']],
  70. 'title' => $this->serviceGetActionTitle($aParams['o'], $aParams['a'], $aParams['iid'], $aParams['cid'], true),
  71. ];
  72. }
  73. public function serviceGetActionTitle($sObject, $sAction, $iInitiatorId, $iContentId, $bFlip = false)
  74. {
  75. if(empty($sAction))
  76. return '';
  77. $oConnection = BxDolConnection::getObjectInstance($sObject);
  78. if(!$oConnection || !$oConnection->isProfileContent())
  79. return '';
  80. $sMethod = 'get_menu_item_title_by_connection';
  81. $aContentProfileInfo = BxDolProfileQuery::getInstance()->getInfoById($iContentId);
  82. if(empty($aContentProfileInfo) || !is_array($aContentProfileInfo) || !BxDolRequest::serviceExists($aContentProfileInfo['type'], $sMethod))
  83. return '';
  84. $aTitles = bx_srv($aContentProfileInfo['type'], $sMethod, [$sObject, '', $iContentId, $iInitiatorId]);
  85. if(empty($aTitles) || !is_array($aTitles))
  86. return '';
  87. $aFlip = ['add' => 'remove', 'remove' => 'add'];
  88. if($bFlip)
  89. $sAction = $aFlip[$sAction];
  90. return !empty($aTitles[$sAction]) ? _t($aTitles[$sAction]) : '';
  91. }
  92. /**
  93. * get grid with friends connections
  94. */
  95. public function serviceConnectionsTable ($iProfileId = 0)
  96. {
  97. if(!$iProfileId && bx_get('profile_id') !== false)
  98. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  99. if(!$iProfileId)
  100. return false;
  101. if(bx_is_api())
  102. return bx_srv('system', 'browse_friends', [
  103. 'profile_id' => $iProfileId
  104. ], 'TemplServiceProfiles');
  105. $oGrid = BxDolGrid::getObjectInstance('sys_grid_connections');
  106. if(!$oGrid)
  107. return false;
  108. $oGrid->setProfile($iProfileId);
  109. return $oGrid->getCode();
  110. }
  111. /**
  112. * get grid with friends connection requests
  113. */
  114. public function serviceConnectionsRequestTable ()
  115. {
  116. $oGrid = BxDolGrid::getObjectInstance('sys_grid_connections_requests');
  117. if (!$oGrid)
  118. return false;
  119. return $oGrid->getCode();
  120. }
  121. /**
  122. * get number of received unconfirmed connections (friend requests)
  123. * @param $sConnectionsObject connections object to get unconfirmed connections from
  124. * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
  125. * @param $bNew return all or only new (appeared since last check) unconfirmed connections
  126. * @return number
  127. */
  128. public function serviceGetUnconfirmedConnectionsNum ($sConnectionsObject, $mixedId = 0, $bNew = false)
  129. {
  130. $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
  131. if (!$oConnection)
  132. return 0;
  133. if (!$mixedId)
  134. $mixedId = bx_get_logged_profile_id();
  135. $sMethodName = 'getConnectedInitiators';
  136. $aMethodParams = [$mixedId, 0];
  137. if($bNew) {
  138. $sMethodName .= 'Since';
  139. $aMethodParams = [$mixedId, BxDolProfileQuery::getInstance()->getProfileTrack($mixedId, 'view_friend_requests'), 0];
  140. }
  141. $i = 0;
  142. $a = call_user_func_array([$oConnection, $sMethodName], $aMethodParams); // get received friend requests
  143. foreach($a as $iId)
  144. if(BxDolProfile::getInstance($iId))
  145. ++$i;
  146. return $i;
  147. }
  148. public function serviceGetLiveUpdatesUnconfirmedConnections($sModule, $sConnectionsObject, $aMenuItemParent, $aMenuItemChild, $iCount = 0)
  149. {
  150. $iProfile = bx_get_logged_profile_id();
  151. $oProfile = BxDolProfile::getInstance($iProfile);
  152. if(!$oProfile || $oProfile->getModule() != $sModule)
  153. return false;
  154. $iCountNew = (int)$this->serviceGetUnconfirmedConnectionsNum($sConnectionsObject, $iProfile);
  155. if($iCountNew == (int)$iCount)
  156. return false;
  157. return array(
  158. 'count' => $iCountNew, // required
  159. 'method' => 'bx_menu_show_live_update(oData)', // required
  160. 'data' => array(
  161. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  162. 'content' => '{count}'
  163. )),
  164. 'mi_parent' => $aMenuItemParent,
  165. 'mi_child' => $aMenuItemChild
  166. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  167. );
  168. }
  169. public function serviceGetLiveUpdatesUnconfirmedConnectionsNew($sModule, $sConnectionsObject, $aMenuItemParent, $aMenuItemChild, $iCount = 0)
  170. {
  171. $iProfile = bx_get_logged_profile_id();
  172. $oProfile = BxDolProfile::getInstance($iProfile);
  173. if(!$oProfile || $oProfile->getModule() != $sModule)
  174. return false;
  175. $iCountNew = (int)$this->serviceGetUnconfirmedConnectionsNum($sConnectionsObject, $iProfile, true);
  176. if($iCountNew == (int)$iCount)
  177. return false;
  178. return [
  179. 'count' => $iCountNew, // required
  180. 'method' => 'bx_menu_show_live_update(oData)', // required
  181. 'data' => [
  182. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', [
  183. 'content' => '{count}'
  184. ]),
  185. 'mi_parent' => $aMenuItemParent,
  186. 'mi_child' => $aMenuItemChild
  187. ], // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  188. ];
  189. }
  190. /**
  191. * get number of initiated connections (like "my subscriptions")
  192. * @param $sConnectionsObject connections object to get connections from
  193. * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
  194. * @return number
  195. */
  196. public function serviceGetConnectedContentNum ($sConnectionsObject, $mixedId = 0, $isMutual = false)
  197. {
  198. $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
  199. if (!$oConnection)
  200. return 0;
  201. if (!$mixedId)
  202. $mixedId = bx_get_logged_profile_id();
  203. $i = 0;
  204. $a = $oConnection->getConnectedContent($mixedId, $isMutual, 0, BX_CONNECTIONS_LIST_NO_LIMIT); // get received friend requests
  205. foreach ($a as $iId)
  206. if (BxDolProfile::getInstance($iId))
  207. ++$i;
  208. return $i;
  209. }
  210. /**
  211. * get number of received connections (like "subscribed me")
  212. * @param $sConnectionsObject connections object to get connections from
  213. * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
  214. * @return number
  215. */
  216. public function serviceGetConnectedInitiatorsNum ($sConnectionsObject, $mixedId = 0, $isMutual = false)
  217. {
  218. $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
  219. if (!$oConnection)
  220. return 0;
  221. if (!$mixedId)
  222. $mixedId = bx_get_logged_profile_id();
  223. $i = 0;
  224. $a = $oConnection->getConnectedInitiators($mixedId, $isMutual, 0, BX_CONNECTIONS_LIST_NO_LIMIT); // get received friend requests
  225. foreach ($a as $iId)
  226. if (BxDolProfile::getInstance($iId))
  227. ++$i;
  228. return $i;
  229. }
  230. /**
  231. * get grid with subscriptions connections
  232. */
  233. public function serviceSubscriptionsTable ($iProfileId = 0)
  234. {
  235. if(!$iProfileId && bx_get('profile_id') !== false)
  236. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  237. $oProfile = false;
  238. if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  239. return false;
  240. $aProfile = $oProfile->getInfo();
  241. if(empty($aProfile) || !is_array($aProfile))
  242. return false;
  243. if(bx_is_api())
  244. return bx_srv('system', 'browse_subscriptions', [
  245. 'profile_id' => $iProfileId,
  246. ], 'TemplServiceProfiles');
  247. $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
  248. if(getParam($CNF['PARAM_PUBLIC_SBSN']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
  249. return false;
  250. $oGrid = BxDolGrid::getObjectInstance('sys_grid_subscriptions');
  251. if (!$oGrid)
  252. return false;
  253. $oGrid->setProfileId($iProfileId);
  254. $sContent = $oGrid->getCode();
  255. if(empty($sContent))
  256. return false;
  257. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  258. 'name' => 'subscriptions',
  259. 'content' => $sContent
  260. ));
  261. }
  262. /**
  263. * get grid with subscribed me connections
  264. */
  265. public function serviceSubscribedMeTable ($iProfileId = 0)
  266. {
  267. if(!$iProfileId && bx_get('profile_id') !== false)
  268. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  269. $oProfile = false;
  270. if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  271. return false;
  272. $aProfile = $oProfile->getInfo();
  273. if(empty($aProfile) || !is_array($aProfile))
  274. return false;
  275. if(bx_is_api())
  276. return bx_srv('system', 'browse_subscribed_me', [
  277. 'profile_id' => $iProfileId,
  278. ], 'TemplServiceProfiles');
  279. $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
  280. if(getParam($CNF['PARAM_PUBLIC_SBSD']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
  281. return false;
  282. $oGrid = BxDolGrid::getObjectInstance('sys_grid_subscribed_me');
  283. if(!$oGrid)
  284. return false;
  285. $oGrid->setProfileId($iProfileId);
  286. $sContent = $oGrid->getCode();
  287. if(empty($sContent))
  288. return false;
  289. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  290. 'name' => 'subscribers',
  291. 'content' => $sContent
  292. ));
  293. }
  294. /**
  295. * get grid with 'relations' connections
  296. */
  297. public function serviceRelationsTable ($iProfileId = 0)
  298. {
  299. if(!BxDolConnectionRelation::isEnabled())
  300. return false;
  301. if(!$iProfileId && bx_get('profile_id') !== false)
  302. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  303. $oProfile = false;
  304. if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  305. return false;
  306. $aProfile = $oProfile->getInfo();
  307. if(empty($aProfile) || !is_array($aProfile))
  308. return false;
  309. if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableFromProfile($aProfile['type']))
  310. return false;
  311. $oGrid = BxDolGrid::getObjectInstance('sys_grid_relations');
  312. if (!$oGrid)
  313. return false;
  314. $oGrid->setProfileId($iProfileId);
  315. $sContent = $oGrid->getCode();
  316. if(empty($sContent))
  317. return false;
  318. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  319. 'name' => 'relations',
  320. 'content' => $sContent
  321. ));
  322. }
  323. /**
  324. * get grid with 'related me' connections
  325. */
  326. public function serviceRelatedMeTable ($iProfileId = 0)
  327. {
  328. if(!BxDolConnectionRelation::isEnabled())
  329. return false;
  330. if(!$iProfileId && bx_get('profile_id') !== false)
  331. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  332. $oProfile = false;
  333. if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  334. return false;
  335. $aProfile = $oProfile->getInfo();
  336. if(empty($aProfile) || !is_array($aProfile))
  337. return false;
  338. if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableWithProfile($aProfile['type']))
  339. return false;
  340. $oGrid = BxDolGrid::getObjectInstance('sys_grid_related_me');
  341. if(!$oGrid)
  342. return false;
  343. $oGrid->setProfileId($iProfileId);
  344. $sContent = $oGrid->getCode();
  345. if(empty($sContent))
  346. return false;
  347. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  348. 'name' => 'related-me',
  349. 'content' => $sContent
  350. ));
  351. }
  352. /*
  353. * Get notification data for Notifications module - action Subscribe.
  354. */
  355. public function serviceGetNotificationsPost($aEvent)
  356. {
  357. $iOwner = (int)$aEvent['owner_id'];
  358. $oOwner = BxDolProfile::getInstance($iOwner);
  359. if(!$oOwner)
  360. return array();
  361. $iProfile = (int)$aEvent['object_id'];
  362. $oProfile = BxDolProfile::getInstance($iProfile);
  363. if(!$oProfile)
  364. return array();
  365. return array(
  366. 'entry_sample' => '_sys_profile_sample_single',
  367. 'entry_url' => str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oOwner->getUrl()),
  368. 'entry_caption' => $oProfile->getDisplayName(),
  369. 'entry_author' => $oProfile->id(),
  370. 'lang_key' => '_sys_profile_subscription_added',
  371. );
  372. }
  373. /*
  374. * Get notification data for Notifications module - action Friend.
  375. */
  376. public function serviceGetNotificationsPostFriendship($aEvent)
  377. {
  378. $iOwner = (int)$aEvent['owner_id'];
  379. $oOwner = BxDolProfile::getInstance($iOwner);
  380. if(!$oOwner)
  381. return array();
  382. $iProfile = (int)$aEvent['object_id'];
  383. $oProfile = BxDolProfile::getInstance($iProfile);
  384. if(!$oProfile)
  385. return array();
  386. $sLangKey = '_sys_profile_friendship_added';
  387. if(isset($aEvent['content']['request']) && (int)$aEvent['content']['request'] == 1)
  388. $sLangKey = '_sys_profile_friend_request_added';
  389. return array(
  390. 'entry_sample' => '_sys_profile_sample_single',
  391. 'entry_url' => str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oOwner->getUrl()),
  392. 'entry_caption' => $oProfile->getDisplayName(),
  393. 'entry_author' => $oProfile->id(),
  394. 'lang_key' => $sLangKey,
  395. );
  396. }
  397. public function serviceAlertResponseConnections($oAlert)
  398. {
  399. $sMethod = 'process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  400. if(!method_exists($this, $sMethod))
  401. return;
  402. $this->$sMethod($oAlert);
  403. }
  404. protected function processSysProfilesFriendsConnectionAdded(&$oAlert)
  405. {
  406. if((int)$oAlert->aExtras['mutual'] != 1)
  407. return;
  408. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  409. $oConnection->addConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content'], array('alert_extras' => array('silent_mode' => 1)));
  410. $oConnection->addConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator'], array('alert_extras' => array('silent_mode' => 1)));
  411. }
  412. protected function processSysProfilesFriendsConnectionRemoved(&$oAlert)
  413. {
  414. if((int)$oAlert->aExtras['mutual'] != 1)
  415. return;
  416. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  417. $oConnection->removeConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content']);
  418. $oConnection->removeConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator']);
  419. }
  420. }
  421. /** @} */