1
0

BxBaseServiceConnections.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableFromProfile($aProfile['type']))
  183. return false;
  184. $oGrid = BxDolGrid::getObjectInstance('sys_grid_relations');
  185. if (!$oGrid)
  186. return false;
  187. $oGrid->setProfileId($iProfileId);
  188. $sContent = $oGrid->getCode();
  189. if(empty($sContent))
  190. return false;
  191. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  192. 'name' => 'relations',
  193. 'content' => $sContent
  194. ));
  195. }
  196. /**
  197. * get grid with 'related me' connections
  198. */
  199. public function serviceRelatedMeTable ($iProfileId = 0)
  200. {
  201. if(!$iProfileId && bx_get('profile_id') !== false)
  202. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  203. $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
  204. if(empty($aProfile) || !is_array($aProfile))
  205. return false;
  206. if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableWithProfile($aProfile['type']))
  207. return false;
  208. $oGrid = BxDolGrid::getObjectInstance('sys_grid_related_me');
  209. if(!$oGrid)
  210. return false;
  211. $oGrid->setProfileId($iProfileId);
  212. $sContent = $oGrid->getCode();
  213. if(empty($sContent))
  214. return false;
  215. return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
  216. 'name' => 'related-me',
  217. 'content' => $sContent
  218. ));
  219. }
  220. /*
  221. * Get notification data for Notifications module - action Subscribe.
  222. */
  223. public function serviceGetNotificationsPost($aEvent)
  224. {
  225. $iProfile = (int)$aEvent['object_id'];
  226. $oProfile = BxDolProfile::getInstance($iProfile);
  227. if(!$oProfile)
  228. return array();
  229. return array(
  230. 'entry_sample' => '_sys_profile_sample_single',
  231. 'entry_url' => $oProfile->getUrl(),
  232. 'entry_caption' => $oProfile->getDisplayName(),
  233. 'entry_author' => $oProfile->id(),
  234. 'lang_key' => '_sys_profile_subscription_added',
  235. );
  236. }
  237. /*
  238. * Get notification data for Notifications module - action Friend.
  239. */
  240. public function serviceGetNotificationsPostFriendship($aEvent)
  241. {
  242. $iProfile = (int)$aEvent['object_id'];
  243. $oProfile = BxDolProfile::getInstance($iProfile);
  244. if(!$oProfile)
  245. return array();
  246. $sLangKey = '_sys_profile_friendship_added';
  247. if(isset($aEvent['content']['request']) && (int)$aEvent['content']['request'] == 1)
  248. $sLangKey = '_sys_profile_friend_request_added';
  249. return array(
  250. 'entry_sample' => '_sys_profile_sample_single',
  251. 'entry_url' => $oProfile->getUrl(),
  252. 'entry_caption' => $oProfile->getDisplayName(),
  253. 'entry_author' => $oProfile->id(),
  254. 'lang_key' => $sLangKey,
  255. );
  256. }
  257. public function serviceAlertResponseConnections($oAlert)
  258. {
  259. $sMethod = 'process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  260. if(!method_exists($this, $sMethod))
  261. return;
  262. $this->$sMethod($oAlert);
  263. }
  264. protected function processSysProfilesFriendsConnectionAdded(&$oAlert)
  265. {
  266. if((int)$oAlert->aExtras['mutual'] != 1)
  267. return;
  268. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  269. $oConnection->addConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content'], array('alert_extras' => array('silent_mode' => 1)));
  270. $oConnection->addConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator'], array('alert_extras' => array('silent_mode' => 1)));
  271. }
  272. protected function processSysProfilesFriendsConnectionRemoved(&$oAlert)
  273. {
  274. if((int)$oAlert->aExtras['mutual'] != 1)
  275. return;
  276. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  277. $oConnection->removeConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content']);
  278. $oConnection->removeConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator']);
  279. }
  280. }
  281. /** @} */