BxGroupsModule.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Groups Groups
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Groups profiles module.
  13. */
  14. class BxGroupsModule extends BxBaseModProfileModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. }
  20. /**
  21. * Get possible recipients for start conversation form
  22. */
  23. public function actionAjaxGetInitialMembers ()
  24. {
  25. $sTerm = bx_get('term');
  26. $a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  27. header('Content-Type:text/javascript; charset=utf-8');
  28. echo(json_encode($a));
  29. }
  30. public function serviceActAsProfile ()
  31. {
  32. return false;
  33. }
  34. /**
  35. * Delete profile from fans and admins tables
  36. * @param $iProfileId profile id
  37. */
  38. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  39. {
  40. $CNF = &$this->_oConfig->CNF;
  41. $this->_oDb->deleteAdminsByProfileId($iProfileId);
  42. if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  43. $oConnection->onDeleteInitiatorAndContent($iProfileId);
  44. }
  45. /**
  46. * Reset group's author for particular group
  47. * @param $iContentId group id
  48. * @return false of error, or number of updated records on success
  49. */
  50. public function serviceReassignEntityAuthor ($iContentId)
  51. {
  52. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  53. if (!$aContentInfo)
  54. return false;
  55. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  56. return false;
  57. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  58. return $this->_oDb->updateAuthorById($iContentId, $aAdmins ? array_pop($aAdmins) : 0);
  59. }
  60. /**
  61. * Reset group's author when author profile is deleted
  62. * @param $iProfileId profile id
  63. * @return number of changed items
  64. */
  65. public function serviceReassignEntitiesByAuthor ($iProfileId)
  66. {
  67. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  68. if (!$a)
  69. return 0;
  70. $iCount = 0;
  71. foreach ($a as $aContentInfo)
  72. $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']]) ? 1 : 0);
  73. return $iCount;
  74. }
  75. public function servicePrepareFields ($aFieldsProfile)
  76. {
  77. $aFieldsProfile['group_name'] = $aFieldsProfile['name'];
  78. $aFieldsProfile['group_desc'] = isset($aFieldsProfile['description']) ? $aFieldsProfile['description'] : '';
  79. unset($aFieldsProfile['name']);
  80. unset($aFieldsProfile['description']);
  81. return $aFieldsProfile;
  82. }
  83. public function serviceOnRemoveConnection ($iGroupProfileId, $iInitiatorId)
  84. {
  85. $CNF = &$this->_oConfig->CNF;
  86. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  87. if (!$oGroupProfile)
  88. return false;
  89. $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
  90. if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
  91. $oConn->removeConnection($iProfileId, $iGroupProfileId);
  92. }
  93. public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $iIgnoreJoinConfirmation = false)
  94. {
  95. $CNF = &$this->_oConfig->CNF;
  96. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  97. if (!$oGroupProfile)
  98. return false;
  99. if (!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  100. return false;
  101. if (!($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  102. return false;
  103. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  104. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  105. // send invitation to the group (@see BxGroupsAlertsResponse)
  106. if ($iIgnoreJoinConfirmation && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  107. bx_alert($this->getName(), 'join_invitation', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array('content' => $aContentInfo, 'entry_title' => $sEntryTitle, 'entry_url' => $sEntryUrl, 'group_profile' => $iGroupProfileId, 'profile' => $iProfileId, 'notification_subobject_id' => $iProfileId, 'object_author_id' => $aContentInfo[$CNF['FIELD_AUTHOR']]));
  108. }
  109. // send notification to group's admins that new connection is pending confirmation (@see BxGroupsAlertsResponse)
  110. elseif (!$iIgnoreJoinConfirmation && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  111. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array('content' => $aContentInfo, 'entry_title' => $sEntryTitle, 'entry_url' => $sEntryUrl, 'group_profile' => $iGroupProfileId, 'profile' => $iProfileId, 'notification_subobject_id' => $iProfileId, 'object_author_id' => $aContentInfo[$CNF['FIELD_AUTHOR']]));
  112. }
  113. // send notification that join request was accepted (@see BxGroupsAlertsResponse)
  114. else if (!$iIgnoreJoinConfirmation && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  115. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array('content' => $aContentInfo, 'entry_title' => $sEntryTitle, 'entry_url' => $sEntryUrl, 'group_profile' => $iGroupProfileId, 'profile' => $iProfileId, 'notification_subobject_id' => $iProfileId, 'object_author_id' => $aContentInfo[$CNF['FIELD_AUTHOR']]));
  116. }
  117. // new fan was added
  118. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  119. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array('content' => $aContentInfo, 'entry_title' => $sEntryTitle, 'entry_url' => $sEntryUrl, 'group_profile' => $iGroupProfileId, 'profile' => $iProfileId, 'notification_subobject_id' => $iProfileId, 'object_author_id' => $aContentInfo[$CNF['FIELD_AUTHOR']]));
  120. return false;
  121. }
  122. // don't automatically add back connection (mutual) if group requires manual join confirmation
  123. if (!$iIgnoreJoinConfirmation && $aContentInfo['join_confirmation'])
  124. return false;
  125. // check if connection already exists
  126. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  127. return false;
  128. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  129. return false;
  130. return true;
  131. }
  132. public function serviceFansTable ()
  133. {
  134. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_CONNECTIONS']);
  135. if (!$oGrid)
  136. return false;
  137. return $oGrid->getCode();
  138. }
  139. public function serviceFans ($iContentId = 0)
  140. {
  141. if (!$iContentId)
  142. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  143. if (!$iContentId)
  144. return false;
  145. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  146. if (!$aContentInfo)
  147. return false;
  148. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  149. return false;
  150. bx_import('BxDolConnection');
  151. $s = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $this->_oConfig->CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  152. if (!$s)
  153. return MsgBox(_t('_sys_txt_empty'));
  154. return $s;
  155. }
  156. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  157. {
  158. if (!$iProfileId)
  159. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  160. if (!$iProfileId)
  161. return '';
  162. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  163. }
  164. /**
  165. * Entry social sharing block
  166. */
  167. public function serviceEntitySocialSharing ($iContentId = 0)
  168. {
  169. if (!$iContentId)
  170. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  171. if (!$iContentId)
  172. return false;
  173. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  174. if (!$aContentInfo)
  175. return false;
  176. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  177. if (!$oGroupProfile)
  178. return false;
  179. $CNF = &$this->_oConfig->CNF;
  180. return $this->_entitySocialSharing ($iContentId, $iContentId, 0, $oGroupProfile->getDisplayName(), false, false, $CNF['OBJECT_VOTES'], $CNF['OBJECT_REPORTS'], $CNF['URI_VIEW_ENTRY']);
  181. }
  182. /**
  183. * Data for Notifications module
  184. */
  185. public function serviceGetNotificationsData()
  186. {
  187. $sModule = $this->_aModule['name'];
  188. return array(
  189. 'handlers' => array(
  190. array('group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'),
  191. array('group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'),
  192. array('group' => $sModule . '_fan_added', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'fan_added', 'module_name' => $sModule, 'module_method' => 'get_notifications_fan_added', 'module_class' => 'Module'),
  193. array('group' => $sModule . '_join_request', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'join_request', 'module_name' => $sModule, 'module_method' => 'get_notifications_join_request', 'module_class' => 'Module', 'module_event_privacy' => 'bx_groups_allow_view_notification_to'),
  194. array('group' => $sModule . '_timeline_post_common', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'timeline_post_common', 'module_name' => $sModule, 'module_method' => 'get_notifications_timeline_post_common', 'module_class' => 'Module'),
  195. ),
  196. 'alerts' => array(
  197. array('unit' => $sModule, 'action' => 'doVote'),
  198. array('unit' => $sModule, 'action' => 'undoVote'),
  199. array('unit' => $sModule, 'action' => 'fan_added'),
  200. array('unit' => $sModule, 'action' => 'join_request'),
  201. array('unit' => $sModule, 'action' => 'timeline_post_common'),
  202. )
  203. );
  204. }
  205. /**
  206. * Notification about new member requst in the group
  207. */
  208. public function serviceGetNotificationsJoinRequest($aEvent)
  209. {
  210. return $this->_serviceGetNotification($aEvent, '_bx_groups_txt_ntfs_join_request');
  211. }
  212. /**
  213. * Notification about new member in the group
  214. */
  215. public function serviceGetNotificationsFanAdded($aEvent)
  216. {
  217. return $this->_serviceGetNotification($aEvent, '_bx_groups_txt_ntfs_fan_added');
  218. }
  219. /**
  220. * Entry post for Timeline module
  221. */
  222. public function serviceGetTimelinePost($aEvent)
  223. {
  224. $a = parent::serviceGetTimelinePost($aEvent);
  225. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  226. $a['content']['url'] = $oGroupProfile->getUrl();
  227. $a['content']['title'] = $oGroupProfile->getDisplayName();
  228. return $a;
  229. }
  230. /**
  231. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  232. */
  233. public function checkAllowedPost ($aDataEntry, $isPerformAction = false)
  234. {
  235. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  236. return CHECK_ACTION_RESULT_ALLOWED;
  237. return _t('_sys_txt_access_denied');
  238. }
  239. /**
  240. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  241. */
  242. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  243. {
  244. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  245. return CHECK_ACTION_RESULT_ALLOWED;
  246. return parent::checkAllowedView ($aDataEntry, $isPerformAction);
  247. }
  248. /**
  249. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  250. */
  251. public function checkAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  252. {
  253. if (!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) && isLogged())
  254. return CHECK_ACTION_RESULT_ALLOWED;
  255. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], true, false);
  256. }
  257. /**
  258. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  259. */
  260. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  261. {
  262. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  263. return CHECK_ACTION_RESULT_ALLOWED;
  264. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  265. }
  266. public function checkAllowedManageAdmins ($mixedDataEntry, $isPerformAction = false)
  267. {
  268. if (is_array($mixedDataEntry)) {
  269. $aDataEntry = $mixedDataEntry;
  270. }
  271. else {
  272. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  273. $aDataEntry = $oGroupProfile && $this->getName() == $oGroupProfile->getModule() ? $this->_oDb->getContentInfoById($oGroupProfile->getContentId()) : array();
  274. }
  275. return parent::checkAllowedEdit ($aDataEntry, $isPerformAction);
  276. }
  277. public function checkAllowedEdit ($aDataEntry, $isPerformAction = false)
  278. {
  279. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  280. if ($this->_oDb->isAdmin($oGroupProfile->id(), bx_get_logged_profile_id(), $aDataEntry))
  281. return CHECK_ACTION_RESULT_ALLOWED;
  282. return parent::checkAllowedEdit ($aDataEntry, $isPerformAction);
  283. }
  284. public function checkAllowedChangeCover ($aDataEntry, $isPerformAction = false)
  285. {
  286. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  287. if ($this->_oDb->isAdmin($oGroupProfile->id(), bx_get_logged_profile_id(), $aDataEntry))
  288. return CHECK_ACTION_RESULT_ALLOWED;
  289. return parent::checkAllowedChangeCover ($aDataEntry, $isPerformAction);
  290. }
  291. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  292. {
  293. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  294. if ($oGroupProfile && $this->_oDb->isAdmin($oGroupProfile->id(), bx_get_logged_profile_id(), $aDataEntry))
  295. return CHECK_ACTION_RESULT_ALLOWED;
  296. return parent::checkAllowedDelete ($aDataEntry, $isPerformAction);
  297. }
  298. public function checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  299. {
  300. if (!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  301. return _t('_sys_txt_access_denied');
  302. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  303. }
  304. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  305. {
  306. $sResult = $this->checkAllowedView($aDataEntry);
  307. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && 'c' != $aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']])
  308. return $sResult;
  309. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  310. }
  311. public function isFan ($iContentId, $iProfileId = false)
  312. {
  313. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  314. return $oGroupProfile && ($oConnection = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  315. }
  316. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl)
  317. {
  318. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  319. return array(
  320. array('url' => $sUrl, 'src' => $oGroupProfile->getPicture()),
  321. );
  322. }
  323. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  324. {
  325. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  326. return array(0, 0, null);
  327. if ($oGroupProfile->getModule() == $this->getName()) {
  328. $iProfileId = $iInitiatorId;
  329. $iGroupProfileId = $oGroupProfile->id();
  330. } else {
  331. $iProfileId = $oGroupProfile->id();
  332. $iGroupProfileId = $iInitiatorId;
  333. }
  334. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  335. }
  336. }
  337. /** @} */