BxBaseServiceConnections.php 17 KB

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