BxBaseServiceConnections.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  274. if(empty($aProfile) || !is_array($aProfile))
  275. return false;
  276. if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableFromProfile($aProfile['type']))
  277. return false;
  278. $oGrid = BxDolGrid::getObjectInstance('sys_grid_relations');
  279. if (!$oGrid)
  280. return false;
  281. $oGrid->setProfileId($iProfileId);
  282. $sContent = $oGrid->getCode();
  283. if(empty($sContent))
  284. return false;
  285. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  286. 'name' => 'relations',
  287. 'content' => $sContent
  288. ));
  289. }
  290. /**
  291. * get grid with 'related me' connections
  292. */
  293. public function serviceRelatedMeTable ($iProfileId = 0)
  294. {
  295. if(!BxDolRelation::isEnabled())
  296. return false;
  297. if(!$iProfileId && bx_get('profile_id') !== false)
  298. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  299. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  300. if(empty($aProfile) || !is_array($aProfile))
  301. return false;
  302. if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableWithProfile($aProfile['type']))
  303. return false;
  304. $oGrid = BxDolGrid::getObjectInstance('sys_grid_related_me');
  305. if(!$oGrid)
  306. return false;
  307. $oGrid->setProfileId($iProfileId);
  308. $sContent = $oGrid->getCode();
  309. if(empty($sContent))
  310. return false;
  311. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  312. 'name' => 'related-me',
  313. 'content' => $sContent
  314. ));
  315. }
  316. /*
  317. * Get notification data for Notifications module - action Subscribe.
  318. */
  319. public function serviceGetNotificationsPost($aEvent)
  320. {
  321. $iOwner = (int)$aEvent['owner_id'];
  322. $oOwner = BxDolProfile::getInstance($iOwner);
  323. if(!$oOwner)
  324. return array();
  325. $iProfile = (int)$aEvent['object_id'];
  326. $oProfile = BxDolProfile::getInstance($iProfile);
  327. if(!$oProfile)
  328. return array();
  329. return array(
  330. 'entry_sample' => '_sys_profile_sample_single',
  331. 'entry_url' => str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oOwner->getUrl()),
  332. 'entry_caption' => $oProfile->getDisplayName(),
  333. 'entry_author' => $oProfile->id(),
  334. 'lang_key' => '_sys_profile_subscription_added',
  335. );
  336. }
  337. /*
  338. * Get notification data for Notifications module - action Friend.
  339. */
  340. public function serviceGetNotificationsPostFriendship($aEvent)
  341. {
  342. $iOwner = (int)$aEvent['owner_id'];
  343. $oOwner = BxDolProfile::getInstance($iOwner);
  344. if(!$oOwner)
  345. return array();
  346. $iProfile = (int)$aEvent['object_id'];
  347. $oProfile = BxDolProfile::getInstance($iProfile);
  348. if(!$oProfile)
  349. return array();
  350. $sLangKey = '_sys_profile_friendship_added';
  351. if(isset($aEvent['content']['request']) && (int)$aEvent['content']['request'] == 1)
  352. $sLangKey = '_sys_profile_friend_request_added';
  353. return array(
  354. 'entry_sample' => '_sys_profile_sample_single',
  355. 'entry_url' => str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oOwner->getUrl()),
  356. 'entry_caption' => $oProfile->getDisplayName(),
  357. 'entry_author' => $oProfile->id(),
  358. 'lang_key' => $sLangKey,
  359. );
  360. }
  361. public function serviceAlertResponseConnections($oAlert)
  362. {
  363. $sMethod = 'process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  364. if(!method_exists($this, $sMethod))
  365. return;
  366. $this->$sMethod($oAlert);
  367. }
  368. protected function processSysProfilesFriendsConnectionAdded(&$oAlert)
  369. {
  370. if((int)$oAlert->aExtras['mutual'] != 1)
  371. return;
  372. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  373. $oConnection->addConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content'], array('alert_extras' => array('silent_mode' => 1)));
  374. $oConnection->addConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator'], array('alert_extras' => array('silent_mode' => 1)));
  375. }
  376. protected function processSysProfilesFriendsConnectionRemoved(&$oAlert)
  377. {
  378. if((int)$oAlert->aExtras['mutual'] != 1)
  379. return;
  380. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  381. $oConnection->removeConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content']);
  382. $oConnection->removeConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator']);
  383. }
  384. }
  385. /** @} */