1
0

BxBaseModGroupsModule.php 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878
  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_SETTINGS', 'change_settings');
  17. define('BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER', 'change_cover');
  18. define('BX_BASE_MOD_GROUPS_ACTION_INVITE', 'invite');
  19. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS', 'manage_fans');
  20. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES', 'manage_roles');
  21. define('BX_BASE_MOD_GROUPS_ACTION_DELETE', 'delete');
  22. define('BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT', 'edit_any');
  23. define('BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT', 'delete_any');
  24. define('BX_BASE_MOD_GROUPS_ACTION_TIMELINE_POST_PIN', 'pin'); //for timeline posts only
  25. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY', 'day');
  26. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK', 'week');
  27. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_MONTH', 'month');
  28. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_YEAR', 'year');
  29. /**
  30. * Groups profiles module.
  31. */
  32. class BxBaseModGroupsModule extends BxBaseModProfileModule
  33. {
  34. function __construct(&$aModule)
  35. {
  36. parent::__construct($aModule);
  37. $CNF = &$this->_oConfig->CNF;
  38. if(isset($CNF['FIELD_PUBLISHED']))
  39. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  40. $CNF['FIELD_PUBLISHED'],
  41. ));
  42. }
  43. /**
  44. * Get possible recipients for start conversation form
  45. */
  46. public function actionAjaxGetInitialMembers ()
  47. {
  48. $sTerm = bx_get('term');
  49. $a = BxDolService::call('system', 'profiles_search', [$sTerm, ['module' => $this->_oConfig->getName()]], 'TemplServiceProfiles');
  50. header('Content-Type:text/javascript; charset=utf-8');
  51. echo(json_encode($a));
  52. }
  53. public function serviceGetInitialMembers ($sParams)
  54. {
  55. $aOptions = json_decode($sParams, true);
  56. if (!$sParams || !isset($aOptions['term']))
  57. return [];
  58. $a = BxDolService::call('system', 'profiles_search', [$aOptions['term'], ['module' => $this->_oConfig->getName()]], 'TemplServiceProfiles');
  59. return $a;
  60. }
  61. /**
  62. * Process Process Invitation
  63. */
  64. public function actionProcessInvite ($sKey, $iGroupProfileId, $bAccept)
  65. {
  66. return $this->serviceProcessInvite($sKey, $iGroupProfileId, $bAccept);
  67. }
  68. public function serviceProcessInvite ($sKey, $iGroupProfileId, $bAccept)
  69. {
  70. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  71. if (isset($aData['invited_profile_id'])){
  72. $CNF = &$this->_oConfig->CNF;
  73. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  74. return '';
  75. $iInvitedProfileId = $aData['invited_profile_id'];
  76. if ($iInvitedProfileId != bx_get_logged_profile_id())
  77. return '';
  78. if ($bAccept){
  79. if($oConnection && !$oConnection->isConnected($iInvitedProfileId, $iGroupProfileId)){
  80. $oConnection->addConnection($iInvitedProfileId, $iGroupProfileId);
  81. $oConnection->addConnection($iGroupProfileId, $iInvitedProfileId);
  82. }
  83. }
  84. $this->_oDb->deleteInviteByKey($sKey, $iGroupProfileId);
  85. }
  86. }
  87. public function actionCheckName()
  88. {
  89. $CNF = &$this->_oConfig->CNF;
  90. $sName = bx_process_input(bx_get('name'));
  91. if(empty($sName))
  92. return echoJson(array());
  93. $sResult = '';
  94. $iId = (int)bx_get('id');
  95. if(!empty($iId)) {
  96. $aPrice = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iId));
  97. if(strcmp($sName, $aPrice[$CNF['FIELD_PRICE_NAME']]) == 0)
  98. $sResult = $sName;
  99. }
  100. echoJson(array(
  101. 'name' => !empty($sResult) ? $sResult : $this->_oConfig->getPriceName($sName)
  102. ));
  103. }
  104. public function actionGetQuestionnaire()
  105. {
  106. $sSource = '';
  107. if(($sSource = bx_get('s')) !== false)
  108. $sSource = bx_process_url_param($sSource);
  109. $sObject = '';
  110. if(($sObject = bx_get('o')) !== false)
  111. $sObject = bx_process_url_param($sObject);
  112. $sAction = '';
  113. if(($sAction = bx_get('a')) !== false)
  114. $sAction = bx_process_url_param($sAction);
  115. $iContentProfileId = 0;
  116. if(($iContentProfileId = bx_get('cpi')) !== false)
  117. $iContentProfileId = (int)$iContentProfileId;
  118. echoJson($this->serviceGetQuestionnaire($sSource, $sObject, $sAction, $iContentProfileId));
  119. }
  120. public function serviceManageTools($sType = 'common')
  121. {
  122. $this->_oTemplate->addJs(['modules/base/groups/js/|manage_tools.js']);
  123. return parent::serviceManageTools($sType);
  124. }
  125. public function decodeDataAPI($aData, $aParams = [])
  126. {
  127. $CNF = $this->_oConfig->CNF;
  128. $aResult = parent::decodeDataAPI($aData, $aParams);
  129. if(isset($aParams['template']) && $aParams['template'] == 'unit_wo_info')
  130. return $aResult;
  131. if(getParam('sys_api_conn_in_prof_units') == 'on' && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) !== false) {
  132. $oProfile = BxDolProfile::getInstanceByContentAndType($aData[$CNF['FIELD_ID']], $this->_aModule['name']);
  133. $aResult['members_count'] = $oConnection->getConnectedInitiatorsCount($oProfile->id(), false);
  134. $aResult['members_list'] = $oConnection->getConnectedListAPI($oProfile->id(), false, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS);
  135. }
  136. return array_merge($aResult, [
  137. 'author_data' => BxDolProfile::getData($aData[$CNF['FIELD_AUTHOR']]),
  138. 'visibility' => $aData[$CNF['FIELD_ALLOW_VIEW_TO']]
  139. ]);
  140. }
  141. public function serviceGetMenuAddonManageTools()
  142. {
  143. $CNF = &$this->_oConfig->CNF;
  144. $aResult = parent::serviceGetMenuAddonManageTools();
  145. if(!empty($CNF['FIELD_STATUS_ADMIN']))
  146. $aResult['counter1_value'] = $this->_oDb->getEntriesNumByParams([[
  147. 'key' => $CNF['FIELD_STATUS_ADMIN'],
  148. 'value' => BX_BASE_MOD_GENERAL_STATUS_PENDING,
  149. 'operator' => '='
  150. ]]);
  151. return $aResult;
  152. }
  153. public function serviceGetOptionsMembersMode()
  154. {
  155. $CNF = &$this->_oConfig->CNF;
  156. return array(
  157. array('key' => '', 'value' => _t('_None')),
  158. array('key' => BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES, 'value' => _t($CNF['T']['option_members_mode_' . BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES])),
  159. );
  160. }
  161. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  162. {
  163. if(empty($sUnitTemplate))
  164. $sUnitTemplate = 'unit.html';
  165. return parent::serviceGetSearchResultUnit($iContentId, $sUnitTemplate);
  166. }
  167. /**
  168. * @see BxBaseModProfileModule::serviceGetSpaceTitle
  169. */
  170. public function serviceGetSpaceTitle()
  171. {
  172. return _t($this->_oConfig->CNF['T']['txt_sample_single']);
  173. }
  174. public function serviceGetFansConnectionObject()
  175. {
  176. $CNF = &$this->_oConfig->CNF;
  177. return ($sKey = 'OBJECT_CONNECTIONS') && !empty($CNF[$sKey]) && ($oConnection = BxDolConnection::getObjectInstance($CNF[$sKey])) !== false ? $oConnection : false;
  178. }
  179. /**
  180. * @see iBxDolProfileService::serviceGetParticipatingProfiles
  181. */
  182. public function serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects = false)
  183. {
  184. $CNF = &$this->_oConfig->CNF;
  185. $mixedCo = $aConnectionObjects;
  186. $bCoEmpty = $mixedCo === false;
  187. $bCoString = !$bCoEmpty && is_string($mixedCo);
  188. $aConnectionObjects = [];
  189. if($bCoEmpty || ($bCoString && $mixedCo == 'subscriptions'))
  190. $aConnectionObjects[] = 'sys_profiles_subscriptions';
  191. if(($bCoEmpty || ($bCoString && $mixedCo == 'fans')) && !empty($CNF['OBJECT_CONNECTIONS']))
  192. $aConnectionObjects[] = $CNF['OBJECT_CONNECTIONS'];
  193. return parent::serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects);
  194. }
  195. public function serviceGetSafeServices()
  196. {
  197. $CNF = &$this->_oConfig->CNF;
  198. $a = [
  199. 'ProcessInvite' => '',
  200. 'GetQuestionnaire' => '',
  201. 'GetInitialMembers' => '',
  202. 'EntityInvite' => '',
  203. 'Invitations' => '',
  204. 'FansWithoutAdmins' => '',
  205. ];
  206. if(!empty($CNF['OBJECT_RECOMMENDATIONS_FANS']))
  207. $a['BrowseRecommendationsFans'] = '';
  208. return array_merge(parent::serviceGetSafeServices(), $a);
  209. }
  210. public function serviceModuleSample ()
  211. {
  212. return parent::serviceModuleSample() ?: '_sys_sample_context';
  213. }
  214. /**
  215. * Check if this module entry can be used as profile
  216. */
  217. public function serviceActAsProfile ()
  218. {
  219. return false;
  220. }
  221. /**
  222. * Check if this module is group profile
  223. */
  224. public function serviceIsGroupProfile ()
  225. {
  226. return true;
  227. }
  228. public function serviceIsEnableForContext($iProfileId = 0)
  229. {
  230. $CNF = &$this->_oConfig->CNF;
  231. $bRet = true;
  232. $sCnfKey = 'ENABLE_FOR_CONTEXT_IN_MODULES';
  233. if(empty($iProfileId) || empty($CNF[$sCnfKey]) || !is_array($CNF[$sCnfKey]))
  234. $bRet = false;
  235. $oProfile = null;
  236. if($bRet && !($oProfile = BxDolProfile::getInstance($iProfileId)))
  237. $bRet = false;
  238. if($bRet && !in_array($oProfile->getModule(), $CNF[$sCnfKey]))
  239. $bRet = false;
  240. bx_alert($this->getName(), 'is_enabled_for_context', $iProfileId, false, [
  241. 'cnf' => $CNF,
  242. 'module' => $this,
  243. 'context_profile' => $oProfile,
  244. 'result' => &$bRet
  245. ]);
  246. return $bRet;
  247. }
  248. /**
  249. * check if provided profile is member of the group
  250. */
  251. public function serviceIsFan ($iGroupProfileId, $iProfileId = false)
  252. {
  253. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  254. return $oGroupProfile !== false && $this->isFan($oGroupProfile->getContentId(), $iProfileId);
  255. }
  256. /**
  257. * check if provided profile is admin of the group
  258. */
  259. public function serviceIsAdmin ($iGroupProfileId, $iProfileId = false)
  260. {
  261. if(!$iProfileId)
  262. $iProfileId = bx_get_logged_profile_id();
  263. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  264. if(!$oGroupProfile)
  265. return false;
  266. $iGroupContentId = $oGroupProfile->getContentId();
  267. if(!$this->isFan($iGroupContentId, $iProfileId))
  268. return false;
  269. $aGroupContentInfo = $this->_oDb->getContentInfoById($iGroupContentId);
  270. return $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aGroupContentInfo);
  271. }
  272. public function serviceGetAdminRole($iGroupProfileId, $iProfileId = false)
  273. {
  274. if(!$iProfileId)
  275. $iProfileId = bx_get_logged_profile_id();
  276. if(!$this->serviceIsAdmin($iGroupProfileId, $iProfileId))
  277. return 0;
  278. return $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  279. }
  280. /*
  281. * Get context (group) advanced members who can perform the specified action.
  282. * If 'Roles' are disabled for the context then all context admins are returned.
  283. */
  284. public function serviceGetAdminsByAction($iGroupProfileId, $mixedAction)
  285. {
  286. if(!$this->_oConfig->isAdmins())
  287. return [];
  288. $aGroupContentInfo = $this->_oDb->getContentInfoByProfileId($iGroupProfileId);
  289. if(empty($aGroupContentInfo) || !is_array($aGroupContentInfo))
  290. return [];
  291. $aAdmins = $this->_oDb->getAdmins($iGroupProfileId);
  292. if(!$this->_oConfig->isRoles())
  293. return $aAdmins;
  294. if(!is_array($mixedAction))
  295. $mixedAction = [$mixedAction];
  296. $aResult = [];
  297. foreach($mixedAction as $sAction)
  298. foreach($aAdmins as $iAdminProfileId)
  299. if(!in_array($iAdminProfileId, $aResult) && $this->isAllowedActionByRole($sAction, $aGroupContentInfo, $iGroupProfileId, $iAdminProfileId))
  300. $aResult[] = $iAdminProfileId;
  301. return $aResult;
  302. }
  303. public function serviceGetAdminsToManageContent($iGroupProfileId)
  304. {
  305. return $this->serviceGetAdminsByAction($iGroupProfileId, [
  306. BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT,
  307. BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT
  308. ]);
  309. }
  310. /**
  311. * Delete profile from fans and admins tables
  312. * @param $iProfileId profile id
  313. */
  314. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  315. {
  316. $CNF = &$this->_oConfig->CNF;
  317. $this->_oDb->deleteAdminsByProfileId($iProfileId);
  318. if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  319. $oConnection->onDeleteInitiatorAndContent($iProfileId);
  320. }
  321. /**
  322. * Reset group's author for particular group
  323. * @param $iContentId group id
  324. * @parem $iAuthorId new author profile ID
  325. * @return false of error, or number of updated records on success
  326. */
  327. public function serviceReassignEntityAuthor ($iContentId, $iAuthorId = 0)
  328. {
  329. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  330. if (!$aContentInfo)
  331. return false;
  332. if (empty($iAuthorId)) {
  333. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  334. if (!$oGroupProfile)
  335. return false;
  336. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  337. if($aAdmins)
  338. $iAuthorId = array_pop($aAdmins);
  339. }
  340. return $this->_oDb->updateAuthorById($iContentId, $iAuthorId);
  341. }
  342. /**
  343. * Entry actions and social sharing block
  344. */
  345. public function serviceEntityAllActions ($mixedContent = false, $aParams = array())
  346. {
  347. $CNF = &$this->_oConfig->CNF;
  348. if(!empty($mixedContent)) {
  349. if(!is_array($mixedContent))
  350. $mixedContent = array((int)$mixedContent, (method_exists($this->_oDb, 'getContentInfoById')) ? $this->_oDb->getContentInfoById((int)$mixedContent) : array());
  351. }
  352. else {
  353. $mixedContent = $this->_getContent();
  354. if($mixedContent === false)
  355. return false;
  356. }
  357. list($iContentId, $aContentInfo) = $mixedContent;
  358. if(!empty($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  359. $aParams = array_merge(array(
  360. 'entry_thumb' => (int)$aContentInfo[$CNF['FIELD_PICTURE']]
  361. ), $aParams);
  362. return parent::serviceEntityAllActions ($mixedContent, $aParams);
  363. }
  364. /**
  365. * Reset group's author when author profile is deleted
  366. * @param $iProfileId author profile id
  367. * @param $iAuthorId new author profile id
  368. * @return number of changed items
  369. */
  370. public function serviceReassignEntitiesByAuthor ($iProfileId, $iAuthorId = 0)
  371. {
  372. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  373. if (!$a)
  374. return 0;
  375. $iCount = 0;
  376. foreach ($a as $aContentInfo)
  377. $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']], $iAuthorId) ? 1 : 0);
  378. return $iCount;
  379. }
  380. public function servicePrepareFields ($aFieldsProfile)
  381. {
  382. $CNF = &$this->_oConfig->CNF;
  383. $aFieldsProfile[$CNF['FIELD_NAME']] = $aFieldsProfile['name'];
  384. $aFieldsProfile[$CNF['FIELD_TEXT']] = isset($aFieldsProfile['description']) ? $aFieldsProfile['description'] : '';
  385. unset($aFieldsProfile['name']);
  386. unset($aFieldsProfile['description']);
  387. return $aFieldsProfile;
  388. }
  389. public function serviceOnRemoveConnection ($iGroupProfileId, $iInitiatorId)
  390. {
  391. $CNF = &$this->_oConfig->CNF;
  392. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  393. if (!$oGroupProfile)
  394. return false;
  395. $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
  396. if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
  397. return $oConn->removeConnection($iProfileId, $iGroupProfileId);
  398. return false;
  399. }
  400. public function serviceAddInvitation ($iContextPid, $iPid, $iPerformerPid = 0)
  401. {
  402. $CNF = &$this->_oConfig->CNF;
  403. $oGroupProfile = false;
  404. if(!($oGroupProfile = BxDolProfile::getInstance($iContextPid)))
  405. return false;
  406. if(!($aContentInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId())))
  407. return false;
  408. $oConnection = false;
  409. if(!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  410. return false;
  411. if($oConnection->isConnected((int)$iPid, $iContextPid) || $oConnection->isConnected($iContextPid, (int)$iPid))
  412. return false;
  413. if(!$iPerformerPid)
  414. $iPerformerPid = bx_get_logged_profile_id();
  415. $sEntryUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  416. if(!empty($CNF['TABLE_INVITES']) && !$this->_oDb->isInviteByInvited($iPid, $iContextPid)) {
  417. $sKey = BxDolKey::getInstance()->getNewKey(false, $CNF['INVITES_KEYS_LIFETIME']);
  418. $this->_oDb->insertInvite($sKey, $iContextPid, $iPerformerPid, $iPid);
  419. $sEntryUrl = bx_append_url_params($sEntryUrl, [
  420. 'key' => $sKey
  421. ]);
  422. }
  423. $sModule = $this->getName();
  424. $sSource = $CNF['OBJECT_CONNECTIONS'];
  425. $iSourceTime = time();
  426. /**
  427. * @hooks
  428. * @hookdef hook-bx_base_groups-join_invitation '{module_name}', 'join_invitation' - hook before adding (sending) new join to context invitation
  429. * - $unit_name - module name
  430. * - $action - equals `join_invitation`
  431. * - $object_id - context id
  432. * - $sender_id - context profile id
  433. * - $extra_params - array of additional params with the following array keys:
  434. * - `content` - [array] context info array as key&value pairs
  435. * - `entry_title` - [string] context title
  436. * - `entry_url` - [string] context URL
  437. * - `group_profile` - [int] context profile id
  438. * - `profile` - [int] profile id who was invited
  439. * - `notification_subobject_id` - [int] profile id who was invited
  440. * - `object_author_id` - [int] context profile id
  441. * @hook @ref hook-bx_base_groups-join_invitation
  442. */
  443. bx_alert($sModule, 'join_invitation', $aContentInfo[$CNF['FIELD_ID']], $iContextPid, [
  444. 'content' => $aContentInfo,
  445. 'entry_title' => $aContentInfo[$CNF['FIELD_NAME']],
  446. 'entry_url' => bx_absolute_url($sEntryUrl),
  447. 'group_profile' => $iContextPid,
  448. 'profile' => $iPid,
  449. 'notification_subobject_id' => $iPid,
  450. 'object_author_id' => $iContextPid,
  451. ]);
  452. /**
  453. * 'Invitation Received' alert for Notifications module.
  454. * Note. It's essential to use Recipient ($iPid) in 'object_author_id' parameter.
  455. * In this case notification will be received by Recipient profile.
  456. */
  457. /**
  458. * @hooks
  459. * @hookdef hook-bx_base_groups-join_invitation_notif '{module_name}', 'join_invitation_notif' - hook before adding new join to context invitation. Is needed for Notifications module.
  460. * - $unit_name - module name
  461. * - $action - equals `join_invitation_notif`
  462. * - $object_id - context id
  463. * - $sender_id - context profile id
  464. * - $extra_params - array of additional params with the following array keys:
  465. * - `object_author_id` - [int] profile id who was invited
  466. * - `privacy_view` - [int] or [string] privacy for view context action, @see BxDolPrivacy
  467. * @hook @ref hook-bx_base_groups-join_invitation_notif
  468. */
  469. bx_alert($sModule, 'join_invitation_notif', $aContentInfo[$CNF['FIELD_ID']], $iContextPid, [
  470. 'object_author_id' => $iPid,
  471. 'privacy_view' => isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 3,
  472. 'source' => $sSource . '_invitation_' . $iSourceTime,
  473. 'source_mac' => $sSource . '_invitation_' . $iPerformerPid . '_' . $iSourceTime,
  474. ]);
  475. return true;
  476. }
  477. public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $bSendInviteOnly = false)
  478. {
  479. $CNF = &$this->_oConfig->CNF;
  480. list($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  481. if(!$oGroupProfile)
  482. return false;
  483. if(!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  484. return false;
  485. $oConnection = false;
  486. if(!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  487. return false;
  488. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  489. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]));
  490. // send invitation to the group
  491. $iPerformerId = bx_get_logged_profile_id();
  492. if($bSendInviteOnly)
  493. return $iProfileId != $iPerformerId ? $this->serviceAddInvitation($oGroupProfile->id(), $iInitiatorId, $iPerformerId) : false;
  494. $sModule = $this->getName();
  495. $sModuleGroup = $oGroupProfile->getModule();
  496. $sSource = $CNF['OBJECT_CONNECTIONS'];
  497. $iSourceTime = time();
  498. // send notification to group's admins that new connection is pending confirmation
  499. if($oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation'] && $aContentInfo[$CNF['FIELD_AUTHOR']] != $iProfileId) {
  500. /**
  501. * @hooks
  502. * @hookdef hook-bx_base_groups-join_request '{module_name}', 'join_request' - hook before adding new join to context request
  503. * - $unit_name - module name
  504. * - $action - equals `join_request`
  505. * - $object_id - context id
  506. * - $sender_id - context profile id
  507. * - $extra_params - array of additional params with the following array keys:
  508. * - `object_author_id` - [int] context profile id
  509. * - `performer_id` - [int] profile id who wants to join
  510. * - `content` - [array] context info array as key&value pairs
  511. * - `entry_title` - [string] context title
  512. * - `entry_url` - [string] context URL
  513. * - `group_profile` - [int] context profile id
  514. * - `profile` - [int] profile id who wants to join
  515. * @hook @ref hook-bx_base_groups-join_request
  516. */
  517. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, [
  518. 'object_author_id' => $iGroupProfileId,
  519. 'performer_id' => $iProfileId,
  520. 'content' => $aContentInfo,
  521. 'entry_title' => $sEntryTitle,
  522. 'entry_url' => $sEntryUrl,
  523. 'group_profile' => $iGroupProfileId,
  524. 'profile' => $iProfileId,
  525. 'source' => $sSource . '_request_' . $iSourceTime,
  526. 'source_mac' => $sSource . '_request_' . $iPerformerId . '_' . $iSourceTime,
  527. ]);
  528. }
  529. // send notification that join request was accepted
  530. else if($oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $sModuleGroup != $sModule && $iProfileId != $iPerformerId) {
  531. /**
  532. * @hooks
  533. * @hookdef hook-bx_base_groups-join_request_accepted '{module_name}', 'join_request_accepted' - hook before accepting join to context request
  534. * It's equivalent to @ref hook-bx_base_groups-join_request
  535. * @hook @ref hook-bx_base_groups-join_request_accepted
  536. */
  537. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, [
  538. 'object_author_id' => $iGroupProfileId,
  539. 'performer_id' => $iProfileId,
  540. 'content' => $aContentInfo,
  541. 'entry_title' => $sEntryTitle,
  542. 'entry_url' => $sEntryUrl,
  543. 'group_profile' => $iGroupProfileId,
  544. 'profile' => $iProfileId
  545. ]);
  546. }
  547. // new fan was added
  548. if($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  549. // follow group on join
  550. if(bx_srv($sModuleGroup, 'act_as_profile'))
  551. $this->addFollower($oGroupProfile->id(), (int)$iInitiatorId);
  552. else
  553. $this->addFollower((int)$iInitiatorId, $oGroupProfile->id());
  554. if($aContentInfo[$CNF['FIELD_AUTHOR']] != $iProfileId) {
  555. /**
  556. * @hooks
  557. * @hookdef hook-bx_base_groups-fan_added '{module_name}', 'fan_added' - hook before adding (registering) new context member
  558. * It's equivalent to @ref hook-bx_base_groups-join_request
  559. * @hook @ref hook-bx_base_groups-fan_added
  560. */
  561. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, [
  562. 'object_author_id' => $iGroupProfileId,
  563. 'performer_id' => $iProfileId,
  564. 'content' => $aContentInfo,
  565. 'entry_title' => $sEntryTitle,
  566. 'entry_url' => $sEntryUrl,
  567. 'group_profile' => $iGroupProfileId,
  568. 'profile' => $iProfileId,
  569. 'source' => $sSource . '_added_' . $iSourceTime,
  570. 'source_mac' => $sSource . '_added_' . $iPerformerId . '_' . $iSourceTime,
  571. ]);
  572. }
  573. $this->doAudit($iGroupProfileId, $iInitiatorId, '_sys_audit_action_group_join_request_accepted');
  574. return false;
  575. }
  576. // don't automatically add connection (mutual) if group requires manual join confirmation
  577. if($aContentInfo['join_confirmation'])
  578. return false;
  579. // check if connection already exists
  580. if($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  581. return false;
  582. if(!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  583. return false;
  584. return true;
  585. }
  586. public function serviceFansTable ()
  587. {
  588. $CNF = &$this->_oConfig->CNF;
  589. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_CONNECTIONS']);
  590. if(!$oGrid)
  591. return false;
  592. if($this->_bIsApi){
  593. return [
  594. bx_api_get_block('grid', $oGrid->getCodeAPI())
  595. ];
  596. }
  597. return $oGrid->getCode();
  598. }
  599. public function serviceInvitesTable ()
  600. {
  601. $CNF = &$this->_oConfig->CNF;
  602. if(!isset($CNF['OBJECT_GRID_INVITES']))
  603. return false;
  604. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_INVITES']);
  605. if(!$oGrid)
  606. return false;
  607. if($this->_bIsApi)
  608. return [
  609. bx_api_get_block('grid', $oGrid->getCodeAPI())
  610. ];
  611. return $oGrid->getCode();
  612. }
  613. public function serviceBansTable ()
  614. {
  615. $CNF = &$this->_oConfig->CNF;
  616. if(!isset($CNF['OBJECT_GRID_BANS']))
  617. return false;
  618. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_BANS']);
  619. if(!$oGrid)
  620. return false;
  621. if($this->_bIsApi)
  622. return [
  623. bx_api_get_block('grid', $oGrid->getCodeAPI())
  624. ];
  625. return $oGrid->getCode();
  626. }
  627. public function serviceInvitations ($iInvitedPid = 0, $bAsArray = false)
  628. {
  629. $CNF = &$this->_oConfig->CNF;
  630. if(!$iInvitedPid)
  631. $iInvitedPid = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  632. if(!$iInvitedPid)
  633. return false;
  634. $aInvites = $this->_oDb->getInvites(['sample' => 'invited_pid', 'invited_pid' => $iInvitedPid]);
  635. if(empty($aInvites) || !is_array($aInvites))
  636. return false;
  637. if($bAsArray)
  638. return $aInvites;
  639. $iStart = $iLimit = 0;
  640. if($this->_bIsApi) {
  641. $aParams = bx_api_get_browse_params($bAsArray);
  642. $iStart = isset($aParams['start']) ? (int)$aParams['start'] : 0;
  643. $iLimit = isset($aParams['per_page']) ? (int)$aParams['per_page'] : 0;
  644. }
  645. else {
  646. $iStart = (int)bx_get('start');
  647. $iLimit = (int)bx_get('per_page');
  648. }
  649. $iLimit = !$iLimit && ($sKey = 'PARAM_NUM_CONNECTIONS_QUICK') && !empty($CNF[$sKey]) && ($iValue = (int)getParam($CNF[$sKey])) ? $iValue : 4;
  650. if($this->_bIsApi) {
  651. $aData = [
  652. 'data' => [],
  653. 'request_url' => '/api.php?r=' . $this->_oConfig->getName() . '/invites/&params[]=' . $iInvitedPid . '&params[]=',
  654. 'params' => [
  655. 'start' => $iStart,
  656. 'per_page' => $iLimit
  657. ]
  658. ];
  659. foreach($aInvites as $aInvite)
  660. $aData['data'][] = BxDolProfile::getData($aInvite['group_profile_id']);
  661. return [bx_api_get_block('profiles_list', $aData)];
  662. }
  663. else
  664. return $this->_serviceBrowseQuick(array_keys($aInvites), $iStart, $iLimit);
  665. }
  666. public function serviceFans ($iContentId = 0, $bAsArray = false)
  667. {
  668. $CNF = &$this->_oConfig->CNF;
  669. if(!$iContentId)
  670. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  671. if(!$iContentId)
  672. return false;
  673. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  674. if(!$aContentInfo)
  675. return false;
  676. if(!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  677. return false;
  678. if(is_bool($bAsArray) && $bAsArray === true)
  679. return BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  680. if($this->_bIsApi) {
  681. $aParams = bx_api_get_browse_params($bAsArray);
  682. $iStart = isset($aParams['start']) ? (int)$aParams['start'] : 0;
  683. $iLimit = isset($aParams['per_page']) ? (int)$aParams['per_page'] : 0;
  684. $iLimit = !$iLimit && ($sKey = 'PARAM_NUM_CONNECTIONS_QUICK') && !empty($CNF[$sKey]) && ($iValue = (int)getParam($CNF[$sKey])) ? $iValue : 4;
  685. $aProfiles = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true, $iStart, $iLimit);
  686. if(empty($aProfiles) || !is_array($aProfiles))
  687. return false;
  688. $aData = [
  689. 'data' => [],
  690. 'request_url' => '/api.php?r=' . $this->_oConfig->getName() . '/fans/&params[]=' . $iContentId . '&params[]=',
  691. 'params' => [
  692. 'start' => $iStart,
  693. 'per_page' => $iLimit
  694. ]
  695. ];
  696. foreach($aProfiles as $iProfileId)
  697. $aData['data'][] = BxDolProfile::getData($iProfileId);
  698. return [bx_api_get_block('profiles_list', $aData)];
  699. }
  700. bx_import('BxDolConnection');
  701. $mixedResult = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  702. if(!$mixedResult)
  703. return MsgBox(_t('_sys_txt_empty'));
  704. return $mixedResult;
  705. }
  706. public function serviceFansWithoutAdmins ($iContentId = 0, $bAsArray = false)
  707. {
  708. if (!$iContentId)
  709. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  710. if (!$iContentId)
  711. return false;
  712. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  713. if (!$aContentInfo)
  714. return false;
  715. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  716. return false;
  717. $CNF = &$this->_oConfig->CNF;
  718. $aFans = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  719. if(empty($aFans) || !is_array($aFans))
  720. return false;
  721. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  722. if(!empty($aAdmins) && is_array($aAdmins) && !($aFans = array_diff($aFans, $aAdmins)))
  723. return false;
  724. if(is_bool($bAsArray) && $bAsArray === true)
  725. return $aFans;
  726. $iStart = $iLimit = 0;
  727. if($this->_bIsApi) {
  728. $aParams = bx_api_get_browse_params($bAsArray);
  729. $iStart = isset($aParams['start']) ? (int)$aParams['start'] : 0;
  730. $iLimit = isset($aParams['per_page']) ? (int)$aParams['per_page'] : 0;
  731. }
  732. else {
  733. $iStart = (int)bx_get('start');
  734. $iLimit = (int)bx_get('per_page');
  735. }
  736. $iLimit = !$iLimit && ($sKey = 'PARAM_NUM_CONNECTIONS_QUICK') && !empty($CNF[$sKey]) && ($iValue = (int)getParam($CNF[$sKey])) ? $iValue : 4;
  737. if($this->_bIsApi) {
  738. $aData = [
  739. 'data' => [],
  740. 'request_url' => '/api.php?r=' . $this->_oConfig->getName() . '/fans_without_admins/&params[]=' . $iContentId . '&params[]=',
  741. 'params' => [
  742. 'start' => $iStart,
  743. 'per_page' => $iLimit
  744. ]
  745. ];
  746. foreach($aProfiles as $iProfileId)
  747. $aData['data'][] = BxDolProfile::getData($iProfileId);
  748. return [bx_api_get_block('profiles_list', $aData)];
  749. }
  750. else
  751. return $this->_serviceBrowseQuick($aFans, $iStart, $iLimit);
  752. }
  753. public function serviceAdmins ($iContentId = 0, $sParams = '')
  754. {
  755. $CNF = &$this->_oConfig->CNF;
  756. if(!$iContentId)
  757. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  758. if(!$iContentId)
  759. return false;
  760. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  761. if(!$oGroupProfile)
  762. return false;
  763. $iStart = (int)bx_get('start');
  764. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? (int)getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  765. if(!$iLimit)
  766. $iLimit = 4;
  767. if($this->_bIsApi && ($aParams = bx_api_get_browse_params($sParams))) {
  768. if(isset($aParams['start']))
  769. $iStart = (int)$aParams['start'];
  770. if(isset($aParams['per_page']))
  771. $iLimit = (int)$aParams['per_page'];
  772. }
  773. $aProfiles = $this->_oDb->getAdmins($oGroupProfile->id(), $iStart, $iLimit+1);
  774. if(empty($aProfiles) || !is_array($aProfiles))
  775. return false;
  776. if($this->_bIsApi) {
  777. $aData = [
  778. 'data' => [],
  779. 'request_url' => '/api.php?r=' . $this->_oConfig->getName() . '/admins/&params[]=' . $iContentId . '&params[]=',
  780. 'params' => [
  781. 'start' => $iStart,
  782. 'per_page' => $iLimit
  783. ]
  784. ];
  785. foreach($aProfiles as $iProfileId)
  786. $aData['data'][] = BxDolProfile::getData($iProfileId);
  787. return [bx_api_get_block('profiles_list', $aData)];
  788. }
  789. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  790. }
  791. public function serviceMembersByRole ($iContentId = 0, $iRole = BX_BASE_MOD_GROUPS_ROLE_COMMON)
  792. {
  793. $CNF = &$this->_oConfig->CNF;
  794. if(!$iContentId)
  795. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  796. if(!$iContentId)
  797. return false;
  798. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  799. if(!$oGroupProfile)
  800. return false;
  801. $iStart = (int)bx_get('start');
  802. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  803. if(!$iLimit)
  804. $iLimit = 4;
  805. $aProfiles = $this->_oDb->getRoles([
  806. 'type' => 'fan_pids_by_group_pid',
  807. 'group_profile_id' => $oGroupProfile->id(),
  808. 'role' => $iRole,
  809. 'start' => $iStart,
  810. 'limit' => $iLimit + 1
  811. ]);
  812. if(empty($aProfiles) || !is_array($aProfiles))
  813. return false;
  814. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  815. }
  816. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  817. {
  818. if (!$iProfileId)
  819. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  820. if (!$iProfileId)
  821. return '';
  822. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  823. }
  824. /**
  825. * Display entries posted into particular context and joined by profile
  826. */
  827. public function serviceBrowseContextJoinedEntries ($iContextPid = 0, $iProfileId = 0, $aParams = [])
  828. {
  829. if(!$iContextPid)
  830. $iContextPid = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  831. if(!$iContextPid)
  832. return $this->_bIsApi ? [] : '';
  833. if(!$iProfileId)
  834. $iProfileId = bx_get_logged_profile_id();
  835. if(!$iProfileId)
  836. return $this->_bIsApi ? [] : '';
  837. $bEmptyMessage = true;
  838. if(isset($aParams['empty_message'])) {
  839. $bEmptyMessage = (bool)$aParams['empty_message'];
  840. unset($aParams['empty_message']);
  841. }
  842. $bAjaxPaginate = true;
  843. if(isset($aParams['ajax_paginate'])) {
  844. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  845. unset($aParams['ajax_paginate']);
  846. }
  847. return $this->_serviceBrowse ('context_joined_entries', ['context' => $iContextPid, 'joined_profile' => $iProfileId], BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  848. }
  849. public function serviceBrowseFollowedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  850. {
  851. if (!$iProfileId)
  852. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  853. if (!$iProfileId)
  854. return '';
  855. return $this->_serviceBrowse ('followed_entries', array('followed_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  856. }
  857. public function serviceBrowseCreatedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  858. {
  859. if (!$iProfileId)
  860. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  861. if (!$iProfileId)
  862. return '';
  863. return $this->_serviceBrowse ('created_entries', array('author' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  864. }
  865. public function serviceBrowseRecommendationsFans ($iProfileId = 0, $aParams = [])
  866. {
  867. $CNF = &$this->_oConfig->CNF;
  868. if($this->_bIsApi)
  869. $aParams = bx_api_get_browse_params($aParams, true);
  870. if(!$iProfileId)
  871. $iProfileId = bx_get_logged_profile_id();
  872. if(!$iProfileId)
  873. return '';
  874. $aParams = array_merge([
  875. 'empty_message' => false,
  876. 'start' => 0,
  877. 'per_page' => 0
  878. ], $aParams);
  879. if(($iStartGet = bx_get('start')) !== false)
  880. $aParams['start'] = (int)$iStartGet;
  881. if(($iPerPageGet = bx_get('per_page')) !== false)
  882. $aParams['per_page'] = (int)$iPerPageGet;
  883. $oRecommendation = BxDolRecommendation::getObjectInstance($CNF['OBJECT_RECOMMENDATIONS_FANS']);
  884. if(!$oRecommendation)
  885. return false;
  886. if($this->_bIsApi) {
  887. $aData = $oRecommendation->getCodeAPI($iProfileId, $aParams);
  888. $aData = array_merge($aData, [
  889. 'module' => 'system',
  890. 'unit' => 'mixed',
  891. 'request_url' => '/api.php?r=' . $this->getName() .'/browse_recommendations_fans&params[]=' . $iProfileId . '&params[]='
  892. ]);
  893. return [bx_api_get_block('browse', $aData)];
  894. }
  895. $sCode = $oRecommendation->getCode($iProfileId, $aParams);
  896. if(!$sCode && $aParams['empty_message'])
  897. $sCode = MsgBox(_t(!empty($aParams['empty_message_text']) ? $aParams['empty_message_text'] : '_Empty'));
  898. return $sCode;
  899. }
  900. public function serviceBrowseMembers ($iProfileId = 0, $aParams = [])
  901. {
  902. $CNF = &$this->_oConfig->CNF;
  903. if(!$iProfileId)
  904. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  905. if(!$iProfileId)
  906. return '';
  907. $oGroupProfile = BxDolProfile::getInstance($iProfileId);
  908. if(!($oGroupProfile))
  909. return '';
  910. return bx_srv('system', 'browse_members', [$oGroupProfile->id(), $CNF['OBJECT_CONNECTIONS'], $aParams], 'TemplServiceProfiles');
  911. }
  912. public function serviceEntityEditQuestionnaire($iProfileId = 0)
  913. {
  914. $CNF = &$this->_oConfig->CNF;
  915. if(!$iProfileId)
  916. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  917. if(!$iProfileId)
  918. return '';
  919. $aProfileInfo = BxDolProfileQuery::getInstance()->getInfoById($iProfileId);
  920. if(empty($aProfileInfo) || !is_array($aProfileInfo))
  921. return '';
  922. $aContentInfo = $this->_oDb->getContentInfoById($aProfileInfo['content_id']);
  923. if(empty($aContentInfo) || !is_array($aContentInfo))
  924. return '';
  925. if($this->checkAllowedEdit($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  926. return MsgBox(_t('_Access denied'));
  927. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_QUESTIONS_MANAGE']);
  928. if(!$oGrid)
  929. return '';
  930. if($this->_bIsApi){
  931. return [
  932. bx_api_get_block('grid', $oGrid->getCodeAPI())
  933. ];
  934. }
  935. return $oGrid->getCode();
  936. }
  937. public function serviceEntityPricing($iProfileId = 0)
  938. {
  939. $CNF = &$this->_oConfig->CNF;
  940. if(!$iProfileId)
  941. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  942. if(!$iProfileId)
  943. return $this->_bIsApi ? [] : '';
  944. if(!$this->_oConfig->isPaidJoin())
  945. return $this->_bIsApi ? [] : '';
  946. $oPayments = BxDolPayments::getInstance();
  947. if(!$oPayments->isActive())
  948. return ($sMsg = _t('_sys_payments_err_no_payments')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox($sMsg);
  949. if($this->checkAllowedUsePaidJoin() !== CHECK_ACTION_RESULT_ALLOWED)
  950. return ($sMsg = _t('_Access denied')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox($sMsg);
  951. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  952. return ($sMsg = _t('_Access denied')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox($sMsg);
  953. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_MANAGE']);
  954. if(!$oGrid)
  955. return '';
  956. $sNote = '';
  957. if(!$oPayments->isAcceptingPayments($this->_iProfileId))
  958. $sNote = MsgBox(_t('_sys_payments_err_not_accept_payments', $oPayments->getDetailsUrl()));
  959. if($this->_bIsApi) {
  960. $aBlocks = [];
  961. if(!empty($sNote))
  962. $aBlocks[] = bx_api_get_msg($sNote);
  963. $aBlocks[] = bx_api_get_block('grid', $oGrid->getCodeAPI(true));
  964. return $aBlocks;
  965. }
  966. return $sNote . $oGrid->getCode();
  967. }
  968. public function serviceEntityJoin($iProfileId = 0)
  969. {
  970. $CNF = &$this->_oConfig->CNF;
  971. if(!$iProfileId)
  972. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  973. if(!$iProfileId)
  974. return $this->_bIsApi ? [] : '';
  975. if(!$this->_oConfig->isPaidJoin())
  976. return $this->_bIsApi ? [] : '';
  977. if($this->_bIsApi && !$this->isPaidJoinByProfile($iProfileId))
  978. return [];
  979. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_VIEW']);
  980. if(!$oGrid)
  981. return $this->_bIsApi ? [] : '';
  982. $oGrid->setProfileId($iProfileId);
  983. if($this->_bIsApi) {
  984. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
  985. return [
  986. bx_api_get_block('pricing', array_merge_recursive($oGrid->getCodeAPI(true), ['settings' => [
  987. 'context_name' => $aContentInfo[$CNF['FIELD_TITLE']],
  988. 'unit' => 'productlist'
  989. ]]))
  990. ];
  991. }
  992. return $oGrid->getCode();
  993. }
  994. public function serviceEntityInvite ($iContentId = 0, $bErrorMsg = true)
  995. {
  996. $CNF = &$this->_oConfig->CNF;
  997. if(!isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE']))
  998. return false;
  999. return $this->_serviceEntityForm ('inviteForm', $iContentId, false, false, $bErrorMsg);
  1000. }
  1001. /**
  1002. * Entry social sharing block
  1003. */
  1004. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  1005. {
  1006. if(!empty($mixedContent)) {
  1007. if(!is_array($mixedContent))
  1008. $mixedContent = array((int)$mixedContent, array());
  1009. }
  1010. else {
  1011. $mixedContent = $this->_getContent();
  1012. if($mixedContent === false)
  1013. return false;
  1014. }
  1015. list($iContentId, $aContentInfo) = $mixedContent;
  1016. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  1017. if(!$oGroupProfile)
  1018. return false;
  1019. return parent::serviceEntitySocialSharing(array($iContentId, $aContentInfo), array(
  1020. 'title' => $oGroupProfile->getDisplayName()
  1021. ));
  1022. }
  1023. public function serviceIsPricingAvaliable($iProfileId)
  1024. {
  1025. if(!$this->_oConfig->isPaidJoin())
  1026. return false;
  1027. if($this->checkAllowedUsePaidJoin() !== CHECK_ACTION_RESULT_ALLOWED)
  1028. return false;
  1029. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  1030. return false;
  1031. return true;
  1032. }
  1033. public function serviceIsPaidJoinAvaliable($iGroupProfileId, $iProfileId = 0)
  1034. {
  1035. return $this->isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId);
  1036. }
  1037. public function serviceIsPaidJoinAvaliableByContent($iGroupContentId, $iProfileId = 0)
  1038. {
  1039. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName());
  1040. if(!$oGroupProfile)
  1041. return false;
  1042. return $this->isPaidJoinByProfileForProfile($oGroupProfile->id(), $iProfileId);
  1043. }
  1044. public function serviceIsFreeJoinAvaliable($iGroupProfileId, $iProfileId = 0)
  1045. {
  1046. return !$this->isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId);
  1047. }
  1048. public function serviceIsFreeJoinAvaliableByContent($iGroupContentId, $iProfileId = 0)
  1049. {
  1050. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName());
  1051. if(!$oGroupProfile)
  1052. return false;
  1053. return !$this->isPaidJoinByProfileForProfile($oGroupProfile->id(), $iProfileId);
  1054. }
  1055. /**
  1056. * Is Paid Join enabled in the group and whether a profile can use it.
  1057. *
  1058. * @param type $iGroupProfileId - Group profile ID.
  1059. * @param type $iProfileId - Profile ID of the user who wants to join.
  1060. * @return boolean
  1061. */
  1062. public function isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId = 0)
  1063. {
  1064. $CNF = &$this->_oConfig->CNF;
  1065. if(empty($iProfileId))
  1066. $iProfileId = $this->_iProfileId;
  1067. if(BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->isConnected($iProfileId, $iGroupProfileId))
  1068. return false;
  1069. return $this->isPaidJoinByProfile($iGroupProfileId);
  1070. }
  1071. /**
  1072. * Is Paid Join enabled as is and whether a group has pricing plans added.
  1073. *
  1074. * @param type $iGroupProfileId - Group profile ID.
  1075. * @return boolean
  1076. */
  1077. public function isPaidJoinByProfile($iGroupProfileId)
  1078. {
  1079. if(!$this->_oConfig->isPaidJoin())
  1080. return false;
  1081. $aPrices = $this->_oDb->getPrices(['type' => 'by_profile_id', 'profile_id' => $iGroupProfileId, 'active' => 1]);
  1082. if(empty($aPrices) || !is_array($aPrices))
  1083. return false;
  1084. return true;
  1085. }
  1086. /**
  1087. * Integration with Payments.
  1088. */
  1089. public function serviceGetPaymentData()
  1090. {
  1091. return $this->_aModule;
  1092. }
  1093. public function serviceGetCartItem($mixedItemId)
  1094. {
  1095. $CNF = &$this->_oConfig->CNF;
  1096. if(!$mixedItemId)
  1097. return [];
  1098. if(is_numeric($mixedItemId))
  1099. $aItem = $this->_oDb->getPrices(['type' => 'by_id', 'value' => (int)$mixedItemId]);
  1100. else
  1101. $aItem = $this->_oDb->getPrices(['type' => 'by_name', 'value' => $mixedItemId]);
  1102. if(empty($aItem) || !is_array($aItem))
  1103. return [];
  1104. if(!$this->isPaidJoinByProfile($aItem['profile_id']))
  1105. return [];
  1106. $oGroupProfile = BxDolProfile::getInstance($aItem['profile_id']);
  1107. if(!$oGroupProfile)
  1108. return [];
  1109. $aGroupProfile = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1110. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  1111. $sTitle = '';
  1112. if(!empty($aItem['period']) && !empty($aItem['period_unit']))
  1113. $sTitle = _t($CNF['T']['txt_cart_item_title'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']], $aItem['period'], $aItem['period_unit']);
  1114. else
  1115. $sTitle = _t($CNF['T']['txt_cart_item_title_lifetime'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']]);
  1116. return [
  1117. 'id' => $aItem['id'],
  1118. 'author_id' => $aGroupProfile[$CNF['FIELD_AUTHOR']],
  1119. 'name' => $aItem['name'],
  1120. 'title' => $sTitle,
  1121. 'description' => '',
  1122. 'url' => $oGroupProfile->getUrl(),
  1123. 'price_single' => $aItem['price'],
  1124. 'price_recurring' => $aItem['price'],
  1125. 'period_recurring' => $aItem['period'],
  1126. 'period_unit_recurring' => $aItem['period_unit'],
  1127. 'trial_recurring' => 0,
  1128. 'added' => $aItem['added']
  1129. ];
  1130. }
  1131. public function serviceGetCartItems($iSellerId)
  1132. {
  1133. $CNF = &$this->_oConfig->CNF;
  1134. if(empty($iSellerId))
  1135. return array();
  1136. $sModule = $this->getName();
  1137. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  1138. $aGroups = $this->_oDb->getEntriesBy(array('type' => 'author', 'author' => $iSellerId));
  1139. $aResult = array();
  1140. foreach($aGroups as $aGroup) {
  1141. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aGroup[$CNF['FIELD_ID']], $sModule);
  1142. if(!$oGroupProfile)
  1143. continue;
  1144. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $oGroupProfile->id()));
  1145. if(empty($aPrices) || !is_array($aPrices))
  1146. continue;
  1147. $sTitle = $oGroupProfile->getDisplayName();
  1148. $sUrl = $oGroupProfile->getUrl();
  1149. foreach($aPrices as $aPrice)
  1150. $aResult[] = array(
  1151. 'id' => $aPrice['id'],
  1152. 'author_id' => $iSellerId,
  1153. 'name' => $aPrice['name'],
  1154. 'title' => _t($CNF['T']['txt_cart_item_title'], $sTitle, $aRoles[$aPrice['role_id']], $aPrice['period'], $aPrice['period_unit']),
  1155. 'description' => '',
  1156. 'url' => $sUrl,
  1157. 'price_single' => $aPrice['price'],
  1158. 'price_recurring' => $aPrice['price'],
  1159. 'period_recurring' => $aPrice['period'],
  1160. 'period_unit_recurring' => $aPrice['period_unit']
  1161. );
  1162. }
  1163. return $aResult;
  1164. }
  1165. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1166. {
  1167. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  1168. }
  1169. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1170. {
  1171. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  1172. }
  1173. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  1174. {
  1175. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  1176. }
  1177. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  1178. {
  1179. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  1180. }
  1181. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1182. {
  1183. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  1184. }
  1185. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1186. {
  1187. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  1188. }
  1189. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  1190. {
  1191. return true;
  1192. }
  1193. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  1194. {
  1195. $CNF = &$this->_oConfig->CNF;
  1196. $aItem = $this->serviceGetCartItem($iItemId);
  1197. if(empty($aItem) || !is_array($aItem))
  1198. return array();
  1199. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  1200. $mixedPeriod = false;
  1201. if((int)$aItemInfo['period'] != 0)
  1202. $mixedPeriod = array(
  1203. 'period' => (int)$aItemInfo['period'],
  1204. 'period_unit' => $aItemInfo['period_unit'],
  1205. 'period_reserve' => $CNF['PARAM_RECURRING_RESERVE']
  1206. );
  1207. if(!$this->setRole($aItemInfo['profile_id'], $iClientId, $aItemInfo['role_id'], $mixedPeriod, $sOrder))
  1208. return array();
  1209. return $aItem;
  1210. }
  1211. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  1212. {
  1213. $aItem = $this->serviceGetCartItem($iItemIdNew);
  1214. if(empty($aItem) || !is_array($aItem))
  1215. return array();
  1216. $aItemInfoOld = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdOld));
  1217. if(empty($aItemInfoOld) || !is_array($aItemInfoOld))
  1218. return array();
  1219. $aItemInfoNew = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdNew));
  1220. if(empty($aItemInfoNew) || !is_array($aItemInfoNew))
  1221. return array();
  1222. if(!$this->unsetRole($aItemInfoOld['profile_id'], $iClientId))
  1223. return array();
  1224. $aResult = $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemIdNew, 1, $sOrder);
  1225. if(empty($aResult) || !is_array($aResult))
  1226. return array();
  1227. return $aItem;
  1228. }
  1229. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  1230. {
  1231. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  1232. if(empty($aItemInfo) || !is_array($aItemInfo))
  1233. return false;
  1234. return $this->unsetRole($aItemInfo['profile_id'], $iClientId);
  1235. }
  1236. public function serviceGetQuestionnaire($sSource, $sObject, $sAction, $iContentProfileId)
  1237. {
  1238. $CNF = &$this->_oConfig->CNF;
  1239. $sMsg = _t('_sys_txt_not_found');
  1240. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iContentProfileId);
  1241. if(empty($aContentInfo) || !is_array($aContentInfo))
  1242. return $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['code' => 1, 'msg' => $sMsg];
  1243. $oForm = null;
  1244. if(($oConnection = BxDolConnection::getObjectInstance($sObject)) !== false) {
  1245. $oForm = $oConnection->getQuestionnaireForm($sAction, $iContentProfileId, ['request' => [
  1246. 's' => $sSource,
  1247. ]]);
  1248. if($oForm === false)
  1249. return $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['code' => 3, 'msg' => $sMsg];
  1250. }
  1251. else
  1252. return $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['code' => 2, 'msg' => $sMsg];
  1253. $oForm->initChecker();
  1254. if($oForm->isSubmittedAndValid()) {
  1255. $iProfileId = bx_get_logged_profile_id();
  1256. $aQuestions = $this->_oDb->getQuestions(['sample' => 'content_pid', 'content_pid' => $iContentProfileId]);
  1257. foreach($aQuestions as $aQuestion)
  1258. $this->_oDb->insertAnswer((int)$aQuestion['id'], $iProfileId, $oForm->getCleanValue('question_' . $aQuestion['id']));
  1259. return $this->_bIsApi ? [] : ['code' => 0, 'o' => $sObject, 'a' => $sAction, 'cpi' => $iContentProfileId, 'ci' => $aContentInfo[$CNF['FIELD_ID']], 'eval' => $this->_oConfig->getJsObject($sSource) . '.connActionPerformed(oData)'];
  1260. }
  1261. if($this->_bIsApi)
  1262. return [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => [
  1263. 'name' => $this->_oModule->getName(),
  1264. 'request' => ['url' => $oForm->aFormAttrs['action'], 'immutable' => true]]
  1265. ])];
  1266. bx_import('BxTemplFunctions');
  1267. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oConfig->getHtmlIds('popup_questionnaire'), _t($CNF['T']['popup_title_questionnaire']), $this->_oTemplate->parseHtmlByName('popup_qnr_questionnaire.html', [
  1268. 'form_id' => $oForm->getId(),
  1269. 'form' => $oForm->getCode(true),
  1270. ]));
  1271. return ['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]];
  1272. }
  1273. /**
  1274. * Data for Notifications module
  1275. */
  1276. public function serviceGetNotificationsData()
  1277. {
  1278. $sModule = $this->_aModule['name'];
  1279. $aSettingsTypes = ['follow_member', 'follow_context'];
  1280. if($this->serviceActAsProfile())
  1281. $aSettingsTypes = ['personal', 'follow_member'];
  1282. return [
  1283. 'handlers' => [
  1284. ['group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'],
  1285. ['group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'],
  1286. ['group' => $sModule . '_score_up', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteUp', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_up', 'module_class' => 'Module'],
  1287. ['group' => $sModule . '_score_down', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteDown', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_down', 'module_class' => 'Module'],
  1288. ['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'],
  1289. ['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'],
  1290. ['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']],
  1291. ['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'],
  1292. //--- Moderation related: For 'admins'.
  1293. ['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'],
  1294. ['group' => $sModule . '_object_reported', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'reported_content', 'module_name' => $sModule, 'module_method' => 'get_notifications_post_reported', 'module_class' => 'Module'],
  1295. ],
  1296. 'settings' => [
  1297. ['group' => 'vote', 'unit' => $sModule, 'action' => 'doVote', 'types' => $aSettingsTypes],
  1298. ['group' => 'score_up', 'unit' => $sModule, 'action' => 'doVoteUp', 'types' => $aSettingsTypes],
  1299. ['group' => 'score_down', 'unit' => $sModule, 'action' => 'doVoteDown', 'types' => $aSettingsTypes],
  1300. ['group' => 'fan', 'unit' => $sModule, 'action' => 'fan_added', 'types' => $aSettingsTypes],
  1301. ['group' => 'invite', 'unit' => $sModule, 'action' => 'join_invitation_notif', 'types' => ['personal']],
  1302. ['group' => 'join', 'unit' => $sModule, 'action' => 'join_request', 'types' => $aSettingsTypes],
  1303. ['group' => 'timeline_post', 'unit' => $sModule, 'action' => 'timeline_post_common', 'types' => $aSettingsTypes],
  1304. //--- Moderation related: For 'admins'.
  1305. ['group' => 'action_required', 'unit' => $sModule, 'action' => 'pending_approval', 'types' => ['personal']],
  1306. ['group' => 'action_required', 'unit' => $sModule, 'action' => 'reported_content', 'types' => ['personal']],
  1307. ],
  1308. 'alerts' => [
  1309. ['unit' => $sModule, 'action' => 'doVote'],
  1310. ['unit' => $sModule, 'action' => 'undoVote'],
  1311. ['unit' => $sModule, 'action' => 'doVoteUp'],
  1312. ['unit' => $sModule, 'action' => 'doVoteDown'],
  1313. ['unit' => $sModule, 'action' => 'fan_added'],
  1314. ['unit' => $sModule, 'action' => 'join_invitation_notif'],
  1315. ['unit' => $sModule, 'action' => 'join_request'],
  1316. ['unit' => $sModule, 'action' => 'timeline_post_common'],
  1317. //--- Moderation related: For 'admins'.
  1318. ['unit' => $sModule, 'action' => 'pending_approval'],
  1319. ['unit' => $sModule, 'action' => 'reported_content'],
  1320. ]
  1321. ];
  1322. }
  1323. public function serviceGetNotificationsInsertData($oAlert, $aHandler, $aDataItems)
  1324. {
  1325. if($oAlert->sAction != 'join_invitation_notif' || empty($aDataItems) || !is_array($aDataItems))
  1326. return $aDataItems;
  1327. foreach($aDataItems as $iIndex => $aDataItem)
  1328. $aDataItems[$iIndex]['object_privacy_view'] = BX_DOL_PG_ALL;
  1329. return $aDataItems;
  1330. }
  1331. /**
  1332. * Notification about new invitation to join the group
  1333. */
  1334. public function serviceGetNotificationsJoinInvitation($aEvent)
  1335. {
  1336. $CNF = &$this->_oConfig->CNF;
  1337. $iContentId = (int)$aEvent['object_id'];
  1338. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  1339. if(!$oGroupProfile)
  1340. return array();
  1341. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1342. if(empty($aContentInfo) || !is_array($aContentInfo))
  1343. return array();
  1344. /*
  1345. * It's essential that 'object_owner_id' contains invited member profile id.
  1346. */
  1347. $oProfile = BxDolProfile::getInstance((int)$aEvent['object_owner_id']);
  1348. if(!$oProfile)
  1349. return array();
  1350. /*
  1351. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  1352. * because Subentry URL has higher display priority and notification
  1353. * should be linked to Group Profile (Group Profile -> Members tab)
  1354. * instead of Personal Profile of invited member.
  1355. */
  1356. $sEntryUrl = bx_absolute_url(str_replace(BX_DOL_URL_ROOT, '', $oGroupProfile->getUrl()), '{bx_url_root}');
  1357. return array(
  1358. 'entry_sample' => $CNF['T']['txt_sample_single'],
  1359. 'entry_url' => $sEntryUrl,
  1360. 'entry_caption' => $oGroupProfile->getDisplayName(),
  1361. 'subentry_sample' => $oProfile->getDisplayName(),
  1362. 'subentry_url' => $sEntryUrl,
  1363. 'lang_key' => $this->_oConfig->CNF['T']['txt_ntfs_join_invitation']
  1364. );
  1365. }
  1366. /**
  1367. * Notification about new member requst in the group
  1368. */
  1369. public function serviceGetNotificationsJoinRequest($aEvent)
  1370. {
  1371. return $this->_serviceGetNotification($aEvent, 'join_request', $this->_oConfig->CNF['T']['txt_ntfs_join_request']);
  1372. }
  1373. /**
  1374. * Notification about new member in the group
  1375. */
  1376. public function serviceGetNotificationsFanAdded($aEvent)
  1377. {
  1378. return $this->_serviceGetNotification($aEvent, 'fan_added', $this->_oConfig->CNF['T']['txt_ntfs_fan_added']);
  1379. }
  1380. protected function _serviceGetNotification($aEvent, $sType, $sLangKey)
  1381. {
  1382. $CNF = &$this->_oConfig->CNF;
  1383. $iContentId = (int)$aEvent['object_id'];
  1384. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  1385. if(!$oGroupProfile)
  1386. return array();
  1387. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1388. if(empty($aContentInfo) || !is_array($aContentInfo))
  1389. return array();
  1390. $oProfile = BxDolProfile::getInstance((int)$aEvent['subobject_id']);
  1391. if(!$oProfile)
  1392. return array();
  1393. $iGroupProfileId = $oGroupProfile->id();
  1394. /*
  1395. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  1396. * because Subentry URL has higher display priority and notification
  1397. * should be linked to Group Profile (Group Profile -> Members tab or Manage page)
  1398. * instead of Personal Profile of a member, who performed an action.
  1399. */
  1400. if($sType == 'join_request' && !empty($CNF['URL_ENTRY_MANAGE']))
  1401. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_MANAGE'], [
  1402. 'profile_id' => $iGroupProfileId
  1403. ]), '{bx_url_root}');
  1404. else if(!empty($CNF['URL_ENTRY_FANS']))
  1405. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_FANS'], [
  1406. 'profile_id' => $iGroupProfileId
  1407. ]), '{bx_url_root}');
  1408. else
  1409. $sEntryUrl = bx_absolute_url(str_replace(BX_DOL_URL_ROOT, '', $oGroupProfile->getUrl()), '{bx_url_root}');
  1410. return [
  1411. 'entry_sample' => $CNF['T']['txt_sample_single'],
  1412. 'entry_url' => $sEntryUrl,
  1413. 'entry_caption' => $oGroupProfile->getDisplayName(),
  1414. 'entry_author' => $iGroupProfileId,
  1415. 'subentry_sample' => $oProfile->getDisplayName(),
  1416. 'subentry_url' => $sEntryUrl,
  1417. 'lang_key' => $sLangKey
  1418. ];
  1419. }
  1420. /**
  1421. * Data for Reputation module
  1422. */
  1423. public function serviceGetReputationData()
  1424. {
  1425. $sModule = $this->_aModule['name'];
  1426. $aResult = parent::serviceGetReputationData();
  1427. $bHandlers = !empty($aResult['handlers']) && is_array($aResult['handlers']);
  1428. $bAlerts = !empty($aResult['alerts']) && is_array($aResult['alerts']);
  1429. /**
  1430. * Add Connections related handlers/alerts.
  1431. */
  1432. if($bHandlers)
  1433. $aResult['handlers'] = array_merge($aResult['handlers'], [
  1434. ['group' => $sModule . '_fan', 'type' => 'insert', 'alert_unit' => $sModule . '_fans', 'alert_action' => 'connection_added', 'points_active' => 1, 'points_passive' => 0],
  1435. ['group' => $sModule . '_fan', 'type' => 'delete', 'alert_unit' => $sModule . '_fans', 'alert_action' => 'connection_removed', 'points_active' => -1, 'points_passive' => 0]
  1436. ]);
  1437. if($bAlerts)
  1438. $aResult['alerts'] = array_merge($aResult['alerts'], [
  1439. ['unit' => $sModule . '_fans', 'action' => 'connection_added'],
  1440. ['unit' => $sModule . '_fans', 'action' => 'connection_removed']
  1441. ]);
  1442. /**
  1443. * Remove Comments and Reactions related handlers/alerts because these actions aren't available in Contexts for now.
  1444. */
  1445. if($bHandlers)
  1446. foreach($aResult['handlers'] as $iKey => $aHandler)
  1447. if(in_array($aHandler['group'], [$sModule . '_comment', $sModule . '_reaction']))
  1448. unset($aResult['handlers'][$iKey]);
  1449. if($bAlerts)
  1450. foreach($aResult['alerts'] as $iKey => $aAlert)
  1451. if($aAlert['unit'] == $sModule . '_reactions' || in_array($aAlert['action'], ['commentPost', 'commentRemoved']))
  1452. unset($aResult['alerts'][$iKey]);
  1453. return $aResult;
  1454. }
  1455. /**
  1456. * Data for Timeline module
  1457. */
  1458. public function serviceGetTimelineData()
  1459. {
  1460. $aResult = BxBaseModGeneralModule::serviceGetTimelineData();
  1461. $sModule = $this->_aModule['name'];
  1462. $aResult['handlers'] = array_merge($aResult['handlers'], [
  1463. ['group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'context_cover_changed'],
  1464. ['group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'context_cover_deleted']
  1465. ]);
  1466. $aResult['alerts'] = array_merge($aResult['alerts'], [
  1467. ['unit' => $sModule, 'action' => 'context_cover_changed'],
  1468. ['unit' => $sModule, 'action' => 'context_cover_deleted']
  1469. ]);
  1470. return $aResult;
  1471. }
  1472. /**
  1473. * Entry post for Timeline module
  1474. */
  1475. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  1476. {
  1477. $CNF = &$this->_oConfig->CNF;
  1478. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  1479. if($a === false)
  1480. return false;
  1481. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  1482. $a['content']['url'] = $oGroupProfile->getUrl();
  1483. $a['content']['title'] = $oGroupProfile->getDisplayName();
  1484. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1485. if($oConnection)
  1486. $a['content']['members'] = $oConnection->getConnectedInitiatorsCount($oGroupProfile->id(), true);
  1487. if(isset($aEvent['object_privacy_view']))
  1488. $a['content']['visibility'] = $aEvent['object_privacy_view'];
  1489. if(isset($CNF['FIELD_PUBLISHED'])) {
  1490. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  1491. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $a['date'])
  1492. $a['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  1493. }
  1494. return $a;
  1495. }
  1496. // ====== PERMISSION METHODS
  1497. /**
  1498. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1499. */
  1500. public function checkAllowedUsePaidJoin($isPerformAction = false)
  1501. {
  1502. // check ACL
  1503. $aCheck = checkActionModule($this->_iProfileId, 'use paid join', $this->getName(), $isPerformAction);
  1504. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  1505. return $aCheck[CHECK_ACTION_MESSAGE];
  1506. return CHECK_ACTION_RESULT_ALLOWED;
  1507. }
  1508. /**
  1509. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1510. */
  1511. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  1512. {
  1513. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  1514. }
  1515. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  1516. {
  1517. $CNF = &$this->_oConfig->CNF;
  1518. $iGroupContentId = (int)$aDataEntry[$CNF['FIELD_ID']];
  1519. $bInvited = false;
  1520. if(!empty($CNF['TABLE_INVITES'])) {
  1521. $iGroupProfileId = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName())->id();
  1522. if(($sKey = bx_get('key')) !== false) {
  1523. $mixedInvited = $this->isInvited($sKey, $iGroupProfileId);
  1524. if($mixedInvited === true)
  1525. $bInvited = true;
  1526. }
  1527. else {
  1528. $mixedInvited = $this->isInvitedByProfileId($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $iGroupProfileId);
  1529. if($mixedInvited === true)
  1530. $bInvited = true;
  1531. }
  1532. }
  1533. if ($this->isFan($iGroupContentId, $iProfileId) || $bInvited)
  1534. return CHECK_ACTION_RESULT_ALLOWED;
  1535. return parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId);
  1536. }
  1537. /**
  1538. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1539. */
  1540. public function checkAllowedCompose(&$aDataEntry, $isPerformAction = false)
  1541. {
  1542. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  1543. return _t('_sys_txt_access_denied');
  1544. return parent::checkAllowedCompose ($aDataEntry, $isPerformAction);
  1545. }
  1546. public function checkAllowedFans(&$aDataEntry, $isPerformAction = false)
  1547. {
  1548. $CNF = &$this->_oConfig->CNF;
  1549. if(($sMsg = $this->checkAllowedView($aDataEntry)) !== CHECK_ACTION_RESULT_ALLOWED)
  1550. return $sMsg;
  1551. return $this->_checkAllowedConnect($aDataEntry, $isPerformAction, [$CNF['OBJECT_GRID_CONNECTIONS'], 'checkAllowedConnectByAcl'], false, false);
  1552. }
  1553. /**
  1554. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1555. */
  1556. public function checkAllowedFanAdd(&$aDataEntry, $isPerformAction = false)
  1557. {
  1558. $mixedResult = $this->_modGroupsCheckAllowedFanAdd($aDataEntry, $isPerformAction);
  1559. /**
  1560. * @hooks
  1561. * @hookdef hook-system-check_allowed_fan_add 'system', 'check_allowed_fan_add' - hook to override the result of checking whether 'fan add' to context action is allowed or not to currently logged in user
  1562. * - $unit_name - equals `system`
  1563. * - $action - equals `check_allowed_fan_add`
  1564. * - $object_id - not used
  1565. * - $sender_id - not used
  1566. * - $extra_params - array of additional params with the following array keys:
  1567. * - `module` - [string] module name
  1568. * - `content_info` - [array] context info array as key&value pairs
  1569. * - `profile_id` - [int] currently logged in profile id to be checked the availability of the action to
  1570. * - `override_result` - [string] or [int] by ref, check action result, can be overridden in hook processing. Return string with an error if action isn't allowed or CHECK_ACTION_RESULT_ALLOWED, @see BxDolAcl
  1571. * @hook @ref hook-system-check_allowed_fan_add
  1572. */
  1573. bx_alert('system', 'check_allowed_fan_add', 0, 0, [
  1574. 'module' => $this->getName(),
  1575. 'content_info' => $aDataEntry,
  1576. 'profile_id' => bx_get_logged_profile_id(),
  1577. 'override_result' => &$mixedResult
  1578. ]);
  1579. return $mixedResult;
  1580. }
  1581. public function _modGroupsCheckAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  1582. {
  1583. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) || !isLogged())
  1584. return _t('_sys_txt_access_denied');
  1585. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, false);
  1586. }
  1587. /**
  1588. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  1589. */
  1590. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  1591. {
  1592. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  1593. return CHECK_ACTION_RESULT_ALLOWED;
  1594. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  1595. }
  1596. protected function _checkAllowedActionByFan($sAction, $aDataEntry, $iProfileId = 0)
  1597. {
  1598. $CNF = &$this->_oConfig->CNF;
  1599. $bRoles = $this->_oConfig->isRoles();
  1600. if(empty($iProfileId))
  1601. $iProfileId = bx_get_logged_profile_id();
  1602. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  1603. if(!$oGroupProfile)
  1604. return $sAction == BX_BASE_MOD_GROUPS_ACTION_DELETE ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_not_found');
  1605. $iGroupProfileId = $oGroupProfile->id();
  1606. if(!$bRoles && $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aDataEntry))
  1607. return CHECK_ACTION_RESULT_ALLOWED;
  1608. if($bRoles && $this->isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId))
  1609. return CHECK_ACTION_RESULT_ALLOWED;
  1610. return _t('_sys_txt_access_denied');
  1611. }
  1612. public function isAllowedActionByRole($mAction, $aDataEntry, $iGroupProfileId, $iProfileId)
  1613. {
  1614. $iProfileRole = $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  1615. if (is_array($mAction)) {
  1616. $sAction = $mAction['action'];
  1617. $sActionModule = $mAction['module'];
  1618. } else {
  1619. $sAction = $mAction;
  1620. $sActionModule = $this->getName();
  1621. }
  1622. $bResult = false;
  1623. if($iProfileId)
  1624. $bResult = $this->isAllowedModuleActionByRole($sActionModule, $sAction, $iProfileRole);
  1625. // in case neither of the profile's roles are having permissions set explicitly then fallback to an old way
  1626. if ($bResult === NULL) {
  1627. $bResult = false;
  1628. if($this->isFanByGroupProfileId($iGroupProfileId)) {
  1629. switch ($sAction) {
  1630. case BX_BASE_MOD_GROUPS_ACTION_DELETE:
  1631. case BX_BASE_MOD_GROUPS_ACTION_EDIT:
  1632. case BX_BASE_MOD_GROUPS_ACTION_CHANGE_SETTINGS:
  1633. case BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER:
  1634. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  1635. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR))
  1636. $bResult = true;
  1637. break;
  1638. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS:
  1639. case BX_BASE_MOD_GROUPS_ACTION_INVITE:
  1640. case BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT:
  1641. case BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT:
  1642. case BX_BASE_MOD_GROUPS_ACTION_TIMELINE_POST_PIN:
  1643. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR) || $this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_MODERATOR))
  1644. $bResult = true;
  1645. break;
  1646. default:
  1647. $bResult = true;
  1648. }
  1649. }
  1650. }
  1651. // in case current user is not allowed to edit/delete this group then
  1652. // if it is a subgroup inside a context then give to the admin roles of a parent context the ability to edit/delete that subgroup.
  1653. if (
  1654. !$bResult &&
  1655. ($sAction == BX_BASE_MOD_GROUPS_ACTION_EDIT || $sAction == BX_BASE_MOD_GROUPS_ACTION_DELETE) &&
  1656. isset($this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']) &&
  1657. $this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO'] &&
  1658. isset($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']]) &&
  1659. $aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']] < 0)
  1660. {
  1661. $iParentContextProfileId = -$aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']];
  1662. $oParentContext = BxDolProfile::getInstance($iParentContextProfileId);
  1663. if ($oParentContext) {
  1664. $sCheckAction = $sAction == BX_BASE_MOD_GROUPS_ACTION_EDIT ? BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT : BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT;
  1665. $aParentDataEntry = bx_srv($oParentContext->getModule(), 'get_info', [$oParentContext->getContentId(), false]);
  1666. return $this->isAllowedActionByRole(['action' => $sCheckAction, 'module' => $oParentContext->getModule()], $aParentDataEntry, $iParentContextProfileId, $iProfileId);
  1667. }
  1668. }
  1669. /**
  1670. * @hooks
  1671. * @hookdef hook-system-check_allowed_action_by_role 'system', 'check_allowed_action_by_role' - hook to override the result of checking whether an action is allowed or not to context member by his role in the context
  1672. * - $unit_name - equals `system`
  1673. * - $action - equals `check_allowed_action_by_role`
  1674. * - $object_id - not used
  1675. * - $sender_id - not used
  1676. * - $extra_params - array of additional params with the following array keys:
  1677. * - `module` - [string] module name
  1678. * - `multi_roles` - [boolean] whether multi roles are enabled in context or not
  1679. * - `action` - [string] action to be checked
  1680. * - `action_module` - [string] module name which the action belongs to
  1681. * - `content_profile_id` - [int] context profile id
  1682. * - `content_info` - [array] context info array as key&value pairs
  1683. * - `profile_id` - [int] profile id to be checked the availability of the action to
  1684. * - `profile_role` - [int] profile role in the context
  1685. * - `override_result` - [boolean] by ref, check action result, can be overridden in hook processing.
  1686. * @hook @ref hook-system-check_allowed_action_by_role
  1687. */
  1688. bx_alert('system', 'check_allowed_action_by_role', 0, 0, [
  1689. 'module' => $this->getName(),
  1690. 'multi_roles' => $this->_oConfig->isMultiRoles(),
  1691. 'action' => $sAction,
  1692. 'action_module' => $sActionModule,
  1693. 'content_profile_id' => $iGroupProfileId,
  1694. 'content_info' => $aDataEntry,
  1695. 'profile_id' => $iProfileId,
  1696. 'profile_role' => $iProfileRole,
  1697. 'override_result' => &$bResult
  1698. ]);
  1699. return $bResult;
  1700. }
  1701. public function isAllowedModuleActionByRole($sModule, $sAction, $iProfileRole)
  1702. {
  1703. if(($aRoles = $this->_oConfig->getRolesData()) && is_array($aRoles))
  1704. foreach ($aRoles as $iRole => $aRoleData) {
  1705. if(($iRole == 0 && $iProfileRole == 0) || ($iRole > 0 && $this->isRole($iProfileRole, $iRole))) {
  1706. $mPermissions = !empty($aRoleData['Data']) ? unserialize($aRoleData['Data']) : false;
  1707. if($mPermissions && isset($mPermissions[$sModule]))
  1708. return isset($mPermissions[$sModule][$sAction]) && $mPermissions[$sModule][$sAction];
  1709. }
  1710. }
  1711. return NULL;
  1712. }
  1713. public function isAllowedModuleActionByProfile($iContentId, $sPostModule, $sAction, $iProfileId = 0) {
  1714. if (!$iProfileId) $iProfileId = bx_get_logged_profile_id();
  1715. if ($iProfileId && $this->isFan($iContentId, $iProfileId)) {
  1716. $sModuleName = $this->getName();
  1717. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $sModuleName);
  1718. $aDataEntry = $this->_oDb->getContentInfoById($iContentId);
  1719. $bResult = $this->isAllowedActionByRole(['action' => $sAction, 'module' => $sPostModule], $aDataEntry, $oGroupProfile->id(), $iProfileId);
  1720. if ($bResult === true) return CHECK_ACTION_RESULT_ALLOWED;
  1721. if ($bResult === false) return _t('_sys_txt_access_denied');
  1722. }
  1723. 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.
  1724. }
  1725. public function isRole($iProfileRole, $iRole)
  1726. {
  1727. if(!$this->_oConfig->isMultiRoles())
  1728. return $iProfileRole == $iRole;
  1729. else
  1730. return $iProfileRole & (1 << ($iRole - 1));
  1731. }
  1732. public function serviceIsRole($iProfileRole, $iRole)
  1733. {
  1734. return $this->isRole($iProfileRole, $iRole);
  1735. }
  1736. public function checkAllowedManageFans($mixedDataEntry, $isPerformAction = false)
  1737. {
  1738. $aDataEntry = array();
  1739. if(!is_array($mixedDataEntry)) {
  1740. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  1741. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  1742. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1743. }
  1744. else
  1745. $aDataEntry = $mixedDataEntry;
  1746. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1747. return CHECK_ACTION_RESULT_ALLOWED;
  1748. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1749. }
  1750. public function checkAllowedManageAdmins($mixedDataEntry, $isPerformAction = false)
  1751. {
  1752. $aDataEntry = array();
  1753. if(!is_array($mixedDataEntry)) {
  1754. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  1755. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  1756. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1757. }
  1758. else
  1759. $aDataEntry = $mixedDataEntry;
  1760. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1761. return CHECK_ACTION_RESULT_ALLOWED;
  1762. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1763. }
  1764. public function checkAllowedEdit($aDataEntry, $isPerformAction = false)
  1765. {
  1766. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_EDIT, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1767. return CHECK_ACTION_RESULT_ALLOWED;
  1768. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1769. }
  1770. public function checkAllowedInvite($aDataEntry, $isPerformAction = false)
  1771. {
  1772. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_INVITE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1773. return CHECK_ACTION_RESULT_ALLOWED;
  1774. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1775. }
  1776. public function checkAllowedChangeCover($aDataEntry, $isPerformAction = false)
  1777. {
  1778. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1779. return CHECK_ACTION_RESULT_ALLOWED;
  1780. return parent::checkAllowedChangeCover($aDataEntry, $isPerformAction);
  1781. }
  1782. public function checkAllowedChangeSettings($aDataEntry, $isPerformAction = false)
  1783. {
  1784. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_SETTINGS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1785. return CHECK_ACTION_RESULT_ALLOWED;
  1786. return parent::checkAllowedChangeSettings($aDataEntry, $isPerformAction);
  1787. }
  1788. public function checkAllowedDelete(&$aDataEntry, $isPerformAction = false)
  1789. {
  1790. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_DELETE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1791. return CHECK_ACTION_RESULT_ALLOWED;
  1792. return parent::checkAllowedDelete($aDataEntry, $isPerformAction);
  1793. }
  1794. public function checkAllowedJoin(&$aDataEntry, $isPerformAction = false)
  1795. {
  1796. if (bx_get('key')){
  1797. $sKey = bx_get('key');
  1798. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  1799. $aData = $this->_oDb->getInviteByKey($sKey, $oGroupProfile->id());
  1800. if (isset($aData['invited_profile_id']) && $aData['invited_profile_id'] == bx_get_logged_profile_id()){
  1801. return CHECK_ACTION_RESULT_ALLOWED;
  1802. }
  1803. }
  1804. return _t('_sys_txt_access_denied');
  1805. }
  1806. public function checkAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1807. {
  1808. $mixedResult = $this->_modGroupsCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1809. /**
  1810. * @hooks
  1811. * @hookdef hook-system-check_allowed_subscribe_add 'system', 'check_allowed_subscribe_add' - hook to override the result of checking whether currently logged in user can subscribe (follow) the context or not
  1812. * - $unit_name - equals `system`
  1813. * - $action - equals `check_allowed_subscribe_add`
  1814. * - $object_id - not used
  1815. * - $sender_id - not used
  1816. * - $extra_params - array of additional params with the following array keys:
  1817. * - `module` - [string] module name
  1818. * - `content_info` - [array] context info array as key&value pairs
  1819. * - `profile_id` - [int] currently logged in profile id to be checked the availability of the action to
  1820. * - `override_result` - [string] or [int] by ref, check action result, can be overridden in hook processing. Return string with an error if action isn't allowed or CHECK_ACTION_RESULT_ALLOWED, @see BxDolAcl
  1821. * @hook @ref hook-system-check_allowed_subscribe_add
  1822. */
  1823. bx_alert('system', 'check_allowed_subscribe_add', 0, 0, [
  1824. 'module' => $this->getName(),
  1825. 'content_info' => $aDataEntry,
  1826. 'profile_id' => bx_get_logged_profile_id(),
  1827. 'override_result' => &$mixedResult
  1828. ]);
  1829. return $mixedResult;
  1830. }
  1831. public function checkAllowedAddContent(&$aDataEntry, $isPerformAction = false)
  1832. {
  1833. $CNF = &$this->_oConfig->CNF;
  1834. $sErrAccessDenied = _t('_sys_txt_access_denied');
  1835. $sModule = $this->getName();
  1836. $iProfileId = bx_get_logged_profile_id();
  1837. $aContexts = BxDolService::call($sModule, 'get_participating_profiles', [$iProfileId]);
  1838. if(!in_array($aDataEntry['profile_id'], $aContexts))
  1839. return $sErrAccessDenied;
  1840. if(bx_srv($sModule, 'check_allowed_post_in_profile', [$aDataEntry[$CNF['FIELD_ID']]]) !== CHECK_ACTION_RESULT_ALLOWED)
  1841. return $sErrAccessDenied;
  1842. return CHECK_ACTION_RESULT_ALLOWED;
  1843. }
  1844. /**
  1845. * Note. Is mainly needed for internal usage. Access level is 'public' to allow outer calls from alerts.
  1846. */
  1847. public function _modGroupsCheckAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1848. {
  1849. $CNF = &$this->_oConfig->CNF;
  1850. if(!$this->isFan($aDataEntry[$CNF['FIELD_ID']]) && (!isset($CNF['PARAM_SBS_WO_JOIN']) || getParam($CNF['PARAM_SBS_WO_JOIN']) != 'on'))
  1851. return _t('_sys_txt_access_denied');
  1852. return parent::_modProfileCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1853. }
  1854. /**
  1855. * @deprecated since version 11.0.3 and can be removed in the next version.
  1856. */
  1857. public function _checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  1858. {
  1859. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  1860. }
  1861. public function doAudit($iGroupProfileId, $iFanId, $sAction)
  1862. {
  1863. $oProfile = BxDolProfile::getInstance($iFanId);
  1864. $iContentId = $oProfile->getContentId();
  1865. $sModule = $oProfile->getModule();
  1866. $oModule = BxDolModule::getInstance($sModule);
  1867. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile') && $oModule->_oConfig){
  1868. $CNF = $oModule->_oConfig->CNF;
  1869. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iContentId))) : array();
  1870. $AuditParams = array(
  1871. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  1872. 'context_profile_id' => $iGroupProfileId,
  1873. 'context_profile_title' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName()
  1874. );
  1875. bx_audit(
  1876. $iContentId,
  1877. $sModule,
  1878. $sAction,
  1879. $AuditParams
  1880. );
  1881. }
  1882. }
  1883. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  1884. {
  1885. $sResult = $this->checkAllowedView($aDataEntry);
  1886. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  1887. // when context is in another context
  1888. if ($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']] < 0 && getParam('sys_check_fan_in_parent_context')) {
  1889. $oParent = BxDolProfile::getInstance(abs($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']]));
  1890. $oModule = $oParent ? BxDolModule::getInstance($oParent->getModule()) : null;
  1891. if ($oModule && method_exists($oModule, 'isFan') && !$oModule->isFan($oParent->getContentId()))
  1892. return _t('_sys_txt_access_denied');
  1893. }
  1894. // if profile view isn't allowed but visibility is in partially visible groups
  1895. // then display buttons to connect (befriend, join) to profile,
  1896. // if other conditions (in parent::_checkAllowedConnect) are met as well
  1897. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && !in_array($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), array('s'))))
  1898. return $sResult;
  1899. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  1900. }
  1901. // ====== COMMON METHODS
  1902. public function onUpdateImage($iContentId, $sFiledName, $sFiledValue, $iProfileId = 0)
  1903. {
  1904. $CNF = &$this->_oConfig->CNF;
  1905. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1906. $aField2Method = [
  1907. $CNF['FIELD_PICTURE'] => 'picture',
  1908. $CNF['FIELD_COVER'] => 'cover',
  1909. ];
  1910. bx_content_cache_del_by_prefix('sprofile_unit_vars:' . $aContentInfo['profile_id'] . ':');
  1911. bx_content_cache_del_by_prefix('menu_sys_toolbar_member_p' . $aContentInfo['profile_id']);
  1912. if(!empty($aField2Method[$sFiledName]))
  1913. /**
  1914. * @hooks
  1915. * @hookdef hook-bx_base_groups-context_picture_changed '{module_name}', 'context_picture_changed' - hook after context picture was changed
  1916. * - $unit_name - module name
  1917. * - $action - equals `context_picture_changed`
  1918. * - $object_id - context id
  1919. * - $sender_id - profile id who performed the action
  1920. * - $extra_params - array of additional params with the following array keys:
  1921. * - `status` - [string] context status
  1922. * - `status_admin` - [string] context admin status
  1923. * - `privacy_view` - [int] or [string] privacy for view context action, @see BxDolPrivacy
  1924. * - `cf` - [int] context's audience filter value
  1925. * @hook @ref hook-bx_base_groups-context_picture_changed
  1926. */
  1927. /**
  1928. * @hooks
  1929. * @hookdef hook-bx_base_groups-context_cover_changed '{module_name}', 'context_cover_changed' - hook after context cover was changed
  1930. * It's equivalent to @ref hook-bx_base_groups-context_picture_changed
  1931. * @hook @ref hook-bx_base_groups-context_cover_changed
  1932. */
  1933. bx_alert($this->getName(), 'context_' . $aField2Method[$sFiledName] . '_changed', $iContentId, $iProfileId, $this->_alertParams($aContentInfo));
  1934. }
  1935. protected function _alertParams($aContentInfo)
  1936. {
  1937. $aParams = parent::_alertParams($aContentInfo);
  1938. $CNF = &$this->_oConfig->CNF;
  1939. if(!empty($CNF['FIELD_CF']) && isset($aContentInfo[$CNF['FIELD_CF']]))
  1940. $aParams['cf'] = $aContentInfo[$CNF['FIELD_CF']];
  1941. return $aParams;
  1942. }
  1943. public function alertAfterAdd($aContentInfo)
  1944. {
  1945. $CNF = &$this->_oConfig->CNF;
  1946. $iId = (int)$aContentInfo[$CNF['FIELD_ID']];
  1947. $iAuthorId = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  1948. $sAction = 'added';
  1949. if(isset($CNF['FIELD_STATUS_ADMIN']) && isset($aContentInfo[$CNF['FIELD_STATUS_ADMIN']]) && $aContentInfo[$CNF['FIELD_STATUS_ADMIN']] == BX_BASE_MOD_GENERAL_STATUS_PENDING)
  1950. $sAction = 'deferred';
  1951. $sModule = $this->getName();
  1952. $aParams = $this->_alertParams($aContentInfo);
  1953. /**
  1954. * @hooks
  1955. * @hookdef hook-system-prepare_alert_params 'system', 'prepare_alert_params' - hook to override alert (hook) params
  1956. * - $unit_name - equals `system`
  1957. * - $action - equals `prepare_alert_params`
  1958. * - $object_id - not used
  1959. * - $sender_id - not used
  1960. * - $extra_params - array of additional params with the following array keys:
  1961. * - `unit` - [string] unit name
  1962. * - `action` - [string] by ref, action, can be overridden in hook processing
  1963. * - `object_id` - [int] by ref, object id, can be overridden in hook processing
  1964. * - `sender_id` - [int] by ref, action performer profile id, can be overridden in hook processing
  1965. * - `extras` - [array] by ref, extra params array as key&value pairs, can be overridden in hook processing
  1966. * @hook @ref hook-system-prepare_alert_params
  1967. */
  1968. bx_alert('system', 'prepare_alert_params', 0, 0, [
  1969. 'unit'=> $sModule,
  1970. 'action' => &$sAction,
  1971. 'object_id' => &$iId,
  1972. 'sender_id' => &$iAuthorId,
  1973. 'extras' => &$aParams,
  1974. 'action_ref' => &$sAction,
  1975. 'object_id_ref' => &$iId,
  1976. 'sender_id_ref' => &$iAuthorId,
  1977. 'extras_ref' => &$aParams
  1978. ]);
  1979. /**
  1980. * @hooks
  1981. * @hookdef hook-bx_base_groups-added '{module_name}', 'added' - hook after context was added (published)
  1982. * - $unit_name - module name
  1983. * - $action - equals `added`
  1984. * - $object_id - context id
  1985. * - $sender_id - not used
  1986. * - $extra_params - array of additional params with the following array keys:
  1987. * - `status` - [string] context status
  1988. * - `status_admin` - [string] context admin status
  1989. * - `privacy_view` - [int] or [string] privacy for view context action, @see BxDolPrivacy
  1990. * - `cf` - [int] context's audience filter value
  1991. * @hook @ref hook-bx_base_groups-added
  1992. */
  1993. /**
  1994. * @hooks
  1995. * @hookdef hook-bx_base_groups-deferred '{module_name}', 'deferred' - hook after context was added with pending approval status
  1996. * It's equivalent to @ref hook-bx_base_groups-added
  1997. * @hook @ref hook-bx_base_groups-deferred
  1998. */
  1999. bx_alert($sModule, $sAction, $iId, false, $aParams);
  2000. $this->_processModerationNotifications($aContentInfo);
  2001. }
  2002. public function addFollower ($iProfileId1, $iProfileId2)
  2003. {
  2004. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  2005. if($oConnectionFollow && !$oConnectionFollow->isConnected($iProfileId1, $iProfileId2)){
  2006. $oConnectionFollow->addConnection($iProfileId1, $iProfileId2);
  2007. return true;
  2008. }
  2009. return false;
  2010. }
  2011. public function isFan ($iContentId, $iProfileId = false)
  2012. {
  2013. $CNF = &$this->_oConfig->CNF;
  2014. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  2015. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  2016. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  2017. return false;
  2018. }
  2019. public function isFanByGroupProfileId ($iGroupProfileId, $iProfileId = false)
  2020. {
  2021. $CNF = &$this->_oConfig->CNF;
  2022. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  2023. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  2024. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  2025. return false;
  2026. }
  2027. public function isInvited ($sKey, $iGroupProfileId)
  2028. {
  2029. $CNF = &$this->_oConfig->CNF;
  2030. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  2031. if (!isset($aData['invited_profile_id']))
  2032. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  2033. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  2034. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  2035. return true;
  2036. }
  2037. public function isInvitedByProfileId ($iProfileId, $iGroupProfileId)
  2038. {
  2039. $CNF = &$this->_oConfig->CNF;
  2040. $aData = $this->_oDb->getInviteByInvited($iProfileId, $iGroupProfileId);
  2041. if (!isset($aData['invited_profile_id']))
  2042. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  2043. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  2044. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  2045. return true;
  2046. }
  2047. public function serviceIsInvited($iGroupProfileId, $iProfileId = false, $sKey = '')
  2048. {
  2049. if(!$iProfileId)
  2050. $iProfileId = bx_get_logged_profile_id();
  2051. if(empty($sKey) && ($sKey = bx_get('key')) !== false)
  2052. $sKey = bx_process_input($sKey);
  2053. $mixedInvited = false;
  2054. if(!empty($sKey))
  2055. $mixedInvited = $this->isInvited($sKey, $iGroupProfileId);
  2056. else if($iProfileId !== false)
  2057. $mixedInvited = $this->isInvitedByProfileId($iProfileId, $iGroupProfileId);
  2058. return $mixedInvited === true;
  2059. }
  2060. public function serviceIsNotInvited($iGroupProfileId, $iProfileId = false, $sKey = '')
  2061. {
  2062. return !$this->serviceIsInvited($iGroupProfileId, $iProfileId, $sKey);
  2063. }
  2064. public function serviceGetInvitedKey($iGroupProfileId, $iProfileId = false)
  2065. {
  2066. $sKey = '';
  2067. if(($sKey = bx_get('key')) !== false)
  2068. $sKey = bx_process_input($sKey);
  2069. if(!$sKey) {
  2070. if(!$iProfileId)
  2071. $iProfileId = bx_get_logged_profile_id();
  2072. if($iProfileId !== false) {
  2073. $aInvite = $this->_oDb->getInviteByInvited($iProfileId, $iGroupProfileId);
  2074. if(!empty($aInvite) && is_array($aInvite))
  2075. $sKey = $aInvite['key'];
  2076. }
  2077. }
  2078. return $sKey;
  2079. }
  2080. public function getRole($iGroupProfileId, $iFanProfileId)
  2081. {
  2082. if(!$this->isFanByGroupProfileId($iGroupProfileId, $iFanProfileId))
  2083. return false;
  2084. return $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  2085. }
  2086. public function setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  2087. {
  2088. $CNF = &$this->_oConfig->CNF;
  2089. if(!$this->_oConfig->isMultiRoles() && is_array($mixedRole))
  2090. $mixedRole = reset($mixedRole);
  2091. if(!isset($CNF['OBJECT_CONNECTIONS']))
  2092. return false;
  2093. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  2094. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  2095. if(!$oConnection || !$oGroupProfile)
  2096. return false;
  2097. if(!$oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->addConnection($iFanProfileId, $iGroupProfileId))
  2098. return false;
  2099. if(!$this->_oDb->setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod, $sOrder))
  2100. return false;
  2101. $this->onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole);
  2102. return true;
  2103. }
  2104. public function onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole)
  2105. {
  2106. $CNF = &$this->_oConfig->CNF;
  2107. $iProfileId = bx_get_logged_profile_id();
  2108. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  2109. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  2110. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  2111. // notify about admin status
  2112. if(!empty($CNF['EMAIL_FAN_SET_ROLE']) && $iFanProfileId != $iProfileId) {
  2113. $aSetRoles = is_array($mixedRole) ? $mixedRole : [$mixedRole];
  2114. $aRolesNames = [];
  2115. foreach ($aSetRoles as $iRole)
  2116. $aRolesNames[] = $aRoles[(int)$iRole];
  2117. sendMailTemplate($CNF['EMAIL_FAN_SET_ROLE'], 0, $iFanProfileId, array(
  2118. 'EntryUrl' => $oGroupProfile->getUrl(),
  2119. 'EntryTitle' => $oGroupProfile->getDisplayName(),
  2120. 'Role' => implode(', ', $aRolesNames),
  2121. ), BX_EMAIL_NOTIFY);
  2122. }
  2123. /**
  2124. * @hooks
  2125. * @hookdef hook-bx_base_groups-set_role '{module_name}', 'set_role' - hook after 'set role' action was applied to context member
  2126. * - $unit_name - module name
  2127. * - $action - equals `set_role`
  2128. * - $object_id - context id
  2129. * - $sender_id - context profile id
  2130. * - $extra_params - array of additional params with the following array keys:
  2131. * - `object_author_id` - [int] context profile id
  2132. * - `performer_id` - [int] performer profile id
  2133. * - `fan_id` - [int] context member profile id
  2134. * - `content` - [array] context info array as key&value pairs
  2135. * - `role` - [int] or [array] role or an array of roles to be set
  2136. * - `group_profile` - [int] context profile id
  2137. * - `profile` - [int] performer profile id
  2138. * @hook @ref hook-bx_base_groups-set_role
  2139. */
  2140. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  2141. 'object_author_id' => $iGroupProfileId,
  2142. 'performer_id' => $iProfileId,
  2143. 'fan_id' => $iFanProfileId,
  2144. 'content' => $aGroupProfileInfo,
  2145. 'role' => $mixedRole,
  2146. 'group_profile' => $iGroupProfileId,
  2147. 'profile' => $iProfileId
  2148. ));
  2149. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  2150. }
  2151. public function unsetRole($iGroupProfileId, $iFanProfileId)
  2152. {
  2153. $CNF = &$this->_oConfig->CNF;
  2154. if(!isset($CNF['OBJECT_CONNECTIONS']))
  2155. return false;
  2156. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  2157. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  2158. if(!$oConnection || !$oGroupProfile)
  2159. return false;
  2160. if($oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->removeConnection($iFanProfileId, $iGroupProfileId))
  2161. return false;
  2162. $iRole = $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  2163. if(!$this->_oDb->unsetRole($iGroupProfileId, $iFanProfileId))
  2164. return false;
  2165. $this->onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole);
  2166. return true;
  2167. }
  2168. public function onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole)
  2169. {
  2170. $CNF = &$this->_oConfig->CNF;
  2171. $iProfileId = bx_get_logged_profile_id();
  2172. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  2173. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  2174. /**
  2175. * @hooks
  2176. * @hookdef hook-bx_base_groups-set_role '{module_name}', 'set_role' - hook after 'set role' action was applied to context member
  2177. * - $unit_name - module name
  2178. * - $action - equals `set_role`
  2179. * - $object_id - context id
  2180. * - $sender_id - context profile id
  2181. * - $extra_params - array of additional params with the following array keys:
  2182. * - `object_author_id` - [int] context profile id
  2183. * - `performer_id` - [int] performer profile id
  2184. * - `fan_id` - [int] context member profile id
  2185. * - `content` - [array] context info array as key&value pairs
  2186. * - `role` - [int] or [array] role or an array of roles to be set
  2187. * - `group_profile` - [int] context profile id
  2188. * - `profile` - [int] performer profile id
  2189. * @hook @ref hook-bx_base_groups-set_role
  2190. */
  2191. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  2192. 'object_author_id' => $iGroupProfileId,
  2193. 'performer_id' => $iProfileId,
  2194. 'fan_id' => $iFanProfileId,
  2195. 'content' => $aGroupProfileInfo,
  2196. 'role' => $iRole,
  2197. 'group_profile' => $iGroupProfileId,
  2198. 'profile' => $iProfileId
  2199. ));
  2200. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  2201. }
  2202. public function getGroupsByFan($iProfileId, $mixedRole = false)
  2203. {
  2204. $CNF = &$this->_oConfig->CNF;
  2205. if(!isset($CNF['OBJECT_CONNECTIONS']))
  2206. return false;
  2207. if($mixedRole === false)
  2208. $mixedRole = BX_BASE_MOD_GROUPS_ROLE_COMMON;
  2209. if(!is_array($mixedRole))
  2210. $mixedRole = [$mixedRole];
  2211. $aResult = [];
  2212. foreach($mixedRole as $iRole) {
  2213. switch($iRole) {
  2214. case BX_BASE_MOD_GROUPS_ROLE_COMMON:
  2215. $aIds = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($iProfileId);
  2216. break;
  2217. default:
  2218. $aIds = $this->_oDb->getRoles([
  2219. 'type' => 'group_pids_by_fan_id',
  2220. 'fan_id' => $iProfileId,
  2221. 'role' => $iRole
  2222. ]);
  2223. }
  2224. $aResult = array_merge($aResult, $aIds);
  2225. }
  2226. return $aResult;
  2227. }
  2228. public function getMenuItemTitleByConnection($sConnection, $sAction, $iContentProfileId, $iInitiatorProfileId = 0)
  2229. {
  2230. $CNF = $this->_oConfig->getCNF();
  2231. $aResult = parent::getMenuItemTitleByConnection($sConnection, $sAction, $iContentProfileId, $iInitiatorProfileId);
  2232. if(!empty($aResult))
  2233. return $aResult;
  2234. $oConnection = BxDolConnection::getObjectInstance($sConnection);
  2235. if(!$oConnection)
  2236. return false;
  2237. if(!$iInitiatorProfileId)
  2238. $iInitiatorProfileId = bx_get_logged_profile_id();
  2239. $aResult = [];
  2240. if($oConnection->isConnectedNotMutual($iInitiatorProfileId, $iContentProfileId))
  2241. $aResult = [
  2242. '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'),
  2243. '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'),
  2244. ];
  2245. else if($oConnection->isConnectedNotMutual($iContentProfileId, $iInitiatorProfileId))
  2246. $aResult = [
  2247. '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'),
  2248. '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'),
  2249. ];
  2250. else if($oConnection->isConnected($iInitiatorProfileId, $iContentProfileId, true))
  2251. $aResult = [
  2252. 'add' => '',
  2253. 'remove' => _t(!empty($CNF['T']['menu_item_title_sm_leave']) ? $CNF['T']['menu_item_title_sm_leave'] : '_sys_menu_item_title_sm_leave'),
  2254. ];
  2255. else
  2256. $aResult = [
  2257. 'add' => _t(!empty($CNF['T']['menu_item_title_sm_join']) ? $CNF['T']['menu_item_title_sm_join'] : '_sys_menu_item_title_sm_join'),
  2258. 'remove' => '',
  2259. ];
  2260. return !empty($sAction) && isset($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
  2261. }
  2262. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  2263. {
  2264. $CNF = &$this->_oConfig->CNF;
  2265. $iImageId = 0;
  2266. $sImageMd = $sImageXl = '';
  2267. if(isset($CNF['FIELD_COVER']) && !empty($aContentInfo[$CNF['FIELD_COVER']])) {
  2268. $iImageId = (int)$aContentInfo[$CNF['FIELD_COVER']];
  2269. $sImageMd = $this->_oConfig->getImageUrl($iImageId, ['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  2270. $sImageXl = $this->_oConfig->getImageUrl($iImageId, ['OBJECT_IMAGES_TRANSCODER_COVER']);
  2271. }
  2272. if($sImageXl == '' && isset($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']])) {
  2273. $iImageId = (int)$aContentInfo[$CNF['FIELD_PICTURE']];
  2274. $sImageMd = $this->_oConfig->getImageUrl($iImageId, ['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  2275. $sImageXl = $this->_oConfig->getImageUrl($iImageId, ['OBJECT_IMAGES_TRANSCODER_COVER']);
  2276. }
  2277. if(empty($sImageXl))
  2278. return [];
  2279. $aImage = [
  2280. 'id' => $iImageId,
  2281. 'url' => $sUrl,
  2282. 'src' => $sImageXl,
  2283. 'src_orig' => $sImageXl
  2284. ];
  2285. if(!empty($sImageMd))
  2286. $aImage['src_medium'] = $sImageMd;
  2287. return [
  2288. $aImage
  2289. ];
  2290. }
  2291. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  2292. {
  2293. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  2294. return array(0, 0, null);
  2295. if ($oGroupProfile->getModule() == $this->getName()) {
  2296. $iProfileId = $iInitiatorId;
  2297. $iGroupProfileId = $oGroupProfile->id();
  2298. } else {
  2299. $iProfileId = $oGroupProfile->id();
  2300. $iGroupProfileId = $iInitiatorId;
  2301. }
  2302. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  2303. }
  2304. }
  2305. /** @} */