BxBaseModGroupsModule.php 76 KB

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