1
0

BxBaseServiceConnections.php 18 KB

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