1
0

BxBaseModGroupsModule.php 19 KB

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