BxBaseModGroupsModule.php 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup BaseGroups Base classes for groups modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. define('BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES', 'multi_roles');
  12. define('BX_BASE_MOD_GROUPS_ROLE_COMMON', 0);
  13. define('BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR', 1);
  14. define('BX_BASE_MOD_GROUPS_ROLE_MODERATOR', 2);
  15. define('BX_BASE_MOD_GROUPS_ACTION_EDIT', 'edit');
  16. define('BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER', 'change_cover');
  17. define('BX_BASE_MOD_GROUPS_ACTION_INVITE', 'invite');
  18. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS', 'manage_fans');
  19. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES', 'manage_roles');
  20. define('BX_BASE_MOD_GROUPS_ACTION_DELETE', 'delete');
  21. define('BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT', 'edit_any');
  22. define('BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT', 'delete_any');
  23. define('BX_BASE_MOD_GROUPS_ACTION_TIMELINE_POST_PIN', 'pin'); //for timeline posts only
  24. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY', 'day');
  25. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK', 'week');
  26. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_MONTH', 'month');
  27. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_YEAR', 'year');
  28. /**
  29. * Groups profiles module.
  30. */
  31. class BxBaseModGroupsModule extends BxBaseModProfileModule
  32. {
  33. function __construct(&$aModule)
  34. {
  35. parent::__construct($aModule);
  36. $CNF = &$this->_oConfig->CNF;
  37. if(isset($CNF['FIELD_PUBLISHED']))
  38. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  39. $CNF['FIELD_PUBLISHED'],
  40. ));
  41. }
  42. /**
  43. * Get possible recipients for start conversation form
  44. */
  45. public function actionAjaxGetInitialMembers ()
  46. {
  47. $sTerm = bx_get('term');
  48. $a = BxDolService::call('system', 'profiles_search', [$sTerm, ['module' => $this->_oConfig->getName()]], 'TemplServiceProfiles');
  49. header('Content-Type:text/javascript; charset=utf-8');
  50. echo(json_encode($a));
  51. }
  52. /**
  53. * Process Process Invitation
  54. */
  55. public function actionProcessInvite ($sKey, $iGroupProfileId, $bAccept)
  56. {
  57. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  58. if (isset($aData['invited_profile_id'])){
  59. $CNF = &$this->_oConfig->CNF;
  60. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  61. return '';
  62. $iInvitedProfileId = $aData['invited_profile_id'];
  63. if ($iInvitedProfileId != bx_get_logged_profile_id())
  64. return '';
  65. if ($bAccept){
  66. if($oConnection && !$oConnection->isConnected($iInvitedProfileId, $iGroupProfileId)){
  67. $oConnection->addConnection($iInvitedProfileId, $iGroupProfileId);
  68. $oConnection->addConnection($iGroupProfileId, $iInvitedProfileId);
  69. }
  70. }
  71. $this->_oDb->deleteInviteByKey($sKey, $iGroupProfileId);
  72. }
  73. }
  74. public function actionCheckName()
  75. {
  76. $CNF = &$this->_oConfig->CNF;
  77. $sName = bx_process_input(bx_get('name'));
  78. if(empty($sName))
  79. return echoJson(array());
  80. $sResult = '';
  81. $iId = (int)bx_get('id');
  82. if(!empty($iId)) {
  83. $aPrice = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iId));
  84. if(strcmp($sName, $aPrice[$CNF['FIELD_PRICE_NAME']]) == 0)
  85. $sResult = $sName;
  86. }
  87. echoJson(array(
  88. 'name' => !empty($sResult) ? $sResult : $this->_oConfig->getPriceName($sName)
  89. ));
  90. }
  91. public function serviceManageTools($sType = 'common')
  92. {
  93. $sResult = parent::serviceManageTools($sType);
  94. if(!empty($sResult))
  95. $this->_oTemplate->addJsSystem(['modules/base/groups/js/|manage_tools.js']);
  96. return $sResult;
  97. }
  98. public function serviceGetMenuAddonManageTools()
  99. {
  100. $CNF = &$this->_oConfig->CNF;
  101. $aResult = parent::serviceGetMenuAddonManageTools();
  102. if(!empty($CNF['FIELD_STATUS_ADMIN']))
  103. $aResult['counter1_value'] = $this->_oDb->getEntriesNumByParams([[
  104. 'key' => $CNF['FIELD_STATUS_ADMIN'],
  105. 'value' => BX_BASE_MOD_GENERAL_STATUS_PENDING,
  106. 'operator' => '='
  107. ]]);
  108. return $aResult;
  109. }
  110. public function serviceGetOptionsMembersMode()
  111. {
  112. $CNF = &$this->_oConfig->CNF;
  113. return array(
  114. array('key' => '', 'value' => _t('_None')),
  115. array('key' => BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES, 'value' => _t($CNF['T']['option_members_mode_' . BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES])),
  116. );
  117. }
  118. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  119. {
  120. if(empty($sUnitTemplate))
  121. $sUnitTemplate = 'unit.html';
  122. return parent::serviceGetSearchResultUnit($iContentId, $sUnitTemplate);
  123. }
  124. /**
  125. * @see BxBaseModProfileModule::serviceGetSpaceTitle
  126. */
  127. public function serviceGetSpaceTitle()
  128. {
  129. return _t($this->_oConfig->CNF['T']['txt_sample_single']);
  130. }
  131. /**
  132. * @see iBxDolProfileService::serviceGetParticipatingProfiles
  133. */
  134. public function serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects = false)
  135. {
  136. if (isset($this->_oConfig->CNF['OBJECT_CONNECTIONS'])){
  137. $aConnectionObjects = array($this->_oConfig->CNF['OBJECT_CONNECTIONS'], 'sys_profiles_subscriptions');
  138. return parent::serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects);
  139. }
  140. return parent::serviceGetParticipatingProfiles($iProfileId);
  141. }
  142. /**
  143. * Check if this module entry can be used as profile
  144. */
  145. public function serviceActAsProfile ()
  146. {
  147. return false;
  148. }
  149. /**
  150. * Check if this module is group profile
  151. */
  152. public function serviceIsGroupProfile ()
  153. {
  154. return true;
  155. }
  156. public function serviceIsEnableForContext($iProfileId = 0)
  157. {
  158. $CNF = &$this->_oConfig->CNF;
  159. $sCnfKey = 'ENABLE_FOR_CONTEXT_IN_MODULES';
  160. if(empty($iProfileId) || empty($CNF[$sCnfKey]) || !is_array($CNF[$sCnfKey]))
  161. return false;
  162. $oProfile = BxDolProfile::getInstance($iProfileId);
  163. if(!$oProfile)
  164. return false;
  165. if(!in_array($oProfile->getModule(), $CNF[$sCnfKey]))
  166. return false;
  167. return true;
  168. }
  169. /**
  170. * check if provided profile is member of the group
  171. */
  172. public function serviceIsFan ($iGroupProfileId, $iProfileId = false)
  173. {
  174. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  175. return $oGroupProfile !== false && $this->isFan($oGroupProfile->getContentId(), $iProfileId);
  176. }
  177. /**
  178. * check if provided profile is admin of the group
  179. */
  180. public function serviceIsAdmin ($iGroupProfileId, $iProfileId = false)
  181. {
  182. if(!$iProfileId)
  183. $iProfileId = bx_get_logged_profile_id();
  184. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  185. if(!$oGroupProfile)
  186. return false;
  187. $iGroupContentId = $oGroupProfile->getContentId();
  188. if(!$this->isFan($iGroupContentId, $iProfileId))
  189. return false;
  190. $aGroupContentInfo = $this->_oDb->getContentInfoById($iGroupContentId);
  191. return $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aGroupContentInfo);
  192. }
  193. public function serviceGetAdminRole($iGroupProfileId, $iProfileId = false)
  194. {
  195. if(!$iProfileId)
  196. $iProfileId = bx_get_logged_profile_id();
  197. if(!$this->serviceIsAdmin($iGroupProfileId, $iProfileId))
  198. return 0;
  199. return $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  200. }
  201. /*
  202. * Get context (group) advanced members who can perform the specified action.
  203. * If 'Roles' are disabled for the context then all context admins are returned.
  204. */
  205. public function serviceGetAdminsByAction($iGroupProfileId, $mixedAction)
  206. {
  207. if(!$this->_oConfig->isAdmins())
  208. return [];
  209. $aGroupContentInfo = $this->_oDb->getContentInfoByProfileId($iGroupProfileId);
  210. if(empty($aGroupContentInfo) || !is_array($aGroupContentInfo))
  211. return [];
  212. $aAdmins = $this->_oDb->getAdmins($iGroupProfileId);
  213. if(!$this->_oConfig->isRoles())
  214. return $aAdmins;
  215. if(!is_array($mixedAction))
  216. $mixedAction = [$mixedAction];
  217. $aResult = [];
  218. foreach($mixedAction as $sAction)
  219. foreach($aAdmins as $iAdminProfileId)
  220. if(!in_array($iAdminProfileId, $aResult) && $this->isAllowedActionByRole($sAction, $aGroupContentInfo, $iGroupProfileId, $iAdminProfileId))
  221. $aResult[] = $iAdminProfileId;
  222. return $aResult;
  223. }
  224. public function serviceGetAdminsToManageContent($iGroupProfileId)
  225. {
  226. return $this->serviceGetAdminsByAction($iGroupProfileId, [
  227. BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT,
  228. BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT
  229. ]);
  230. }
  231. /**
  232. * Delete profile from fans and admins tables
  233. * @param $iProfileId profile id
  234. */
  235. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  236. {
  237. $CNF = &$this->_oConfig->CNF;
  238. $this->_oDb->deleteAdminsByProfileId($iProfileId);
  239. if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  240. $oConnection->onDeleteInitiatorAndContent($iProfileId);
  241. }
  242. /**
  243. * Reset group's author for particular group
  244. * @param $iContentId group id
  245. * @parem $iAuthorId new author profile ID
  246. * @return false of error, or number of updated records on success
  247. */
  248. public function serviceReassignEntityAuthor ($iContentId, $iAuthorId = 0)
  249. {
  250. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  251. if (!$aContentInfo)
  252. return false;
  253. if (empty($iAuthorId)) {
  254. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  255. if (!$oGroupProfile)
  256. return false;
  257. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  258. if($aAdmins)
  259. $iAuthorId = array_pop($aAdmins);
  260. }
  261. return $this->_oDb->updateAuthorById($iContentId, $iAuthorId);
  262. }
  263. /**
  264. * Entry actions and social sharing block
  265. */
  266. public function serviceEntityAllActions ($mixedContent = false, $aParams = array())
  267. {
  268. $CNF = &$this->_oConfig->CNF;
  269. if(!empty($mixedContent)) {
  270. if(!is_array($mixedContent))
  271. $mixedContent = array((int)$mixedContent, (method_exists($this->_oDb, 'getContentInfoById')) ? $this->_oDb->getContentInfoById((int)$mixedContent) : array());
  272. }
  273. else {
  274. $mixedContent = $this->_getContent();
  275. if($mixedContent === false)
  276. return false;
  277. }
  278. list($iContentId, $aContentInfo) = $mixedContent;
  279. if(!empty($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  280. $aParams = array_merge(array(
  281. 'entry_thumb' => (int)$aContentInfo[$CNF['FIELD_PICTURE']]
  282. ), $aParams);
  283. return parent::serviceEntityAllActions ($mixedContent, $aParams);
  284. }
  285. /**
  286. * Reset group's author when author profile is deleted
  287. * @param $iProfileId author profile id
  288. * @param $iAuthorId new author profile id
  289. * @return number of changed items
  290. */
  291. public function serviceReassignEntitiesByAuthor ($iProfileId, $iAuthorId = 0)
  292. {
  293. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  294. if (!$a)
  295. return 0;
  296. $iCount = 0;
  297. foreach ($a as $aContentInfo)
  298. $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']], $iAuthorId) ? 1 : 0);
  299. return $iCount;
  300. }
  301. public function servicePrepareFields ($aFieldsProfile)
  302. {
  303. $CNF = &$this->_oConfig->CNF;
  304. $aFieldsProfile[$CNF['FIELD_NAME']] = $aFieldsProfile['name'];
  305. $aFieldsProfile[$CNF['FIELD_TEXT']] = isset($aFieldsProfile['description']) ? $aFieldsProfile['description'] : '';
  306. unset($aFieldsProfile['name']);
  307. unset($aFieldsProfile['description']);
  308. return $aFieldsProfile;
  309. }
  310. public function serviceOnRemoveConnection ($iGroupProfileId, $iInitiatorId)
  311. {
  312. $CNF = &$this->_oConfig->CNF;
  313. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  314. if (!$oGroupProfile)
  315. return false;
  316. $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
  317. if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
  318. return $oConn->removeConnection($iProfileId, $iGroupProfileId);
  319. return false;
  320. }
  321. public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $bSendInviteOnly = false)
  322. {
  323. $CNF = &$this->_oConfig->CNF;
  324. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  325. if (!$oGroupProfile)
  326. return false;
  327. if (!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  328. return false;
  329. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  330. return false;
  331. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  332. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]));
  333. // send invitation to the group
  334. $sModule = $this->getName();
  335. if ($bSendInviteOnly && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  336. bx_alert($sModule, 'join_invitation', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  337. 'content' => $aContentInfo,
  338. 'entry_title' => $sEntryTitle,
  339. 'entry_url' => $sEntryUrl,
  340. 'group_profile' => $iGroupProfileId,
  341. 'profile' => $iProfileId,
  342. 'notification_subobject_id' => $iProfileId,
  343. 'object_author_id' => $iGroupProfileId
  344. ));
  345. /**
  346. * 'Invitation Received' alert for Notifications module.
  347. * Note. It's essential to use Recipient ($iInitiatorId) in 'object_author_id' parameter.
  348. * In this case notification will be received by Recipient profile.
  349. */
  350. bx_alert($sModule, 'join_invitation_notif', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  351. 'object_author_id' => $iInitiatorId,
  352. 'privacy_view' => isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 3,
  353. ));
  354. }
  355. // send notification to group's admins that new connection is pending confirmation
  356. elseif (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  357. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  358. 'object_author_id' => $iGroupProfileId,
  359. 'performer_id' => $iProfileId,
  360. 'content' => $aContentInfo,
  361. 'entry_title' => $sEntryTitle,
  362. 'entry_url' => $sEntryUrl,
  363. 'group_profile' => $iGroupProfileId,
  364. 'profile' => $iProfileId
  365. ));
  366. }
  367. // send notification that join request was accepted
  368. else if (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  369. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  370. 'object_author_id' => $iGroupProfileId,
  371. 'performer_id' => $iProfileId,
  372. 'content' => $aContentInfo,
  373. 'entry_title' => $sEntryTitle,
  374. 'entry_url' => $sEntryUrl,
  375. 'group_profile' => $iGroupProfileId,
  376. 'profile' => $iProfileId
  377. ));
  378. }
  379. // new fan was added
  380. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  381. // follow group on join
  382. if (BxDolService::call($oGroupProfile->getModule(), 'act_as_profile')){
  383. $this->addFollower($oGroupProfile->id(), (int)$iInitiatorId);
  384. }
  385. else{
  386. $this->addFollower((int)$iInitiatorId, $oGroupProfile->id());
  387. }
  388. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  389. 'object_author_id' => $iGroupProfileId,
  390. 'performer_id' => $iProfileId,
  391. 'content' => $aContentInfo,
  392. 'entry_title' => $sEntryTitle,
  393. 'entry_url' => $sEntryUrl,
  394. 'group_profile' => $iGroupProfileId,
  395. 'profile' => $iProfileId,
  396. ));
  397. $this->doAudit($iGroupProfileId, $iInitiatorId, '_sys_audit_action_group_join_request_accepted');
  398. return false;
  399. }
  400. // don't automatically add connection (mutual) if group requires manual join confirmation
  401. if ($bSendInviteOnly || $aContentInfo['join_confirmation'])
  402. return false;
  403. // check if connection already exists
  404. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  405. return false;
  406. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  407. return false;
  408. return true;
  409. }
  410. public function serviceFansTable ()
  411. {
  412. $CNF = &$this->_oConfig->CNF;
  413. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_CONNECTIONS']);
  414. if(!$oGrid)
  415. return false;
  416. return $oGrid->getCode();
  417. }
  418. public function serviceInvitesTable ()
  419. {
  420. $CNF = &$this->_oConfig->CNF;
  421. if(!isset($CNF['OBJECT_GRID_INVITES']))
  422. return false;
  423. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_INVITES']);
  424. if(!$oGrid)
  425. return false;
  426. return $oGrid->getCode();
  427. }
  428. public function serviceFans ($iContentId = 0, $bAsArray = false)
  429. {
  430. if (!$iContentId)
  431. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  432. if (!$iContentId)
  433. return false;
  434. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  435. if (!$aContentInfo)
  436. return false;
  437. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  438. return false;
  439. if(!$bAsArray) {
  440. bx_import('BxDolConnection');
  441. $mixedResult = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $this->_oConfig->CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  442. if (!$mixedResult)
  443. return MsgBox(_t('_sys_txt_empty'));
  444. }
  445. else
  446. $mixedResult = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  447. return $mixedResult;
  448. }
  449. public function serviceFansWithoutAdmins ($iContentId = 0, $bAsArray = false)
  450. {
  451. if (!$iContentId)
  452. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  453. if (!$iContentId)
  454. return false;
  455. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  456. if (!$aContentInfo)
  457. return false;
  458. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  459. return false;
  460. $CNF = &$this->_oConfig->CNF;
  461. $aFans = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  462. if(empty($aFans) || !is_array($aFans))
  463. return false;
  464. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  465. if(!empty($aAdmins) && is_array($aAdmins))
  466. $aFans = array_diff($aFans, $aAdmins);
  467. $iStart = (int)bx_get('start');
  468. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  469. if(!$iLimit)
  470. $iLimit = 4;
  471. return $this->_serviceBrowseQuick($aFans, $iStart, $iLimit);
  472. }
  473. public function serviceAdmins ($iContentId = 0)
  474. {
  475. $CNF = &$this->_oConfig->CNF;
  476. if(!$iContentId)
  477. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  478. if(!$iContentId)
  479. return false;
  480. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  481. if(!$oGroupProfile)
  482. return false;
  483. $iStart = (int)bx_get('start');
  484. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  485. if(!$iLimit)
  486. $iLimit = 4;
  487. $aProfiles = $this->_oDb->getAdmins($oGroupProfile->id(), $iStart, $iLimit+1);
  488. if(empty($aProfiles) || !is_array($aProfiles))
  489. return false;
  490. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  491. }
  492. public function serviceMembersByRole ($iContentId = 0, $iRole = BX_BASE_MOD_GROUPS_ROLE_COMMON)
  493. {
  494. $CNF = &$this->_oConfig->CNF;
  495. if(!$iContentId)
  496. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  497. if(!$iContentId)
  498. return false;
  499. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  500. if(!$oGroupProfile)
  501. return false;
  502. $iStart = (int)bx_get('start');
  503. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  504. if(!$iLimit)
  505. $iLimit = 4;
  506. $aProfiles = $this->_oDb->getRoles([
  507. 'type' => 'fan_pids_by_group_pid',
  508. 'group_profile_id' => $oGroupProfile->id(),
  509. 'role' => $iRole,
  510. 'start' => $iStart,
  511. 'limit' => $iLimit + 1
  512. ]);
  513. if(empty($aProfiles) || !is_array($aProfiles))
  514. return false;
  515. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  516. }
  517. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  518. {
  519. if (!$iProfileId)
  520. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  521. if (!$iProfileId)
  522. return '';
  523. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  524. }
  525. public function serviceBrowseCreatedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  526. {
  527. if (!$iProfileId)
  528. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  529. if (!$iProfileId)
  530. return '';
  531. return $this->_serviceBrowse ('created_entries', array('author' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  532. }
  533. public function serviceEntityPricing($iProfileId = 0)
  534. {
  535. $CNF = &$this->_oConfig->CNF;
  536. if(!$iProfileId)
  537. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  538. if(!$iProfileId)
  539. return '';
  540. if(!$this->_oConfig->isPaidJoin())
  541. return '';
  542. $oPayments = BxDolPayments::getInstance();
  543. if(!$oPayments->isActive())
  544. return MsgBox(_t('_sys_payments_err_no_payments'));
  545. if($this->checkAllowedUsePaidJoin() !== CHECK_ACTION_RESULT_ALLOWED)
  546. return MsgBox(_t('_Access denied'));
  547. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  548. return MsgBox(_t('_Access denied'));
  549. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_MANAGE']);
  550. if(!$oGrid)
  551. return '';
  552. $sNote = '';
  553. if(!$oPayments->isAcceptingPayments($this->_iProfileId))
  554. $sNote = MsgBox(_t('_sys_payments_err_not_accept_payments', $oPayments->getDetailsUrl()));
  555. return $sNote . $oGrid->getCode();
  556. }
  557. public function serviceEntityJoin($iProfileId = 0)
  558. {
  559. $CNF = &$this->_oConfig->CNF;
  560. if(!$iProfileId)
  561. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  562. if(!$iProfileId)
  563. return '';
  564. if(!$this->_oConfig->isPaidJoin())
  565. return '';
  566. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_VIEW']);
  567. if(!$oGrid)
  568. return '';
  569. return $oGrid->getCode();
  570. }
  571. public function serviceEntityInvite ($iContentId = 0, $bErrorMsg = true)
  572. {
  573. $CNF = &$this->_oConfig->CNF;
  574. if(!isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE']))
  575. return false;
  576. return $this->_serviceEntityForm ('inviteForm', $iContentId, false, false, $bErrorMsg);
  577. }
  578. /**
  579. * Entry social sharing block
  580. */
  581. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  582. {
  583. if(!empty($mixedContent)) {
  584. if(!is_array($mixedContent))
  585. $mixedContent = array((int)$mixedContent, array());
  586. }
  587. else {
  588. $mixedContent = $this->_getContent();
  589. if($mixedContent === false)
  590. return false;
  591. }
  592. list($iContentId, $aContentInfo) = $mixedContent;
  593. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  594. if(!$oGroupProfile)
  595. return false;
  596. return parent::serviceEntitySocialSharing(array($iContentId, $aContentInfo), array(
  597. 'title' => $oGroupProfile->getDisplayName()
  598. ));
  599. }
  600. public function serviceIsPricingAvaliable($iProfileId)
  601. {
  602. if(!$this->_oConfig->isPaidJoin())
  603. return false;
  604. if($this->checkAllowedUsePaidJoin() !== CHECK_ACTION_RESULT_ALLOWED)
  605. return false;
  606. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  607. return false;
  608. return true;
  609. }
  610. public function serviceIsPaidJoinAvaliable($iGroupProfileId, $iProfileId = 0)
  611. {
  612. return $this->isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId);
  613. }
  614. public function serviceIsPaidJoinAvaliableByContent($iGroupContentId, $iProfileId = 0)
  615. {
  616. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName());
  617. if(!$oGroupProfile)
  618. return false;
  619. return $this->isPaidJoinByProfileForProfile($oGroupProfile->id(), $iProfileId);
  620. }
  621. public function serviceIsFreeJoinAvaliable($iGroupProfileId, $iProfileId = 0)
  622. {
  623. return !$this->isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId);
  624. }
  625. public function serviceIsFreeJoinAvaliableByContent($iGroupContentId, $iProfileId = 0)
  626. {
  627. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName());
  628. if(!$oGroupProfile)
  629. return false;
  630. return !$this->isPaidJoinByProfileForProfile($oGroupProfile->id(), $iProfileId);
  631. }
  632. /**
  633. * Is Paid Join enabled in the group and whether a profile can use it.
  634. *
  635. * @param type $iGroupProfileId - Group profile ID.
  636. * @param type $iProfileId - Profile ID of the user who wants to join.
  637. * @return boolean
  638. */
  639. public function isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId = 0)
  640. {
  641. $CNF = &$this->_oConfig->CNF;
  642. if(empty($iProfileId))
  643. $iProfileId = $this->_iProfileId;
  644. if(BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->isConnected($iProfileId, $iGroupProfileId))
  645. return false;
  646. return $this->isPaidJoinByProfile($iGroupProfileId);
  647. }
  648. /**
  649. * Is Paid Join enabled as is and whether a group has pricing plans added.
  650. *
  651. * @param type $iGroupProfileId - Group profile ID.
  652. * @return boolean
  653. */
  654. public function isPaidJoinByProfile($iGroupProfileId)
  655. {
  656. if(!$this->_oConfig->isPaidJoin())
  657. return false;
  658. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $iGroupProfileId));
  659. if(empty($aPrices) || !is_array($aPrices))
  660. return false;
  661. return true;
  662. }
  663. /**
  664. * Integration with Payments.
  665. */
  666. public function serviceGetPaymentData()
  667. {
  668. return $this->_aModule;
  669. }
  670. public function serviceGetCartItem($mixedItemId)
  671. {
  672. $CNF = &$this->_oConfig->CNF;
  673. if(!$mixedItemId)
  674. return array();
  675. if(is_numeric($mixedItemId))
  676. $aItem = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => (int)$mixedItemId));
  677. else
  678. $aItem = $this->_oDb->getPrices(array('type' => 'by_name', 'value' => $mixedItemId));
  679. if(empty($aItem) || !is_array($aItem))
  680. return array();
  681. if(!$this->isPaidJoinByProfile($aItem['profile_id']))
  682. return array();
  683. $oGroupProfile = BxDolProfile::getInstance($aItem['profile_id']);
  684. if(!$oGroupProfile)
  685. return array();
  686. $aGroupProfile = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  687. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  688. $sTitle = '';
  689. if(!empty($aItem['period']) && !empty($aItem['period_unit']))
  690. $sTitle = _t($CNF['T']['txt_cart_item_title'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']], $aItem['period'], $aItem['period_unit']);
  691. else
  692. $sTitle = _t($CNF['T']['txt_cart_item_title_lifetime'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']]);
  693. return array (
  694. 'id' => $aItem['id'],
  695. 'author_id' => $aGroupProfile[$CNF['FIELD_AUTHOR']],
  696. 'name' => $aItem['name'],
  697. 'title' => $sTitle,
  698. 'description' => '',
  699. 'url' => $oGroupProfile->getUrl(),
  700. 'price_single' => $aItem['price'],
  701. 'price_recurring' => $aItem['price'],
  702. 'period_recurring' => $aItem['period'],
  703. 'period_unit_recurring' => $aItem['period_unit'],
  704. 'trial_recurring' => 0
  705. );
  706. }
  707. public function serviceGetCartItems($iSellerId)
  708. {
  709. $CNF = &$this->_oConfig->CNF;
  710. if(empty($iSellerId))
  711. return array();
  712. $sModule = $this->getName();
  713. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  714. $aGroups = $this->_oDb->getEntriesBy(array('type' => 'author', 'author' => $iSellerId));
  715. $aResult = array();
  716. foreach($aGroups as $aGroup) {
  717. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aGroup[$CNF['FIELD_ID']], $sModule);
  718. if(!$oGroupProfile)
  719. continue;
  720. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $oGroupProfile->id()));
  721. if(empty($aPrices) || !is_array($aPrices))
  722. continue;
  723. $sTitle = $oGroupProfile->getDisplayName();
  724. $sUrl = $oGroupProfile->getUrl();
  725. foreach($aPrices as $aPrice)
  726. $aResult[] = array(
  727. 'id' => $aPrice['id'],
  728. 'author_id' => $iSellerId,
  729. 'name' => $aPrice['name'],
  730. 'title' => _t($CNF['T']['txt_cart_item_title'], $sTitle, $aRoles[$aPrice['role_id']], $aPrice['period'], $aPrice['period_unit']),
  731. 'description' => '',
  732. 'url' => $sUrl,
  733. 'price_single' => $aPrice['price'],
  734. 'price_recurring' => $aPrice['price'],
  735. 'period_recurring' => $aPrice['period'],
  736. 'period_unit_recurring' => $aPrice['period_unit']
  737. );
  738. }
  739. return $aResult;
  740. }
  741. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  742. {
  743. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  744. }
  745. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  746. {
  747. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  748. }
  749. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  750. {
  751. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  752. }
  753. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  754. {
  755. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  756. }
  757. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  758. {
  759. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  760. }
  761. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  762. {
  763. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  764. }
  765. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  766. {
  767. return true;
  768. }
  769. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  770. {
  771. $CNF = &$this->_oConfig->CNF;
  772. $aItem = $this->serviceGetCartItem($iItemId);
  773. if(empty($aItem) || !is_array($aItem))
  774. return array();
  775. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  776. $mixedPeriod = false;
  777. if((int)$aItemInfo['period'] != 0)
  778. $mixedPeriod = array(
  779. 'period' => (int)$aItemInfo['period'],
  780. 'period_unit' => $aItemInfo['period_unit'],
  781. 'period_reserve' => $CNF['PARAM_RECURRING_RESERVE']
  782. );
  783. if(!$this->setRole($aItemInfo['profile_id'], $iClientId, $aItemInfo['role_id'], $mixedPeriod, $sOrder))
  784. return array();
  785. return $aItem;
  786. }
  787. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  788. {
  789. $aItem = $this->serviceGetCartItem($iItemIdNew);
  790. if(empty($aItem) || !is_array($aItem))
  791. return array();
  792. $aItemInfoOld = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdOld));
  793. if(empty($aItemInfoOld) || !is_array($aItemInfoOld))
  794. return array();
  795. $aItemInfoNew = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdNew));
  796. if(empty($aItemInfoNew) || !is_array($aItemInfoNew))
  797. return array();
  798. if(!$this->unsetRole($aItemInfoOld['profile_id'], $iClientId))
  799. return array();
  800. $aResult = $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemIdNew, 1, $sOrder);
  801. if(empty($aResult) || !is_array($aResult))
  802. return array();
  803. return $aItem;
  804. }
  805. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  806. {
  807. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  808. if(empty($aItemInfo) || !is_array($aItemInfo))
  809. return false;
  810. return $this->unsetRole($aItemInfo['profile_id'], $iClientId);
  811. }
  812. /**
  813. * Data for Notifications module
  814. */
  815. public function serviceGetNotificationsData()
  816. {
  817. $sModule = $this->_aModule['name'];
  818. $aSettingsTypes = ['follow_member', 'follow_context'];
  819. if($this->serviceActAsProfile())
  820. $aSettingsTypes = ['personal', 'follow_member'];
  821. return [
  822. 'handlers' => [
  823. ['group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'],
  824. ['group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'],
  825. ['group' => $sModule . '_score_up', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteUp', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_up', 'module_class' => 'Module'],
  826. ['group' => $sModule . '_score_down', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteDown', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_down', 'module_class' => 'Module'],
  827. ['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'],
  828. ['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'],
  829. ['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']],
  830. ['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'],
  831. //--- Moderation related: For 'admins'.
  832. ['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'],
  833. ],
  834. 'settings' => [
  835. ['group' => 'vote', 'unit' => $sModule, 'action' => 'doVote', 'types' => $aSettingsTypes],
  836. ['group' => 'score_up', 'unit' => $sModule, 'action' => 'doVoteUp', 'types' => $aSettingsTypes],
  837. ['group' => 'score_down', 'unit' => $sModule, 'action' => 'doVoteDown', 'types' => $aSettingsTypes],
  838. ['group' => 'fan', 'unit' => $sModule, 'action' => 'fan_added', 'types' => $aSettingsTypes],
  839. ['group' => 'invite', 'unit' => $sModule, 'action' => 'join_invitation_notif', 'types' => ['personal']],
  840. ['group' => 'join', 'unit' => $sModule, 'action' => 'join_request', 'types' => $aSettingsTypes],
  841. ['group' => 'timeline_post', 'unit' => $sModule, 'action' => 'timeline_post_common', 'types' => $aSettingsTypes],
  842. //--- Moderation related: For 'admins'.
  843. ['group' => 'action_required', 'unit' => $sModule, 'action' => 'pending_approval', 'types' => ['personal']],
  844. ],
  845. 'alerts' => [
  846. ['unit' => $sModule, 'action' => 'doVote'],
  847. ['unit' => $sModule, 'action' => 'undoVote'],
  848. ['unit' => $sModule, 'action' => 'doVoteUp'],
  849. ['unit' => $sModule, 'action' => 'doVoteDown'],
  850. ['unit' => $sModule, 'action' => 'fan_added'],
  851. ['unit' => $sModule, 'action' => 'join_invitation_notif'],
  852. ['unit' => $sModule, 'action' => 'join_request'],
  853. ['unit' => $sModule, 'action' => 'timeline_post_common'],
  854. //--- Moderation related: For 'admins'.
  855. ['unit' => $sModule, 'action' => 'pending_approval'],
  856. ]
  857. ];
  858. }
  859. public function serviceGetNotificationsInsertData($oAlert, $aHandler, $aDataItems)
  860. {
  861. if($oAlert->sAction != 'join_invitation_notif' || empty($aDataItems) || !is_array($aDataItems))
  862. return $aDataItems;
  863. foreach($aDataItems as $iIndex => $aDataItem)
  864. $aDataItems[$iIndex]['object_privacy_view'] = BX_DOL_PG_ALL;
  865. return $aDataItems;
  866. }
  867. /**
  868. * Notification about new invitation to join the group
  869. */
  870. public function serviceGetNotificationsJoinInvitation($aEvent)
  871. {
  872. $CNF = &$this->_oConfig->CNF;
  873. $iContentId = (int)$aEvent['object_id'];
  874. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  875. if(!$oGroupProfile)
  876. return array();
  877. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  878. if(empty($aContentInfo) || !is_array($aContentInfo))
  879. return array();
  880. /*
  881. * It's essential that 'object_owner_id' contains invited member profile id.
  882. */
  883. $oProfile = BxDolProfile::getInstance((int)$aEvent['object_owner_id']);
  884. if(!$oProfile)
  885. return array();
  886. /*
  887. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  888. * because Subentry URL has higher display priority and notification
  889. * should be linked to Group Profile (Group Profile -> Members tab)
  890. * instead of Personal Profile of invited member.
  891. */
  892. $sEntryUrl = bx_absolute_url(str_replace(BX_DOL_URL_ROOT, '', $oGroupProfile->getUrl()), '{bx_url_root}');
  893. return array(
  894. 'entry_sample' => $CNF['T']['txt_sample_single'],
  895. 'entry_url' => $sEntryUrl,
  896. 'entry_caption' => $oGroupProfile->getDisplayName(),
  897. 'subentry_sample' => $oProfile->getDisplayName(),
  898. 'subentry_url' => $sEntryUrl,
  899. 'lang_key' => $this->_oConfig->CNF['T']['txt_ntfs_join_invitation']
  900. );
  901. }
  902. /**
  903. * Notification about new member requst in the group
  904. */
  905. public function serviceGetNotificationsJoinRequest($aEvent)
  906. {
  907. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_join_request']);
  908. }
  909. /**
  910. * Notification about new member in the group
  911. */
  912. public function serviceGetNotificationsFanAdded($aEvent)
  913. {
  914. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_fan_added']);
  915. }
  916. protected function _serviceGetNotification($aEvent, $sLangKey)
  917. {
  918. $CNF = &$this->_oConfig->CNF;
  919. $iContentId = (int)$aEvent['object_id'];
  920. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  921. if(!$oGroupProfile)
  922. return array();
  923. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  924. if(empty($aContentInfo) || !is_array($aContentInfo))
  925. return array();
  926. $oProfile = BxDolProfile::getInstance((int)$aEvent['subobject_id']);
  927. if(!$oProfile)
  928. return array();
  929. /*
  930. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  931. * because Subentry URL has higher display priority and notification
  932. * should be linked to Group Profile (Group Profile -> Members tab)
  933. * instead of Personal Profile of a member, who performed an action.
  934. */
  935. if(empty($CNF['URL_ENTRY_FANS']))
  936. $sEntryUrl = bx_absolute_url(str_replace(BX_DOL_URL_ROOT, '', $oGroupProfile->getUrl()), '{bx_url_root}');
  937. else
  938. $sEntryUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_FANS'], [
  939. 'profile_id' => $oGroupProfile->id()
  940. ]);
  941. return [
  942. 'entry_sample' => $CNF['T']['txt_sample_single'],
  943. 'entry_url' => $sEntryUrl,
  944. 'entry_caption' => $oGroupProfile->getDisplayName(),
  945. 'entry_author' => $oGroupProfile->id(),
  946. 'subentry_sample' => $oProfile->getDisplayName(),
  947. 'subentry_url' => $sEntryUrl,
  948. 'lang_key' => $sLangKey
  949. ];
  950. }
  951. /**
  952. * Data for Timeline module
  953. */
  954. public function serviceGetTimelineData()
  955. {
  956. return BxBaseModGeneralModule::serviceGetTimelineData();
  957. }
  958. /**
  959. * Entry post for Timeline module
  960. */
  961. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  962. {
  963. $CNF = &$this->_oConfig->CNF;
  964. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  965. if($a === false)
  966. return false;
  967. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  968. $a['content']['url'] = $oGroupProfile->getUrl();
  969. $a['content']['title'] = $oGroupProfile->getDisplayName();
  970. if(isset($CNF['FIELD_PUBLISHED'])) {
  971. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  972. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $a['date'])
  973. $a['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  974. }
  975. return $a;
  976. }
  977. // ====== PERMISSION METHODS
  978. /**
  979. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  980. */
  981. public function checkAllowedUsePaidJoin($isPerformAction = false)
  982. {
  983. // check ACL
  984. $aCheck = checkActionModule($this->_iProfileId, 'use paid join', $this->getName(), $isPerformAction);
  985. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  986. return $aCheck[CHECK_ACTION_MESSAGE];
  987. return CHECK_ACTION_RESULT_ALLOWED;
  988. }
  989. /**
  990. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  991. */
  992. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  993. {
  994. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  995. }
  996. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  997. {
  998. $CNF = &$this->_oConfig->CNF;
  999. $iGroupContentId = (int)$aDataEntry[$CNF['FIELD_ID']];
  1000. $bInvited = false;
  1001. if(!empty($CNF['TABLE_INVITES'])) {
  1002. $iGroupProfileId = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName())->id();
  1003. if(($sKey = bx_get('key')) !== false) {
  1004. $mixedInvited = $this->isInvited($sKey, $iGroupProfileId);
  1005. if($mixedInvited === true)
  1006. $bInvited = true;
  1007. }
  1008. else {
  1009. $mixedInvited = $this->isInvitedByProfileId($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $iGroupProfileId);
  1010. if($mixedInvited === true)
  1011. $bInvited = true;
  1012. }
  1013. }
  1014. if ($this->isFan($iGroupContentId, $iProfileId) || $bInvited)
  1015. return CHECK_ACTION_RESULT_ALLOWED;
  1016. return parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId);
  1017. }
  1018. /**
  1019. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1020. */
  1021. public function checkAllowedCompose(&$aDataEntry, $isPerformAction = false)
  1022. {
  1023. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  1024. return _t('_sys_txt_access_denied');
  1025. return parent::checkAllowedCompose ($aDataEntry, $isPerformAction);
  1026. }
  1027. /**
  1028. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1029. */
  1030. public function checkAllowedFanAdd(&$aDataEntry, $isPerformAction = false)
  1031. {
  1032. $mixedResult = $this->_modGroupsCheckAllowedFanAdd($aDataEntry, $isPerformAction);
  1033. // call alert to allow custom checks
  1034. bx_alert('system', 'check_allowed_fan_add', 0, 0, array(
  1035. 'module' => $this->getName(),
  1036. 'content_info' => $aDataEntry,
  1037. 'profile_id' => bx_get_logged_profile_id(),
  1038. 'override_result' => &$mixedResult
  1039. ));
  1040. return $mixedResult;
  1041. }
  1042. public function _modGroupsCheckAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  1043. {
  1044. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) || !isLogged())
  1045. return _t('_sys_txt_access_denied');
  1046. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], true, false);
  1047. }
  1048. /**
  1049. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1050. */
  1051. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  1052. {
  1053. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  1054. return CHECK_ACTION_RESULT_ALLOWED;
  1055. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  1056. }
  1057. protected function _checkAllowedActionByFan($sAction, $aDataEntry, $iProfileId = 0)
  1058. {
  1059. $CNF = &$this->_oConfig->CNF;
  1060. $bRoles = $this->_oConfig->isRoles();
  1061. if(empty($iProfileId))
  1062. $iProfileId = bx_get_logged_profile_id();
  1063. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  1064. if(!$oGroupProfile)
  1065. return $sAction == BX_BASE_MOD_GROUPS_ACTION_DELETE ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_not_found');
  1066. $iGroupProfileId = $oGroupProfile->id();
  1067. if(!$bRoles && $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aDataEntry))
  1068. return CHECK_ACTION_RESULT_ALLOWED;
  1069. if($bRoles && $this->isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId))
  1070. return CHECK_ACTION_RESULT_ALLOWED;
  1071. return _t('_sys_txt_access_denied');
  1072. }
  1073. public function isAllowedActionByRole($mAction, $aDataEntry, $iGroupProfileId, $iProfileId)
  1074. {
  1075. $iProfileRole = $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  1076. if (is_array($mAction)) {
  1077. $sAction = $mAction['action'];
  1078. $sActionModule = $mAction['module'];
  1079. } else {
  1080. $sAction = $mAction;
  1081. $sActionModule = $this->getName();
  1082. }
  1083. $bResult = false;
  1084. if($iProfileId)
  1085. $bResult = $this->isAllowedModuleActionByRole($sActionModule, $sAction, $iProfileRole);
  1086. // in case neither of the profile's roles are having permissions set explicitly then fallback to an old way
  1087. if ($bResult === NULL) {
  1088. $bResult = false;
  1089. if($this->isFanByGroupProfileId($iGroupProfileId)) {
  1090. switch ($sAction) {
  1091. case BX_BASE_MOD_GROUPS_ACTION_DELETE:
  1092. case BX_BASE_MOD_GROUPS_ACTION_EDIT:
  1093. case BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER:
  1094. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  1095. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR))
  1096. $bResult = true;
  1097. break;
  1098. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS:
  1099. case BX_BASE_MOD_GROUPS_ACTION_INVITE:
  1100. case BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT:
  1101. case BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT:
  1102. case BX_BASE_MOD_GROUPS_ACTION_TIMELINE_POST_PIN:
  1103. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR) || $this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_MODERATOR))
  1104. $bResult = true;
  1105. break;
  1106. default:
  1107. $bResult = true;
  1108. }
  1109. }
  1110. }
  1111. // call alert to allow custom checks
  1112. bx_alert('system', 'check_allowed_action_by_role', 0, 0, [
  1113. 'module' => $this->getName(),
  1114. 'multi_roles' => $this->_oConfig->isMultiRoles(),
  1115. 'action' => $sAction,
  1116. 'action_module' => $sActionModule,
  1117. 'content_profile_id' => $iGroupProfileId,
  1118. 'content_info' => $aDataEntry,
  1119. 'profile_id' => $iProfileId,
  1120. 'profile_role' => $iProfileRole,
  1121. 'override_result' => &$bResult
  1122. ]);
  1123. return $bResult;
  1124. }
  1125. public function isAllowedModuleActionByRole($sModule, $sAction, $iProfileRole)
  1126. {
  1127. static $aRoles;
  1128. if (!$aRoles && isset($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES']) && !empty($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES']))
  1129. $aRoles = BxBaseFormView::getDataItems($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES'], true, BX_DATA_VALUES_ALL);
  1130. if ($aRoles) {
  1131. foreach ($aRoles as $iRole => $aRoleData) {
  1132. if ($iRole == 0 && $iProfileRole == 0 || $iRole > 0 && $this->isRole($iProfileRole, $iRole)) {
  1133. $mPermissions = isset($aRoles[$iRole]) && isset($aRoles[$iRole]['Data']) && !empty($aRoles[$iRole]['Data']) ? unserialize($aRoles[$iRole]['Data']) : false;
  1134. if ($mPermissions && isset($mPermissions[$sModule])) {
  1135. return isset($mPermissions[$sModule][$sAction]) && $mPermissions[$sModule][$sAction];
  1136. }
  1137. }
  1138. }
  1139. }
  1140. return NULL;
  1141. }
  1142. public function isAllowedModuleActionByProfile($iContentId, $sPostModule, $sAction, $iProfileId = 0) {
  1143. if (!$iProfileId) $iProfileId = bx_get_logged_profile_id();
  1144. if ($iProfileId && $this->isFan($iContentId, $iProfileId)) {
  1145. $sModuleName = $this->getName();
  1146. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $sModuleName);
  1147. $aDataEntry = $this->_oDb->getContentInfoById($iContentId);
  1148. $bResult = $this->isAllowedActionByRole(['action' => $sAction, 'module' => $sPostModule], $aDataEntry, $oGroupProfile->id(), $iProfileId);
  1149. if ($bResult === true) return CHECK_ACTION_RESULT_ALLOWED;
  1150. if ($bResult === false) return _t('_sys_txt_access_denied');
  1151. }
  1152. 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.
  1153. }
  1154. public function isRole($iProfileRole, $iRole)
  1155. {
  1156. if(!$this->_oConfig->isMultiRoles())
  1157. return $iProfileRole == $iRole;
  1158. else
  1159. return $iProfileRole & (1 << ($iRole - 1));
  1160. }
  1161. public function serviceIsRole($iProfileRole, $iRole)
  1162. {
  1163. return $this->isRole($iProfileRole, $iRole);
  1164. }
  1165. public function checkAllowedManageFans($mixedDataEntry, $isPerformAction = false)
  1166. {
  1167. $aDataEntry = array();
  1168. if(!is_array($mixedDataEntry)) {
  1169. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  1170. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  1171. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1172. }
  1173. else
  1174. $aDataEntry = $mixedDataEntry;
  1175. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1176. return CHECK_ACTION_RESULT_ALLOWED;
  1177. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1178. }
  1179. public function checkAllowedManageAdmins($mixedDataEntry, $isPerformAction = false)
  1180. {
  1181. $aDataEntry = array();
  1182. if(!is_array($mixedDataEntry)) {
  1183. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  1184. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  1185. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1186. }
  1187. else
  1188. $aDataEntry = $mixedDataEntry;
  1189. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1190. return CHECK_ACTION_RESULT_ALLOWED;
  1191. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1192. }
  1193. public function checkAllowedEdit($aDataEntry, $isPerformAction = false)
  1194. {
  1195. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_EDIT, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1196. return CHECK_ACTION_RESULT_ALLOWED;
  1197. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1198. }
  1199. public function checkAllowedInvite($aDataEntry, $isPerformAction = false)
  1200. {
  1201. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_INVITE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1202. return CHECK_ACTION_RESULT_ALLOWED;
  1203. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1204. }
  1205. public function checkAllowedChangeCover($aDataEntry, $isPerformAction = false)
  1206. {
  1207. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1208. return CHECK_ACTION_RESULT_ALLOWED;
  1209. return parent::checkAllowedChangeCover($aDataEntry, $isPerformAction);
  1210. }
  1211. public function checkAllowedDelete(&$aDataEntry, $isPerformAction = false)
  1212. {
  1213. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_DELETE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1214. return CHECK_ACTION_RESULT_ALLOWED;
  1215. return parent::checkAllowedDelete($aDataEntry, $isPerformAction);
  1216. }
  1217. public function checkAllowedJoin(&$aDataEntry, $isPerformAction = false)
  1218. {
  1219. if (bx_get('key')){
  1220. $sKey = bx_get('key');
  1221. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  1222. $aData = $this->_oDb->getInviteByKey($sKey, $oGroupProfile->id());
  1223. if (isset($aData['invited_profile_id']) && $aData['invited_profile_id'] == bx_get_logged_profile_id()){
  1224. return CHECK_ACTION_RESULT_ALLOWED;
  1225. }
  1226. }
  1227. return _t('_sys_txt_access_denied');
  1228. }
  1229. public function checkAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1230. {
  1231. $mixedResult = $this->_modGroupsCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1232. // call alert to allow custom checks
  1233. bx_alert('system', 'check_allowed_subscribe_add', 0, 0, array(
  1234. 'module' => $this->getName(),
  1235. 'content_info' => $aDataEntry,
  1236. 'profile_id' => bx_get_logged_profile_id(),
  1237. 'override_result' => &$mixedResult
  1238. ));
  1239. return $mixedResult;
  1240. }
  1241. /**
  1242. * Note. Is mainly needed for internal usage. Access level is 'public' to allow outer calls from alerts.
  1243. */
  1244. public function _modGroupsCheckAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1245. {
  1246. $CNF = &$this->_oConfig->CNF;
  1247. if(!$this->isFan($aDataEntry[$CNF['FIELD_ID']]) && (!isset($CNF['PARAM_SBS_WO_JOIN']) || getParam($CNF['PARAM_SBS_WO_JOIN']) != 'on'))
  1248. return _t('_sys_txt_access_denied');
  1249. return parent::_modProfileCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1250. }
  1251. /**
  1252. * @deprecated since version 11.0.3 and can be removed in the next version.
  1253. */
  1254. public function _checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  1255. {
  1256. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  1257. }
  1258. public function doAudit($iGroupProfileId, $iFanId, $sAction)
  1259. {
  1260. $oProfile = BxDolProfile::getInstance($iFanId);
  1261. $iContentId = $oProfile->getContentId();
  1262. $sModule = $oProfile->getModule();
  1263. $oModule = BxDolModule::getInstance($sModule);
  1264. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile') && $oModule->_oConfig){
  1265. $CNF = $oModule->_oConfig->CNF;
  1266. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iContentId))) : array();
  1267. $AuditParams = array(
  1268. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  1269. 'context_profile_id' => $iGroupProfileId,
  1270. 'context_profile_title' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName()
  1271. );
  1272. bx_audit(
  1273. $iContentId,
  1274. $sModule,
  1275. $sAction,
  1276. $AuditParams
  1277. );
  1278. }
  1279. }
  1280. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  1281. {
  1282. $sResult = $this->checkAllowedView($aDataEntry);
  1283. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  1284. // if profile view isn't allowed but visibility is in partially visible groups
  1285. // then display buttons to connect (befriend, join) to profile,
  1286. // if other conditions (in parent::_checkAllowedConnect) are met as well
  1287. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && !in_array($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), array('s'))))
  1288. return $sResult;
  1289. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  1290. }
  1291. // ====== COMMON METHODS
  1292. protected function _alertParams($aContentInfo)
  1293. {
  1294. $aParams = parent::_alertParams($aContentInfo);
  1295. $CNF = &$this->_oConfig->CNF;
  1296. if(!empty($CNF['FIELD_CF']) && isset($aContentInfo[$CNF['FIELD_CF']]))
  1297. $aParams['cf'] = $aContentInfo[$CNF['FIELD_CF']];
  1298. return $aParams;
  1299. }
  1300. public function alertAfterAdd($aContentInfo)
  1301. {
  1302. $CNF = &$this->_oConfig->CNF;
  1303. $iId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1304. $iAuthorId = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  1305. $sAction = 'added';
  1306. if(isset($CNF['FIELD_STATUS_ADMIN']) && isset($aContentInfo[$CNF['FIELD_STATUS_ADMIN']]) && $aContentInfo[$CNF['FIELD_STATUS_ADMIN']] == BX_BASE_MOD_GENERAL_STATUS_PENDING)
  1307. $sAction = 'deferred';
  1308. $sModule = $this->getName();
  1309. $aParams = $this->_alertParams($aContentInfo);
  1310. bx_alert('system', 'prepare_alert_params', 0, 0, [
  1311. 'unit'=> $sModule,
  1312. 'action' => &$sAction,
  1313. 'object_id' => &$iId,
  1314. 'sender_id' => &$iAuthorId,
  1315. 'extras' => &$aParams
  1316. ]);
  1317. bx_alert($sModule, $sAction, $iId, false, $aParams);
  1318. $this->_processModerationNotifications($aContentInfo);
  1319. }
  1320. public function addFollower ($iProfileId1, $iProfileId2)
  1321. {
  1322. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  1323. if($oConnectionFollow && !$oConnectionFollow->isConnected($iProfileId1, $iProfileId2)){
  1324. $oConnectionFollow->addConnection($iProfileId1, $iProfileId2);
  1325. return true;
  1326. }
  1327. return false;
  1328. }
  1329. public function isFan ($iContentId, $iProfileId = false)
  1330. {
  1331. $CNF = &$this->_oConfig->CNF;
  1332. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  1333. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1334. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1335. return false;
  1336. }
  1337. public function isFanByGroupProfileId ($iGroupProfileId, $iProfileId = false)
  1338. {
  1339. $CNF = &$this->_oConfig->CNF;
  1340. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1341. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1342. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1343. return false;
  1344. }
  1345. public function isInvited ($sKey, $iGroupProfileId)
  1346. {
  1347. $CNF = &$this->_oConfig->CNF;
  1348. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  1349. if (!isset($aData['invited_profile_id']))
  1350. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1351. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1352. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1353. return true;
  1354. }
  1355. public function isInvitedByProfileId ($iProfileId, $iGroupProfileId)
  1356. {
  1357. $CNF = &$this->_oConfig->CNF;
  1358. $aData = $this->_oDb->getInviteByInvited($iProfileId, $iGroupProfileId);
  1359. if (!isset($aData['invited_profile_id']))
  1360. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1361. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1362. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1363. return true;
  1364. }
  1365. public function serviceIsInvited($iGroupProfileId, $iProfileId = false, $sKey = '')
  1366. {
  1367. if(!$iProfileId)
  1368. $iProfileId = bx_get_logged_profile_id();
  1369. if(empty($sKey) && ($sKey = bx_get('key')) !== false)
  1370. $sKey = bx_process_input($sKey);
  1371. $mixedInvited = false;
  1372. if(!empty($sKey))
  1373. $mixedInvited = $this->isInvited($sKey, $iGroupProfileId);
  1374. else if($iProfileId !== false)
  1375. $mixedInvited = $this->isInvitedByProfileId($iProfileId, $iGroupProfileId);
  1376. return $mixedInvited === true;
  1377. }
  1378. public function serviceIsNotInvited($iGroupProfileId, $iProfileId = false, $sKey = '')
  1379. {
  1380. return !$this->serviceIsInvited($iGroupProfileId, $iProfileId, $sKey);
  1381. }
  1382. public function serviceGetInvitedKey($iGroupProfileId, $iProfileId = false)
  1383. {
  1384. $sKey = '';
  1385. if(($sKey = bx_get('key')) !== false)
  1386. $sKey = bx_process_input($sKey);
  1387. if(!$sKey) {
  1388. if(!$iProfileId)
  1389. $iProfileId = bx_get_logged_profile_id();
  1390. if($iProfileId !== false) {
  1391. $aInvite = $this->_oDb->getInviteByInvited($iProfileId, $iGroupProfileId);
  1392. if(!empty($aInvite) && is_array($aInvite))
  1393. $sKey = $aInvite['key'];
  1394. }
  1395. }
  1396. return $sKey;
  1397. }
  1398. public function getRole($iGroupProfileId, $iFanProfileId)
  1399. {
  1400. if(!$this->isFanByGroupProfileId($iGroupProfileId, $iFanProfileId))
  1401. return false;
  1402. return $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1403. }
  1404. public function setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  1405. {
  1406. $CNF = &$this->_oConfig->CNF;
  1407. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1408. return false;
  1409. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1410. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1411. if(!$oConnection || !$oGroupProfile)
  1412. return false;
  1413. if(!$oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->addConnection($iFanProfileId, $iGroupProfileId))
  1414. return false;
  1415. if(!$this->_oDb->setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod, $sOrder))
  1416. return false;
  1417. $this->onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole);
  1418. return true;
  1419. }
  1420. public function onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole)
  1421. {
  1422. $CNF = &$this->_oConfig->CNF;
  1423. $iProfileId = bx_get_logged_profile_id();
  1424. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1425. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1426. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  1427. // notify about admin status
  1428. if(!empty($CNF['EMAIL_FAN_SET_ROLE']) && $iFanProfileId != $iProfileId) {
  1429. $aSetRoles = is_array($mixedRole) ? $mixedRole : [$mixedRole];
  1430. $aRolesNames = [];
  1431. foreach ($aSetRoles as $iRole)
  1432. $aRolesNames[] = $aRoles[(int)$iRole];
  1433. sendMailTemplate($CNF['EMAIL_FAN_SET_ROLE'], 0, $iFanProfileId, array(
  1434. 'EntryUrl' => $oGroupProfile->getUrl(),
  1435. 'EntryTitle' => $oGroupProfile->getDisplayName(),
  1436. 'Role' => implode(', ', $aRolesNames),
  1437. ), BX_EMAIL_NOTIFY);
  1438. }
  1439. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1440. 'object_author_id' => $iGroupProfileId,
  1441. 'performer_id' => $iProfileId,
  1442. 'fan_id' => $iFanProfileId,
  1443. 'content' => $aGroupProfileInfo,
  1444. 'role' => $mixedRole,
  1445. 'group_profile' => $iGroupProfileId,
  1446. 'profile' => $iProfileId
  1447. ));
  1448. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1449. }
  1450. public function unsetRole($iGroupProfileId, $iFanProfileId)
  1451. {
  1452. $CNF = &$this->_oConfig->CNF;
  1453. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1454. return false;
  1455. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1456. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1457. if(!$oConnection || !$oGroupProfile)
  1458. return false;
  1459. if($oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->removeConnection($iFanProfileId, $iGroupProfileId))
  1460. return false;
  1461. $iRole = $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1462. if(!$this->_oDb->unsetRole($iGroupProfileId, $iFanProfileId))
  1463. return false;
  1464. $this->onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole);
  1465. return true;
  1466. }
  1467. public function onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole)
  1468. {
  1469. $CNF = &$this->_oConfig->CNF;
  1470. $iProfileId = bx_get_logged_profile_id();
  1471. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1472. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1473. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1474. 'object_author_id' => $iGroupProfileId,
  1475. 'performer_id' => $iProfileId,
  1476. 'fan_id' => $iFanProfileId,
  1477. 'content' => $aGroupProfileInfo,
  1478. 'role' => $iRole,
  1479. 'group_profile' => $iGroupProfileId,
  1480. 'profile' => $iProfileId
  1481. ));
  1482. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1483. }
  1484. public function getGroupsByFan($iProfileId, $mixedRole = false)
  1485. {
  1486. $CNF = &$this->_oConfig->CNF;
  1487. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1488. return false;
  1489. if($mixedRole === false)
  1490. $mixedRole = BX_BASE_MOD_GROUPS_ROLE_COMMON;
  1491. if(!is_array($mixedRole))
  1492. $mixedRole = [$mixedRole];
  1493. $aResult = [];
  1494. foreach($mixedRole as $iRole) {
  1495. switch($iRole) {
  1496. case BX_BASE_MOD_GROUPS_ROLE_COMMON:
  1497. $aIds = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($iProfileId);
  1498. break;
  1499. default:
  1500. $aIds = $this->_oDb->getRoles([
  1501. 'type' => 'group_pids_by_fan_id',
  1502. 'fan_id' => $iProfileId,
  1503. 'role' => $iRole
  1504. ]);
  1505. }
  1506. $aResult = array_merge($aResult, $aIds);
  1507. }
  1508. return $aResult;
  1509. }
  1510. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  1511. {
  1512. $CNF = &$this->_oConfig->CNF;
  1513. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  1514. $sSrc = '';
  1515. if(isset($CNF['FIELD_COVER']) && !empty($aContentInfo[$CNF['FIELD_COVER']]))
  1516. $sSrc = $oGroupProfile->getCover();
  1517. if(empty($sSrc) && isset($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  1518. $sSrc = $oGroupProfile->getPicture();
  1519. return empty($sSrc) ? array() : array(
  1520. array('id' => $aContentInfo[$CNF['FIELD_PICTURE']], 'url' => $sUrl, 'src' => $sSrc, 'src_orig' => $sSrc),
  1521. );
  1522. }
  1523. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  1524. {
  1525. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  1526. return array(0, 0, null);
  1527. if ($oGroupProfile->getModule() == $this->getName()) {
  1528. $iProfileId = $iInitiatorId;
  1529. $iGroupProfileId = $oGroupProfile->id();
  1530. } else {
  1531. $iProfileId = $oGroupProfile->id();
  1532. $iGroupProfileId = $iInitiatorId;
  1533. }
  1534. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  1535. }
  1536. }
  1537. /** @} */