BxBaseModGroupsModule.php 111 KB

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