BxBaseServiceConnections.php 16 KB

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