1
0

BxBaseModGroupsModule.php 110 KB

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