BxBaseModGroupsModule.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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 BaseGroups Base classes for groups modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. define('BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR', 1);
  12. define('BX_BASE_MOD_GROUPS_ROLE_MODERATOR', 2);
  13. define('BX_BASE_MOD_GROUPS_ACTION_EDIT', 'edit');
  14. define('BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER', 'change_cover');
  15. define('BX_BASE_MOD_GROUPS_ACTION_INVITE', 'invite');
  16. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS', 'manage_fans');
  17. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES', 'manage_roles');
  18. define('BX_BASE_MOD_GROUPS_ACTION_DELETE', 'delete');
  19. /**
  20. * Groups profiles module.
  21. */
  22. class BxBaseModGroupsModule extends BxBaseModProfileModule
  23. {
  24. function __construct(&$aModule)
  25. {
  26. parent::__construct($aModule);
  27. }
  28. /**
  29. * Get possible recipients for start conversation form
  30. */
  31. public function actionAjaxGetInitialMembers ()
  32. {
  33. $sTerm = bx_get('term');
  34. $a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  35. header('Content-Type:text/javascript; charset=utf-8');
  36. echo(json_encode($a));
  37. }
  38. /**
  39. * Process Process Invitation
  40. */
  41. public function actionProcessInvite ($sKey, $iGroupProfileId, $bAccept)
  42. {
  43. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  44. if (isset($aData['invited_profile_id'])){
  45. $CNF = &$this->_oConfig->CNF;
  46. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  47. return '';
  48. $iInvitedProfileId = $aData['invited_profile_id'];
  49. if ($iInvitedProfileId != bx_get_logged_profile_id())
  50. return '';
  51. if ($bAccept){
  52. if($oConnection && !$oConnection->isConnected($iInvitedProfileId, $iGroupProfileId)){
  53. $oConnection->addConnection($iInvitedProfileId, $iGroupProfileId);
  54. $oConnection->addConnection($iGroupProfileId, $iInvitedProfileId);
  55. }
  56. }
  57. $this->_oDb->deleteInviteByKey($sKey, $iGroupProfileId);
  58. }
  59. }
  60. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  61. {
  62. if(empty($sUnitTemplate))
  63. $sUnitTemplate = 'unit.html';
  64. return parent::serviceGetSearchResultUnit($iContentId, $sUnitTemplate);
  65. }
  66. /**
  67. * @see BxBaseModProfileModule::serviceGetSpaceTitle
  68. */
  69. public function serviceGetSpaceTitle()
  70. {
  71. return _t($this->_oConfig->CNF['T']['txt_sample_single']);
  72. }
  73. /**
  74. * @see iBxDolProfileService::serviceGetParticipatingProfiles
  75. */
  76. public function serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects = false)
  77. {
  78. if (isset($this->_oConfig->CNF['OBJECT_CONNECTIONS'])){
  79. $aConnectionObjects = array($this->_oConfig->CNF['OBJECT_CONNECTIONS'], 'sys_profiles_subscriptions');
  80. return parent::serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects);
  81. }
  82. return parent::serviceGetParticipatingProfiles($iProfileId);
  83. }
  84. /**
  85. * Check if this module entry can be used as profile
  86. */
  87. public function serviceActAsProfile ()
  88. {
  89. return false;
  90. }
  91. /**
  92. * Check if this module is group profile
  93. */
  94. public function serviceIsGroupProfile ()
  95. {
  96. return true;
  97. }
  98. /**
  99. * check if provided profile is member of the group
  100. */
  101. public function serviceIsFan ($iGroupProfileId, $iProfileId = false)
  102. {
  103. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  104. return $this->isFan($oGroupProfile->getContentId(), $iProfileId);
  105. }
  106. /**
  107. * check if provided profile is admin of the group
  108. */
  109. public function serviceIsAdmin ($iGroupProfileId, $iProfileId = false)
  110. {
  111. if(!$iProfileId)
  112. $iProfileId = bx_get_logged_profile_id();
  113. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  114. $iGroupContentId = $oGroupProfile->getContentId();
  115. if(!$this->isFan($iGroupContentId, $iProfileId))
  116. return false;
  117. $aGroupContentInfo = $this->_oDb->getContentInfoById($iGroupContentId);
  118. return $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aGroupContentInfo);
  119. }
  120. /**
  121. * Delete profile from fans and admins tables
  122. * @param $iProfileId profile id
  123. */
  124. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  125. {
  126. $CNF = &$this->_oConfig->CNF;
  127. $this->_oDb->deleteAdminsByProfileId($iProfileId);
  128. if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  129. $oConnection->onDeleteInitiatorAndContent($iProfileId);
  130. }
  131. /**
  132. * Reset group's author for particular group
  133. * @param $iContentId group id
  134. * @return false of error, or number of updated records on success
  135. */
  136. public function serviceReassignEntityAuthor ($iContentId)
  137. {
  138. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  139. if (!$aContentInfo)
  140. return false;
  141. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  142. return false;
  143. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  144. return $this->_oDb->updateAuthorById($iContentId, $aAdmins ? array_pop($aAdmins) : 0);
  145. }
  146. /**
  147. * Entry actions and social sharing block
  148. */
  149. public function serviceEntityAllActions ($mixedContent = false, $aParams = array())
  150. {
  151. $CNF = &$this->_oConfig->CNF;
  152. if(!empty($mixedContent)) {
  153. if(!is_array($mixedContent))
  154. $mixedContent = array((int)$mixedContent, (method_exists($this->_oDb, 'getContentInfoById')) ? $this->_oDb->getContentInfoById((int)$mixedContent) : array());
  155. }
  156. else {
  157. $mixedContent = $this->_getContent();
  158. if($mixedContent === false)
  159. return false;
  160. }
  161. list($iContentId, $aContentInfo) = $mixedContent;
  162. if(!empty($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  163. $aParams = array_merge(array(
  164. 'entry_thumb' => (int)$aContentInfo[$CNF['FIELD_PICTURE']]
  165. ), $aParams);
  166. return parent::serviceEntityAllActions ($mixedContent, $aParams);
  167. }
  168. /**
  169. * Reset group's author when author profile is deleted
  170. * @param $iProfileId profile id
  171. * @return number of changed items
  172. */
  173. public function serviceReassignEntitiesByAuthor ($iProfileId)
  174. {
  175. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  176. if (!$a)
  177. return 0;
  178. $iCount = 0;
  179. foreach ($a as $aContentInfo)
  180. $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']]) ? 1 : 0);
  181. return $iCount;
  182. }
  183. public function servicePrepareFields ($aFieldsProfile)
  184. {
  185. $CNF = &$this->_oConfig->CNF;
  186. $aFieldsProfile[$CNF['FIELD_NAME']] = $aFieldsProfile['name'];
  187. $aFieldsProfile[$CNF['FIELD_TEXT']] = isset($aFieldsProfile['description']) ? $aFieldsProfile['description'] : '';
  188. unset($aFieldsProfile['name']);
  189. unset($aFieldsProfile['description']);
  190. return $aFieldsProfile;
  191. }
  192. public function serviceOnRemoveConnection ($iGroupProfileId, $iInitiatorId)
  193. {
  194. $CNF = &$this->_oConfig->CNF;
  195. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  196. if (!$oGroupProfile)
  197. return false;
  198. $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
  199. if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
  200. return $oConn->removeConnection($iProfileId, $iGroupProfileId);
  201. return false;
  202. }
  203. public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $bSendInviteOnly = false)
  204. {
  205. $CNF = &$this->_oConfig->CNF;
  206. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  207. if (!$oGroupProfile)
  208. return false;
  209. if (!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  210. return false;
  211. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  212. return false;
  213. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  214. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  215. // send invitation to the group
  216. if ($bSendInviteOnly && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  217. 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' => $iGroupProfileId));
  218. }
  219. // send notification to group's admins that new connection is pending confirmation
  220. elseif (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  221. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  222. 'object_author_id' => $iGroupProfileId,
  223. 'performer_id' => $iProfileId,
  224. 'content' => $aContentInfo,
  225. 'entry_title' => $sEntryTitle,
  226. 'entry_url' => $sEntryUrl,
  227. 'group_profile' => $iGroupProfileId,
  228. 'profile' => $iProfileId
  229. ));
  230. }
  231. // send notification that join request was accepted
  232. else if (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  233. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  234. 'object_author_id' => $iGroupProfileId,
  235. 'performer_id' => $iProfileId,
  236. 'content' => $aContentInfo,
  237. 'entry_title' => $sEntryTitle,
  238. 'entry_url' => $sEntryUrl,
  239. 'group_profile' => $iGroupProfileId,
  240. 'profile' => $iProfileId
  241. ));
  242. }
  243. // new fan was added
  244. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  245. // follow group on join
  246. if (BxDolService::call($oGroupProfile->getModule(), 'act_as_profile')){
  247. $this->addFollower($oGroupProfile->id(), (int)$iInitiatorId);
  248. }
  249. else{
  250. $this->addFollower((int)$iInitiatorId, $oGroupProfile->id());
  251. }
  252. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  253. 'object_author_id' => $iGroupProfileId,
  254. 'performer_id' => $iProfileId,
  255. 'content' => $aContentInfo,
  256. 'entry_title' => $sEntryTitle,
  257. 'entry_url' => $sEntryUrl,
  258. 'group_profile' => $iGroupProfileId,
  259. 'profile' => $iProfileId,
  260. ));
  261. $this->doAudit($iGroupProfileId, $iInitiatorId, '_sys_audit_action_group_join_request_accepted');
  262. return false;
  263. }
  264. // don't automatically add connection (mutual) if group requires manual join confirmation
  265. if ($bSendInviteOnly || $aContentInfo['join_confirmation'])
  266. return false;
  267. // check if connection already exists
  268. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  269. return false;
  270. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  271. return false;
  272. return true;
  273. }
  274. public function serviceFansTable ()
  275. {
  276. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_CONNECTIONS']);
  277. if (!$oGrid)
  278. return false;
  279. return $oGrid->getCode();
  280. }
  281. public function serviceInvitesTable ()
  282. {
  283. if (!isset($this->_oConfig->CNF['OBJECT_GRID_INVITES']))
  284. return false;
  285. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_INVITES']);
  286. if (!$oGrid)
  287. return false;
  288. return $oGrid->getCode();
  289. }
  290. public function serviceFans ($iContentId = 0, $bAsArray = false)
  291. {
  292. if (!$iContentId)
  293. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  294. if (!$iContentId)
  295. return false;
  296. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  297. if (!$aContentInfo)
  298. return false;
  299. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  300. return false;
  301. if(!$bAsArray) {
  302. bx_import('BxDolConnection');
  303. $mixedResult = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $this->_oConfig->CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  304. if (!$mixedResult)
  305. return MsgBox(_t('_sys_txt_empty'));
  306. }
  307. else
  308. $mixedResult = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  309. return $mixedResult;
  310. }
  311. public function serviceAdmins ($iContentId = 0)
  312. {
  313. $CNF = &$this->_oConfig->CNF;
  314. if(!$iContentId)
  315. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  316. if(!$iContentId)
  317. return false;
  318. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  319. if(!$oGroupProfile)
  320. return false;
  321. $iStart = (int)bx_get('start');
  322. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  323. if(!$iLimit)
  324. $iLimit = 4;
  325. $aProfiles = $this->_oDb->getAdmins($oGroupProfile->id(), $iStart, $iLimit+1);
  326. if(empty($aProfiles) || !is_array($aProfiles))
  327. return false;
  328. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  329. }
  330. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  331. {
  332. if (!$iProfileId)
  333. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  334. if (!$iProfileId)
  335. return '';
  336. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  337. }
  338. public function serviceBrowseCreatedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  339. {
  340. if (!$iProfileId)
  341. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  342. if (!$iProfileId)
  343. return '';
  344. return $this->_serviceBrowse ('created_entries', array('author' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  345. }
  346. public function serviceEntityInvite ($iContentId = 0, $bErrorMsg = true)
  347. {
  348. if (isset($this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE']))
  349. return $this->_serviceEntityForm ('editDataForm', $iContentId, $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE'], false, $bErrorMsg);
  350. return false;
  351. }
  352. /**
  353. * Entry social sharing block
  354. */
  355. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  356. {
  357. if(!empty($mixedContent)) {
  358. if(!is_array($mixedContent))
  359. $mixedContent = array((int)$mixedContent, array());
  360. }
  361. else {
  362. $mixedContent = $this->_getContent();
  363. if($mixedContent === false)
  364. return false;
  365. }
  366. list($iContentId, $aContentInfo) = $mixedContent;
  367. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  368. if(!$oGroupProfile)
  369. return false;
  370. return parent::serviceEntitySocialSharing(array($iContentId, $aContentInfo), array(
  371. 'title' => $oGroupProfile->getDisplayName()
  372. ));
  373. }
  374. /**
  375. * Data for Notifications module
  376. */
  377. public function serviceGetNotificationsData()
  378. {
  379. $sModule = $this->_aModule['name'];
  380. $aSettingsTypes = array('follow_member', 'follow_context');
  381. if($this->serviceActAsProfile())
  382. $aSettingsTypes = array('personal', 'follow_member');
  383. return array(
  384. 'handlers' => array(
  385. array('group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'),
  386. array('group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'),
  387. array('group' => $sModule . '_score_up', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteUp', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_up', 'module_class' => 'Module'),
  388. array('group' => $sModule . '_score_down', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteDown', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_down', 'module_class' => 'Module'),
  389. 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'),
  390. 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']),
  391. 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'),
  392. ),
  393. 'settings' => array(
  394. array('group' => 'vote', 'unit' => $sModule, 'action' => 'doVote', 'types' => $aSettingsTypes),
  395. array('group' => 'score_up', 'unit' => $sModule, 'action' => 'doVoteUp', 'types' => $aSettingsTypes),
  396. array('group' => 'score_down', 'unit' => $sModule, 'action' => 'doVoteDown', 'types' => $aSettingsTypes),
  397. array('group' => 'fan', 'unit' => $sModule, 'action' => 'fan_added', 'types' => $aSettingsTypes),
  398. array('group' => 'join', 'unit' => $sModule, 'action' => 'join_request', 'types' => $aSettingsTypes),
  399. array('group' => 'timeline_post', 'unit' => $sModule, 'action' => 'timeline_post_common', 'types' => $aSettingsTypes)
  400. ),
  401. 'alerts' => array(
  402. array('unit' => $sModule, 'action' => 'doVote'),
  403. array('unit' => $sModule, 'action' => 'undoVote'),
  404. array('unit' => $sModule, 'action' => 'doVoteUp'),
  405. array('unit' => $sModule, 'action' => 'doVoteDown'),
  406. array('unit' => $sModule, 'action' => 'fan_added'),
  407. array('unit' => $sModule, 'action' => 'join_request'),
  408. array('unit' => $sModule, 'action' => 'timeline_post_common'),
  409. )
  410. );
  411. }
  412. /**
  413. * Notification about new member requst in the group
  414. */
  415. public function serviceGetNotificationsJoinRequest($aEvent)
  416. {
  417. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_join_request']);
  418. }
  419. /**
  420. * Notification about new member in the group
  421. */
  422. public function serviceGetNotificationsFanAdded($aEvent)
  423. {
  424. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_fan_added']);
  425. }
  426. protected function _serviceGetNotification($aEvent, $sLangKey)
  427. {
  428. $CNF = &$this->_oConfig->CNF;
  429. $iContentId = (int)$aEvent['object_id'];
  430. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  431. if(!$oGroupProfile)
  432. return array();
  433. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  434. if(empty($aContentInfo) || !is_array($aContentInfo))
  435. return array();
  436. $oProfile = BxDolProfile::getInstance((int)$aEvent['subobject_id']);
  437. if(!$oProfile)
  438. return array();
  439. /*
  440. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  441. * because Subentry URL has higher display priority and notification
  442. * should be linked to Group Profile (Group Profile -> Members tab)
  443. * instead of Personal Profile of a member, who performed an action.
  444. */
  445. $sEntryUrl = $oGroupProfile->getUrl();
  446. if(!empty($CNF['URL_ENTRY_FANS']))
  447. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_FANS'], array(
  448. 'profile_id' => $oGroupProfile->id()
  449. ));
  450. return array(
  451. 'entry_sample' => $CNF['T']['txt_sample_single'],
  452. 'entry_url' => $sEntryUrl,
  453. 'entry_caption' => $oGroupProfile->getDisplayName(),
  454. 'entry_author' => $oGroupProfile->id(),
  455. 'subentry_sample' => $oProfile->getDisplayName(),
  456. 'subentry_url' => $sEntryUrl,
  457. 'lang_key' => $sLangKey
  458. );
  459. }
  460. /**
  461. * Data for Timeline module
  462. */
  463. public function serviceGetTimelineData()
  464. {
  465. return BxBaseModGeneralModule::serviceGetTimelineData();
  466. }
  467. /**
  468. * Entry post for Timeline module
  469. */
  470. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  471. {
  472. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  473. if($a === false)
  474. return false;
  475. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  476. $a['content']['url'] = $oGroupProfile->getUrl();
  477. $a['content']['title'] = $oGroupProfile->getDisplayName();
  478. return $a;
  479. }
  480. // ====== PERMISSION METHODS
  481. /**
  482. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  483. */
  484. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  485. {
  486. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  487. }
  488. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  489. {
  490. $bInvited = false;
  491. if (bx_get('key')){
  492. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  493. $mixedInvited = $this->isInvited(bx_get('key'), $oGroupProfile->id());
  494. if ($mixedInvited === true)
  495. $bInvited = true;
  496. }
  497. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $iProfileId) || $bInvited)
  498. return CHECK_ACTION_RESULT_ALLOWED;
  499. return parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId);
  500. }
  501. /**
  502. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  503. */
  504. public function checkAllowedCompose(&$aDataEntry, $isPerformAction = false)
  505. {
  506. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  507. return _t('_sys_txt_access_denied');
  508. return parent::checkAllowedCompose ($aDataEntry, $isPerformAction);
  509. }
  510. /**
  511. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  512. */
  513. public function checkAllowedFanAdd(&$aDataEntry, $isPerformAction = false)
  514. {
  515. $mixedResult = $this->_modGroupsCheckAllowedFanAdd($aDataEntry, $isPerformAction);
  516. // call alert to allow custom checks
  517. bx_alert('system', 'check_allowed_fan_add', 0, 0, array(
  518. 'module' => $this->getName(),
  519. 'content_info' => $aDataEntry,
  520. 'profile_id' => bx_get_logged_profile_id(),
  521. 'override_result' => &$mixedResult
  522. ));
  523. return $mixedResult;
  524. }
  525. public function _modGroupsCheckAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  526. {
  527. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) || !isLogged())
  528. return _t('_sys_txt_access_denied');
  529. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], true, false);
  530. }
  531. /**
  532. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  533. */
  534. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  535. {
  536. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  537. return CHECK_ACTION_RESULT_ALLOWED;
  538. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  539. }
  540. protected function _checkAllowedActionByFan($sAction, $aDataEntry, $iPerformerId = 0)
  541. {
  542. $CNF = &$this->_oConfig->CNF;
  543. $bRoles = $this->_oConfig->isRoles();
  544. if(empty($iPerformerId))
  545. $iPerformerId = bx_get_logged_profile_id();
  546. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  547. $iGroupProfileId = $oGroupProfile->id();
  548. if(!$bRoles && $this->_oDb->isAdmin($iGroupProfileId, $iPerformerId, $aDataEntry))
  549. return CHECK_ACTION_RESULT_ALLOWED;
  550. if($bRoles && $this->isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iPerformerId))
  551. return CHECK_ACTION_RESULT_ALLOWED;
  552. return _t('_sys_txt_access_denied');
  553. }
  554. public function isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iPerformerId)
  555. {
  556. $bResult = false;
  557. $iRole = $this->_oDb->getRole($iGroupProfileId, $iPerformerId);
  558. if(!empty($iRole))
  559. switch($sAction) {
  560. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  561. if($iRole == BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR)
  562. $bResult = true;
  563. break;
  564. default:
  565. $bResult = true;
  566. }
  567. // call alert to allow custom checks
  568. bx_alert('system', 'check_allowed_action_by_role', 0, 0, array(
  569. 'module' => $this->getName(),
  570. 'action' => $sAction,
  571. 'content_info' => $aDataEntry,
  572. 'profile_id' => $iPerformerId,
  573. 'profile_role' => $iRole,
  574. 'override_result' => &$bResult
  575. ));
  576. return $bResult;
  577. }
  578. public function checkAllowedManageFans($mixedDataEntry, $isPerformAction = false)
  579. {
  580. $aDataEntry = array();
  581. if(!is_array($mixedDataEntry)) {
  582. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  583. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  584. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  585. }
  586. else
  587. $aDataEntry = $mixedDataEntry;
  588. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  589. return CHECK_ACTION_RESULT_ALLOWED;
  590. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  591. }
  592. public function checkAllowedManageAdmins($mixedDataEntry, $isPerformAction = false)
  593. {
  594. $aDataEntry = array();
  595. if(!is_array($mixedDataEntry)) {
  596. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  597. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  598. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  599. }
  600. else
  601. $aDataEntry = $mixedDataEntry;
  602. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  603. return CHECK_ACTION_RESULT_ALLOWED;
  604. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  605. }
  606. public function checkAllowedEdit($aDataEntry, $isPerformAction = false)
  607. {
  608. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_EDIT, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  609. return CHECK_ACTION_RESULT_ALLOWED;
  610. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  611. }
  612. public function checkAllowedInvite($aDataEntry, $isPerformAction = false)
  613. {
  614. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_INVITE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  615. return CHECK_ACTION_RESULT_ALLOWED;
  616. return $this->checkAllowedEdit ($aDataEntry, $isPerformAction);
  617. }
  618. public function checkAllowedChangeCover($aDataEntry, $isPerformAction = false)
  619. {
  620. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  621. return CHECK_ACTION_RESULT_ALLOWED;
  622. return parent::checkAllowedChangeCover($aDataEntry, $isPerformAction);
  623. }
  624. public function checkAllowedDelete(&$aDataEntry, $isPerformAction = false)
  625. {
  626. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_DELETE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  627. return CHECK_ACTION_RESULT_ALLOWED;
  628. return parent::checkAllowedDelete($aDataEntry, $isPerformAction);
  629. }
  630. public function checkAllowedJoin(&$aDataEntry, $isPerformAction = false)
  631. {
  632. if (bx_get('key')){
  633. $sKey = bx_get('key');
  634. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  635. $aData = $this->_oDb->getInviteByKey($sKey, $oGroupProfile->id());
  636. if (isset($aData['invited_profile_id']) && $aData['invited_profile_id'] == bx_get_logged_profile_id()){
  637. return CHECK_ACTION_RESULT_ALLOWED;
  638. }
  639. }
  640. return _t('_sys_txt_access_denied');
  641. }
  642. public function checkAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  643. {
  644. $mixedResult = $this->_modGroupsCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  645. // call alert to allow custom checks
  646. bx_alert('system', 'check_allowed_subscribe_add', 0, 0, array(
  647. 'module' => $this->getName(),
  648. 'content_info' => $aDataEntry,
  649. 'profile_id' => bx_get_logged_profile_id(),
  650. 'override_result' => &$mixedResult
  651. ));
  652. return $mixedResult;
  653. }
  654. /**
  655. * Note. Is mainly needed for internal usage. Access level is 'public' to allow outer calls from alerts.
  656. */
  657. public function _modGroupsCheckAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  658. {
  659. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  660. return _t('_sys_txt_access_denied');
  661. return parent::_modProfileCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  662. }
  663. /**
  664. * @deprecated since version 11.0.3 and can be removed in the next version.
  665. */
  666. public function _checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  667. {
  668. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  669. }
  670. public function doAudit($iGroupProfileId, $iFanId, $sAction)
  671. {
  672. $oProfile = BxDolProfile::getInstance($iFanId);
  673. $iContentId = $oProfile->getContentId();
  674. $sModule = $oProfile->getModule();
  675. $oModule = BxDolModule::getInstance($sModule);
  676. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile') && $oModule->_oConfig){
  677. $CNF = $oModule->_oConfig->CNF;
  678. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iContentId))) : array();
  679. $AuditParams = array(
  680. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  681. 'context_profile_id' => $iGroupProfileId,
  682. 'context_profile_title' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName()
  683. );
  684. bx_audit(
  685. $iContentId,
  686. $sModule,
  687. $sAction,
  688. $AuditParams
  689. );
  690. }
  691. }
  692. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  693. {
  694. $sResult = $this->checkAllowedView($aDataEntry);
  695. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  696. // if profile view isn't allowed but visibility is in partially visible groups
  697. // then display buttons to connect (befriend, join) to profile,
  698. // if other conditions (in parent::_checkAllowedConnect) are met as well
  699. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && !in_array($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), array('s'))))
  700. return $sResult;
  701. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  702. }
  703. public function addFollower ($iProfileId1, $iProfileId2)
  704. {
  705. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  706. if($oConnectionFollow && !$oConnectionFollow->isConnected($iProfileId1, $iProfileId2)){
  707. $oConnectionFollow->addConnection($iProfileId1, $iProfileId2);
  708. return true;
  709. }
  710. return false;
  711. }
  712. public function isFan ($iContentId, $iProfileId = false)
  713. {
  714. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  715. if (isset($this->_oConfig->CNF['OBJECT_CONNECTIONS']))
  716. return $oGroupProfile && ($oConnection = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  717. return false;
  718. }
  719. public function isInvited ($sKey, $iProfileId)
  720. {
  721. $CNF = &$this->_oConfig->CNF;
  722. $aData = $this->_oDb->getInviteByKey($sKey, $iProfileId);
  723. if (!isset($aData['invited_profile_id']))
  724. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  725. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  726. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  727. return true;
  728. }
  729. public function setRole($iGroupProfileId, $iFanProfileId, $iRole)
  730. {
  731. if(($mixedResult = $this->checkAllowedManageAdmins($iGroupProfileId)) !== CHECK_ACTION_RESULT_ALLOWED)
  732. return array('code' => 1, 'msg' => $mixedResult);
  733. if(!$this->_oDb->setRole($iGroupProfileId, $iFanProfileId, $iRole))
  734. return array('code' => 2, 'msg' => _t('_error occured'));
  735. $this->onSetRole($iGroupProfileId, $iFanProfileId, $iRole);
  736. return array('code' => 0);
  737. }
  738. public function onSetRole($iGroupProfileId, $iFanProfileId, $iRole)
  739. {
  740. $CNF = &$this->_oConfig->CNF;
  741. $iProfileId = bx_get_logged_profile_id();
  742. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  743. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  744. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  745. // notify about admin status
  746. if(!empty($CNF['EMAIL_FAN_SET_ROLE']) && $iFanProfileId != $iProfileId)
  747. sendMailTemplate($CNF['EMAIL_FAN_SET_ROLE'], 0, $iFanProfileId, array(
  748. 'EntryUrl' => $oGroupProfile->getUrl(),
  749. 'EntryTitle' => $oGroupProfile->getDisplayName(),
  750. 'Role' => $aRoles[$iRole]
  751. ), BX_EMAIL_NOTIFY);
  752. // subscribe advanced group members automatically
  753. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  754. if($iRole != 0)
  755. $oConnection->addConnection($iFanProfileId, $iGroupProfileId);
  756. else
  757. $oConnection->removeConnection($iFanProfileId, $iGroupProfileId);
  758. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  759. 'object_author_id' => $iGroupProfileId,
  760. 'performer_id' => $iProfileId,
  761. 'fan_id' => $iFanProfileId,
  762. 'content' => $aGroupProfileInfo,
  763. 'role' => $iRole,
  764. 'group_profile' => $iGroupProfileId,
  765. 'profile' => $iProfileId
  766. ));
  767. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  768. }
  769. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  770. {
  771. $CNF = &$this->_oConfig->CNF;
  772. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  773. $sSrc = '';
  774. if(isset($CNF['FIELD_COVER']) && !empty($aContentInfo[$CNF['FIELD_COVER']]))
  775. $sSrc = $oGroupProfile->getCover();
  776. if(empty($sSrc) && isset($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  777. $sSrc = $oGroupProfile->getPicture();
  778. return empty($sSrc) ? array() : array(
  779. array('id' => $aContentInfo[$CNF['FIELD_PICTURE']], 'url' => $sUrl, 'src' => $sSrc, 'src_orig' => $sSrc),
  780. );
  781. }
  782. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  783. {
  784. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  785. return array(0, 0, null);
  786. if ($oGroupProfile->getModule() == $this->getName()) {
  787. $iProfileId = $iInitiatorId;
  788. $iGroupProfileId = $oGroupProfile->id();
  789. } else {
  790. $iProfileId = $oGroupProfile->id();
  791. $iGroupProfileId = $iInitiatorId;
  792. }
  793. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  794. }
  795. }
  796. /** @} */