BxBaseServiceConnections.php 16 KB

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