BxBaseServiceConnections.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  208. if(empty($aProfile) || !is_array($aProfile))
  209. return false;
  210. if(bx_is_api())
  211. return bx_srv('system', 'browse_subscriptions', [
  212. 'profile_id' => $iProfileId,
  213. ], 'TemplServiceProfiles');
  214. $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
  215. if(getParam($CNF['PARAM_PUBLIC_SBSN']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
  216. return false;
  217. $oGrid = BxDolGrid::getObjectInstance('sys_grid_subscriptions');
  218. if (!$oGrid)
  219. return false;
  220. $oGrid->setProfileId($iProfileId);
  221. $sContent = $oGrid->getCode();
  222. if(empty($sContent))
  223. return false;
  224. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  225. 'name' => 'subscriptions',
  226. 'content' => $sContent
  227. ));
  228. }
  229. /**
  230. * get grid with subscribed me connections
  231. */
  232. public function serviceSubscribedMeTable ($iProfileId = 0)
  233. {
  234. if(!$iProfileId && bx_get('profile_id') !== false)
  235. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  236. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  237. if(empty($aProfile) || !is_array($aProfile))
  238. return false;
  239. if(bx_is_api())
  240. return bx_srv('system', 'browse_subscribed_me', [
  241. 'profile_id' => $iProfileId,
  242. ], 'TemplServiceProfiles');
  243. $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
  244. if(getParam($CNF['PARAM_PUBLIC_SBSD']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
  245. return false;
  246. $oGrid = BxDolGrid::getObjectInstance('sys_grid_subscribed_me');
  247. if(!$oGrid)
  248. return false;
  249. $oGrid->setProfileId($iProfileId);
  250. $sContent = $oGrid->getCode();
  251. if(empty($sContent))
  252. return false;
  253. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  254. 'name' => 'subscribers',
  255. 'content' => $sContent
  256. ));
  257. }
  258. /**
  259. * get grid with 'relations' connections
  260. */
  261. public function serviceRelationsTable ($iProfileId = 0)
  262. {
  263. if(!BxDolRelation::isEnabled())
  264. return false;
  265. if(!$iProfileId && bx_get('profile_id') !== false)
  266. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  267. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  268. if(empty($aProfile) || !is_array($aProfile))
  269. return false;
  270. if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableFromProfile($aProfile['type']))
  271. return false;
  272. $oGrid = BxDolGrid::getObjectInstance('sys_grid_relations');
  273. if (!$oGrid)
  274. return false;
  275. $oGrid->setProfileId($iProfileId);
  276. $sContent = $oGrid->getCode();
  277. if(empty($sContent))
  278. return false;
  279. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  280. 'name' => 'relations',
  281. 'content' => $sContent
  282. ));
  283. }
  284. /**
  285. * get grid with 'related me' connections
  286. */
  287. public function serviceRelatedMeTable ($iProfileId = 0)
  288. {
  289. if(!BxDolRelation::isEnabled())
  290. return false;
  291. if(!$iProfileId && bx_get('profile_id') !== false)
  292. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  293. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  294. if(empty($aProfile) || !is_array($aProfile))
  295. return false;
  296. if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableWithProfile($aProfile['type']))
  297. return false;
  298. $oGrid = BxDolGrid::getObjectInstance('sys_grid_related_me');
  299. if(!$oGrid)
  300. return false;
  301. $oGrid->setProfileId($iProfileId);
  302. $sContent = $oGrid->getCode();
  303. if(empty($sContent))
  304. return false;
  305. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  306. 'name' => 'related-me',
  307. 'content' => $sContent
  308. ));
  309. }
  310. /*
  311. * Get notification data for Notifications module - action Subscribe.
  312. */
  313. public function serviceGetNotificationsPost($aEvent)
  314. {
  315. $iOwner = (int)$aEvent['owner_id'];
  316. $oOwner = BxDolProfile::getInstance($iOwner);
  317. if(!$oOwner)
  318. return array();
  319. $iProfile = (int)$aEvent['object_id'];
  320. $oProfile = BxDolProfile::getInstance($iProfile);
  321. if(!$oProfile)
  322. return array();
  323. return array(
  324. 'entry_sample' => '_sys_profile_sample_single',
  325. 'entry_url' => str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oOwner->getUrl()),
  326. 'entry_caption' => $oProfile->getDisplayName(),
  327. 'entry_author' => $oProfile->id(),
  328. 'lang_key' => '_sys_profile_subscription_added',
  329. );
  330. }
  331. /*
  332. * Get notification data for Notifications module - action Friend.
  333. */
  334. public function serviceGetNotificationsPostFriendship($aEvent)
  335. {
  336. $iOwner = (int)$aEvent['owner_id'];
  337. $oOwner = BxDolProfile::getInstance($iOwner);
  338. if(!$oOwner)
  339. return array();
  340. $iProfile = (int)$aEvent['object_id'];
  341. $oProfile = BxDolProfile::getInstance($iProfile);
  342. if(!$oProfile)
  343. return array();
  344. $sLangKey = '_sys_profile_friendship_added';
  345. if(isset($aEvent['content']['request']) && (int)$aEvent['content']['request'] == 1)
  346. $sLangKey = '_sys_profile_friend_request_added';
  347. return array(
  348. 'entry_sample' => '_sys_profile_sample_single',
  349. 'entry_url' => str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oOwner->getUrl()),
  350. 'entry_caption' => $oProfile->getDisplayName(),
  351. 'entry_author' => $oProfile->id(),
  352. 'lang_key' => $sLangKey,
  353. );
  354. }
  355. public function serviceAlertResponseConnections($oAlert)
  356. {
  357. $sMethod = 'process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  358. if(!method_exists($this, $sMethod))
  359. return;
  360. $this->$sMethod($oAlert);
  361. }
  362. protected function processSysProfilesFriendsConnectionAdded(&$oAlert)
  363. {
  364. if((int)$oAlert->aExtras['mutual'] != 1)
  365. return;
  366. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  367. $oConnection->addConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content'], array('alert_extras' => array('silent_mode' => 1)));
  368. $oConnection->addConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator'], array('alert_extras' => array('silent_mode' => 1)));
  369. }
  370. protected function processSysProfilesFriendsConnectionRemoved(&$oAlert)
  371. {
  372. if((int)$oAlert->aExtras['mutual'] != 1)
  373. return;
  374. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  375. $oConnection->removeConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content']);
  376. $oConnection->removeConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator']);
  377. }
  378. }
  379. /** @} */