1
0

BxBaseModGroupsModule.php 73 KB

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