BxBaseModGroupsModule.php 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  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_MMODE_MULTI_ROLES', 'multi_roles');
  12. define('BX_BASE_MOD_GROUPS_ROLE_COMMON', 0);
  13. define('BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR', 1);
  14. define('BX_BASE_MOD_GROUPS_ROLE_MODERATOR', 2);
  15. define('BX_BASE_MOD_GROUPS_ACTION_EDIT', 'edit');
  16. define('BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER', 'change_cover');
  17. define('BX_BASE_MOD_GROUPS_ACTION_INVITE', 'invite');
  18. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS', 'manage_fans');
  19. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES', 'manage_roles');
  20. define('BX_BASE_MOD_GROUPS_ACTION_DELETE', 'delete');
  21. define('BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT', 'edit_any');
  22. define('BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT', 'delete_any');
  23. define('BX_BASE_MOD_GROUPS_ACTION_TIMELINE_POST_PIN', 'pin'); //for timeline posts only
  24. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY', 'day');
  25. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK', 'week');
  26. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_MONTH', 'month');
  27. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_YEAR', 'year');
  28. /**
  29. * Groups profiles module.
  30. */
  31. class BxBaseModGroupsModule extends BxBaseModProfileModule
  32. {
  33. function __construct(&$aModule)
  34. {
  35. parent::__construct($aModule);
  36. $CNF = &$this->_oConfig->CNF;
  37. if(isset($CNF['FIELD_PUBLISHED']))
  38. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  39. $CNF['FIELD_PUBLISHED'],
  40. ));
  41. }
  42. /**
  43. * Get possible recipients for start conversation form
  44. */
  45. public function actionAjaxGetInitialMembers ()
  46. {
  47. $sTerm = bx_get('term');
  48. $a = BxDolService::call('system', 'profiles_search', [$sTerm, ['module' => $this->_oConfig->getName()]], 'TemplServiceProfiles');
  49. header('Content-Type:text/javascript; charset=utf-8');
  50. echo(json_encode($a));
  51. }
  52. /**
  53. * Process Process Invitation
  54. */
  55. public function actionProcessInvite ($sKey, $iGroupProfileId, $bAccept)
  56. {
  57. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  58. if (isset($aData['invited_profile_id'])){
  59. $CNF = &$this->_oConfig->CNF;
  60. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  61. return '';
  62. $iInvitedProfileId = $aData['invited_profile_id'];
  63. if ($iInvitedProfileId != bx_get_logged_profile_id())
  64. return '';
  65. if ($bAccept){
  66. if($oConnection && !$oConnection->isConnected($iInvitedProfileId, $iGroupProfileId)){
  67. $oConnection->addConnection($iInvitedProfileId, $iGroupProfileId);
  68. $oConnection->addConnection($iGroupProfileId, $iInvitedProfileId);
  69. }
  70. }
  71. $this->_oDb->deleteInviteByKey($sKey, $iGroupProfileId);
  72. }
  73. }
  74. public function actionCheckName()
  75. {
  76. $CNF = &$this->_oConfig->CNF;
  77. $sName = bx_process_input(bx_get('name'));
  78. if(empty($sName))
  79. return echoJson(array());
  80. $sResult = '';
  81. $iId = (int)bx_get('id');
  82. if(!empty($iId)) {
  83. $aPrice = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iId));
  84. if(strcmp($sName, $aPrice[$CNF['FIELD_PRICE_NAME']]) == 0)
  85. $sResult = $sName;
  86. }
  87. echoJson(array(
  88. 'name' => !empty($sResult) ? $sResult : $this->_oConfig->getPriceName($sName)
  89. ));
  90. }
  91. public function serviceManageTools($sType = 'common')
  92. {
  93. $sResult = parent::serviceManageTools($sType);
  94. if(!empty($sResult))
  95. $this->_oTemplate->addJsSystem(['modules/base/groups/js/|manage_tools.js']);
  96. return $sResult;
  97. }
  98. public function serviceGetMenuAddonManageTools()
  99. {
  100. $CNF = &$this->_oConfig->CNF;
  101. $aResult = parent::serviceGetMenuAddonManageTools();
  102. if(!empty($CNF['FIELD_STATUS_ADMIN']))
  103. $aResult['counter1_value'] = $this->_oDb->getEntriesNumByParams([[
  104. 'key' => $CNF['FIELD_STATUS_ADMIN'],
  105. 'value' => BX_BASE_MOD_GENERAL_STATUS_PENDING,
  106. 'operator' => '='
  107. ]]);
  108. return $aResult;
  109. }
  110. public function serviceGetOptionsMembersMode()
  111. {
  112. $CNF = &$this->_oConfig->CNF;
  113. return array(
  114. array('key' => '', 'value' => _t('_None')),
  115. array('key' => BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES, 'value' => _t($CNF['T']['option_members_mode_' . BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES])),
  116. );
  117. }
  118. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  119. {
  120. if(empty($sUnitTemplate))
  121. $sUnitTemplate = 'unit.html';
  122. return parent::serviceGetSearchResultUnit($iContentId, $sUnitTemplate);
  123. }
  124. /**
  125. * @see BxBaseModProfileModule::serviceGetSpaceTitle
  126. */
  127. public function serviceGetSpaceTitle()
  128. {
  129. return _t($this->_oConfig->CNF['T']['txt_sample_single']);
  130. }
  131. /**
  132. * @see iBxDolProfileService::serviceGetParticipatingProfiles
  133. */
  134. public function serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects = false)
  135. {
  136. if (isset($this->_oConfig->CNF['OBJECT_CONNECTIONS'])){
  137. $aConnectionObjects = array($this->_oConfig->CNF['OBJECT_CONNECTIONS'], 'sys_profiles_subscriptions');
  138. return parent::serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects);
  139. }
  140. return parent::serviceGetParticipatingProfiles($iProfileId);
  141. }
  142. /**
  143. * Check if this module entry can be used as profile
  144. */
  145. public function serviceActAsProfile ()
  146. {
  147. return false;
  148. }
  149. /**
  150. * Check if this module is group profile
  151. */
  152. public function serviceIsGroupProfile ()
  153. {
  154. return true;
  155. }
  156. public function serviceIsEnableForContext($iProfileId = 0)
  157. {
  158. $CNF = &$this->_oConfig->CNF;
  159. $sCnfKey = 'ENABLE_FOR_CONTEXT_IN_MODULES';
  160. if(empty($iProfileId) || empty($CNF[$sCnfKey]) || !is_array($CNF[$sCnfKey]))
  161. return false;
  162. if(in_array(BxDolProfile::getInstance($iProfileId)->getModule(), $CNF[$sCnfKey]))
  163. return true;
  164. return false;
  165. }
  166. /**
  167. * check if provided profile is member of the group
  168. */
  169. public function serviceIsFan ($iGroupProfileId, $iProfileId = false)
  170. {
  171. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  172. return $this->isFan($oGroupProfile->getContentId(), $iProfileId);
  173. }
  174. /**
  175. * check if provided profile is admin of the group
  176. */
  177. public function serviceIsAdmin ($iGroupProfileId, $iProfileId = false)
  178. {
  179. if(!$iProfileId)
  180. $iProfileId = bx_get_logged_profile_id();
  181. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  182. $iGroupContentId = $oGroupProfile->getContentId();
  183. if(!$this->isFan($iGroupContentId, $iProfileId))
  184. return false;
  185. $aGroupContentInfo = $this->_oDb->getContentInfoById($iGroupContentId);
  186. return $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aGroupContentInfo);
  187. }
  188. public function serviceGetAdminRole($iGroupProfileId, $iProfileId = false)
  189. {
  190. if(!$iProfileId)
  191. $iProfileId = bx_get_logged_profile_id();
  192. if(!$this->serviceIsAdmin($iGroupProfileId, $iProfileId))
  193. return 0;
  194. return $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  195. }
  196. /*
  197. * Get context (group) advanced members who can perform the specified action.
  198. * If 'Roles' are disabled for the context then all context admins are returned.
  199. */
  200. public function serviceGetAdminsByAction($iGroupProfileId, $mixedAction)
  201. {
  202. if(!$this->_oConfig->isAdmins())
  203. return [];
  204. $aGroupContentInfo = $this->_oDb->getContentInfoByProfileId($iGroupProfileId);
  205. if(empty($aGroupContentInfo) || !is_array($aGroupContentInfo))
  206. return [];
  207. $aAdmins = $this->_oDb->getAdmins($iGroupProfileId);
  208. if(!$this->_oConfig->isRoles())
  209. return $aAdmins;
  210. if(!is_array($mixedAction))
  211. $mixedAction = [$mixedAction];
  212. $aResult = [];
  213. foreach($mixedAction as $sAction)
  214. foreach($aAdmins as $iAdminProfileId)
  215. if(!in_array($iAdminProfileId, $aResult) && $this->isAllowedActionByRole($sAction, $aGroupContentInfo, $iGroupProfileId, $iAdminProfileId))
  216. $aResult[] = $iAdminProfileId;
  217. return $aResult;
  218. }
  219. public function serviceGetAdminsToManageContent($iGroupProfileId)
  220. {
  221. return $this->serviceGetAdminsByAction($iGroupProfileId, [
  222. BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT,
  223. BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT
  224. ]);
  225. }
  226. /**
  227. * Delete profile from fans and admins tables
  228. * @param $iProfileId profile id
  229. */
  230. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  231. {
  232. $CNF = &$this->_oConfig->CNF;
  233. $this->_oDb->deleteAdminsByProfileId($iProfileId);
  234. if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  235. $oConnection->onDeleteInitiatorAndContent($iProfileId);
  236. }
  237. /**
  238. * Reset group's author for particular group
  239. * @param $iContentId group id
  240. * @parem $iAuthorId new author profile ID
  241. * @return false of error, or number of updated records on success
  242. */
  243. public function serviceReassignEntityAuthor ($iContentId, $iAuthorId = 0)
  244. {
  245. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  246. if (!$aContentInfo)
  247. return false;
  248. if (empty($iAuthorId)) {
  249. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  250. if (!$oGroupProfile)
  251. return false;
  252. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  253. if($aAdmins)
  254. $iAuthorId = array_pop($aAdmins);
  255. }
  256. return $this->_oDb->updateAuthorById($iContentId, $iAuthorId);
  257. }
  258. /**
  259. * Entry actions and social sharing block
  260. */
  261. public function serviceEntityAllActions ($mixedContent = false, $aParams = array())
  262. {
  263. $CNF = &$this->_oConfig->CNF;
  264. if(!empty($mixedContent)) {
  265. if(!is_array($mixedContent))
  266. $mixedContent = array((int)$mixedContent, (method_exists($this->_oDb, 'getContentInfoById')) ? $this->_oDb->getContentInfoById((int)$mixedContent) : array());
  267. }
  268. else {
  269. $mixedContent = $this->_getContent();
  270. if($mixedContent === false)
  271. return false;
  272. }
  273. list($iContentId, $aContentInfo) = $mixedContent;
  274. if(!empty($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  275. $aParams = array_merge(array(
  276. 'entry_thumb' => (int)$aContentInfo[$CNF['FIELD_PICTURE']]
  277. ), $aParams);
  278. return parent::serviceEntityAllActions ($mixedContent, $aParams);
  279. }
  280. /**
  281. * Reset group's author when author profile is deleted
  282. * @param $iProfileId author profile id
  283. * @param $iAuthorId new author profile id
  284. * @return number of changed items
  285. */
  286. public function serviceReassignEntitiesByAuthor ($iProfileId, $iAuthorId = 0)
  287. {
  288. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  289. if (!$a)
  290. return 0;
  291. $iCount = 0;
  292. foreach ($a as $aContentInfo)
  293. $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']], $iAuthorId) ? 1 : 0);
  294. return $iCount;
  295. }
  296. public function servicePrepareFields ($aFieldsProfile)
  297. {
  298. $CNF = &$this->_oConfig->CNF;
  299. $aFieldsProfile[$CNF['FIELD_NAME']] = $aFieldsProfile['name'];
  300. $aFieldsProfile[$CNF['FIELD_TEXT']] = isset($aFieldsProfile['description']) ? $aFieldsProfile['description'] : '';
  301. unset($aFieldsProfile['name']);
  302. unset($aFieldsProfile['description']);
  303. return $aFieldsProfile;
  304. }
  305. public function serviceOnRemoveConnection ($iGroupProfileId, $iInitiatorId)
  306. {
  307. $CNF = &$this->_oConfig->CNF;
  308. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  309. if (!$oGroupProfile)
  310. return false;
  311. $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
  312. if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
  313. return $oConn->removeConnection($iProfileId, $iGroupProfileId);
  314. return false;
  315. }
  316. public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $bSendInviteOnly = false)
  317. {
  318. $CNF = &$this->_oConfig->CNF;
  319. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  320. if (!$oGroupProfile)
  321. return false;
  322. if (!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  323. return false;
  324. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  325. return false;
  326. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  327. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  328. // send invitation to the group
  329. $sModule = $this->getName();
  330. if ($bSendInviteOnly && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  331. bx_alert($sModule, 'join_invitation', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  332. 'content' => $aContentInfo,
  333. 'entry_title' => $sEntryTitle,
  334. 'entry_url' => $sEntryUrl,
  335. 'group_profile' => $iGroupProfileId,
  336. 'profile' => $iProfileId,
  337. 'notification_subobject_id' => $iProfileId,
  338. 'object_author_id' => $iGroupProfileId
  339. ));
  340. /**
  341. * 'Invitation Received' alert for Notifications module.
  342. * Note. It's essential to use Recipient ($iInitiatorId) in 'object_author_id' parameter.
  343. * In this case notification will be received by Recipient profile.
  344. */
  345. bx_alert($sModule, 'join_invitation_notif', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  346. 'object_author_id' => $iInitiatorId,
  347. 'privacy_view' => isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 3,
  348. ));
  349. }
  350. // send notification to group's admins that new connection is pending confirmation
  351. elseif (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  352. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  353. 'object_author_id' => $iGroupProfileId,
  354. 'performer_id' => $iProfileId,
  355. 'content' => $aContentInfo,
  356. 'entry_title' => $sEntryTitle,
  357. 'entry_url' => $sEntryUrl,
  358. 'group_profile' => $iGroupProfileId,
  359. 'profile' => $iProfileId
  360. ));
  361. }
  362. // send notification that join request was accepted
  363. else if (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  364. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  365. 'object_author_id' => $iGroupProfileId,
  366. 'performer_id' => $iProfileId,
  367. 'content' => $aContentInfo,
  368. 'entry_title' => $sEntryTitle,
  369. 'entry_url' => $sEntryUrl,
  370. 'group_profile' => $iGroupProfileId,
  371. 'profile' => $iProfileId
  372. ));
  373. }
  374. // new fan was added
  375. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  376. // follow group on join
  377. if (BxDolService::call($oGroupProfile->getModule(), 'act_as_profile')){
  378. $this->addFollower($oGroupProfile->id(), (int)$iInitiatorId);
  379. }
  380. else{
  381. $this->addFollower((int)$iInitiatorId, $oGroupProfile->id());
  382. }
  383. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  384. 'object_author_id' => $iGroupProfileId,
  385. 'performer_id' => $iProfileId,
  386. 'content' => $aContentInfo,
  387. 'entry_title' => $sEntryTitle,
  388. 'entry_url' => $sEntryUrl,
  389. 'group_profile' => $iGroupProfileId,
  390. 'profile' => $iProfileId,
  391. ));
  392. $this->doAudit($iGroupProfileId, $iInitiatorId, '_sys_audit_action_group_join_request_accepted');
  393. return false;
  394. }
  395. // don't automatically add connection (mutual) if group requires manual join confirmation
  396. if ($bSendInviteOnly || $aContentInfo['join_confirmation'])
  397. return false;
  398. // check if connection already exists
  399. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  400. return false;
  401. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  402. return false;
  403. return true;
  404. }
  405. public function serviceFansTable ()
  406. {
  407. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_CONNECTIONS']);
  408. if (!$oGrid)
  409. return false;
  410. return $oGrid->getCode();
  411. }
  412. public function serviceInvitesTable ()
  413. {
  414. if (!isset($this->_oConfig->CNF['OBJECT_GRID_INVITES']))
  415. return false;
  416. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_INVITES']);
  417. if (!$oGrid)
  418. return false;
  419. return $oGrid->getCode();
  420. }
  421. public function serviceFans ($iContentId = 0, $bAsArray = false)
  422. {
  423. if (!$iContentId)
  424. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  425. if (!$iContentId)
  426. return false;
  427. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  428. if (!$aContentInfo)
  429. return false;
  430. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  431. return false;
  432. if(!$bAsArray) {
  433. bx_import('BxDolConnection');
  434. $mixedResult = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $this->_oConfig->CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  435. if (!$mixedResult)
  436. return MsgBox(_t('_sys_txt_empty'));
  437. }
  438. else
  439. $mixedResult = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  440. return $mixedResult;
  441. }
  442. public function serviceFansWithoutAdmins ($iContentId = 0, $bAsArray = false)
  443. {
  444. if (!$iContentId)
  445. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  446. if (!$iContentId)
  447. return false;
  448. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  449. if (!$aContentInfo)
  450. return false;
  451. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  452. return false;
  453. $CNF = &$this->_oConfig->CNF;
  454. $aFans = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  455. if(empty($aFans) || !is_array($aFans))
  456. return false;
  457. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  458. if(!empty($aAdmins) && is_array($aAdmins))
  459. $aFans = array_diff($aFans, $aAdmins);
  460. $iStart = (int)bx_get('start');
  461. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  462. if(!$iLimit)
  463. $iLimit = 4;
  464. return $this->_serviceBrowseQuick($aFans, $iStart, $iLimit);
  465. }
  466. public function serviceAdmins ($iContentId = 0)
  467. {
  468. $CNF = &$this->_oConfig->CNF;
  469. if(!$iContentId)
  470. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  471. if(!$iContentId)
  472. return false;
  473. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  474. if(!$oGroupProfile)
  475. return false;
  476. $iStart = (int)bx_get('start');
  477. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  478. if(!$iLimit)
  479. $iLimit = 4;
  480. $aProfiles = $this->_oDb->getAdmins($oGroupProfile->id(), $iStart, $iLimit+1);
  481. if(empty($aProfiles) || !is_array($aProfiles))
  482. return false;
  483. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  484. }
  485. public function serviceMembersByRole ($iContentId = 0, $iRole = BX_BASE_MOD_GROUPS_ROLE_COMMON)
  486. {
  487. $CNF = &$this->_oConfig->CNF;
  488. if(!$iContentId)
  489. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  490. if(!$iContentId)
  491. return false;
  492. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  493. if(!$oGroupProfile)
  494. return false;
  495. $iStart = (int)bx_get('start');
  496. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  497. if(!$iLimit)
  498. $iLimit = 4;
  499. $aProfiles = $this->_oDb->getRoles([
  500. 'type' => 'fan_pids_by_group_pid',
  501. 'group_profile_id' => $oGroupProfile->id(),
  502. 'role' => $iRole,
  503. 'start' => $iStart,
  504. 'limit' => $iLimit + 1
  505. ]);
  506. if(empty($aProfiles) || !is_array($aProfiles))
  507. return false;
  508. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  509. }
  510. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  511. {
  512. if (!$iProfileId)
  513. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  514. if (!$iProfileId)
  515. return '';
  516. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  517. }
  518. public function serviceBrowseCreatedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  519. {
  520. if (!$iProfileId)
  521. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  522. if (!$iProfileId)
  523. return '';
  524. return $this->_serviceBrowse ('created_entries', array('author' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  525. }
  526. public function serviceEntityPricing($iProfileId = 0)
  527. {
  528. $CNF = &$this->_oConfig->CNF;
  529. if(!$iProfileId)
  530. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  531. if(!$iProfileId)
  532. return '';
  533. if(!$this->_oConfig->isPaidJoin())
  534. return '';
  535. if($this->checkAllowedUsePaidJoin() !== CHECK_ACTION_RESULT_ALLOWED)
  536. return MsgBox(_t('_Access denied'));
  537. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  538. return MsgBox(_t('_Access denied'));
  539. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_MANAGE']);
  540. if(!$oGrid)
  541. return '';
  542. return $oGrid->getCode();
  543. }
  544. public function serviceEntityJoin($iProfileId = 0)
  545. {
  546. $CNF = &$this->_oConfig->CNF;
  547. if(!$iProfileId)
  548. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  549. if(!$iProfileId)
  550. return '';
  551. if(!$this->_oConfig->isPaidJoin())
  552. return '';
  553. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_VIEW']);
  554. if(!$oGrid)
  555. return '';
  556. return $oGrid->getCode();
  557. }
  558. public function serviceEntityInvite ($iContentId = 0, $bErrorMsg = true)
  559. {
  560. $CNF = &$this->_oConfig->CNF;
  561. if(!isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE']))
  562. return false;
  563. return $this->_serviceEntityForm ('inviteForm', $iContentId, false, false, $bErrorMsg);
  564. }
  565. /**
  566. * Entry social sharing block
  567. */
  568. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  569. {
  570. if(!empty($mixedContent)) {
  571. if(!is_array($mixedContent))
  572. $mixedContent = array((int)$mixedContent, array());
  573. }
  574. else {
  575. $mixedContent = $this->_getContent();
  576. if($mixedContent === false)
  577. return false;
  578. }
  579. list($iContentId, $aContentInfo) = $mixedContent;
  580. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  581. if(!$oGroupProfile)
  582. return false;
  583. return parent::serviceEntitySocialSharing(array($iContentId, $aContentInfo), array(
  584. 'title' => $oGroupProfile->getDisplayName()
  585. ));
  586. }
  587. public function serviceIsPricingAvaliable($iProfileId)
  588. {
  589. if(!$this->_oConfig->isPaidJoin())
  590. return false;
  591. if($this->checkAllowedUsePaidJoin() !== CHECK_ACTION_RESULT_ALLOWED)
  592. return false;
  593. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  594. return false;
  595. return true;
  596. }
  597. public function serviceIsPaidJoinAvaliable($iGroupProfileId, $iProfileId = 0)
  598. {
  599. return $this->isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId);
  600. }
  601. public function serviceIsPaidJoinAvaliableByContent($iGroupContentId, $iProfileId = 0)
  602. {
  603. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName());
  604. if(!$oGroupProfile)
  605. return false;
  606. return $this->isPaidJoinByProfileForProfile($oGroupProfile->id(), $iProfileId);
  607. }
  608. public function serviceIsFreeJoinAvaliable($iGroupProfileId, $iProfileId = 0)
  609. {
  610. return !$this->isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId);
  611. }
  612. public function serviceIsFreeJoinAvaliableByContent($iGroupContentId, $iProfileId = 0)
  613. {
  614. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName());
  615. if(!$oGroupProfile)
  616. return false;
  617. return !$this->isPaidJoinByProfileForProfile($oGroupProfile->id(), $iProfileId);
  618. }
  619. /**
  620. * Is Paid Join enabled in the group and whether a profile can use it.
  621. *
  622. * @param type $iGroupProfileId - Group profile ID.
  623. * @param type $iProfileId - Profile ID of the user who wants to join.
  624. * @return boolean
  625. */
  626. public function isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId = 0)
  627. {
  628. $CNF = &$this->_oConfig->CNF;
  629. if(empty($iProfileId))
  630. $iProfileId = $this->_iProfileId;
  631. if(BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->isConnected($iProfileId, $iGroupProfileId))
  632. return false;
  633. return $this->isPaidJoinByProfile($iGroupProfileId);
  634. }
  635. /**
  636. * Is Paid Join enabled as is and whether a group has pricing plans added.
  637. *
  638. * @param type $iGroupProfileId - Group profile ID.
  639. * @return boolean
  640. */
  641. public function isPaidJoinByProfile($iGroupProfileId)
  642. {
  643. if(!$this->_oConfig->isPaidJoin())
  644. return false;
  645. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $iGroupProfileId));
  646. if(empty($aPrices) || !is_array($aPrices))
  647. return false;
  648. return true;
  649. }
  650. /**
  651. * Integration with Payments.
  652. */
  653. public function serviceGetPaymentData()
  654. {
  655. return $this->_aModule;
  656. }
  657. public function serviceGetCartItem($mixedItemId)
  658. {
  659. $CNF = &$this->_oConfig->CNF;
  660. if(!$mixedItemId)
  661. return array();
  662. if(is_numeric($mixedItemId))
  663. $aItem = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => (int)$mixedItemId));
  664. else
  665. $aItem = $this->_oDb->getPrices(array('type' => 'by_name', 'value' => $mixedItemId));
  666. if(empty($aItem) || !is_array($aItem))
  667. return array();
  668. if(!$this->isPaidJoinByProfile($aItem['profile_id']))
  669. return array();
  670. $oGroupProfile = BxDolProfile::getInstance($aItem['profile_id']);
  671. if(!$oGroupProfile)
  672. return array();
  673. $aGroupProfile = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  674. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  675. $sTitle = '';
  676. if(!empty($aItem['period']) && !empty($aItem['period_unit']))
  677. $sTitle = _t($CNF['T']['txt_cart_item_title'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']], $aItem['period'], $aItem['period_unit']);
  678. else
  679. $sTitle = _t($CNF['T']['txt_cart_item_title_lifetime'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']]);
  680. return array (
  681. 'id' => $aItem['id'],
  682. 'author_id' => $aGroupProfile[$CNF['FIELD_AUTHOR']],
  683. 'name' => $aItem['name'],
  684. 'title' => $sTitle,
  685. 'description' => '',
  686. 'url' => $oGroupProfile->getUrl(),
  687. 'price_single' => $aItem['price'],
  688. 'price_recurring' => $aItem['price'],
  689. 'period_recurring' => $aItem['period'],
  690. 'period_unit_recurring' => $aItem['period_unit'],
  691. 'trial_recurring' => 0
  692. );
  693. }
  694. public function serviceGetCartItems($iSellerId)
  695. {
  696. $CNF = &$this->_oConfig->CNF;
  697. if(empty($iSellerId))
  698. return array();
  699. $sModule = $this->getName();
  700. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  701. $aGroups = $this->_oDb->getEntriesBy(array('type' => 'author', 'author' => $iSellerId));
  702. $aResult = array();
  703. foreach($aGroups as $aGroup) {
  704. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aGroup[$CNF['FIELD_ID']], $sModule);
  705. if(!$oGroupProfile)
  706. continue;
  707. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $oGroupProfile->id()));
  708. if(empty($aPrices) || !is_array($aPrices))
  709. continue;
  710. $sTitle = $oGroupProfile->getDisplayName();
  711. $sUrl = $oGroupProfile->getUrl();
  712. foreach($aPrices as $aPrice)
  713. $aResult[] = array(
  714. 'id' => $aPrice['id'],
  715. 'author_id' => $iSellerId,
  716. 'name' => $aPrice['name'],
  717. 'title' => _t($CNF['T']['txt_cart_item_title'], $sTitle, $aRoles[$aPrice['role_id']], $aPrice['period'], $aPrice['period_unit']),
  718. 'description' => '',
  719. 'url' => $sUrl,
  720. 'price_single' => $aPrice['price'],
  721. 'price_recurring' => $aPrice['price'],
  722. 'period_recurring' => $aPrice['period'],
  723. 'period_unit_recurring' => $aPrice['period_unit']
  724. );
  725. }
  726. return $aResult;
  727. }
  728. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  729. {
  730. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  731. }
  732. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  733. {
  734. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  735. }
  736. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  737. {
  738. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  739. }
  740. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  741. {
  742. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  743. }
  744. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  745. {
  746. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  747. }
  748. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  749. {
  750. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  751. }
  752. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  753. {
  754. return true;
  755. }
  756. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  757. {
  758. $CNF = &$this->_oConfig->CNF;
  759. $aItem = $this->serviceGetCartItem($iItemId);
  760. if(empty($aItem) || !is_array($aItem))
  761. return array();
  762. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  763. $mixedPeriod = false;
  764. if((int)$aItemInfo['period'] != 0)
  765. $mixedPeriod = array(
  766. 'period' => (int)$aItemInfo['period'],
  767. 'period_unit' => $aItemInfo['period_unit'],
  768. 'period_reserve' => $CNF['PARAM_RECURRING_RESERVE']
  769. );
  770. if(!$this->setRole($aItemInfo['profile_id'], $iClientId, $aItemInfo['role_id'], $mixedPeriod, $sOrder))
  771. return array();
  772. return $aItem;
  773. }
  774. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  775. {
  776. $aItem = $this->serviceGetCartItem($iItemIdNew);
  777. if(empty($aItem) || !is_array($aItem))
  778. return array();
  779. $aItemInfoOld = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdOld));
  780. if(empty($aItemInfoOld) || !is_array($aItemInfoOld))
  781. return array();
  782. $aItemInfoNew = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdNew));
  783. if(empty($aItemInfoNew) || !is_array($aItemInfoNew))
  784. return array();
  785. if(!$this->unsetRole($aItemInfoOld['profile_id'], $iClientId))
  786. return array();
  787. $aResult = $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemIdNew, 1, $sOrder);
  788. if(empty($aResult) || !is_array($aResult))
  789. return array();
  790. return $aItem;
  791. }
  792. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  793. {
  794. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  795. if(empty($aItemInfo) || !is_array($aItemInfo))
  796. return false;
  797. return $this->unsetRole($aItemInfo['profile_id'], $iClientId);
  798. }
  799. /**
  800. * Data for Notifications module
  801. */
  802. public function serviceGetNotificationsData()
  803. {
  804. $sModule = $this->_aModule['name'];
  805. $aSettingsTypes = ['follow_member', 'follow_context'];
  806. if($this->serviceActAsProfile())
  807. $aSettingsTypes = ['personal', 'follow_member'];
  808. return [
  809. 'handlers' => [
  810. ['group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'],
  811. ['group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'],
  812. ['group' => $sModule . '_score_up', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteUp', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_up', 'module_class' => 'Module'],
  813. ['group' => $sModule . '_score_down', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteDown', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_down', 'module_class' => 'Module'],
  814. ['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'],
  815. ['group' => $sModule . '_join_invitation', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'join_invitation_notif', 'module_name' => $sModule, 'module_method' => 'get_notifications_join_invitation', 'module_class' => 'Module'],
  816. ['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']],
  817. ['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'],
  818. //--- Moderation related: For 'admins'.
  819. ['group' => $sModule . '_object_pending_approval', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'pending_approval', 'module_name' => $sModule, 'module_method' => 'get_notifications_post_pending_approval', 'module_class' => 'Module'],
  820. ],
  821. 'settings' => [
  822. ['group' => 'vote', 'unit' => $sModule, 'action' => 'doVote', 'types' => $aSettingsTypes],
  823. ['group' => 'score_up', 'unit' => $sModule, 'action' => 'doVoteUp', 'types' => $aSettingsTypes],
  824. ['group' => 'score_down', 'unit' => $sModule, 'action' => 'doVoteDown', 'types' => $aSettingsTypes],
  825. ['group' => 'fan', 'unit' => $sModule, 'action' => 'fan_added', 'types' => $aSettingsTypes],
  826. ['group' => 'invite', 'unit' => $sModule, 'action' => 'join_invitation_notif', 'types' => ['personal']],
  827. ['group' => 'join', 'unit' => $sModule, 'action' => 'join_request', 'types' => $aSettingsTypes],
  828. ['group' => 'timeline_post', 'unit' => $sModule, 'action' => 'timeline_post_common', 'types' => $aSettingsTypes],
  829. //--- Moderation related: For 'admins'.
  830. ['group' => 'action_required', 'unit' => $sModule, 'action' => 'pending_approval', 'types' => ['personal']],
  831. ],
  832. 'alerts' => [
  833. ['unit' => $sModule, 'action' => 'doVote'],
  834. ['unit' => $sModule, 'action' => 'undoVote'],
  835. ['unit' => $sModule, 'action' => 'doVoteUp'],
  836. ['unit' => $sModule, 'action' => 'doVoteDown'],
  837. ['unit' => $sModule, 'action' => 'fan_added'],
  838. ['unit' => $sModule, 'action' => 'join_invitation_notif'],
  839. ['unit' => $sModule, 'action' => 'join_request'],
  840. ['unit' => $sModule, 'action' => 'timeline_post_common'],
  841. //--- Moderation related: For 'admins'.
  842. ['unit' => $sModule, 'action' => 'pending_approval'],
  843. ]
  844. ];
  845. }
  846. public function serviceGetNotificationsInsertData($oAlert, $aHandler, $aDataItems)
  847. {
  848. if($oAlert->sAction != 'join_invitation_notif' || empty($aDataItems) || !is_array($aDataItems))
  849. return $aDataItems;
  850. foreach($aDataItems as $iIndex => $aDataItem)
  851. $aDataItems[$iIndex]['object_privacy_view'] = BX_DOL_PG_ALL;
  852. return $aDataItems;
  853. }
  854. /**
  855. * Notification about new invitation to join the group
  856. */
  857. public function serviceGetNotificationsJoinInvitation($aEvent)
  858. {
  859. $CNF = &$this->_oConfig->CNF;
  860. $iContentId = (int)$aEvent['object_id'];
  861. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  862. if(!$oGroupProfile)
  863. return array();
  864. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  865. if(empty($aContentInfo) || !is_array($aContentInfo))
  866. return array();
  867. /*
  868. * It's essential that 'object_owner_id' contains invited member profile id.
  869. */
  870. $oProfile = BxDolProfile::getInstance((int)$aEvent['object_owner_id']);
  871. if(!$oProfile)
  872. return array();
  873. /*
  874. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  875. * because Subentry URL has higher display priority and notification
  876. * should be linked to Group Profile (Group Profile -> Members tab)
  877. * instead of Personal Profile of invited member.
  878. */
  879. $sEntryUrl = $oGroupProfile->getUrl();
  880. return array(
  881. 'entry_sample' => $CNF['T']['txt_sample_single'],
  882. 'entry_url' => $sEntryUrl,
  883. 'entry_caption' => $oGroupProfile->getDisplayName(),
  884. 'subentry_sample' => $oProfile->getDisplayName(),
  885. 'subentry_url' => $sEntryUrl,
  886. 'lang_key' => $this->_oConfig->CNF['T']['txt_ntfs_join_invitation']
  887. );
  888. }
  889. /**
  890. * Notification about new member requst in the group
  891. */
  892. public function serviceGetNotificationsJoinRequest($aEvent)
  893. {
  894. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_join_request']);
  895. }
  896. /**
  897. * Notification about new member in the group
  898. */
  899. public function serviceGetNotificationsFanAdded($aEvent)
  900. {
  901. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_fan_added']);
  902. }
  903. protected function _serviceGetNotification($aEvent, $sLangKey)
  904. {
  905. $CNF = &$this->_oConfig->CNF;
  906. $iContentId = (int)$aEvent['object_id'];
  907. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  908. if(!$oGroupProfile)
  909. return array();
  910. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  911. if(empty($aContentInfo) || !is_array($aContentInfo))
  912. return array();
  913. $oProfile = BxDolProfile::getInstance((int)$aEvent['subobject_id']);
  914. if(!$oProfile)
  915. return array();
  916. /*
  917. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  918. * because Subentry URL has higher display priority and notification
  919. * should be linked to Group Profile (Group Profile -> Members tab)
  920. * instead of Personal Profile of a member, who performed an action.
  921. */
  922. $sEntryUrl = $oGroupProfile->getUrl();
  923. if(!empty($CNF['URL_ENTRY_FANS']))
  924. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_FANS'], array(
  925. 'profile_id' => $oGroupProfile->id()
  926. ));
  927. return array(
  928. 'entry_sample' => $CNF['T']['txt_sample_single'],
  929. 'entry_url' => $sEntryUrl,
  930. 'entry_caption' => $oGroupProfile->getDisplayName(),
  931. 'entry_author' => $oGroupProfile->id(),
  932. 'subentry_sample' => $oProfile->getDisplayName(),
  933. 'subentry_url' => $sEntryUrl,
  934. 'lang_key' => $sLangKey
  935. );
  936. }
  937. /**
  938. * Data for Timeline module
  939. */
  940. public function serviceGetTimelineData()
  941. {
  942. return BxBaseModGeneralModule::serviceGetTimelineData();
  943. }
  944. /**
  945. * Entry post for Timeline module
  946. */
  947. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  948. {
  949. $CNF = &$this->_oConfig->CNF;
  950. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  951. if($a === false)
  952. return false;
  953. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  954. $a['content']['url'] = $oGroupProfile->getUrl();
  955. $a['content']['title'] = $oGroupProfile->getDisplayName();
  956. if(isset($CNF['FIELD_PUBLISHED'])) {
  957. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  958. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $a['date'])
  959. $a['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  960. }
  961. return $a;
  962. }
  963. // ====== PERMISSION METHODS
  964. /**
  965. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  966. */
  967. public function checkAllowedUsePaidJoin($isPerformAction = false)
  968. {
  969. // check ACL
  970. $aCheck = checkActionModule($this->_iProfileId, 'use paid join', $this->getName(), $isPerformAction);
  971. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  972. return $aCheck[CHECK_ACTION_MESSAGE];
  973. return CHECK_ACTION_RESULT_ALLOWED;
  974. }
  975. /**
  976. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  977. */
  978. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  979. {
  980. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  981. }
  982. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  983. {
  984. $CNF = &$this->_oConfig->CNF;
  985. $iGroupContentId = (int)$aDataEntry[$CNF['FIELD_ID']];
  986. $bInvited = false;
  987. if(!empty($CNF['TABLE_INVITES'])) {
  988. $iGroupProfileId = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName())->id();
  989. if(($sKey = bx_get('key')) !== false) {
  990. $mixedInvited = $this->isInvited($sKey, $iGroupProfileId);
  991. if($mixedInvited === true)
  992. $bInvited = true;
  993. }
  994. else {
  995. $mixedInvited = $this->isInvitedByProfileId($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $iGroupProfileId);
  996. if($mixedInvited === true)
  997. $bInvited = true;
  998. }
  999. }
  1000. if ($this->isFan($iGroupContentId, $iProfileId) || $bInvited)
  1001. return CHECK_ACTION_RESULT_ALLOWED;
  1002. return parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId);
  1003. }
  1004. /**
  1005. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1006. */
  1007. public function checkAllowedCompose(&$aDataEntry, $isPerformAction = false)
  1008. {
  1009. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  1010. return _t('_sys_txt_access_denied');
  1011. return parent::checkAllowedCompose ($aDataEntry, $isPerformAction);
  1012. }
  1013. /**
  1014. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1015. */
  1016. public function checkAllowedFanAdd(&$aDataEntry, $isPerformAction = false)
  1017. {
  1018. $mixedResult = $this->_modGroupsCheckAllowedFanAdd($aDataEntry, $isPerformAction);
  1019. // call alert to allow custom checks
  1020. bx_alert('system', 'check_allowed_fan_add', 0, 0, array(
  1021. 'module' => $this->getName(),
  1022. 'content_info' => $aDataEntry,
  1023. 'profile_id' => bx_get_logged_profile_id(),
  1024. 'override_result' => &$mixedResult
  1025. ));
  1026. return $mixedResult;
  1027. }
  1028. public function _modGroupsCheckAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  1029. {
  1030. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) || !isLogged())
  1031. return _t('_sys_txt_access_denied');
  1032. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], true, false);
  1033. }
  1034. /**
  1035. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1036. */
  1037. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  1038. {
  1039. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  1040. return CHECK_ACTION_RESULT_ALLOWED;
  1041. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  1042. }
  1043. protected function _checkAllowedActionByFan($sAction, $aDataEntry, $iProfileId = 0)
  1044. {
  1045. $CNF = &$this->_oConfig->CNF;
  1046. $bRoles = $this->_oConfig->isRoles();
  1047. if(empty($iProfileId))
  1048. $iProfileId = bx_get_logged_profile_id();
  1049. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  1050. if(!$oGroupProfile)
  1051. return $sAction == BX_BASE_MOD_GROUPS_ACTION_DELETE ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_not_found');
  1052. $iGroupProfileId = $oGroupProfile->id();
  1053. if(!$bRoles && $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aDataEntry))
  1054. return CHECK_ACTION_RESULT_ALLOWED;
  1055. if($bRoles && $this->isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId))
  1056. return CHECK_ACTION_RESULT_ALLOWED;
  1057. return _t('_sys_txt_access_denied');
  1058. }
  1059. public function isAllowedActionByRole($mAction, $aDataEntry, $iGroupProfileId, $iProfileId)
  1060. {
  1061. $iProfileRole = $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  1062. if (is_array($mAction)) {
  1063. $sAction = $mAction['action'];
  1064. $sActionModule = $mAction['module'];
  1065. } else {
  1066. $sAction = $mAction;
  1067. $sActionModule = $this->getName();
  1068. }
  1069. $bResult = false;
  1070. if($iProfileId)
  1071. $bResult = $this->isAllowedModuleActionByRole($sActionModule, $sAction, $iProfileRole);
  1072. // in case neither of the profile's roles are having permissions set explicitly then fallback to an old way
  1073. if ($bResult === NULL) {
  1074. $bResult = false;
  1075. if($this->isFanByGroupProfileId($iGroupProfileId)) {
  1076. switch ($sAction) {
  1077. case BX_BASE_MOD_GROUPS_ACTION_DELETE:
  1078. case BX_BASE_MOD_GROUPS_ACTION_EDIT:
  1079. case BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER:
  1080. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  1081. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR))
  1082. $bResult = true;
  1083. break;
  1084. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS:
  1085. case BX_BASE_MOD_GROUPS_ACTION_INVITE:
  1086. case BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT:
  1087. case BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT:
  1088. case BX_BASE_MOD_GROUPS_ACTION_TIMELINE_POST_PIN:
  1089. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR) || $this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_MODERATOR))
  1090. $bResult = true;
  1091. break;
  1092. default:
  1093. $bResult = true;
  1094. }
  1095. }
  1096. }
  1097. // call alert to allow custom checks
  1098. bx_alert('system', 'check_allowed_action_by_role', 0, 0, [
  1099. 'module' => $this->getName(),
  1100. 'multi_roles' => $this->_oConfig->isMultiRoles(),
  1101. 'action' => $sAction,
  1102. 'action_module' => $sActionModule,
  1103. 'content_profile_id' => $iGroupProfileId,
  1104. 'content_info' => $aDataEntry,
  1105. 'profile_id' => $iProfileId,
  1106. 'profile_role' => $iProfileRole,
  1107. 'override_result' => &$bResult
  1108. ]);
  1109. return $bResult;
  1110. }
  1111. public function isAllowedModuleActionByRole($sModule, $sAction, $iProfileRole)
  1112. {
  1113. static $aRoles;
  1114. if (!$aRoles && isset($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES']) && !empty($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES']))
  1115. $aRoles = BxBaseFormView::getDataItems($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES'], true, BX_DATA_VALUES_ALL);
  1116. if ($aRoles) {
  1117. foreach ($aRoles as $iRole => $aRoleData) {
  1118. if ($iRole == 0 && $iProfileRole == 0 || $iRole > 0 && $this->isRole($iProfileRole, $iRole)) {
  1119. $mPermissions = isset($aRoles[$iRole]) && isset($aRoles[$iRole]['Data']) && !empty($aRoles[$iRole]['Data']) ? unserialize($aRoles[$iRole]['Data']) : false;
  1120. if ($mPermissions && isset($mPermissions[$sModule])) {
  1121. return isset($mPermissions[$sModule][$sAction]) && $mPermissions[$sModule][$sAction];
  1122. }
  1123. }
  1124. }
  1125. }
  1126. return NULL;
  1127. }
  1128. public function isAllowedModuleActionByProfile($iContentId, $sPostModule, $sAction, $iProfileId = 0) {
  1129. if (!$iProfileId) $iProfileId = bx_get_logged_profile_id();
  1130. if ($iProfileId && $this->isFan($iContentId, $iProfileId)) {
  1131. $sModuleName = $this->getName();
  1132. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $sModuleName);
  1133. $aDataEntry = $this->_oDb->getContentInfoById($iContentId);
  1134. $bResult = $this->isAllowedActionByRole(['action' => $sAction, 'module' => $sPostModule], $aDataEntry, $oGroupProfile->id(), $iProfileId);
  1135. if ($bResult === true) return CHECK_ACTION_RESULT_ALLOWED;
  1136. if ($bResult === false) return _t('_sys_txt_access_denied');
  1137. }
  1138. return NULL; //undefined, because the profile is either not a fan or his role is not having permissions defined. So process the default way then.
  1139. }
  1140. public function isRole($iProfileRole, $iRole)
  1141. {
  1142. if(!$this->_oConfig->isMultiRoles())
  1143. return $iProfileRole == $iRole;
  1144. else
  1145. return $iProfileRole & (1 << ($iRole - 1));
  1146. }
  1147. public function serviceIsRole($iProfileRole, $iRole)
  1148. {
  1149. return $this->isRole($iProfileRole, $iRole);
  1150. }
  1151. public function checkAllowedManageFans($mixedDataEntry, $isPerformAction = false)
  1152. {
  1153. $aDataEntry = array();
  1154. if(!is_array($mixedDataEntry)) {
  1155. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  1156. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  1157. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1158. }
  1159. else
  1160. $aDataEntry = $mixedDataEntry;
  1161. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1162. return CHECK_ACTION_RESULT_ALLOWED;
  1163. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1164. }
  1165. public function checkAllowedManageAdmins($mixedDataEntry, $isPerformAction = false)
  1166. {
  1167. $aDataEntry = array();
  1168. if(!is_array($mixedDataEntry)) {
  1169. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  1170. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  1171. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1172. }
  1173. else
  1174. $aDataEntry = $mixedDataEntry;
  1175. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1176. return CHECK_ACTION_RESULT_ALLOWED;
  1177. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1178. }
  1179. public function checkAllowedEdit($aDataEntry, $isPerformAction = false)
  1180. {
  1181. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_EDIT, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1182. return CHECK_ACTION_RESULT_ALLOWED;
  1183. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1184. }
  1185. public function checkAllowedInvite($aDataEntry, $isPerformAction = false)
  1186. {
  1187. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_INVITE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1188. return CHECK_ACTION_RESULT_ALLOWED;
  1189. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1190. }
  1191. public function checkAllowedChangeCover($aDataEntry, $isPerformAction = false)
  1192. {
  1193. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1194. return CHECK_ACTION_RESULT_ALLOWED;
  1195. return parent::checkAllowedChangeCover($aDataEntry, $isPerformAction);
  1196. }
  1197. public function checkAllowedDelete(&$aDataEntry, $isPerformAction = false)
  1198. {
  1199. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_DELETE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1200. return CHECK_ACTION_RESULT_ALLOWED;
  1201. return parent::checkAllowedDelete($aDataEntry, $isPerformAction);
  1202. }
  1203. public function checkAllowedJoin(&$aDataEntry, $isPerformAction = false)
  1204. {
  1205. if (bx_get('key')){
  1206. $sKey = bx_get('key');
  1207. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  1208. $aData = $this->_oDb->getInviteByKey($sKey, $oGroupProfile->id());
  1209. if (isset($aData['invited_profile_id']) && $aData['invited_profile_id'] == bx_get_logged_profile_id()){
  1210. return CHECK_ACTION_RESULT_ALLOWED;
  1211. }
  1212. }
  1213. return _t('_sys_txt_access_denied');
  1214. }
  1215. public function checkAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1216. {
  1217. $mixedResult = $this->_modGroupsCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1218. // call alert to allow custom checks
  1219. bx_alert('system', 'check_allowed_subscribe_add', 0, 0, array(
  1220. 'module' => $this->getName(),
  1221. 'content_info' => $aDataEntry,
  1222. 'profile_id' => bx_get_logged_profile_id(),
  1223. 'override_result' => &$mixedResult
  1224. ));
  1225. return $mixedResult;
  1226. }
  1227. /**
  1228. * Note. Is mainly needed for internal usage. Access level is 'public' to allow outer calls from alerts.
  1229. */
  1230. public function _modGroupsCheckAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1231. {
  1232. $CNF = &$this->_oConfig->CNF;
  1233. if(!$this->isFan($aDataEntry[$CNF['FIELD_ID']]) && (!isset($CNF['PARAM_SBS_WO_JOIN']) || getParam($CNF['PARAM_SBS_WO_JOIN']) != 'on'))
  1234. return _t('_sys_txt_access_denied');
  1235. return parent::_modProfileCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1236. }
  1237. /**
  1238. * @deprecated since version 11.0.3 and can be removed in the next version.
  1239. */
  1240. public function _checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  1241. {
  1242. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  1243. }
  1244. public function doAudit($iGroupProfileId, $iFanId, $sAction)
  1245. {
  1246. $oProfile = BxDolProfile::getInstance($iFanId);
  1247. $iContentId = $oProfile->getContentId();
  1248. $sModule = $oProfile->getModule();
  1249. $oModule = BxDolModule::getInstance($sModule);
  1250. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile') && $oModule->_oConfig){
  1251. $CNF = $oModule->_oConfig->CNF;
  1252. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iContentId))) : array();
  1253. $AuditParams = array(
  1254. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  1255. 'context_profile_id' => $iGroupProfileId,
  1256. 'context_profile_title' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName()
  1257. );
  1258. bx_audit(
  1259. $iContentId,
  1260. $sModule,
  1261. $sAction,
  1262. $AuditParams
  1263. );
  1264. }
  1265. }
  1266. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  1267. {
  1268. $sResult = $this->checkAllowedView($aDataEntry);
  1269. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  1270. // if profile view isn't allowed but visibility is in partially visible groups
  1271. // then display buttons to connect (befriend, join) to profile,
  1272. // if other conditions (in parent::_checkAllowedConnect) are met as well
  1273. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && !in_array($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), array('s'))))
  1274. return $sResult;
  1275. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  1276. }
  1277. // ====== COMMON METHODS
  1278. protected function _alertParams($aContentInfo)
  1279. {
  1280. $aParams = parent::_alertParams($aContentInfo);
  1281. $CNF = &$this->_oConfig->CNF;
  1282. if(!empty($CNF['FIELD_CF']) && isset($aContentInfo[$CNF['FIELD_CF']]))
  1283. $aParams['cf'] = $aContentInfo[$CNF['FIELD_CF']];
  1284. return $aParams;
  1285. }
  1286. public function alertAfterAdd($aContentInfo)
  1287. {
  1288. $CNF = &$this->_oConfig->CNF;
  1289. $iId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1290. $iAuthorId = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  1291. $sAction = 'added';
  1292. if(isset($CNF['FIELD_STATUS_ADMIN']) && isset($aContentInfo[$CNF['FIELD_STATUS_ADMIN']]) && $aContentInfo[$CNF['FIELD_STATUS_ADMIN']] == BX_BASE_MOD_GENERAL_STATUS_PENDING)
  1293. $sAction = 'deferred';
  1294. $sModule = $this->getName();
  1295. $aParams = $this->_alertParams($aContentInfo);
  1296. bx_alert('system', 'prepare_alert_params', 0, 0, [
  1297. 'unit'=> $sModule,
  1298. 'action' => &$sAction,
  1299. 'object_id' => &$iId,
  1300. 'sender_id' => &$iAuthorId,
  1301. 'extras' => &$aParams
  1302. ]);
  1303. bx_alert($sModule, $sAction, $iId, false, $aParams);
  1304. $this->_processModerationNotifications($aContentInfo);
  1305. }
  1306. public function addFollower ($iProfileId1, $iProfileId2)
  1307. {
  1308. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  1309. if($oConnectionFollow && !$oConnectionFollow->isConnected($iProfileId1, $iProfileId2)){
  1310. $oConnectionFollow->addConnection($iProfileId1, $iProfileId2);
  1311. return true;
  1312. }
  1313. return false;
  1314. }
  1315. public function isFan ($iContentId, $iProfileId = false)
  1316. {
  1317. $CNF = &$this->_oConfig->CNF;
  1318. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  1319. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1320. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1321. return false;
  1322. }
  1323. public function isFanByGroupProfileId ($iGroupProfileId, $iProfileId = false)
  1324. {
  1325. $CNF = &$this->_oConfig->CNF;
  1326. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1327. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1328. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1329. return false;
  1330. }
  1331. public function isInvited ($sKey, $iGroupProfileId)
  1332. {
  1333. $CNF = &$this->_oConfig->CNF;
  1334. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  1335. if (!isset($aData['invited_profile_id']))
  1336. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1337. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1338. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1339. return true;
  1340. }
  1341. public function isInvitedByProfileId ($iProfileId, $iGroupProfileId)
  1342. {
  1343. $CNF = &$this->_oConfig->CNF;
  1344. $aData = $this->_oDb->getInviteByInvited($iProfileId, $iGroupProfileId);
  1345. if (!isset($aData['invited_profile_id']))
  1346. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1347. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1348. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1349. return true;
  1350. }
  1351. public function serviceIsInvited($iGroupProfileId, $iProfileId = false, $sKey = '')
  1352. {
  1353. if(!$iProfileId)
  1354. $iProfileId = bx_get_logged_profile_id();
  1355. if(empty($sKey) && ($sKey = bx_get('key')) !== false)
  1356. $sKey = bx_process_input($sKey);
  1357. $mixedInvited = false;
  1358. if(!empty($sKey))
  1359. $mixedInvited = $this->isInvited($sKey, $iGroupProfileId);
  1360. else if($iProfileId !== false)
  1361. $mixedInvited = $this->isInvitedByProfileId($iProfileId, $iGroupProfileId);
  1362. return $mixedInvited === true;
  1363. }
  1364. public function serviceIsNotInvited($iGroupProfileId, $iProfileId = false, $sKey = '')
  1365. {
  1366. return !$this->serviceIsInvited($iGroupProfileId, $iProfileId, $sKey);
  1367. }
  1368. public function serviceGetInvitedKey($iGroupProfileId, $iProfileId = false)
  1369. {
  1370. $sKey = '';
  1371. if(($sKey = bx_get('key')) !== false)
  1372. $sKey = bx_process_input($sKey);
  1373. if(!$sKey) {
  1374. if(!$iProfileId)
  1375. $iProfileId = bx_get_logged_profile_id();
  1376. if($iProfileId !== false) {
  1377. $aInvite = $this->_oDb->getInviteByInvited($iProfileId, $iGroupProfileId);
  1378. if(!empty($aInvite) && is_array($aInvite))
  1379. $sKey = $aInvite['key'];
  1380. }
  1381. }
  1382. return $sKey;
  1383. }
  1384. public function getRole($iGroupProfileId, $iFanProfileId)
  1385. {
  1386. if(!$this->isFanByGroupProfileId($iGroupProfileId, $iFanProfileId))
  1387. return false;
  1388. return $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1389. }
  1390. public function setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  1391. {
  1392. $CNF = &$this->_oConfig->CNF;
  1393. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1394. return false;
  1395. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1396. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1397. if(!$oConnection || !$oGroupProfile)
  1398. return false;
  1399. if(!$oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !($oConnection->addConnection($iFanProfileId, $iGroupProfileId) && $oConnection->addConnection($iGroupProfileId, $iFanProfileId)))
  1400. return false;
  1401. if(!$this->_oDb->setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod, $sOrder))
  1402. return false;
  1403. $this->onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole);
  1404. return true;
  1405. }
  1406. public function onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole)
  1407. {
  1408. $CNF = &$this->_oConfig->CNF;
  1409. $iProfileId = bx_get_logged_profile_id();
  1410. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1411. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1412. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  1413. // notify about admin status
  1414. if(!empty($CNF['EMAIL_FAN_SET_ROLE']) && $iFanProfileId != $iProfileId) {
  1415. $aSetRoles = is_array($mixedRole) ? $mixedRole : [$mixedRole];
  1416. $aRolesNames = [];
  1417. foreach ($aSetRoles as $iRole)
  1418. $aRolesNames[] = $aRoles[(int)$iRole];
  1419. sendMailTemplate($CNF['EMAIL_FAN_SET_ROLE'], 0, $iFanProfileId, array(
  1420. 'EntryUrl' => $oGroupProfile->getUrl(),
  1421. 'EntryTitle' => $oGroupProfile->getDisplayName(),
  1422. 'Role' => implode(', ', $aRolesNames),
  1423. ), BX_EMAIL_NOTIFY);
  1424. }
  1425. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1426. 'object_author_id' => $iGroupProfileId,
  1427. 'performer_id' => $iProfileId,
  1428. 'fan_id' => $iFanProfileId,
  1429. 'content' => $aGroupProfileInfo,
  1430. 'role' => $mixedRole,
  1431. 'group_profile' => $iGroupProfileId,
  1432. 'profile' => $iProfileId
  1433. ));
  1434. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1435. }
  1436. public function unsetRole($iGroupProfileId, $iFanProfileId)
  1437. {
  1438. $CNF = &$this->_oConfig->CNF;
  1439. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1440. return false;
  1441. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1442. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1443. if(!$oConnection || !$oGroupProfile)
  1444. return false;
  1445. if($oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->removeConnection($iFanProfileId, $iGroupProfileId))
  1446. return false;
  1447. $iRole = $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1448. if(!$this->_oDb->unsetRole($iGroupProfileId, $iFanProfileId))
  1449. return false;
  1450. $this->onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole);
  1451. return true;
  1452. }
  1453. public function onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole)
  1454. {
  1455. $CNF = &$this->_oConfig->CNF;
  1456. $iProfileId = bx_get_logged_profile_id();
  1457. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1458. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1459. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1460. 'object_author_id' => $iGroupProfileId,
  1461. 'performer_id' => $iProfileId,
  1462. 'fan_id' => $iFanProfileId,
  1463. 'content' => $aGroupProfileInfo,
  1464. 'role' => $iRole,
  1465. 'group_profile' => $iGroupProfileId,
  1466. 'profile' => $iProfileId
  1467. ));
  1468. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1469. }
  1470. public function getGroupsByFan($iProfileId, $mixedRole = false)
  1471. {
  1472. $CNF = &$this->_oConfig->CNF;
  1473. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1474. return false;
  1475. if($mixedRole === false)
  1476. $mixedRole = BX_BASE_MOD_GROUPS_ROLE_COMMON;
  1477. if(!is_array($mixedRole))
  1478. $mixedRole = [$mixedRole];
  1479. $aResult = [];
  1480. foreach($mixedRole as $iRole) {
  1481. switch($iRole) {
  1482. case BX_BASE_MOD_GROUPS_ROLE_COMMON:
  1483. $aIds = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($iProfileId);
  1484. break;
  1485. default:
  1486. $aIds = $this->_oDb->getRoles([
  1487. 'type' => 'group_pids_by_fan_id',
  1488. 'fan_id' => $iProfileId,
  1489. 'role' => $iRole
  1490. ]);
  1491. }
  1492. $aResult = array_merge($aResult, $aIds);
  1493. }
  1494. return $aResult;
  1495. }
  1496. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  1497. {
  1498. $CNF = &$this->_oConfig->CNF;
  1499. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  1500. $sSrc = '';
  1501. if(isset($CNF['FIELD_COVER']) && !empty($aContentInfo[$CNF['FIELD_COVER']]))
  1502. $sSrc = $oGroupProfile->getCover();
  1503. if(empty($sSrc) && isset($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  1504. $sSrc = $oGroupProfile->getPicture();
  1505. return empty($sSrc) ? array() : array(
  1506. array('id' => $aContentInfo[$CNF['FIELD_PICTURE']], 'url' => $sUrl, 'src' => $sSrc, 'src_orig' => $sSrc),
  1507. );
  1508. }
  1509. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  1510. {
  1511. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  1512. return array(0, 0, null);
  1513. if ($oGroupProfile->getModule() == $this->getName()) {
  1514. $iProfileId = $iInitiatorId;
  1515. $iGroupProfileId = $oGroupProfile->id();
  1516. } else {
  1517. $iProfileId = $oGroupProfile->id();
  1518. $iGroupProfileId = $iInitiatorId;
  1519. }
  1520. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  1521. }
  1522. }
  1523. /** @} */