1
0

BxBaseModGroupsModule.php 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440
  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. public function serviceGetInitialMembers ($sParams)
  53. {
  54. $aOptions = json_decode($sParams, true);
  55. if (!$sParams || !isset($aOptions['term']))
  56. return [];
  57. $a = BxDolService::call('system', 'profiles_search', [$aOptions['term'], ['module' => $this->_oConfig->getName()]], 'TemplServiceProfiles');
  58. return $a;
  59. }
  60. /**
  61. * Process Process Invitation
  62. */
  63. public function actionProcessInvite ($sKey, $iGroupProfileId, $bAccept)
  64. {
  65. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  66. if (isset($aData['invited_profile_id'])){
  67. $CNF = &$this->_oConfig->CNF;
  68. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  69. return '';
  70. $iInvitedProfileId = $aData['invited_profile_id'];
  71. if ($iInvitedProfileId != bx_get_logged_profile_id())
  72. return '';
  73. if ($bAccept){
  74. if($oConnection && !$oConnection->isConnected($iInvitedProfileId, $iGroupProfileId)){
  75. $oConnection->addConnection($iInvitedProfileId, $iGroupProfileId);
  76. $oConnection->addConnection($iGroupProfileId, $iInvitedProfileId);
  77. }
  78. }
  79. $this->_oDb->deleteInviteByKey($sKey, $iGroupProfileId);
  80. }
  81. }
  82. public function actionCheckName()
  83. {
  84. $CNF = &$this->_oConfig->CNF;
  85. $sName = bx_process_input(bx_get('name'));
  86. if(empty($sName))
  87. return echoJson(array());
  88. $sResult = '';
  89. $iId = (int)bx_get('id');
  90. if(!empty($iId)) {
  91. $aPrice = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iId));
  92. if(strcmp($sName, $aPrice[$CNF['FIELD_PRICE_NAME']]) == 0)
  93. $sResult = $sName;
  94. }
  95. echoJson(array(
  96. 'name' => !empty($sResult) ? $sResult : $this->_oConfig->getPriceName($sName)
  97. ));
  98. }
  99. public function actionGetQuestionnaire()
  100. {
  101. $sSource = '';
  102. if(($sSource = bx_get('s')) !== false)
  103. $sSource = bx_process_url_param($sSource);
  104. $sObject = '';
  105. if(($sObject = bx_get('o')) !== false)
  106. $sObject = bx_process_url_param($sObject);
  107. $sAction = '';
  108. if(($sAction = bx_get('a')) !== false)
  109. $sAction = bx_process_url_param($sAction);
  110. $iContentProfileId = 0;
  111. if(($iContentProfileId = bx_get('cpi')) !== false)
  112. $iContentProfileId = (int)$iContentProfileId;
  113. echoJson($this->serviceGetQuestionnaire($sSource, $sObject, $sAction, $iContentProfileId));
  114. }
  115. public function serviceManageTools($sType = 'common')
  116. {
  117. $this->_oTemplate->addJs(['modules/base/groups/js/|manage_tools.js']);
  118. return parent::serviceManageTools($sType);
  119. }
  120. public function decodeDataAPI($aData, $aParams = [])
  121. {
  122. $CNF = $this->_oConfig->CNF;
  123. $aResult = parent::decodeDataAPI($aData, $aParams);
  124. if(isset($aParams['template']) && $aParams['template'] == 'unit_wo_info')
  125. return $aResult;
  126. if(getParam('sys_api_conn_in_prof_units') == 'on' && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) !== false) {
  127. $oProfile = BxDolProfile::getInstanceByContentAndType($aData[$CNF['FIELD_ID']], $this->_aModule['name']);
  128. $aResult['members_count'] = $oConnection->getConnectedInitiatorsCount($oProfile->id(), false);
  129. $aResult['members_list'] = $oConnection->getConnectedListAPI($oProfile->id(), false, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS);
  130. }
  131. $aResult['visibility'] = $aData[$CNF['FIELD_ALLOW_VIEW_TO']];
  132. return $aResult;
  133. }
  134. public function serviceGetMenuAddonManageTools()
  135. {
  136. $CNF = &$this->_oConfig->CNF;
  137. $aResult = parent::serviceGetMenuAddonManageTools();
  138. if(!empty($CNF['FIELD_STATUS_ADMIN']))
  139. $aResult['counter1_value'] = $this->_oDb->getEntriesNumByParams([[
  140. 'key' => $CNF['FIELD_STATUS_ADMIN'],
  141. 'value' => BX_BASE_MOD_GENERAL_STATUS_PENDING,
  142. 'operator' => '='
  143. ]]);
  144. return $aResult;
  145. }
  146. public function serviceGetOptionsMembersMode()
  147. {
  148. $CNF = &$this->_oConfig->CNF;
  149. return array(
  150. array('key' => '', 'value' => _t('_None')),
  151. array('key' => BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES, 'value' => _t($CNF['T']['option_members_mode_' . BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES])),
  152. );
  153. }
  154. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  155. {
  156. if(empty($sUnitTemplate))
  157. $sUnitTemplate = 'unit.html';
  158. return parent::serviceGetSearchResultUnit($iContentId, $sUnitTemplate);
  159. }
  160. /**
  161. * @see BxBaseModProfileModule::serviceGetSpaceTitle
  162. */
  163. public function serviceGetSpaceTitle()
  164. {
  165. return _t($this->_oConfig->CNF['T']['txt_sample_single']);
  166. }
  167. /**
  168. * @see iBxDolProfileService::serviceGetParticipatingProfiles
  169. */
  170. public function serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects = false)
  171. {
  172. if (isset($this->_oConfig->CNF['OBJECT_CONNECTIONS'])){
  173. $aConnectionObjects = array($this->_oConfig->CNF['OBJECT_CONNECTIONS'], 'sys_profiles_subscriptions');
  174. return parent::serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects);
  175. }
  176. return parent::serviceGetParticipatingProfiles($iProfileId);
  177. }
  178. public function serviceGetSafeServices()
  179. {
  180. return array_merge(parent::serviceGetSafeServices(), [
  181. 'GetQuestionnaire' => '',
  182. 'GetInitialMembers' => '',
  183. ]);
  184. }
  185. /**
  186. * Check if this module entry can be used as profile
  187. */
  188. public function serviceActAsProfile ()
  189. {
  190. return false;
  191. }
  192. /**
  193. * Check if this module is group profile
  194. */
  195. public function serviceIsGroupProfile ()
  196. {
  197. return true;
  198. }
  199. public function serviceIsEnableForContext($iProfileId = 0)
  200. {
  201. $CNF = &$this->_oConfig->CNF;
  202. $sCnfKey = 'ENABLE_FOR_CONTEXT_IN_MODULES';
  203. if(empty($iProfileId) || empty($CNF[$sCnfKey]) || !is_array($CNF[$sCnfKey]))
  204. return false;
  205. $oProfile = BxDolProfile::getInstance($iProfileId);
  206. if(!$oProfile)
  207. return false;
  208. if(!in_array($oProfile->getModule(), $CNF[$sCnfKey]))
  209. return false;
  210. return true;
  211. }
  212. /**
  213. * check if provided profile is member of the group
  214. */
  215. public function serviceIsFan ($iGroupProfileId, $iProfileId = false)
  216. {
  217. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  218. return $oGroupProfile !== false && $this->isFan($oGroupProfile->getContentId(), $iProfileId);
  219. }
  220. /**
  221. * check if provided profile is admin of the group
  222. */
  223. public function serviceIsAdmin ($iGroupProfileId, $iProfileId = false)
  224. {
  225. if(!$iProfileId)
  226. $iProfileId = bx_get_logged_profile_id();
  227. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  228. if(!$oGroupProfile)
  229. return false;
  230. $iGroupContentId = $oGroupProfile->getContentId();
  231. if(!$this->isFan($iGroupContentId, $iProfileId))
  232. return false;
  233. $aGroupContentInfo = $this->_oDb->getContentInfoById($iGroupContentId);
  234. return $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aGroupContentInfo);
  235. }
  236. public function serviceGetAdminRole($iGroupProfileId, $iProfileId = false)
  237. {
  238. if(!$iProfileId)
  239. $iProfileId = bx_get_logged_profile_id();
  240. if(!$this->serviceIsAdmin($iGroupProfileId, $iProfileId))
  241. return 0;
  242. return $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  243. }
  244. /*
  245. * Get context (group) advanced members who can perform the specified action.
  246. * If 'Roles' are disabled for the context then all context admins are returned.
  247. */
  248. public function serviceGetAdminsByAction($iGroupProfileId, $mixedAction)
  249. {
  250. if(!$this->_oConfig->isAdmins())
  251. return [];
  252. $aGroupContentInfo = $this->_oDb->getContentInfoByProfileId($iGroupProfileId);
  253. if(empty($aGroupContentInfo) || !is_array($aGroupContentInfo))
  254. return [];
  255. $aAdmins = $this->_oDb->getAdmins($iGroupProfileId);
  256. if(!$this->_oConfig->isRoles())
  257. return $aAdmins;
  258. if(!is_array($mixedAction))
  259. $mixedAction = [$mixedAction];
  260. $aResult = [];
  261. foreach($mixedAction as $sAction)
  262. foreach($aAdmins as $iAdminProfileId)
  263. if(!in_array($iAdminProfileId, $aResult) && $this->isAllowedActionByRole($sAction, $aGroupContentInfo, $iGroupProfileId, $iAdminProfileId))
  264. $aResult[] = $iAdminProfileId;
  265. return $aResult;
  266. }
  267. public function serviceGetAdminsToManageContent($iGroupProfileId)
  268. {
  269. return $this->serviceGetAdminsByAction($iGroupProfileId, [
  270. BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT,
  271. BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT
  272. ]);
  273. }
  274. /**
  275. * Delete profile from fans and admins tables
  276. * @param $iProfileId profile id
  277. */
  278. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  279. {
  280. $CNF = &$this->_oConfig->CNF;
  281. $this->_oDb->deleteAdminsByProfileId($iProfileId);
  282. if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  283. $oConnection->onDeleteInitiatorAndContent($iProfileId);
  284. }
  285. /**
  286. * Reset group's author for particular group
  287. * @param $iContentId group id
  288. * @parem $iAuthorId new author profile ID
  289. * @return false of error, or number of updated records on success
  290. */
  291. public function serviceReassignEntityAuthor ($iContentId, $iAuthorId = 0)
  292. {
  293. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  294. if (!$aContentInfo)
  295. return false;
  296. if (empty($iAuthorId)) {
  297. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  298. if (!$oGroupProfile)
  299. return false;
  300. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  301. if($aAdmins)
  302. $iAuthorId = array_pop($aAdmins);
  303. }
  304. return $this->_oDb->updateAuthorById($iContentId, $iAuthorId);
  305. }
  306. /**
  307. * Entry actions and social sharing block
  308. */
  309. public function serviceEntityAllActions ($mixedContent = false, $aParams = array())
  310. {
  311. $CNF = &$this->_oConfig->CNF;
  312. if(!empty($mixedContent)) {
  313. if(!is_array($mixedContent))
  314. $mixedContent = array((int)$mixedContent, (method_exists($this->_oDb, 'getContentInfoById')) ? $this->_oDb->getContentInfoById((int)$mixedContent) : array());
  315. }
  316. else {
  317. $mixedContent = $this->_getContent();
  318. if($mixedContent === false)
  319. return false;
  320. }
  321. list($iContentId, $aContentInfo) = $mixedContent;
  322. if(!empty($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  323. $aParams = array_merge(array(
  324. 'entry_thumb' => (int)$aContentInfo[$CNF['FIELD_PICTURE']]
  325. ), $aParams);
  326. return parent::serviceEntityAllActions ($mixedContent, $aParams);
  327. }
  328. /**
  329. * Reset group's author when author profile is deleted
  330. * @param $iProfileId author profile id
  331. * @param $iAuthorId new author profile id
  332. * @return number of changed items
  333. */
  334. public function serviceReassignEntitiesByAuthor ($iProfileId, $iAuthorId = 0)
  335. {
  336. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  337. if (!$a)
  338. return 0;
  339. $iCount = 0;
  340. foreach ($a as $aContentInfo)
  341. $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']], $iAuthorId) ? 1 : 0);
  342. return $iCount;
  343. }
  344. public function servicePrepareFields ($aFieldsProfile)
  345. {
  346. $CNF = &$this->_oConfig->CNF;
  347. $aFieldsProfile[$CNF['FIELD_NAME']] = $aFieldsProfile['name'];
  348. $aFieldsProfile[$CNF['FIELD_TEXT']] = isset($aFieldsProfile['description']) ? $aFieldsProfile['description'] : '';
  349. unset($aFieldsProfile['name']);
  350. unset($aFieldsProfile['description']);
  351. return $aFieldsProfile;
  352. }
  353. public function serviceOnRemoveConnection ($iGroupProfileId, $iInitiatorId)
  354. {
  355. $CNF = &$this->_oConfig->CNF;
  356. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  357. if (!$oGroupProfile)
  358. return false;
  359. $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
  360. if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
  361. return $oConn->removeConnection($iProfileId, $iGroupProfileId);
  362. return false;
  363. }
  364. public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $bSendInviteOnly = false)
  365. {
  366. $CNF = &$this->_oConfig->CNF;
  367. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  368. if (!$oGroupProfile)
  369. return false;
  370. if (!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  371. return false;
  372. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  373. return false;
  374. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  375. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]));
  376. // send invitation to the group
  377. $sModule = $this->getName();
  378. if ($bSendInviteOnly && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  379. /**
  380. * @hooks
  381. * @hookdef hook-bx_base_groups-join_invitation '{module_name}', 'join_invitation' - hook before adding (sending) new join to context invitation
  382. * - $unit_name - module name
  383. * - $action - equals `join_invitation`
  384. * - $object_id - context id
  385. * - $sender_id - context profile id
  386. * - $extra_params - array of additional params with the following array keys:
  387. * - `content` - [array] context info array as key&value pairs
  388. * - `entry_title` - [string] context title
  389. * - `entry_url` - [string] context URL
  390. * - `group_profile` - [int] context profile id
  391. * - `profile` - [int] profile id who was invited
  392. * - `notification_subobject_id` - [int] profile id who was invited
  393. * - `object_author_id` - [int] context profile id
  394. * @hook @ref hook-bx_base_groups-join_invitation
  395. */
  396. bx_alert($sModule, 'join_invitation', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, [
  397. 'content' => $aContentInfo,
  398. 'entry_title' => $sEntryTitle,
  399. 'entry_url' => $sEntryUrl,
  400. 'group_profile' => $iGroupProfileId,
  401. 'profile' => $iProfileId,
  402. 'notification_subobject_id' => $iProfileId,
  403. 'object_author_id' => $iGroupProfileId
  404. ]);
  405. /**
  406. * 'Invitation Received' alert for Notifications module.
  407. * Note. It's essential to use Recipient ($iInitiatorId) in 'object_author_id' parameter.
  408. * In this case notification will be received by Recipient profile.
  409. */
  410. /**
  411. * @hooks
  412. * @hookdef hook-bx_base_groups-join_invitation_notif '{module_name}', 'join_invitation_notif' - hook before adding new join to context invitation. Is needed for Notifications module.
  413. * - $unit_name - module name
  414. * - $action - equals `join_invitation_notif`
  415. * - $object_id - context id
  416. * - $sender_id - context profile id
  417. * - $extra_params - array of additional params with the following array keys:
  418. * - `object_author_id` - [int] profile id who was invited
  419. * - `privacy_view` - [int] or [string] privacy for view context action, @see BxDolPrivacy
  420. * @hook @ref hook-bx_base_groups-join_invitation_notif
  421. */
  422. bx_alert($sModule, 'join_invitation_notif', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, [
  423. 'object_author_id' => $iInitiatorId,
  424. 'privacy_view' => isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 3,
  425. ]);
  426. }
  427. // send notification to group's admins that new connection is pending confirmation
  428. elseif (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  429. /**
  430. * @hooks
  431. * @hookdef hook-bx_base_groups-join_request '{module_name}', 'join_request' - hook before adding new join to context request
  432. * - $unit_name - module name
  433. * - $action - equals `join_request`
  434. * - $object_id - context id
  435. * - $sender_id - context profile id
  436. * - $extra_params - array of additional params with the following array keys:
  437. * - `object_author_id` - [int] context profile id
  438. * - `performer_id` - [int] profile id who wants to join
  439. * - `content` - [array] context info array as key&value pairs
  440. * - `entry_title` - [string] context title
  441. * - `entry_url` - [string] context URL
  442. * - `group_profile` - [int] context profile id
  443. * - `profile` - [int] profile id who wants to join
  444. * @hook @ref hook-bx_base_groups-join_request
  445. */
  446. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, [
  447. 'object_author_id' => $iGroupProfileId,
  448. 'performer_id' => $iProfileId,
  449. 'content' => $aContentInfo,
  450. 'entry_title' => $sEntryTitle,
  451. 'entry_url' => $sEntryUrl,
  452. 'group_profile' => $iGroupProfileId,
  453. 'profile' => $iProfileId
  454. ]);
  455. }
  456. // send notification that join request was accepted
  457. else if (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  458. /**
  459. * @hooks
  460. * @hookdef hook-bx_base_groups-join_request_accepted '{module_name}', 'join_request_accepted' - hook before accepting join to context request
  461. * It's equivalent to @ref hook-bx_base_groups-join_request
  462. * @hook @ref hook-bx_base_groups-join_request_accepted
  463. */
  464. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, [
  465. 'object_author_id' => $iGroupProfileId,
  466. 'performer_id' => $iProfileId,
  467. 'content' => $aContentInfo,
  468. 'entry_title' => $sEntryTitle,
  469. 'entry_url' => $sEntryUrl,
  470. 'group_profile' => $iGroupProfileId,
  471. 'profile' => $iProfileId
  472. ]);
  473. }
  474. // new fan was added
  475. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  476. // follow group on join
  477. if (BxDolService::call($oGroupProfile->getModule(), 'act_as_profile')){
  478. $this->addFollower($oGroupProfile->id(), (int)$iInitiatorId);
  479. }
  480. else{
  481. $this->addFollower((int)$iInitiatorId, $oGroupProfile->id());
  482. }
  483. /**
  484. * @hooks
  485. * @hookdef hook-bx_base_groups-fan_added '{module_name}', 'fan_added' - hook before adding (registering) new context member
  486. * It's equivalent to @ref hook-bx_base_groups-join_request
  487. * @hook @ref hook-bx_base_groups-fan_added
  488. */
  489. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, [
  490. 'object_author_id' => $iGroupProfileId,
  491. 'performer_id' => $iProfileId,
  492. 'content' => $aContentInfo,
  493. 'entry_title' => $sEntryTitle,
  494. 'entry_url' => $sEntryUrl,
  495. 'group_profile' => $iGroupProfileId,
  496. 'profile' => $iProfileId,
  497. ]);
  498. $this->doAudit($iGroupProfileId, $iInitiatorId, '_sys_audit_action_group_join_request_accepted');
  499. return false;
  500. }
  501. // don't automatically add connection (mutual) if group requires manual join confirmation
  502. if ($bSendInviteOnly || $aContentInfo['join_confirmation'])
  503. return false;
  504. // check if connection already exists
  505. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  506. return false;
  507. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  508. return false;
  509. return true;
  510. }
  511. public function serviceFansTable ()
  512. {
  513. $CNF = &$this->_oConfig->CNF;
  514. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_CONNECTIONS']);
  515. if(!$oGrid)
  516. return false;
  517. if($this->_bIsApi){
  518. return [
  519. bx_api_get_block('grid', $oGrid->getCodeAPI())
  520. ];
  521. }
  522. return $oGrid->getCode();
  523. }
  524. public function serviceInvitesTable ()
  525. {
  526. $CNF = &$this->_oConfig->CNF;
  527. if(!isset($CNF['OBJECT_GRID_INVITES']))
  528. return false;
  529. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_INVITES']);
  530. if(!$oGrid)
  531. return false;
  532. if($this->_bIsApi)
  533. return [
  534. bx_api_get_block('grid', $oGrid->getCodeAPI())
  535. ];
  536. return $oGrid->getCode();
  537. }
  538. public function serviceBansTable ()
  539. {
  540. $CNF = &$this->_oConfig->CNF;
  541. if(!isset($CNF['OBJECT_GRID_BANS']))
  542. return false;
  543. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_BANS']);
  544. if(!$oGrid)
  545. return false;
  546. if($this->_bIsApi)
  547. return [
  548. bx_api_get_block('grid', $oGrid->getCodeAPI())
  549. ];
  550. return $oGrid->getCode();
  551. }
  552. public function serviceFans ($iContentId = 0, $bAsArray = false)
  553. {
  554. $CNF = &$this->_oConfig->CNF;
  555. if (!$iContentId)
  556. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  557. if (!$iContentId)
  558. return false;
  559. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  560. if (!$aContentInfo)
  561. return false;
  562. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  563. return false;
  564. if(!$bAsArray) {
  565. bx_import('BxDolConnection');
  566. $mixedResult = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  567. if (!$mixedResult)
  568. return MsgBox(_t('_sys_txt_empty'));
  569. }
  570. else
  571. $mixedResult = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  572. return $mixedResult;
  573. }
  574. public function serviceFansWithoutAdmins ($iContentId = 0, $bAsArray = false)
  575. {
  576. if (!$iContentId)
  577. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  578. if (!$iContentId)
  579. return false;
  580. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  581. if (!$aContentInfo)
  582. return false;
  583. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  584. return false;
  585. $CNF = &$this->_oConfig->CNF;
  586. $aFans = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  587. if(empty($aFans) || !is_array($aFans))
  588. return false;
  589. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  590. if(!empty($aAdmins) && is_array($aAdmins))
  591. $aFans = array_diff($aFans, $aAdmins);
  592. $iStart = (int)bx_get('start');
  593. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  594. if(!$iLimit)
  595. $iLimit = 4;
  596. return $this->_serviceBrowseQuick($aFans, $iStart, $iLimit);
  597. }
  598. public function serviceAdmins ($iContentId = 0)
  599. {
  600. $CNF = &$this->_oConfig->CNF;
  601. if(!$iContentId)
  602. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  603. if(!$iContentId)
  604. return false;
  605. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  606. if(!$oGroupProfile)
  607. return false;
  608. $iStart = (int)bx_get('start');
  609. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  610. if(!$iLimit)
  611. $iLimit = 4;
  612. $aProfiles = $this->_oDb->getAdmins($oGroupProfile->id(), $iStart, $iLimit+1);
  613. if(empty($aProfiles) || !is_array($aProfiles))
  614. return false;
  615. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  616. }
  617. public function serviceMembersByRole ($iContentId = 0, $iRole = BX_BASE_MOD_GROUPS_ROLE_COMMON)
  618. {
  619. $CNF = &$this->_oConfig->CNF;
  620. if(!$iContentId)
  621. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  622. if(!$iContentId)
  623. return false;
  624. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  625. if(!$oGroupProfile)
  626. return false;
  627. $iStart = (int)bx_get('start');
  628. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  629. if(!$iLimit)
  630. $iLimit = 4;
  631. $aProfiles = $this->_oDb->getRoles([
  632. 'type' => 'fan_pids_by_group_pid',
  633. 'group_profile_id' => $oGroupProfile->id(),
  634. 'role' => $iRole,
  635. 'start' => $iStart,
  636. 'limit' => $iLimit + 1
  637. ]);
  638. if(empty($aProfiles) || !is_array($aProfiles))
  639. return false;
  640. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  641. }
  642. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  643. {
  644. if (!$iProfileId)
  645. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  646. if (!$iProfileId)
  647. return '';
  648. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  649. }
  650. public function serviceBrowseFollowedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  651. {
  652. if (!$iProfileId)
  653. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  654. if (!$iProfileId)
  655. return '';
  656. return $this->_serviceBrowse ('followed_entries', array('followed_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  657. }
  658. public function serviceBrowseCreatedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  659. {
  660. if (!$iProfileId)
  661. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  662. if (!$iProfileId)
  663. return '';
  664. return $this->_serviceBrowse ('created_entries', array('author' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  665. }
  666. public function serviceBrowseRecommendationsFans ($iProfileId = 0, $aParams = [])
  667. {
  668. $CNF = &$this->_oConfig->CNF;
  669. if($this->_bIsApi)
  670. $aParams = bx_api_get_browse_params($aParams, true);
  671. if(!$iProfileId)
  672. $iProfileId = bx_get_logged_profile_id();
  673. if(!$iProfileId)
  674. return '';
  675. $aParams = array_merge([
  676. 'empty_message' => false,
  677. 'start' => 0,
  678. 'per_page' => 0
  679. ], $aParams);
  680. if(($iStartGet = bx_get('start')) !== false)
  681. $aParams['start'] = (int)$iStartGet;
  682. if(($iPerPageGet = bx_get('per_page')) !== false)
  683. $aParams['per_page'] = (int)$iPerPageGet;
  684. $oRecommendation = BxDolRecommendation::getObjectInstance($CNF['OBJECT_RECOMMENDATIONS_FANS']);
  685. if(!$oRecommendation)
  686. return false;
  687. if($this->_bIsApi) {
  688. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams);
  689. $aData = array_merge($aData, [
  690. 'module' => 'system',
  691. 'unit' => 'mixed',
  692. 'request_url' => '/api.php?r=bx_groups/browse_recommendations_fans&params[]=' . $iProfileId . '&params[]='
  693. ]);
  694. return [bx_api_get_block('browse', $aData)];
  695. }
  696. $sCode = $oRecommendation->getCode($iProfileId, $aParams);
  697. if(!$sCode && $aParams['empty_message'])
  698. $sCode = MsgBox(_t(!empty($aParams['empty_message_text']) ? $aParams['empty_message_text'] : '_Empty'));
  699. return $sCode;
  700. }
  701. public function serviceBrowseMembers ($iProfileId = 0, $aParams = [])
  702. {
  703. $CNF = &$this->_oConfig->CNF;
  704. if(!$iProfileId)
  705. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  706. if(!$iProfileId)
  707. return '';
  708. $oGroupProfile = BxDolProfile::getInstance($iProfileId);
  709. if(!($oGroupProfile))
  710. return '';
  711. return bx_srv('system', 'browse_members', [$oGroupProfile->id(), $CNF['OBJECT_CONNECTIONS'], $aParams], 'TemplServiceProfiles');
  712. }
  713. public function serviceEntityPricing($iProfileId = 0)
  714. {
  715. $CNF = &$this->_oConfig->CNF;
  716. if(!$iProfileId)
  717. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  718. if(!$iProfileId)
  719. return '';
  720. if(!$this->_oConfig->isPaidJoin())
  721. return '';
  722. $oPayments = BxDolPayments::getInstance();
  723. if(!$oPayments->isActive())
  724. return MsgBox(_t('_sys_payments_err_no_payments'));
  725. if($this->checkAllowedUsePaidJoin() !== CHECK_ACTION_RESULT_ALLOWED)
  726. return MsgBox(_t('_Access denied'));
  727. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  728. return MsgBox(_t('_Access denied'));
  729. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_MANAGE']);
  730. if(!$oGrid)
  731. return '';
  732. $sNote = '';
  733. if(!$oPayments->isAcceptingPayments($this->_iProfileId))
  734. $sNote = MsgBox(_t('_sys_payments_err_not_accept_payments', $oPayments->getDetailsUrl()));
  735. return $sNote . $oGrid->getCode();
  736. }
  737. public function serviceEntityJoin($iProfileId = 0)
  738. {
  739. $CNF = &$this->_oConfig->CNF;
  740. if(!$iProfileId)
  741. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  742. if(!$iProfileId)
  743. return '';
  744. if(!$this->_oConfig->isPaidJoin())
  745. return '';
  746. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_VIEW']);
  747. if(!$oGrid)
  748. return '';
  749. return $oGrid->getCode();
  750. }
  751. public function serviceEntityInvite ($iContentId = 0, $bErrorMsg = true)
  752. {
  753. $CNF = &$this->_oConfig->CNF;
  754. if(!isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE']))
  755. return false;
  756. return $this->_serviceEntityForm ('inviteForm', $iContentId, false, false, $bErrorMsg);
  757. }
  758. /**
  759. * Entry social sharing block
  760. */
  761. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  762. {
  763. if(!empty($mixedContent)) {
  764. if(!is_array($mixedContent))
  765. $mixedContent = array((int)$mixedContent, array());
  766. }
  767. else {
  768. $mixedContent = $this->_getContent();
  769. if($mixedContent === false)
  770. return false;
  771. }
  772. list($iContentId, $aContentInfo) = $mixedContent;
  773. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  774. if(!$oGroupProfile)
  775. return false;
  776. return parent::serviceEntitySocialSharing(array($iContentId, $aContentInfo), array(
  777. 'title' => $oGroupProfile->getDisplayName()
  778. ));
  779. }
  780. public function serviceIsPricingAvaliable($iProfileId)
  781. {
  782. if(!$this->_oConfig->isPaidJoin())
  783. return false;
  784. if($this->checkAllowedUsePaidJoin() !== CHECK_ACTION_RESULT_ALLOWED)
  785. return false;
  786. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  787. return false;
  788. return true;
  789. }
  790. public function serviceIsPaidJoinAvaliable($iGroupProfileId, $iProfileId = 0)
  791. {
  792. return $this->isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId);
  793. }
  794. public function serviceIsPaidJoinAvaliableByContent($iGroupContentId, $iProfileId = 0)
  795. {
  796. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName());
  797. if(!$oGroupProfile)
  798. return false;
  799. return $this->isPaidJoinByProfileForProfile($oGroupProfile->id(), $iProfileId);
  800. }
  801. public function serviceIsFreeJoinAvaliable($iGroupProfileId, $iProfileId = 0)
  802. {
  803. return !$this->isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId);
  804. }
  805. public function serviceIsFreeJoinAvaliableByContent($iGroupContentId, $iProfileId = 0)
  806. {
  807. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName());
  808. if(!$oGroupProfile)
  809. return false;
  810. return !$this->isPaidJoinByProfileForProfile($oGroupProfile->id(), $iProfileId);
  811. }
  812. /**
  813. * Is Paid Join enabled in the group and whether a profile can use it.
  814. *
  815. * @param type $iGroupProfileId - Group profile ID.
  816. * @param type $iProfileId - Profile ID of the user who wants to join.
  817. * @return boolean
  818. */
  819. public function isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId = 0)
  820. {
  821. $CNF = &$this->_oConfig->CNF;
  822. if(empty($iProfileId))
  823. $iProfileId = $this->_iProfileId;
  824. if(BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->isConnected($iProfileId, $iGroupProfileId))
  825. return false;
  826. return $this->isPaidJoinByProfile($iGroupProfileId);
  827. }
  828. /**
  829. * Is Paid Join enabled as is and whether a group has pricing plans added.
  830. *
  831. * @param type $iGroupProfileId - Group profile ID.
  832. * @return boolean
  833. */
  834. public function isPaidJoinByProfile($iGroupProfileId)
  835. {
  836. if(!$this->_oConfig->isPaidJoin())
  837. return false;
  838. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $iGroupProfileId));
  839. if(empty($aPrices) || !is_array($aPrices))
  840. return false;
  841. return true;
  842. }
  843. /**
  844. * Integration with Payments.
  845. */
  846. public function serviceGetPaymentData()
  847. {
  848. return $this->_aModule;
  849. }
  850. public function serviceGetCartItem($mixedItemId)
  851. {
  852. $CNF = &$this->_oConfig->CNF;
  853. if(!$mixedItemId)
  854. return array();
  855. if(is_numeric($mixedItemId))
  856. $aItem = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => (int)$mixedItemId));
  857. else
  858. $aItem = $this->_oDb->getPrices(array('type' => 'by_name', 'value' => $mixedItemId));
  859. if(empty($aItem) || !is_array($aItem))
  860. return array();
  861. if(!$this->isPaidJoinByProfile($aItem['profile_id']))
  862. return array();
  863. $oGroupProfile = BxDolProfile::getInstance($aItem['profile_id']);
  864. if(!$oGroupProfile)
  865. return array();
  866. $aGroupProfile = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  867. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  868. $sTitle = '';
  869. if(!empty($aItem['period']) && !empty($aItem['period_unit']))
  870. $sTitle = _t($CNF['T']['txt_cart_item_title'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']], $aItem['period'], $aItem['period_unit']);
  871. else
  872. $sTitle = _t($CNF['T']['txt_cart_item_title_lifetime'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']]);
  873. return array (
  874. 'id' => $aItem['id'],
  875. 'author_id' => $aGroupProfile[$CNF['FIELD_AUTHOR']],
  876. 'name' => $aItem['name'],
  877. 'title' => $sTitle,
  878. 'description' => '',
  879. 'url' => $oGroupProfile->getUrl(),
  880. 'price_single' => $aItem['price'],
  881. 'price_recurring' => $aItem['price'],
  882. 'period_recurring' => $aItem['period'],
  883. 'period_unit_recurring' => $aItem['period_unit'],
  884. 'trial_recurring' => 0
  885. );
  886. }
  887. public function serviceGetCartItems($iSellerId)
  888. {
  889. $CNF = &$this->_oConfig->CNF;
  890. if(empty($iSellerId))
  891. return array();
  892. $sModule = $this->getName();
  893. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  894. $aGroups = $this->_oDb->getEntriesBy(array('type' => 'author', 'author' => $iSellerId));
  895. $aResult = array();
  896. foreach($aGroups as $aGroup) {
  897. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aGroup[$CNF['FIELD_ID']], $sModule);
  898. if(!$oGroupProfile)
  899. continue;
  900. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $oGroupProfile->id()));
  901. if(empty($aPrices) || !is_array($aPrices))
  902. continue;
  903. $sTitle = $oGroupProfile->getDisplayName();
  904. $sUrl = $oGroupProfile->getUrl();
  905. foreach($aPrices as $aPrice)
  906. $aResult[] = array(
  907. 'id' => $aPrice['id'],
  908. 'author_id' => $iSellerId,
  909. 'name' => $aPrice['name'],
  910. 'title' => _t($CNF['T']['txt_cart_item_title'], $sTitle, $aRoles[$aPrice['role_id']], $aPrice['period'], $aPrice['period_unit']),
  911. 'description' => '',
  912. 'url' => $sUrl,
  913. 'price_single' => $aPrice['price'],
  914. 'price_recurring' => $aPrice['price'],
  915. 'period_recurring' => $aPrice['period'],
  916. 'period_unit_recurring' => $aPrice['period_unit']
  917. );
  918. }
  919. return $aResult;
  920. }
  921. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  922. {
  923. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  924. }
  925. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  926. {
  927. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  928. }
  929. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  930. {
  931. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  932. }
  933. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  934. {
  935. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  936. }
  937. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  938. {
  939. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  940. }
  941. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  942. {
  943. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  944. }
  945. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  946. {
  947. return true;
  948. }
  949. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  950. {
  951. $CNF = &$this->_oConfig->CNF;
  952. $aItem = $this->serviceGetCartItem($iItemId);
  953. if(empty($aItem) || !is_array($aItem))
  954. return array();
  955. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  956. $mixedPeriod = false;
  957. if((int)$aItemInfo['period'] != 0)
  958. $mixedPeriod = array(
  959. 'period' => (int)$aItemInfo['period'],
  960. 'period_unit' => $aItemInfo['period_unit'],
  961. 'period_reserve' => $CNF['PARAM_RECURRING_RESERVE']
  962. );
  963. if(!$this->setRole($aItemInfo['profile_id'], $iClientId, $aItemInfo['role_id'], $mixedPeriod, $sOrder))
  964. return array();
  965. return $aItem;
  966. }
  967. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  968. {
  969. $aItem = $this->serviceGetCartItem($iItemIdNew);
  970. if(empty($aItem) || !is_array($aItem))
  971. return array();
  972. $aItemInfoOld = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdOld));
  973. if(empty($aItemInfoOld) || !is_array($aItemInfoOld))
  974. return array();
  975. $aItemInfoNew = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdNew));
  976. if(empty($aItemInfoNew) || !is_array($aItemInfoNew))
  977. return array();
  978. if(!$this->unsetRole($aItemInfoOld['profile_id'], $iClientId))
  979. return array();
  980. $aResult = $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemIdNew, 1, $sOrder);
  981. if(empty($aResult) || !is_array($aResult))
  982. return array();
  983. return $aItem;
  984. }
  985. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  986. {
  987. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  988. if(empty($aItemInfo) || !is_array($aItemInfo))
  989. return false;
  990. return $this->unsetRole($aItemInfo['profile_id'], $iClientId);
  991. }
  992. public function serviceGetQuestionnaire($sSource, $sObject, $sAction, $iContentProfileId)
  993. {
  994. $CNF = &$this->_oConfig->CNF;
  995. $sMsg = _t('_sys_txt_not_found');
  996. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iContentProfileId);
  997. if(empty($aContentInfo) || !is_array($aContentInfo))
  998. return $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['code' => 1, 'msg' => $sMsg];
  999. $oForm = null;
  1000. if(($oConnection = BxDolConnection::getObjectInstance($sObject)) !== false) {
  1001. $oForm = $oConnection->getQuestionnaireForm($sAction, $iContentProfileId, ['request' => [
  1002. 's' => $sSource,
  1003. ]]);
  1004. if($oForm === false)
  1005. return $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['code' => 3, 'msg' => $sMsg];
  1006. }
  1007. else
  1008. return $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['code' => 2, 'msg' => $sMsg];
  1009. $oForm->initChecker();
  1010. if($oForm->isSubmittedAndValid()) {
  1011. $iProfileId = bx_get_logged_profile_id();
  1012. $aQuestions = $this->_oDb->getQuestions(['sample' => 'content_pid', 'content_pid' => $iContentProfileId]);
  1013. foreach($aQuestions as $aQuestion)
  1014. $this->_oDb->insertAnswer((int)$aQuestion['id'], $iProfileId, $oForm->getCleanValue('question_' . $aQuestion['id']));
  1015. return $this->_bIsApi ? [] : ['code' => 0, 'o' => $sObject, 'a' => $sAction, 'cpi' => $iContentProfileId, 'ci' => $aContentInfo[$CNF['FIELD_ID']], 'eval' => $this->_oConfig->getJsObject($sSource) . '.connActionPerformed(oData)'];
  1016. }
  1017. if($this->_bIsApi)
  1018. return [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => [
  1019. 'name' => $this->_oModule->getName(),
  1020. 'request' => ['url' => $oForm->aFormAttrs['action'], 'immutable' => true]]
  1021. ])];
  1022. bx_import('BxTemplFunctions');
  1023. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oConfig->getHtmlIds('popup_questionnaire'), _t($CNF['T']['popup_title_questionnaire']), $this->_oTemplate->parseHtmlByName('popup_qnr_questionnaire.html', [
  1024. 'form_id' => $oForm->getId(),
  1025. 'form' => $oForm->getCode(true),
  1026. ]));
  1027. return ['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]];
  1028. }
  1029. /**
  1030. * Data for Notifications module
  1031. */
  1032. public function serviceGetNotificationsData()
  1033. {
  1034. $sModule = $this->_aModule['name'];
  1035. $aSettingsTypes = ['follow_member', 'follow_context'];
  1036. if($this->serviceActAsProfile())
  1037. $aSettingsTypes = ['personal', 'follow_member'];
  1038. return [
  1039. 'handlers' => [
  1040. ['group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'],
  1041. ['group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'],
  1042. ['group' => $sModule . '_score_up', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteUp', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_up', 'module_class' => 'Module'],
  1043. ['group' => $sModule . '_score_down', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteDown', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_down', 'module_class' => 'Module'],
  1044. ['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'],
  1045. ['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'],
  1046. ['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']],
  1047. ['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'],
  1048. //--- Moderation related: For 'admins'.
  1049. ['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'],
  1050. ],
  1051. 'settings' => [
  1052. ['group' => 'vote', 'unit' => $sModule, 'action' => 'doVote', 'types' => $aSettingsTypes],
  1053. ['group' => 'score_up', 'unit' => $sModule, 'action' => 'doVoteUp', 'types' => $aSettingsTypes],
  1054. ['group' => 'score_down', 'unit' => $sModule, 'action' => 'doVoteDown', 'types' => $aSettingsTypes],
  1055. ['group' => 'fan', 'unit' => $sModule, 'action' => 'fan_added', 'types' => $aSettingsTypes],
  1056. ['group' => 'invite', 'unit' => $sModule, 'action' => 'join_invitation_notif', 'types' => ['personal']],
  1057. ['group' => 'join', 'unit' => $sModule, 'action' => 'join_request', 'types' => $aSettingsTypes],
  1058. ['group' => 'timeline_post', 'unit' => $sModule, 'action' => 'timeline_post_common', 'types' => $aSettingsTypes],
  1059. //--- Moderation related: For 'admins'.
  1060. ['group' => 'action_required', 'unit' => $sModule, 'action' => 'pending_approval', 'types' => ['personal']],
  1061. ],
  1062. 'alerts' => [
  1063. ['unit' => $sModule, 'action' => 'doVote'],
  1064. ['unit' => $sModule, 'action' => 'undoVote'],
  1065. ['unit' => $sModule, 'action' => 'doVoteUp'],
  1066. ['unit' => $sModule, 'action' => 'doVoteDown'],
  1067. ['unit' => $sModule, 'action' => 'fan_added'],
  1068. ['unit' => $sModule, 'action' => 'join_invitation_notif'],
  1069. ['unit' => $sModule, 'action' => 'join_request'],
  1070. ['unit' => $sModule, 'action' => 'timeline_post_common'],
  1071. //--- Moderation related: For 'admins'.
  1072. ['unit' => $sModule, 'action' => 'pending_approval'],
  1073. ]
  1074. ];
  1075. }
  1076. public function serviceGetNotificationsInsertData($oAlert, $aHandler, $aDataItems)
  1077. {
  1078. if($oAlert->sAction != 'join_invitation_notif' || empty($aDataItems) || !is_array($aDataItems))
  1079. return $aDataItems;
  1080. foreach($aDataItems as $iIndex => $aDataItem)
  1081. $aDataItems[$iIndex]['object_privacy_view'] = BX_DOL_PG_ALL;
  1082. return $aDataItems;
  1083. }
  1084. /**
  1085. * Notification about new invitation to join the group
  1086. */
  1087. public function serviceGetNotificationsJoinInvitation($aEvent)
  1088. {
  1089. $CNF = &$this->_oConfig->CNF;
  1090. $iContentId = (int)$aEvent['object_id'];
  1091. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  1092. if(!$oGroupProfile)
  1093. return array();
  1094. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1095. if(empty($aContentInfo) || !is_array($aContentInfo))
  1096. return array();
  1097. /*
  1098. * It's essential that 'object_owner_id' contains invited member profile id.
  1099. */
  1100. $oProfile = BxDolProfile::getInstance((int)$aEvent['object_owner_id']);
  1101. if(!$oProfile)
  1102. return array();
  1103. /*
  1104. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  1105. * because Subentry URL has higher display priority and notification
  1106. * should be linked to Group Profile (Group Profile -> Members tab)
  1107. * instead of Personal Profile of invited member.
  1108. */
  1109. $sEntryUrl = bx_absolute_url(str_replace(BX_DOL_URL_ROOT, '', $oGroupProfile->getUrl()), '{bx_url_root}');
  1110. return array(
  1111. 'entry_sample' => $CNF['T']['txt_sample_single'],
  1112. 'entry_url' => $sEntryUrl,
  1113. 'entry_caption' => $oGroupProfile->getDisplayName(),
  1114. 'subentry_sample' => $oProfile->getDisplayName(),
  1115. 'subentry_url' => $sEntryUrl,
  1116. 'lang_key' => $this->_oConfig->CNF['T']['txt_ntfs_join_invitation']
  1117. );
  1118. }
  1119. /**
  1120. * Notification about new member requst in the group
  1121. */
  1122. public function serviceGetNotificationsJoinRequest($aEvent)
  1123. {
  1124. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_join_request']);
  1125. }
  1126. /**
  1127. * Notification about new member in the group
  1128. */
  1129. public function serviceGetNotificationsFanAdded($aEvent)
  1130. {
  1131. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_fan_added']);
  1132. }
  1133. protected function _serviceGetNotification($aEvent, $sLangKey)
  1134. {
  1135. $CNF = &$this->_oConfig->CNF;
  1136. $iContentId = (int)$aEvent['object_id'];
  1137. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  1138. if(!$oGroupProfile)
  1139. return array();
  1140. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1141. if(empty($aContentInfo) || !is_array($aContentInfo))
  1142. return array();
  1143. $oProfile = BxDolProfile::getInstance((int)$aEvent['subobject_id']);
  1144. if(!$oProfile)
  1145. return array();
  1146. /*
  1147. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  1148. * because Subentry URL has higher display priority and notification
  1149. * should be linked to Group Profile (Group Profile -> Members tab)
  1150. * instead of Personal Profile of a member, who performed an action.
  1151. */
  1152. if(empty($CNF['URL_ENTRY_FANS']))
  1153. $sEntryUrl = bx_absolute_url(str_replace(BX_DOL_URL_ROOT, '', $oGroupProfile->getUrl()), '{bx_url_root}');
  1154. else
  1155. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_FANS'], [
  1156. 'profile_id' => $oGroupProfile->id()
  1157. ]), '{bx_url_root}');
  1158. return [
  1159. 'entry_sample' => $CNF['T']['txt_sample_single'],
  1160. 'entry_url' => $sEntryUrl,
  1161. 'entry_caption' => $oGroupProfile->getDisplayName(),
  1162. 'entry_author' => $oGroupProfile->id(),
  1163. 'subentry_sample' => $oProfile->getDisplayName(),
  1164. 'subentry_url' => $sEntryUrl,
  1165. 'lang_key' => $sLangKey
  1166. ];
  1167. }
  1168. /**
  1169. * Data for Timeline module
  1170. */
  1171. public function serviceGetTimelineData()
  1172. {
  1173. $aResult = BxBaseModGeneralModule::serviceGetTimelineData();
  1174. $sModule = $this->_aModule['name'];
  1175. $aResult['handlers'] = array_merge($aResult['handlers'], [
  1176. ['group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'context_cover_changed'],
  1177. ['group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'context_cover_deleted']
  1178. ]);
  1179. $aResult['alerts'] = array_merge($aResult['alerts'], [
  1180. ['unit' => $sModule, 'action' => 'context_cover_changed'],
  1181. ['unit' => $sModule, 'action' => 'context_cover_deleted']
  1182. ]);
  1183. return $aResult;
  1184. }
  1185. /**
  1186. * Entry post for Timeline module
  1187. */
  1188. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  1189. {
  1190. $CNF = &$this->_oConfig->CNF;
  1191. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  1192. if($a === false)
  1193. return false;
  1194. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  1195. $a['content']['url'] = $oGroupProfile->getUrl();
  1196. $a['content']['title'] = $oGroupProfile->getDisplayName();
  1197. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1198. if($oConnection)
  1199. $a['content']['members'] = $oConnection->getConnectedInitiatorsCount($oGroupProfile->id(), true);
  1200. if(isset($aEvent['object_privacy_view']))
  1201. $a['content']['visibility'] = $aEvent['object_privacy_view'];
  1202. if(isset($CNF['FIELD_PUBLISHED'])) {
  1203. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  1204. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $a['date'])
  1205. $a['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  1206. }
  1207. return $a;
  1208. }
  1209. // ====== PERMISSION METHODS
  1210. /**
  1211. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1212. */
  1213. public function checkAllowedUsePaidJoin($isPerformAction = false)
  1214. {
  1215. // check ACL
  1216. $aCheck = checkActionModule($this->_iProfileId, 'use paid join', $this->getName(), $isPerformAction);
  1217. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  1218. return $aCheck[CHECK_ACTION_MESSAGE];
  1219. return CHECK_ACTION_RESULT_ALLOWED;
  1220. }
  1221. /**
  1222. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1223. */
  1224. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  1225. {
  1226. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  1227. }
  1228. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  1229. {
  1230. $CNF = &$this->_oConfig->CNF;
  1231. $iGroupContentId = (int)$aDataEntry[$CNF['FIELD_ID']];
  1232. $bInvited = false;
  1233. if(!empty($CNF['TABLE_INVITES'])) {
  1234. $iGroupProfileId = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName())->id();
  1235. if(($sKey = bx_get('key')) !== false) {
  1236. $mixedInvited = $this->isInvited($sKey, $iGroupProfileId);
  1237. if($mixedInvited === true)
  1238. $bInvited = true;
  1239. }
  1240. else {
  1241. $mixedInvited = $this->isInvitedByProfileId($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $iGroupProfileId);
  1242. if($mixedInvited === true)
  1243. $bInvited = true;
  1244. }
  1245. }
  1246. if ($this->isFan($iGroupContentId, $iProfileId) || $bInvited)
  1247. return CHECK_ACTION_RESULT_ALLOWED;
  1248. return parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId);
  1249. }
  1250. /**
  1251. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1252. */
  1253. public function checkAllowedCompose(&$aDataEntry, $isPerformAction = false)
  1254. {
  1255. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  1256. return _t('_sys_txt_access_denied');
  1257. return parent::checkAllowedCompose ($aDataEntry, $isPerformAction);
  1258. }
  1259. /**
  1260. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1261. */
  1262. public function checkAllowedFanAdd(&$aDataEntry, $isPerformAction = false)
  1263. {
  1264. $mixedResult = $this->_modGroupsCheckAllowedFanAdd($aDataEntry, $isPerformAction);
  1265. /**
  1266. * @hooks
  1267. * @hookdef hook-system-check_allowed_fan_add 'system', 'check_allowed_fan_add' - hook to override the result of checking whether 'fan add' to context action is allowed or not to currently logged in user
  1268. * - $unit_name - equals `system`
  1269. * - $action - equals `check_allowed_fan_add`
  1270. * - $object_id - not used
  1271. * - $sender_id - not used
  1272. * - $extra_params - array of additional params with the following array keys:
  1273. * - `module` - [string] module name
  1274. * - `content_info` - [array] context info array as key&value pairs
  1275. * - `profile_id` - [int] currently logged in profile id to be checked the availability of the action to
  1276. * - `override_result` - [string] or [int] by ref, check action result, can be overridden in hook processing. Return string with an error if action isn't allowed or CHECK_ACTION_RESULT_ALLOWED, @see BxDolAcl
  1277. * @hook @ref hook-system-check_allowed_fan_add
  1278. */
  1279. bx_alert('system', 'check_allowed_fan_add', 0, 0, [
  1280. 'module' => $this->getName(),
  1281. 'content_info' => $aDataEntry,
  1282. 'profile_id' => bx_get_logged_profile_id(),
  1283. 'override_result' => &$mixedResult
  1284. ]);
  1285. return $mixedResult;
  1286. }
  1287. public function _modGroupsCheckAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  1288. {
  1289. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) || !isLogged())
  1290. return _t('_sys_txt_access_denied');
  1291. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, false);
  1292. }
  1293. /**
  1294. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1295. */
  1296. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  1297. {
  1298. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  1299. return CHECK_ACTION_RESULT_ALLOWED;
  1300. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  1301. }
  1302. protected function _checkAllowedActionByFan($sAction, $aDataEntry, $iProfileId = 0)
  1303. {
  1304. $CNF = &$this->_oConfig->CNF;
  1305. $bRoles = $this->_oConfig->isRoles();
  1306. if(empty($iProfileId))
  1307. $iProfileId = bx_get_logged_profile_id();
  1308. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  1309. if(!$oGroupProfile)
  1310. return $sAction == BX_BASE_MOD_GROUPS_ACTION_DELETE ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_not_found');
  1311. $iGroupProfileId = $oGroupProfile->id();
  1312. if(!$bRoles && $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aDataEntry))
  1313. return CHECK_ACTION_RESULT_ALLOWED;
  1314. if($bRoles && $this->isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId))
  1315. return CHECK_ACTION_RESULT_ALLOWED;
  1316. return _t('_sys_txt_access_denied');
  1317. }
  1318. public function isAllowedActionByRole($mAction, $aDataEntry, $iGroupProfileId, $iProfileId)
  1319. {
  1320. $iProfileRole = $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  1321. if (is_array($mAction)) {
  1322. $sAction = $mAction['action'];
  1323. $sActionModule = $mAction['module'];
  1324. } else {
  1325. $sAction = $mAction;
  1326. $sActionModule = $this->getName();
  1327. }
  1328. $bResult = false;
  1329. if($iProfileId)
  1330. $bResult = $this->isAllowedModuleActionByRole($sActionModule, $sAction, $iProfileRole);
  1331. // in case neither of the profile's roles are having permissions set explicitly then fallback to an old way
  1332. if ($bResult === NULL) {
  1333. $bResult = false;
  1334. if($this->isFanByGroupProfileId($iGroupProfileId)) {
  1335. switch ($sAction) {
  1336. case BX_BASE_MOD_GROUPS_ACTION_DELETE:
  1337. case BX_BASE_MOD_GROUPS_ACTION_EDIT:
  1338. case BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER:
  1339. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  1340. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR))
  1341. $bResult = true;
  1342. break;
  1343. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS:
  1344. case BX_BASE_MOD_GROUPS_ACTION_INVITE:
  1345. case BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT:
  1346. case BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT:
  1347. case BX_BASE_MOD_GROUPS_ACTION_TIMELINE_POST_PIN:
  1348. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR) || $this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_MODERATOR))
  1349. $bResult = true;
  1350. break;
  1351. default:
  1352. $bResult = true;
  1353. }
  1354. }
  1355. }
  1356. /**
  1357. * @hooks
  1358. * @hookdef hook-system-check_allowed_action_by_role 'system', 'check_allowed_action_by_role' - hook to override the result of checking whether an action is allowed or not to context member by his role in the context
  1359. * - $unit_name - equals `system`
  1360. * - $action - equals `check_allowed_action_by_role`
  1361. * - $object_id - not used
  1362. * - $sender_id - not used
  1363. * - $extra_params - array of additional params with the following array keys:
  1364. * - `module` - [string] module name
  1365. * - `multi_roles` - [boolean] whether multi roles are enabled in context or not
  1366. * - `action` - [string] action to be checked
  1367. * - `action_module` - [string] module name which the action belongs to
  1368. * - `content_profile_id` - [int] context profile id
  1369. * - `content_info` - [array] context info array as key&value pairs
  1370. * - `profile_id` - [int] profile id to be checked the availability of the action to
  1371. * - `profile_role` - [int] profile role in the context
  1372. * - `override_result` - [boolean] by ref, check action result, can be overridden in hook processing.
  1373. * @hook @ref hook-system-check_allowed_action_by_role
  1374. */
  1375. bx_alert('system', 'check_allowed_action_by_role', 0, 0, [
  1376. 'module' => $this->getName(),
  1377. 'multi_roles' => $this->_oConfig->isMultiRoles(),
  1378. 'action' => $sAction,
  1379. 'action_module' => $sActionModule,
  1380. 'content_profile_id' => $iGroupProfileId,
  1381. 'content_info' => $aDataEntry,
  1382. 'profile_id' => $iProfileId,
  1383. 'profile_role' => $iProfileRole,
  1384. 'override_result' => &$bResult
  1385. ]);
  1386. return $bResult;
  1387. }
  1388. public function isAllowedModuleActionByRole($sModule, $sAction, $iProfileRole)
  1389. {
  1390. static $aRoles;
  1391. if (!$aRoles && isset($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES']) && !empty($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES']))
  1392. $aRoles = BxBaseFormView::getDataItems($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES'], true, BX_DATA_VALUES_ALL);
  1393. if ($aRoles) {
  1394. foreach ($aRoles as $iRole => $aRoleData) {
  1395. if ($iRole == 0 && $iProfileRole == 0 || $iRole > 0 && $this->isRole($iProfileRole, $iRole)) {
  1396. $mPermissions = isset($aRoles[$iRole]) && isset($aRoles[$iRole]['Data']) && !empty($aRoles[$iRole]['Data']) ? unserialize($aRoles[$iRole]['Data']) : false;
  1397. if ($mPermissions && isset($mPermissions[$sModule])) {
  1398. return isset($mPermissions[$sModule][$sAction]) && $mPermissions[$sModule][$sAction];
  1399. }
  1400. }
  1401. }
  1402. }
  1403. return NULL;
  1404. }
  1405. public function isAllowedModuleActionByProfile($iContentId, $sPostModule, $sAction, $iProfileId = 0) {
  1406. if (!$iProfileId) $iProfileId = bx_get_logged_profile_id();
  1407. if ($iProfileId && $this->isFan($iContentId, $iProfileId)) {
  1408. $sModuleName = $this->getName();
  1409. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $sModuleName);
  1410. $aDataEntry = $this->_oDb->getContentInfoById($iContentId);
  1411. $bResult = $this->isAllowedActionByRole(['action' => $sAction, 'module' => $sPostModule], $aDataEntry, $oGroupProfile->id(), $iProfileId);
  1412. if ($bResult === true) return CHECK_ACTION_RESULT_ALLOWED;
  1413. if ($bResult === false) return _t('_sys_txt_access_denied');
  1414. }
  1415. 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.
  1416. }
  1417. public function isRole($iProfileRole, $iRole)
  1418. {
  1419. if(!$this->_oConfig->isMultiRoles())
  1420. return $iProfileRole == $iRole;
  1421. else
  1422. return $iProfileRole & (1 << ($iRole - 1));
  1423. }
  1424. public function serviceIsRole($iProfileRole, $iRole)
  1425. {
  1426. return $this->isRole($iProfileRole, $iRole);
  1427. }
  1428. public function checkAllowedManageFans($mixedDataEntry, $isPerformAction = false)
  1429. {
  1430. $aDataEntry = array();
  1431. if(!is_array($mixedDataEntry)) {
  1432. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  1433. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  1434. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1435. }
  1436. else
  1437. $aDataEntry = $mixedDataEntry;
  1438. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1439. return CHECK_ACTION_RESULT_ALLOWED;
  1440. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1441. }
  1442. public function checkAllowedManageAdmins($mixedDataEntry, $isPerformAction = false)
  1443. {
  1444. $aDataEntry = array();
  1445. if(!is_array($mixedDataEntry)) {
  1446. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  1447. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  1448. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1449. }
  1450. else
  1451. $aDataEntry = $mixedDataEntry;
  1452. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1453. return CHECK_ACTION_RESULT_ALLOWED;
  1454. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1455. }
  1456. public function checkAllowedEdit($aDataEntry, $isPerformAction = false)
  1457. {
  1458. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_EDIT, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1459. return CHECK_ACTION_RESULT_ALLOWED;
  1460. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1461. }
  1462. public function checkAllowedInvite($aDataEntry, $isPerformAction = false)
  1463. {
  1464. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_INVITE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1465. return CHECK_ACTION_RESULT_ALLOWED;
  1466. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1467. }
  1468. public function checkAllowedChangeCover($aDataEntry, $isPerformAction = false)
  1469. {
  1470. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1471. return CHECK_ACTION_RESULT_ALLOWED;
  1472. return parent::checkAllowedChangeCover($aDataEntry, $isPerformAction);
  1473. }
  1474. public function checkAllowedDelete(&$aDataEntry, $isPerformAction = false)
  1475. {
  1476. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_DELETE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1477. return CHECK_ACTION_RESULT_ALLOWED;
  1478. return parent::checkAllowedDelete($aDataEntry, $isPerformAction);
  1479. }
  1480. public function checkAllowedJoin(&$aDataEntry, $isPerformAction = false)
  1481. {
  1482. if (bx_get('key')){
  1483. $sKey = bx_get('key');
  1484. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  1485. $aData = $this->_oDb->getInviteByKey($sKey, $oGroupProfile->id());
  1486. if (isset($aData['invited_profile_id']) && $aData['invited_profile_id'] == bx_get_logged_profile_id()){
  1487. return CHECK_ACTION_RESULT_ALLOWED;
  1488. }
  1489. }
  1490. return _t('_sys_txt_access_denied');
  1491. }
  1492. public function checkAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1493. {
  1494. $mixedResult = $this->_modGroupsCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1495. /**
  1496. * @hooks
  1497. * @hookdef hook-system-check_allowed_subscribe_add 'system', 'check_allowed_subscribe_add' - hook to override the result of checking whether currently logged in user can subscribe (follow) the context or not
  1498. * - $unit_name - equals `system`
  1499. * - $action - equals `check_allowed_subscribe_add`
  1500. * - $object_id - not used
  1501. * - $sender_id - not used
  1502. * - $extra_params - array of additional params with the following array keys:
  1503. * - `module` - [string] module name
  1504. * - `content_info` - [array] context info array as key&value pairs
  1505. * - `profile_id` - [int] currently logged in profile id to be checked the availability of the action to
  1506. * - `override_result` - [string] or [int] by ref, check action result, can be overridden in hook processing. Return string with an error if action isn't allowed or CHECK_ACTION_RESULT_ALLOWED, @see BxDolAcl
  1507. * @hook @ref hook-system-check_allowed_subscribe_add
  1508. */
  1509. bx_alert('system', 'check_allowed_subscribe_add', 0, 0, [
  1510. 'module' => $this->getName(),
  1511. 'content_info' => $aDataEntry,
  1512. 'profile_id' => bx_get_logged_profile_id(),
  1513. 'override_result' => &$mixedResult
  1514. ]);
  1515. return $mixedResult;
  1516. }
  1517. /**
  1518. * Note. Is mainly needed for internal usage. Access level is 'public' to allow outer calls from alerts.
  1519. */
  1520. public function _modGroupsCheckAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1521. {
  1522. $CNF = &$this->_oConfig->CNF;
  1523. if(!$this->isFan($aDataEntry[$CNF['FIELD_ID']]) && (!isset($CNF['PARAM_SBS_WO_JOIN']) || getParam($CNF['PARAM_SBS_WO_JOIN']) != 'on'))
  1524. return _t('_sys_txt_access_denied');
  1525. return parent::_modProfileCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1526. }
  1527. /**
  1528. * @deprecated since version 11.0.3 and can be removed in the next version.
  1529. */
  1530. public function _checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  1531. {
  1532. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  1533. }
  1534. public function doAudit($iGroupProfileId, $iFanId, $sAction)
  1535. {
  1536. $oProfile = BxDolProfile::getInstance($iFanId);
  1537. $iContentId = $oProfile->getContentId();
  1538. $sModule = $oProfile->getModule();
  1539. $oModule = BxDolModule::getInstance($sModule);
  1540. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile') && $oModule->_oConfig){
  1541. $CNF = $oModule->_oConfig->CNF;
  1542. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iContentId))) : array();
  1543. $AuditParams = array(
  1544. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  1545. 'context_profile_id' => $iGroupProfileId,
  1546. 'context_profile_title' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName()
  1547. );
  1548. bx_audit(
  1549. $iContentId,
  1550. $sModule,
  1551. $sAction,
  1552. $AuditParams
  1553. );
  1554. }
  1555. }
  1556. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  1557. {
  1558. $sResult = $this->checkAllowedView($aDataEntry);
  1559. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  1560. // if profile view isn't allowed but visibility is in partially visible groups
  1561. // then display buttons to connect (befriend, join) to profile,
  1562. // if other conditions (in parent::_checkAllowedConnect) are met as well
  1563. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && !in_array($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), array('s'))))
  1564. return $sResult;
  1565. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  1566. }
  1567. // ====== COMMON METHODS
  1568. public function onUpdateImage($iContentId, $sFiledName, $sFiledValue, $iProfileId = 0)
  1569. {
  1570. $CNF = &$this->_oConfig->CNF;
  1571. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1572. $aField2Method = [
  1573. $CNF['FIELD_PICTURE'] => 'picture',
  1574. $CNF['FIELD_COVER'] => 'cover',
  1575. ];
  1576. if(!empty($aField2Method[$sFiledName]))
  1577. /**
  1578. * @hooks
  1579. * @hookdef hook-bx_base_groups-context_picture_changed '{module_name}', 'context_picture_changed' - hook after context picture was changed
  1580. * - $unit_name - module name
  1581. * - $action - equals `context_picture_changed`
  1582. * - $object_id - context id
  1583. * - $sender_id - profile id who performed the action
  1584. * - $extra_params - array of additional params with the following array keys:
  1585. * - `status` - [string] context status
  1586. * - `status_admin` - [string] context admin status
  1587. * - `privacy_view` - [int] or [string] privacy for view context action, @see BxDolPrivacy
  1588. * - `cf` - [int] context's audience filter value
  1589. * @hook @ref hook-bx_base_groups-context_picture_changed
  1590. */
  1591. /**
  1592. * @hooks
  1593. * @hookdef hook-bx_base_groups-context_cover_changed '{module_name}', 'context_cover_changed' - hook after context cover was changed
  1594. * It's equivalent to @ref hook-bx_base_groups-context_picture_changed
  1595. * @hook @ref hook-bx_base_groups-context_cover_changed
  1596. */
  1597. bx_alert($this->getName(), 'context_' . $aField2Method[$sFiledName] . '_changed', $iContentId, $iProfileId, $this->_alertParams($aContentInfo));
  1598. }
  1599. protected function _alertParams($aContentInfo)
  1600. {
  1601. $aParams = parent::_alertParams($aContentInfo);
  1602. $CNF = &$this->_oConfig->CNF;
  1603. if(!empty($CNF['FIELD_CF']) && isset($aContentInfo[$CNF['FIELD_CF']]))
  1604. $aParams['cf'] = $aContentInfo[$CNF['FIELD_CF']];
  1605. return $aParams;
  1606. }
  1607. public function alertAfterAdd($aContentInfo)
  1608. {
  1609. $CNF = &$this->_oConfig->CNF;
  1610. $iId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1611. $iAuthorId = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  1612. $sAction = 'added';
  1613. if(isset($CNF['FIELD_STATUS_ADMIN']) && isset($aContentInfo[$CNF['FIELD_STATUS_ADMIN']]) && $aContentInfo[$CNF['FIELD_STATUS_ADMIN']] == BX_BASE_MOD_GENERAL_STATUS_PENDING)
  1614. $sAction = 'deferred';
  1615. $sModule = $this->getName();
  1616. $aParams = $this->_alertParams($aContentInfo);
  1617. /**
  1618. * @hooks
  1619. * @hookdef hook-system-prepare_alert_params 'system', 'prepare_alert_params' - hook to override alert (hook) params
  1620. * - $unit_name - equals `system`
  1621. * - $action - equals `prepare_alert_params`
  1622. * - $object_id - not used
  1623. * - $sender_id - not used
  1624. * - $extra_params - array of additional params with the following array keys:
  1625. * - `unit` - [string] unit name
  1626. * - `action` - [string] by ref, action, can be overridden in hook processing
  1627. * - `object_id` - [int] by ref, object id, can be overridden in hook processing
  1628. * - `sender_id` - [int] by ref, action performer profile id, can be overridden in hook processing
  1629. * - `extras` - [array] by ref, extra params array as key&value pairs, can be overridden in hook processing
  1630. * @hook @ref hook-system-prepare_alert_params
  1631. */
  1632. bx_alert('system', 'prepare_alert_params', 0, 0, [
  1633. 'unit'=> $sModule,
  1634. 'action' => &$sAction,
  1635. 'object_id' => &$iId,
  1636. 'sender_id' => &$iAuthorId,
  1637. 'extras' => &$aParams
  1638. ]);
  1639. /**
  1640. * @hooks
  1641. * @hookdef hook-bx_base_groups-added '{module_name}', 'added' - hook after context was added (published)
  1642. * - $unit_name - module name
  1643. * - $action - equals `added`
  1644. * - $object_id - context id
  1645. * - $sender_id - not used
  1646. * - $extra_params - array of additional params with the following array keys:
  1647. * - `status` - [string] context status
  1648. * - `status_admin` - [string] context admin status
  1649. * - `privacy_view` - [int] or [string] privacy for view context action, @see BxDolPrivacy
  1650. * - `cf` - [int] context's audience filter value
  1651. * @hook @ref hook-bx_base_groups-added
  1652. */
  1653. /**
  1654. * @hooks
  1655. * @hookdef hook-bx_base_groups-deferred '{module_name}', 'deferred' - hook after context was added with pending approval status
  1656. * It's equivalent to @ref hook-bx_base_groups-added
  1657. * @hook @ref hook-bx_base_groups-deferred
  1658. */
  1659. bx_alert($sModule, $sAction, $iId, false, $aParams);
  1660. $this->_processModerationNotifications($aContentInfo);
  1661. }
  1662. public function addFollower ($iProfileId1, $iProfileId2)
  1663. {
  1664. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  1665. if($oConnectionFollow && !$oConnectionFollow->isConnected($iProfileId1, $iProfileId2)){
  1666. $oConnectionFollow->addConnection($iProfileId1, $iProfileId2);
  1667. return true;
  1668. }
  1669. return false;
  1670. }
  1671. public function isFan ($iContentId, $iProfileId = false)
  1672. {
  1673. $CNF = &$this->_oConfig->CNF;
  1674. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  1675. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1676. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1677. return false;
  1678. }
  1679. public function isFanByGroupProfileId ($iGroupProfileId, $iProfileId = false)
  1680. {
  1681. $CNF = &$this->_oConfig->CNF;
  1682. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1683. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1684. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1685. return false;
  1686. }
  1687. public function isInvited ($sKey, $iGroupProfileId)
  1688. {
  1689. $CNF = &$this->_oConfig->CNF;
  1690. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  1691. if (!isset($aData['invited_profile_id']))
  1692. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1693. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1694. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1695. return true;
  1696. }
  1697. public function isInvitedByProfileId ($iProfileId, $iGroupProfileId)
  1698. {
  1699. $CNF = &$this->_oConfig->CNF;
  1700. $aData = $this->_oDb->getInviteByInvited($iProfileId, $iGroupProfileId);
  1701. if (!isset($aData['invited_profile_id']))
  1702. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1703. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1704. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1705. return true;
  1706. }
  1707. public function serviceIsInvited($iGroupProfileId, $iProfileId = false, $sKey = '')
  1708. {
  1709. if(!$iProfileId)
  1710. $iProfileId = bx_get_logged_profile_id();
  1711. if(empty($sKey) && ($sKey = bx_get('key')) !== false)
  1712. $sKey = bx_process_input($sKey);
  1713. $mixedInvited = false;
  1714. if(!empty($sKey))
  1715. $mixedInvited = $this->isInvited($sKey, $iGroupProfileId);
  1716. else if($iProfileId !== false)
  1717. $mixedInvited = $this->isInvitedByProfileId($iProfileId, $iGroupProfileId);
  1718. return $mixedInvited === true;
  1719. }
  1720. public function serviceIsNotInvited($iGroupProfileId, $iProfileId = false, $sKey = '')
  1721. {
  1722. return !$this->serviceIsInvited($iGroupProfileId, $iProfileId, $sKey);
  1723. }
  1724. public function serviceGetInvitedKey($iGroupProfileId, $iProfileId = false)
  1725. {
  1726. $sKey = '';
  1727. if(($sKey = bx_get('key')) !== false)
  1728. $sKey = bx_process_input($sKey);
  1729. if(!$sKey) {
  1730. if(!$iProfileId)
  1731. $iProfileId = bx_get_logged_profile_id();
  1732. if($iProfileId !== false) {
  1733. $aInvite = $this->_oDb->getInviteByInvited($iProfileId, $iGroupProfileId);
  1734. if(!empty($aInvite) && is_array($aInvite))
  1735. $sKey = $aInvite['key'];
  1736. }
  1737. }
  1738. return $sKey;
  1739. }
  1740. public function getRole($iGroupProfileId, $iFanProfileId)
  1741. {
  1742. if(!$this->isFanByGroupProfileId($iGroupProfileId, $iFanProfileId))
  1743. return false;
  1744. return $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1745. }
  1746. public function setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  1747. {
  1748. $CNF = &$this->_oConfig->CNF;
  1749. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1750. return false;
  1751. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1752. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1753. if(!$oConnection || !$oGroupProfile)
  1754. return false;
  1755. if(!$oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->addConnection($iFanProfileId, $iGroupProfileId))
  1756. return false;
  1757. if(!$this->_oDb->setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod, $sOrder))
  1758. return false;
  1759. $this->onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole);
  1760. return true;
  1761. }
  1762. public function onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole)
  1763. {
  1764. $CNF = &$this->_oConfig->CNF;
  1765. $iProfileId = bx_get_logged_profile_id();
  1766. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1767. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1768. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  1769. // notify about admin status
  1770. if(!empty($CNF['EMAIL_FAN_SET_ROLE']) && $iFanProfileId != $iProfileId) {
  1771. $aSetRoles = is_array($mixedRole) ? $mixedRole : [$mixedRole];
  1772. $aRolesNames = [];
  1773. foreach ($aSetRoles as $iRole)
  1774. $aRolesNames[] = $aRoles[(int)$iRole];
  1775. sendMailTemplate($CNF['EMAIL_FAN_SET_ROLE'], 0, $iFanProfileId, array(
  1776. 'EntryUrl' => $oGroupProfile->getUrl(),
  1777. 'EntryTitle' => $oGroupProfile->getDisplayName(),
  1778. 'Role' => implode(', ', $aRolesNames),
  1779. ), BX_EMAIL_NOTIFY);
  1780. }
  1781. /**
  1782. * @hooks
  1783. * @hookdef hook-bx_base_groups-set_role '{module_name}', 'set_role' - hook after 'set role' action was applied to context member
  1784. * - $unit_name - module name
  1785. * - $action - equals `set_role`
  1786. * - $object_id - context id
  1787. * - $sender_id - context profile id
  1788. * - $extra_params - array of additional params with the following array keys:
  1789. * - `object_author_id` - [int] context profile id
  1790. * - `performer_id` - [int] performer profile id
  1791. * - `fan_id` - [int] context member profile id
  1792. * - `content` - [array] context info array as key&value pairs
  1793. * - `role` - [int] or [array] role or an array of roles to be set
  1794. * - `group_profile` - [int] context profile id
  1795. * - `profile` - [int] performer profile id
  1796. * @hook @ref hook-bx_base_groups-set_role
  1797. */
  1798. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1799. 'object_author_id' => $iGroupProfileId,
  1800. 'performer_id' => $iProfileId,
  1801. 'fan_id' => $iFanProfileId,
  1802. 'content' => $aGroupProfileInfo,
  1803. 'role' => $mixedRole,
  1804. 'group_profile' => $iGroupProfileId,
  1805. 'profile' => $iProfileId
  1806. ));
  1807. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1808. }
  1809. public function unsetRole($iGroupProfileId, $iFanProfileId)
  1810. {
  1811. $CNF = &$this->_oConfig->CNF;
  1812. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1813. return false;
  1814. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1815. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1816. if(!$oConnection || !$oGroupProfile)
  1817. return false;
  1818. if($oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->removeConnection($iFanProfileId, $iGroupProfileId))
  1819. return false;
  1820. $iRole = $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1821. if(!$this->_oDb->unsetRole($iGroupProfileId, $iFanProfileId))
  1822. return false;
  1823. $this->onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole);
  1824. return true;
  1825. }
  1826. public function onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole)
  1827. {
  1828. $CNF = &$this->_oConfig->CNF;
  1829. $iProfileId = bx_get_logged_profile_id();
  1830. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1831. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1832. /**
  1833. * @hooks
  1834. * @hookdef hook-bx_base_groups-set_role '{module_name}', 'set_role' - hook after 'set role' action was applied to context member
  1835. * - $unit_name - module name
  1836. * - $action - equals `set_role`
  1837. * - $object_id - context id
  1838. * - $sender_id - context profile id
  1839. * - $extra_params - array of additional params with the following array keys:
  1840. * - `object_author_id` - [int] context profile id
  1841. * - `performer_id` - [int] performer profile id
  1842. * - `fan_id` - [int] context member profile id
  1843. * - `content` - [array] context info array as key&value pairs
  1844. * - `role` - [int] or [array] role or an array of roles to be set
  1845. * - `group_profile` - [int] context profile id
  1846. * - `profile` - [int] performer profile id
  1847. * @hook @ref hook-bx_base_groups-set_role
  1848. */
  1849. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1850. 'object_author_id' => $iGroupProfileId,
  1851. 'performer_id' => $iProfileId,
  1852. 'fan_id' => $iFanProfileId,
  1853. 'content' => $aGroupProfileInfo,
  1854. 'role' => $iRole,
  1855. 'group_profile' => $iGroupProfileId,
  1856. 'profile' => $iProfileId
  1857. ));
  1858. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1859. }
  1860. public function getGroupsByFan($iProfileId, $mixedRole = false)
  1861. {
  1862. $CNF = &$this->_oConfig->CNF;
  1863. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1864. return false;
  1865. if($mixedRole === false)
  1866. $mixedRole = BX_BASE_MOD_GROUPS_ROLE_COMMON;
  1867. if(!is_array($mixedRole))
  1868. $mixedRole = [$mixedRole];
  1869. $aResult = [];
  1870. foreach($mixedRole as $iRole) {
  1871. switch($iRole) {
  1872. case BX_BASE_MOD_GROUPS_ROLE_COMMON:
  1873. $aIds = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($iProfileId);
  1874. break;
  1875. default:
  1876. $aIds = $this->_oDb->getRoles([
  1877. 'type' => 'group_pids_by_fan_id',
  1878. 'fan_id' => $iProfileId,
  1879. 'role' => $iRole
  1880. ]);
  1881. }
  1882. $aResult = array_merge($aResult, $aIds);
  1883. }
  1884. return $aResult;
  1885. }
  1886. public function getMenuItemTitleByConnection($sConnection, $sAction, $iContentProfileId, $iInitiatorProfileId = 0)
  1887. {
  1888. $CNF = $this->_oConfig->getCNF();
  1889. $aResult = parent::getMenuItemTitleByConnection($sConnection, $sAction, $iContentProfileId, $iInitiatorProfileId);
  1890. if(!empty($aResult))
  1891. return $aResult;
  1892. $oConnection = BxDolConnection::getObjectInstance($sConnection);
  1893. if(!$oConnection)
  1894. return false;
  1895. if(!$iInitiatorProfileId)
  1896. $iInitiatorProfileId = bx_get_logged_profile_id();
  1897. $aResult = [];
  1898. if($oConnection->isConnectedNotMutual($iInitiatorProfileId, $iContentProfileId))
  1899. $aResult = [
  1900. 'add' => _t(!empty($CNF['T']['menu_item_title_sm_join_requested']) ? $CNF['T']['menu_item_title_sm_join_requested'] : '_sys_menu_item_title_sm_join_requested'),
  1901. 'remove' => _t(!empty($CNF['T']['menu_item_title_sm_leave_cancel']) ? $CNF['T']['menu_item_title_sm_leave_cancel'] : '_sys_menu_item_title_sm_leave_cancel'),
  1902. ];
  1903. else if($oConnection->isConnectedNotMutual($iContentProfileId, $iInitiatorProfileId))
  1904. $aResult = [
  1905. 'add' => _t(!empty($CNF['T']['menu_item_title_sm_join_confirm']) ? $CNF['T']['menu_item_title_sm_join_confirm'] : '_sys_menu_item_title_sm_join_confirm'),
  1906. 'remove' => _t(!empty($CNF['T']['menu_item_title_sm_leave_reject']) ? $CNF['T']['menu_item_title_sm_leave_reject'] : '_sys_menu_item_title_sm_leave_reject'),
  1907. ];
  1908. else if($oConnection->isConnected($iInitiatorProfileId, $iContentProfileId, true))
  1909. $aResult = [
  1910. 'add' => '',
  1911. 'remove' => _t(!empty($CNF['T']['menu_item_title_sm_leave']) ? $CNF['T']['menu_item_title_sm_leave'] : '_sys_menu_item_title_sm_leave'),
  1912. ];
  1913. else
  1914. $aResult = [
  1915. 'add' => _t(!empty($CNF['T']['menu_item_title_sm_join']) ? $CNF['T']['menu_item_title_sm_join'] : '_sys_menu_item_title_sm_join'),
  1916. 'remove' => '',
  1917. ];
  1918. return !empty($sAction) && isset($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
  1919. }
  1920. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  1921. {
  1922. $CNF = &$this->_oConfig->CNF;
  1923. $iImageId = 0;
  1924. $sImageMd = $sImageXl = '';
  1925. if(isset($CNF['FIELD_COVER']) && !empty($aContentInfo[$CNF['FIELD_COVER']])) {
  1926. $iImageId = (int)$aContentInfo[$CNF['FIELD_COVER']];
  1927. $sImageMd = $this->_oConfig->getImageUrl($iImageId, ['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  1928. $sImageXl = $this->_oConfig->getImageUrl($iImageId, ['OBJECT_IMAGES_TRANSCODER_COVER']);
  1929. }
  1930. if($sImageXl == '' && isset($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']])) {
  1931. $iImageId = (int)$aContentInfo[$CNF['FIELD_PICTURE']];
  1932. $sImageMd = $this->_oConfig->getImageUrl($iImageId, ['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  1933. $sImageXl = $this->_oConfig->getImageUrl($iImageId, ['OBJECT_IMAGES_TRANSCODER_COVER']);
  1934. }
  1935. if(empty($sImageXl))
  1936. return [];
  1937. $aImage = [
  1938. 'id' => $iImageId,
  1939. 'url' => $sUrl,
  1940. 'src' => $sImageXl,
  1941. 'src_orig' => $sImageXl
  1942. ];
  1943. if(!empty($sImageMd))
  1944. $aImage['src_medium'] = $sImageMd;
  1945. return [
  1946. $aImage
  1947. ];
  1948. }
  1949. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  1950. {
  1951. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  1952. return array(0, 0, null);
  1953. if ($oGroupProfile->getModule() == $this->getName()) {
  1954. $iProfileId = $iInitiatorId;
  1955. $iGroupProfileId = $oGroupProfile->id();
  1956. } else {
  1957. $iProfileId = $oGroupProfile->id();
  1958. $iGroupProfileId = $iInitiatorId;
  1959. }
  1960. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  1961. }
  1962. }
  1963. /** @} */