BxBaseModGroupsModule.php 85 KB

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