BxBaseServiceConnections.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. * get grid with friends connections
  20. */
  21. public function serviceConnectionsTable ($iProfileId = 0)
  22. {
  23. if(!$iProfileId && bx_get('profile_id') !== false)
  24. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  25. $oGrid = BxDolGrid::getObjectInstance('sys_grid_connections');
  26. if (!$oGrid)
  27. return false;
  28. $oGrid->setProfile($iProfileId);
  29. return $oGrid->getCode();
  30. }
  31. /**
  32. * get grid with friends connection requests
  33. */
  34. public function serviceConnectionsRequestTable ()
  35. {
  36. $oGrid = BxDolGrid::getObjectInstance('sys_grid_connections_requests');
  37. if (!$oGrid)
  38. return false;
  39. return $oGrid->getCode();
  40. }
  41. /**
  42. * get number of received unconfirmed connections (friend requests)
  43. * @param $sConnectionsObject connections object to get unconfirmed connections from
  44. * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
  45. * @return number
  46. */
  47. public function serviceGetUnconfirmedConnectionsNum ($sConnectionsObject, $mixedId = 0)
  48. {
  49. $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
  50. if (!$oConnection)
  51. return 0;
  52. if (!$mixedId)
  53. $mixedId = bx_get_logged_profile_id();
  54. $i = 0;
  55. $a = $oConnection->getConnectedInitiators($mixedId, 0); // get received friend requests
  56. foreach ($a as $iId)
  57. if (BxDolProfile::getInstance($iId))
  58. ++$i;
  59. return $i;
  60. }
  61. public function serviceGetLiveUpdatesUnconfirmedConnections($sModule, $sConnectionsObject, $aMenuItemParent, $aMenuItemChild, $iCount = 0)
  62. {
  63. $iProfile = bx_get_logged_profile_id();
  64. $oProfile = BxDolProfile::getInstance($iProfile);
  65. if(!$oProfile || $oProfile->getModule() != $sModule)
  66. return false;
  67. $iCountNew = (int)$this->serviceGetUnconfirmedConnectionsNum($sConnectionsObject, $iProfile);
  68. if($iCountNew == (int)$iCount)
  69. return false;
  70. return array(
  71. 'count' => $iCountNew, // required
  72. 'method' => 'bx_menu_show_live_update(oData)', // required
  73. 'data' => array(
  74. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  75. 'content' => '{count}'
  76. )),
  77. 'mi_parent' => $aMenuItemParent,
  78. 'mi_child' => $aMenuItemChild
  79. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  80. );
  81. }
  82. /**
  83. * get number of initiated connections (like "my subscriptions")
  84. * @param $sConnectionsObject connections object to get connections from
  85. * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
  86. * @return number
  87. */
  88. public function serviceGetConnectedContentNum ($sConnectionsObject, $mixedId = 0, $isMutual = false)
  89. {
  90. $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
  91. if (!$oConnection)
  92. return 0;
  93. if (!$mixedId)
  94. $mixedId = bx_get_logged_profile_id();
  95. $i = 0;
  96. $a = $oConnection->getConnectedContent($mixedId, $isMutual, 0, BX_CONNECTIONS_LIST_NO_LIMIT); // get received friend requests
  97. foreach ($a as $iId)
  98. if (BxDolProfile::getInstance($iId))
  99. ++$i;
  100. return $i;
  101. }
  102. /**
  103. * get number of received connections (like "subscribed me")
  104. * @param $sConnectionsObject connections object to get connections from
  105. * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
  106. * @return number
  107. */
  108. public function serviceGetConnectedInitiatorsNum ($sConnectionsObject, $mixedId = 0, $isMutual = false)
  109. {
  110. $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
  111. if (!$oConnection)
  112. return 0;
  113. if (!$mixedId)
  114. $mixedId = bx_get_logged_profile_id();
  115. $i = 0;
  116. $a = $oConnection->getConnectedInitiators($mixedId, $isMutual, 0, BX_CONNECTIONS_LIST_NO_LIMIT); // get received friend requests
  117. foreach ($a as $iId)
  118. if (BxDolProfile::getInstance($iId))
  119. ++$i;
  120. return $i;
  121. }
  122. /**
  123. * get grid with subscriptions connections
  124. */
  125. public function serviceSubscriptionsTable ($iProfileId = 0)
  126. {
  127. if(!$iProfileId && bx_get('profile_id') !== false)
  128. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  129. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  130. if(empty($aProfile) || !is_array($aProfile))
  131. return false;
  132. $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
  133. if(getParam($CNF['PARAM_PUBLIC_SBSN']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
  134. return false;
  135. $oGrid = BxDolGrid::getObjectInstance('sys_grid_subscriptions');
  136. if (!$oGrid)
  137. return false;
  138. $oGrid->setProfileId($iProfileId);
  139. $sContent = $oGrid->getCode();
  140. if(empty($sContent))
  141. return false;
  142. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  143. 'name' => 'subscriptions',
  144. 'content' => $sContent
  145. ));
  146. }
  147. /**
  148. * get grid with subscribed me connections
  149. */
  150. public function serviceSubscribedMeTable ($iProfileId = 0)
  151. {
  152. if(!$iProfileId && bx_get('profile_id') !== false)
  153. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  154. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  155. if(empty($aProfile) || !is_array($aProfile))
  156. return false;
  157. $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
  158. if(getParam($CNF['PARAM_PUBLIC_SBSD']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
  159. return false;
  160. $oGrid = BxDolGrid::getObjectInstance('sys_grid_subscribed_me');
  161. if(!$oGrid)
  162. return false;
  163. $oGrid->setProfileId($iProfileId);
  164. $sContent = $oGrid->getCode();
  165. if(empty($sContent))
  166. return false;
  167. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  168. 'name' => 'subscribers',
  169. 'content' => $sContent
  170. ));
  171. }
  172. /**
  173. * get grid with 'relations' connections
  174. */
  175. public function serviceRelationsTable ($iProfileId = 0)
  176. {
  177. if(!$iProfileId && bx_get('profile_id') !== false)
  178. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  179. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  180. if(empty($aProfile) || !is_array($aProfile))
  181. return false;
  182. $oGrid = BxDolGrid::getObjectInstance('sys_grid_relations');
  183. if (!$oGrid)
  184. return false;
  185. $oGrid->setProfileId($iProfileId);
  186. $sContent = $oGrid->getCode();
  187. if(empty($sContent))
  188. return false;
  189. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  190. 'name' => 'relations',
  191. 'content' => $sContent
  192. ));
  193. }
  194. /**
  195. * get grid with 'related me' connections
  196. */
  197. public function serviceRelatedMeTable ($iProfileId = 0)
  198. {
  199. if(!$iProfileId && bx_get('profile_id') !== false)
  200. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  201. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  202. if(empty($aProfile) || !is_array($aProfile))
  203. return false;
  204. $oGrid = BxDolGrid::getObjectInstance('sys_grid_related_me');
  205. if(!$oGrid)
  206. return false;
  207. $oGrid->setProfileId($iProfileId);
  208. $sContent = $oGrid->getCode();
  209. if(empty($sContent))
  210. return false;
  211. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  212. 'name' => 'related-me',
  213. 'content' => $sContent
  214. ));
  215. }
  216. /*
  217. * Get notification data for Notifications module - action Subscribe.
  218. */
  219. public function serviceGetNotificationsPost($aEvent)
  220. {
  221. $iProfile = (int)$aEvent['object_id'];
  222. $oProfile = BxDolProfile::getInstance($iProfile);
  223. if(!$oProfile)
  224. return array();
  225. return array(
  226. 'entry_sample' => '_sys_profile_sample_single',
  227. 'entry_url' => $oProfile->getUrl(),
  228. 'entry_caption' => $oProfile->getDisplayName(),
  229. 'entry_author' => $oProfile->id(),
  230. 'lang_key' => '_sys_profile_subscription_added',
  231. );
  232. }
  233. /*
  234. * Get notification data for Notifications module - action Friend.
  235. */
  236. public function serviceGetNotificationsPostFriendship($aEvent)
  237. {
  238. $iProfile = (int)$aEvent['object_id'];
  239. $oProfile = BxDolProfile::getInstance($iProfile);
  240. if(!$oProfile)
  241. return array();
  242. $sLangKey = '_sys_profile_friendship_added';
  243. if(isset($aEvent['content']['request']) && (int)$aEvent['content']['request'] == 1)
  244. $sLangKey = '_sys_profile_friend_request_added';
  245. return array(
  246. 'entry_sample' => '_sys_profile_sample_single',
  247. 'entry_url' => $oProfile->getUrl(),
  248. 'entry_caption' => $oProfile->getDisplayName(),
  249. 'entry_author' => $oProfile->id(),
  250. 'lang_key' => $sLangKey,
  251. );
  252. }
  253. public function serviceAlertResponseConnections($oAlert)
  254. {
  255. $sMethod = 'process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  256. if(!method_exists($this, $sMethod))
  257. return;
  258. $this->$sMethod($oAlert);
  259. }
  260. protected function processSysProfilesFriendsConnectionAdded(&$oAlert)
  261. {
  262. if((int)$oAlert->aExtras['mutual'] != 1)
  263. return;
  264. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  265. $oConnection->addConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content'], array('alert_extras' => array('silent_mode' => 1)));
  266. $oConnection->addConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator'], array('alert_extras' => array('silent_mode' => 1)));
  267. }
  268. protected function processSysProfilesFriendsConnectionRemoved(&$oAlert)
  269. {
  270. if((int)$oAlert->aExtras['mutual'] != 1)
  271. return;
  272. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  273. $oConnection->removeConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content']);
  274. $oConnection->removeConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator']);
  275. }
  276. }
  277. /** @} */