BxBaseModGroupsModule.php 110 KB

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