BxBaseModGroupsModule.php 70 KB

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