1
0

BxBaseModGroupsModule.php 110 KB

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