1
0

BxBaseModGroupsModule.php 113 KB

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