BxBaseServiceConnections.php 17 KB

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