BxBaseServiceConnections.php 17 KB

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