BxBaseServiceConnections.php 8.2 KB

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