1
0

BxBaseModGroupsModule.php 73 KB

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