BxBaseModGroupsModule.php 110 KB

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