BxBaseModGroupsModule.php 112 KB

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