BxGroupsModule.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $iIgnoreJoinConfirmation = false)
  84. {
  85. $CNF = &$this->_oConfig->CNF;
  86. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  87. return false;
  88. if ($oGroupProfile->getModule() == $this->getName()) {
  89. $iProfileId = $iInitiatorId;
  90. $iGroupProfileId = $oGroupProfile->id();
  91. } else {
  92. $iProfileId = $oGroupProfile->id();
  93. $iGroupProfileId = $iInitiatorId;
  94. }
  95. if (!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  96. return false;
  97. if (!($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  98. return false;
  99. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  100. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  101. // send invitation to the group (@see BxGroupsAlertsResponse)
  102. if ($iIgnoreJoinConfirmation && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  103. bx_alert($this->getName(), 'join_invitation', $iProfileId, bx_get_logged_profile_id(), array('content' => $aContentInfo, 'entry_title' => $sEntryTitle, 'entry_url' => $sEntryUrl, 'group_profile' => $iGroupProfileId));
  104. }
  105. // send notification to group's admins that new connection is pending confirmation (@see BxGroupsAlertsResponse)
  106. elseif (!$iIgnoreJoinConfirmation && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  107. bx_alert($this->getName(), 'join_request', $iProfileId, bx_get_logged_profile_id(), array('content' => $aContentInfo, 'entry_title' => $sEntryTitle, 'entry_url' => $sEntryUrl, 'group_profile' => $iGroupProfileId));
  108. }
  109. // send notification that join request was accepted (@see BxGroupsAlertsResponse)
  110. else if (!$iIgnoreJoinConfirmation && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  111. bx_alert($this->getName(), 'join_request_accepted', $iProfileId, bx_get_logged_profile_id(), array('content' => $aContentInfo, 'entry_title' => $sEntryTitle, 'entry_url' => $sEntryUrl, 'group_profile' => $iGroupProfileId));
  112. }
  113. // new fan was added
  114. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  115. bx_alert($this->getName(), 'fan_added', $iProfileId, bx_get_logged_profile_id(), array('content' => $aContentInfo, 'entry_title' => $sEntryTitle, 'entry_url' => $sEntryUrl, 'group_profile' => $iGroupProfileId));
  116. return false;
  117. }
  118. // don't automatically add back connection (mutual) if group requires manual join confirmation
  119. if (!$iIgnoreJoinConfirmation && $aContentInfo['join_confirmation'])
  120. return false;
  121. // check if connection already exists
  122. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  123. return false;
  124. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  125. return false;
  126. return true;
  127. }
  128. public function serviceFansTable ()
  129. {
  130. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_CONNECTIONS']);
  131. if (!$oGrid)
  132. return false;
  133. return $oGrid->getCode();
  134. }
  135. public function serviceFans ($iContentId = 0)
  136. {
  137. if (!$iContentId)
  138. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  139. if (!$iContentId)
  140. return false;
  141. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  142. if (!$aContentInfo)
  143. return false;
  144. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  145. return false;
  146. bx_import('BxDolConnection');
  147. $s = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $this->_oConfig->CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  148. if (!$s)
  149. return MsgBox(_t('_sys_txt_empty'));
  150. return $s;
  151. }
  152. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  153. {
  154. if (!$iProfileId)
  155. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  156. if (!$iProfileId)
  157. return '';
  158. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  159. }
  160. /**
  161. * Entry social sharing block
  162. */
  163. public function serviceEntitySocialSharing ($iContentId = 0)
  164. {
  165. if (!$iContentId)
  166. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  167. if (!$iContentId)
  168. return false;
  169. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  170. if (!$aContentInfo)
  171. return false;
  172. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  173. if (!$oGroupProfile)
  174. return false;
  175. $CNF = &$this->_oConfig->CNF;
  176. return $this->_entitySocialSharing ($iContentId, $iContentId, 0, $oGroupProfile->getDisplayName(), false, false, $CNF['OBJECT_VOTES'], $CNF['OBJECT_REPORTS'], $CNF['URI_VIEW_ENTRY']);
  177. }
  178. /**
  179. * Data for Notifications module
  180. */
  181. public function serviceGetNotificationsData()
  182. {
  183. $sModule = $this->_aModule['name'];
  184. return array(
  185. 'handlers' => array(
  186. array('group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'),
  187. array('group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'),
  188. ),
  189. 'alerts' => array(
  190. array('unit' => $sModule, 'action' => 'doVote'),
  191. array('unit' => $sModule, 'action' => 'undoVote'),
  192. )
  193. );
  194. }
  195. /**
  196. * Entry post for Timeline module
  197. */
  198. public function serviceGetTimelinePost($aEvent)
  199. {
  200. $a = parent::serviceGetTimelinePost($aEvent);
  201. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  202. $a['content']['url'] = $oGroupProfile->getUrl();
  203. $a['content']['title'] = $oGroupProfile->getDisplayName();
  204. return $a;
  205. }
  206. /**
  207. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  208. */
  209. public function checkAllowedPost ($aDataEntry, $isPerformAction = false)
  210. {
  211. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  212. return CHECK_ACTION_RESULT_ALLOWED;
  213. return _t('_sys_txt_access_denied');
  214. }
  215. /**
  216. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  217. */
  218. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  219. {
  220. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  221. return CHECK_ACTION_RESULT_ALLOWED;
  222. return parent::checkAllowedView ($aDataEntry, $isPerformAction);
  223. }
  224. /**
  225. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  226. */
  227. public function checkAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  228. {
  229. if (!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) && isLogged())
  230. return CHECK_ACTION_RESULT_ALLOWED;
  231. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], true, false);
  232. }
  233. /**
  234. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  235. */
  236. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  237. {
  238. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  239. return CHECK_ACTION_RESULT_ALLOWED;
  240. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  241. }
  242. public function checkAllowedManageAdmins ($mixedDataEntry, $isPerformAction = false)
  243. {
  244. if (is_array($mixedDataEntry)) {
  245. $aDataEntry = $mixedDataEntry;
  246. }
  247. else {
  248. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  249. $aDataEntry = $oGroupProfile && $this->getName() == $oGroupProfile->getModule() ? $this->_oDb->getContentInfoById($oGroupProfile->getContentId()) : array();
  250. }
  251. return parent::checkAllowedEdit ($aDataEntry, $isPerformAction);
  252. }
  253. public function checkAllowedEdit ($aDataEntry, $isPerformAction = false)
  254. {
  255. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  256. if ($this->_oDb->isAdmin($oGroupProfile->id(), bx_get_logged_profile_id(), $aDataEntry))
  257. return CHECK_ACTION_RESULT_ALLOWED;
  258. return parent::checkAllowedEdit ($aDataEntry, $isPerformAction);
  259. }
  260. public function checkAllowedChangeCover ($aDataEntry, $isPerformAction = false)
  261. {
  262. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  263. if ($this->_oDb->isAdmin($oGroupProfile->id(), bx_get_logged_profile_id(), $aDataEntry))
  264. return CHECK_ACTION_RESULT_ALLOWED;
  265. return parent::checkAllowedChangeCover ($aDataEntry, $isPerformAction);
  266. }
  267. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  268. {
  269. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  270. if ($oGroupProfile && $this->_oDb->isAdmin($oGroupProfile->id(), bx_get_logged_profile_id(), $aDataEntry))
  271. return CHECK_ACTION_RESULT_ALLOWED;
  272. return parent::checkAllowedDelete ($aDataEntry, $isPerformAction);
  273. }
  274. public function checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  275. {
  276. if (!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  277. return _t('_sys_txt_access_denied');
  278. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  279. }
  280. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  281. {
  282. $sResult = $this->checkAllowedView($aDataEntry);
  283. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && 'c' != $aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']])
  284. return $sResult;
  285. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  286. }
  287. public function isFan ($iContentId, $iProfileId = false)
  288. {
  289. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  290. return $oGroupProfile && ($oConnection = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  291. }
  292. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl)
  293. {
  294. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  295. return array(
  296. array('url' => $sUrl, 'src' => $oGroupProfile->getPicture()),
  297. );
  298. }
  299. }
  300. /** @} */