BxBaseModGroupsModule.php 109 KB

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