1
0

BxBaseModGroupsModule.php 85 KB

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