BxBaseModGroupsModule.php 85 KB

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