BxBaseModGroupsModule.php 73 KB

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