BxBaseServiceConnections.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. * @return number
  124. */
  125. public function serviceGetUnconfirmedConnectionsNum ($sConnectionsObject, $mixedId = 0)
  126. {
  127. $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
  128. if (!$oConnection)
  129. return 0;
  130. if (!$mixedId)
  131. $mixedId = bx_get_logged_profile_id();
  132. $i = 0;
  133. $a = $oConnection->getConnectedInitiators($mixedId, 0); // get received friend requests
  134. foreach ($a as $iId)
  135. if (BxDolProfile::getInstance($iId))
  136. ++$i;
  137. return $i;
  138. }
  139. public function serviceGetLiveUpdatesUnconfirmedConnections($sModule, $sConnectionsObject, $aMenuItemParent, $aMenuItemChild, $iCount = 0)
  140. {
  141. $iProfile = bx_get_logged_profile_id();
  142. $oProfile = BxDolProfile::getInstance($iProfile);
  143. if(!$oProfile || $oProfile->getModule() != $sModule)
  144. return false;
  145. $iCountNew = (int)$this->serviceGetUnconfirmedConnectionsNum($sConnectionsObject, $iProfile);
  146. if($iCountNew == (int)$iCount)
  147. return false;
  148. return array(
  149. 'count' => $iCountNew, // required
  150. 'method' => 'bx_menu_show_live_update(oData)', // required
  151. 'data' => array(
  152. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  153. 'content' => '{count}'
  154. )),
  155. 'mi_parent' => $aMenuItemParent,
  156. 'mi_child' => $aMenuItemChild
  157. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  158. );
  159. }
  160. /**
  161. * get number of initiated connections (like "my subscriptions")
  162. * @param $sConnectionsObject connections object to get connections from
  163. * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
  164. * @return number
  165. */
  166. public function serviceGetConnectedContentNum ($sConnectionsObject, $mixedId = 0, $isMutual = false)
  167. {
  168. $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
  169. if (!$oConnection)
  170. return 0;
  171. if (!$mixedId)
  172. $mixedId = bx_get_logged_profile_id();
  173. $i = 0;
  174. $a = $oConnection->getConnectedContent($mixedId, $isMutual, 0, BX_CONNECTIONS_LIST_NO_LIMIT); // get received friend requests
  175. foreach ($a as $iId)
  176. if (BxDolProfile::getInstance($iId))
  177. ++$i;
  178. return $i;
  179. }
  180. /**
  181. * get number of received connections (like "subscribed me")
  182. * @param $sConnectionsObject connections object to get connections from
  183. * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
  184. * @return number
  185. */
  186. public function serviceGetConnectedInitiatorsNum ($sConnectionsObject, $mixedId = 0, $isMutual = false)
  187. {
  188. $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
  189. if (!$oConnection)
  190. return 0;
  191. if (!$mixedId)
  192. $mixedId = bx_get_logged_profile_id();
  193. $i = 0;
  194. $a = $oConnection->getConnectedInitiators($mixedId, $isMutual, 0, BX_CONNECTIONS_LIST_NO_LIMIT); // get received friend requests
  195. foreach ($a as $iId)
  196. if (BxDolProfile::getInstance($iId))
  197. ++$i;
  198. return $i;
  199. }
  200. /**
  201. * get grid with subscriptions connections
  202. */
  203. public function serviceSubscriptionsTable ($iProfileId = 0)
  204. {
  205. if(!$iProfileId && bx_get('profile_id') !== false)
  206. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  207. $oProfile = false;
  208. if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  209. return false;
  210. $aProfile = $oProfile->getInfo();
  211. if(empty($aProfile) || !is_array($aProfile))
  212. return false;
  213. if(bx_is_api())
  214. return bx_srv('system', 'browse_subscriptions', [
  215. 'profile_id' => $iProfileId,
  216. ], 'TemplServiceProfiles');
  217. $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
  218. if(getParam($CNF['PARAM_PUBLIC_SBSN']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
  219. return false;
  220. $oGrid = BxDolGrid::getObjectInstance('sys_grid_subscriptions');
  221. if (!$oGrid)
  222. return false;
  223. $oGrid->setProfileId($iProfileId);
  224. $sContent = $oGrid->getCode();
  225. if(empty($sContent))
  226. return false;
  227. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  228. 'name' => 'subscriptions',
  229. 'content' => $sContent
  230. ));
  231. }
  232. /**
  233. * get grid with subscribed me connections
  234. */
  235. public function serviceSubscribedMeTable ($iProfileId = 0)
  236. {
  237. if(!$iProfileId && bx_get('profile_id') !== false)
  238. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  239. $oProfile = false;
  240. if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  241. return false;
  242. $aProfile = $oProfile->getInfo();
  243. if(empty($aProfile) || !is_array($aProfile))
  244. return false;
  245. if(bx_is_api())
  246. return bx_srv('system', 'browse_subscribed_me', [
  247. 'profile_id' => $iProfileId,
  248. ], 'TemplServiceProfiles');
  249. $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
  250. if(getParam($CNF['PARAM_PUBLIC_SBSD']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
  251. return false;
  252. $oGrid = BxDolGrid::getObjectInstance('sys_grid_subscribed_me');
  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' => 'subscribers',
  261. 'content' => $sContent
  262. ));
  263. }
  264. /**
  265. * get grid with 'relations' connections
  266. */
  267. public function serviceRelationsTable ($iProfileId = 0)
  268. {
  269. if(!BxDolRelation::isEnabled())
  270. return false;
  271. if(!$iProfileId && bx_get('profile_id') !== false)
  272. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  273. $oProfile = false;
  274. if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  275. return false;
  276. $aProfile = $oProfile->getInfo();
  277. if(empty($aProfile) || !is_array($aProfile))
  278. return false;
  279. if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableFromProfile($aProfile['type']))
  280. return false;
  281. $oGrid = BxDolGrid::getObjectInstance('sys_grid_relations');
  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' => 'relations',
  290. 'content' => $sContent
  291. ));
  292. }
  293. /**
  294. * get grid with 'related me' connections
  295. */
  296. public function serviceRelatedMeTable ($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. $oProfile = false;
  303. if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  304. return false;
  305. $aProfile = $oProfile->getInfo();
  306. if(empty($aProfile) || !is_array($aProfile))
  307. return false;
  308. if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableWithProfile($aProfile['type']))
  309. return false;
  310. $oGrid = BxDolGrid::getObjectInstance('sys_grid_related_me');
  311. if(!$oGrid)
  312. return false;
  313. $oGrid->setProfileId($iProfileId);
  314. $sContent = $oGrid->getCode();
  315. if(empty($sContent))
  316. return false;
  317. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  318. 'name' => 'related-me',
  319. 'content' => $sContent
  320. ));
  321. }
  322. /*
  323. * Get notification data for Notifications module - action Subscribe.
  324. */
  325. public function serviceGetNotificationsPost($aEvent)
  326. {
  327. $iOwner = (int)$aEvent['owner_id'];
  328. $oOwner = BxDolProfile::getInstance($iOwner);
  329. if(!$oOwner)
  330. return array();
  331. $iProfile = (int)$aEvent['object_id'];
  332. $oProfile = BxDolProfile::getInstance($iProfile);
  333. if(!$oProfile)
  334. return array();
  335. return array(
  336. 'entry_sample' => '_sys_profile_sample_single',
  337. 'entry_url' => str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oOwner->getUrl()),
  338. 'entry_caption' => $oProfile->getDisplayName(),
  339. 'entry_author' => $oProfile->id(),
  340. 'lang_key' => '_sys_profile_subscription_added',
  341. );
  342. }
  343. /*
  344. * Get notification data for Notifications module - action Friend.
  345. */
  346. public function serviceGetNotificationsPostFriendship($aEvent)
  347. {
  348. $iOwner = (int)$aEvent['owner_id'];
  349. $oOwner = BxDolProfile::getInstance($iOwner);
  350. if(!$oOwner)
  351. return array();
  352. $iProfile = (int)$aEvent['object_id'];
  353. $oProfile = BxDolProfile::getInstance($iProfile);
  354. if(!$oProfile)
  355. return array();
  356. $sLangKey = '_sys_profile_friendship_added';
  357. if(isset($aEvent['content']['request']) && (int)$aEvent['content']['request'] == 1)
  358. $sLangKey = '_sys_profile_friend_request_added';
  359. return array(
  360. 'entry_sample' => '_sys_profile_sample_single',
  361. 'entry_url' => str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oOwner->getUrl()),
  362. 'entry_caption' => $oProfile->getDisplayName(),
  363. 'entry_author' => $oProfile->id(),
  364. 'lang_key' => $sLangKey,
  365. );
  366. }
  367. public function serviceAlertResponseConnections($oAlert)
  368. {
  369. $sMethod = 'process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  370. if(!method_exists($this, $sMethod))
  371. return;
  372. $this->$sMethod($oAlert);
  373. }
  374. protected function processSysProfilesFriendsConnectionAdded(&$oAlert)
  375. {
  376. if((int)$oAlert->aExtras['mutual'] != 1)
  377. return;
  378. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  379. $oConnection->addConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content'], array('alert_extras' => array('silent_mode' => 1)));
  380. $oConnection->addConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator'], array('alert_extras' => array('silent_mode' => 1)));
  381. }
  382. protected function processSysProfilesFriendsConnectionRemoved(&$oAlert)
  383. {
  384. if((int)$oAlert->aExtras['mutual'] != 1)
  385. return;
  386. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  387. $oConnection->removeConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content']);
  388. $oConnection->removeConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator']);
  389. }
  390. }
  391. /** @} */