1
0

BxBaseServiceConnections.php 12 KB

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