BxBaseModGroupsModule.php 74 KB

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