BxBaseModGroupsModule.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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_MMODE_PAID_JOIN', 'paid_join');
  13. define('BX_BASE_MOD_GROUPS_ROLE_COMMON', 0);
  14. define('BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR', 1);
  15. define('BX_BASE_MOD_GROUPS_ROLE_MODERATOR', 2);
  16. define('BX_BASE_MOD_GROUPS_ACTION_EDIT', 'edit');
  17. define('BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER', 'change_cover');
  18. define('BX_BASE_MOD_GROUPS_ACTION_INVITE', 'invite');
  19. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS', 'manage_fans');
  20. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES', 'manage_roles');
  21. define('BX_BASE_MOD_GROUPS_ACTION_DELETE', 'delete');
  22. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY', 'day');
  23. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK', 'week');
  24. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_MONTH', 'month');
  25. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_YEAR', 'year');
  26. /**
  27. * Groups profiles module.
  28. */
  29. class BxBaseModGroupsModule extends BxBaseModProfileModule
  30. {
  31. function __construct(&$aModule)
  32. {
  33. parent::__construct($aModule);
  34. }
  35. /**
  36. * Get possible recipients for start conversation form
  37. */
  38. public function actionAjaxGetInitialMembers ()
  39. {
  40. $sTerm = bx_get('term');
  41. $a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  42. header('Content-Type:text/javascript; charset=utf-8');
  43. echo(json_encode($a));
  44. }
  45. /**
  46. * Process Process Invitation
  47. */
  48. public function actionProcessInvite ($sKey, $iGroupProfileId, $bAccept)
  49. {
  50. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  51. if (isset($aData['invited_profile_id'])){
  52. $CNF = &$this->_oConfig->CNF;
  53. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  54. return '';
  55. $iInvitedProfileId = $aData['invited_profile_id'];
  56. if ($iInvitedProfileId != bx_get_logged_profile_id())
  57. return '';
  58. if ($bAccept){
  59. if($oConnection && !$oConnection->isConnected($iInvitedProfileId, $iGroupProfileId)){
  60. $oConnection->addConnection($iInvitedProfileId, $iGroupProfileId);
  61. $oConnection->addConnection($iGroupProfileId, $iInvitedProfileId);
  62. }
  63. }
  64. $this->_oDb->deleteInviteByKey($sKey, $iGroupProfileId);
  65. }
  66. }
  67. public function serviceGetOptionsMembersMode()
  68. {
  69. $CNF = &$this->_oConfig->CNF;
  70. return array(
  71. array('key' => '', 'value' => _t('_None')),
  72. array('key' => BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES, 'value' => _t($CNF['T']['option_members_mode_' . BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES])),
  73. );
  74. }
  75. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  76. {
  77. if(empty($sUnitTemplate))
  78. $sUnitTemplate = 'unit.html';
  79. return parent::serviceGetSearchResultUnit($iContentId, $sUnitTemplate);
  80. }
  81. /**
  82. * @see BxBaseModProfileModule::serviceGetSpaceTitle
  83. */
  84. public function serviceGetSpaceTitle()
  85. {
  86. return _t($this->_oConfig->CNF['T']['txt_sample_single']);
  87. }
  88. /**
  89. * @see iBxDolProfileService::serviceGetParticipatingProfiles
  90. */
  91. public function serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects = false)
  92. {
  93. if (isset($this->_oConfig->CNF['OBJECT_CONNECTIONS'])){
  94. $aConnectionObjects = array($this->_oConfig->CNF['OBJECT_CONNECTIONS'], 'sys_profiles_subscriptions');
  95. return parent::serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects);
  96. }
  97. return parent::serviceGetParticipatingProfiles($iProfileId);
  98. }
  99. /**
  100. * Check if this module entry can be used as profile
  101. */
  102. public function serviceActAsProfile ()
  103. {
  104. return false;
  105. }
  106. /**
  107. * Check if this module is group profile
  108. */
  109. public function serviceIsGroupProfile ()
  110. {
  111. return true;
  112. }
  113. public function serviceIsEnableForContext($iProfileId = 0)
  114. {
  115. $CNF = &$this->_oConfig->CNF;
  116. $sCnfKey = 'ENABLE_FOR_CONTEXT_IN_MODULES';
  117. if(empty($iProfileId) || empty($CNF[$sCnfKey]) || !is_array($CNF[$sCnfKey]))
  118. return false;
  119. if(in_array(BxDolProfile::getInstance($iProfileId)->getModule(), $CNF[$sCnfKey]))
  120. return true;
  121. return false;
  122. }
  123. /**
  124. * check if provided profile is member of the group
  125. */
  126. public function serviceIsFan ($iGroupProfileId, $iProfileId = false)
  127. {
  128. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  129. return $this->isFan($oGroupProfile->getContentId(), $iProfileId);
  130. }
  131. /**
  132. * check if provided profile is admin of the group
  133. */
  134. public function serviceIsAdmin ($iGroupProfileId, $iProfileId = false)
  135. {
  136. if(!$iProfileId)
  137. $iProfileId = bx_get_logged_profile_id();
  138. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  139. $iGroupContentId = $oGroupProfile->getContentId();
  140. if(!$this->isFan($iGroupContentId, $iProfileId))
  141. return false;
  142. $aGroupContentInfo = $this->_oDb->getContentInfoById($iGroupContentId);
  143. return $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aGroupContentInfo);
  144. }
  145. public function serviceGetAdminRole($iGroupProfileId, $iProfileId = false)
  146. {
  147. if(!$iProfileId)
  148. $iProfileId = bx_get_logged_profile_id();
  149. if(!$this->serviceIsAdmin($iGroupProfileId, $iProfileId))
  150. return 0;
  151. return $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  152. }
  153. /**
  154. * Delete profile from fans and admins tables
  155. * @param $iProfileId profile id
  156. */
  157. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  158. {
  159. $CNF = &$this->_oConfig->CNF;
  160. $this->_oDb->deleteAdminsByProfileId($iProfileId);
  161. if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  162. $oConnection->onDeleteInitiatorAndContent($iProfileId);
  163. }
  164. /**
  165. * Reset group's author for particular group
  166. * @param $iContentId group id
  167. * @return false of error, or number of updated records on success
  168. */
  169. public function serviceReassignEntityAuthor ($iContentId)
  170. {
  171. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  172. if (!$aContentInfo)
  173. return false;
  174. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  175. return false;
  176. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  177. return $this->_oDb->updateAuthorById($iContentId, $aAdmins ? array_pop($aAdmins) : 0);
  178. }
  179. /**
  180. * Entry actions and social sharing block
  181. */
  182. public function serviceEntityAllActions ($mixedContent = false, $aParams = array())
  183. {
  184. $CNF = &$this->_oConfig->CNF;
  185. if(!empty($mixedContent)) {
  186. if(!is_array($mixedContent))
  187. $mixedContent = array((int)$mixedContent, (method_exists($this->_oDb, 'getContentInfoById')) ? $this->_oDb->getContentInfoById((int)$mixedContent) : array());
  188. }
  189. else {
  190. $mixedContent = $this->_getContent();
  191. if($mixedContent === false)
  192. return false;
  193. }
  194. list($iContentId, $aContentInfo) = $mixedContent;
  195. if(!empty($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  196. $aParams = array_merge(array(
  197. 'entry_thumb' => (int)$aContentInfo[$CNF['FIELD_PICTURE']]
  198. ), $aParams);
  199. return parent::serviceEntityAllActions ($mixedContent, $aParams);
  200. }
  201. /**
  202. * Reset group's author when author profile is deleted
  203. * @param $iProfileId profile id
  204. * @return number of changed items
  205. */
  206. public function serviceReassignEntitiesByAuthor ($iProfileId)
  207. {
  208. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  209. if (!$a)
  210. return 0;
  211. $iCount = 0;
  212. foreach ($a as $aContentInfo)
  213. $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']]) ? 1 : 0);
  214. return $iCount;
  215. }
  216. public function servicePrepareFields ($aFieldsProfile)
  217. {
  218. $CNF = &$this->_oConfig->CNF;
  219. $aFieldsProfile[$CNF['FIELD_NAME']] = $aFieldsProfile['name'];
  220. $aFieldsProfile[$CNF['FIELD_TEXT']] = isset($aFieldsProfile['description']) ? $aFieldsProfile['description'] : '';
  221. unset($aFieldsProfile['name']);
  222. unset($aFieldsProfile['description']);
  223. return $aFieldsProfile;
  224. }
  225. public function serviceOnRemoveConnection ($iGroupProfileId, $iInitiatorId)
  226. {
  227. $CNF = &$this->_oConfig->CNF;
  228. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  229. if (!$oGroupProfile)
  230. return false;
  231. $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
  232. if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
  233. return $oConn->removeConnection($iProfileId, $iGroupProfileId);
  234. return false;
  235. }
  236. public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $bSendInviteOnly = false)
  237. {
  238. $CNF = &$this->_oConfig->CNF;
  239. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  240. if (!$oGroupProfile)
  241. return false;
  242. if (!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  243. return false;
  244. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  245. return false;
  246. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  247. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  248. // send invitation to the group
  249. $sModule = $this->getName();
  250. if ($bSendInviteOnly && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  251. bx_alert($sModule, 'join_invitation', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  252. 'content' => $aContentInfo,
  253. 'entry_title' => $sEntryTitle,
  254. 'entry_url' => $sEntryUrl,
  255. 'group_profile' => $iGroupProfileId,
  256. 'profile' => $iProfileId,
  257. 'notification_subobject_id' => $iProfileId,
  258. 'object_author_id' => $iGroupProfileId
  259. ));
  260. /**
  261. * 'Invitation Received' alert for Notifications module.
  262. * Note. It's essential to use Recipient ($iInitiatorId) in 'object_author_id' parameter.
  263. * In this case notification will be received by Recipient profile.
  264. */
  265. bx_alert($sModule, 'join_invitation_notif', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  266. 'object_author_id' => $iInitiatorId,
  267. 'privacy_view' => isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 3,
  268. ));
  269. }
  270. // send notification to group's admins that new connection is pending confirmation
  271. elseif (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  272. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  273. 'object_author_id' => $iGroupProfileId,
  274. 'performer_id' => $iProfileId,
  275. 'content' => $aContentInfo,
  276. 'entry_title' => $sEntryTitle,
  277. 'entry_url' => $sEntryUrl,
  278. 'group_profile' => $iGroupProfileId,
  279. 'profile' => $iProfileId
  280. ));
  281. }
  282. // send notification that join request was accepted
  283. else if (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  284. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  285. 'object_author_id' => $iGroupProfileId,
  286. 'performer_id' => $iProfileId,
  287. 'content' => $aContentInfo,
  288. 'entry_title' => $sEntryTitle,
  289. 'entry_url' => $sEntryUrl,
  290. 'group_profile' => $iGroupProfileId,
  291. 'profile' => $iProfileId
  292. ));
  293. }
  294. // new fan was added
  295. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  296. // follow group on join
  297. if (BxDolService::call($oGroupProfile->getModule(), 'act_as_profile')){
  298. $this->addFollower($oGroupProfile->id(), (int)$iInitiatorId);
  299. }
  300. else{
  301. $this->addFollower((int)$iInitiatorId, $oGroupProfile->id());
  302. }
  303. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  304. 'object_author_id' => $iGroupProfileId,
  305. 'performer_id' => $iProfileId,
  306. 'content' => $aContentInfo,
  307. 'entry_title' => $sEntryTitle,
  308. 'entry_url' => $sEntryUrl,
  309. 'group_profile' => $iGroupProfileId,
  310. 'profile' => $iProfileId,
  311. ));
  312. $this->doAudit($iGroupProfileId, $iInitiatorId, '_sys_audit_action_group_join_request_accepted');
  313. return false;
  314. }
  315. // don't automatically add connection (mutual) if group requires manual join confirmation
  316. if ($bSendInviteOnly || $aContentInfo['join_confirmation'])
  317. return false;
  318. // check if connection already exists
  319. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  320. return false;
  321. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  322. return false;
  323. return true;
  324. }
  325. public function serviceFansTable ()
  326. {
  327. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_CONNECTIONS']);
  328. if (!$oGrid)
  329. return false;
  330. return $oGrid->getCode();
  331. }
  332. public function serviceInvitesTable ()
  333. {
  334. if (!isset($this->_oConfig->CNF['OBJECT_GRID_INVITES']))
  335. return false;
  336. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_INVITES']);
  337. if (!$oGrid)
  338. return false;
  339. return $oGrid->getCode();
  340. }
  341. public function serviceFans ($iContentId = 0, $bAsArray = false)
  342. {
  343. if (!$iContentId)
  344. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  345. if (!$iContentId)
  346. return false;
  347. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  348. if (!$aContentInfo)
  349. return false;
  350. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  351. return false;
  352. if(!$bAsArray) {
  353. bx_import('BxDolConnection');
  354. $mixedResult = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $this->_oConfig->CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  355. if (!$mixedResult)
  356. return MsgBox(_t('_sys_txt_empty'));
  357. }
  358. else
  359. $mixedResult = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  360. return $mixedResult;
  361. }
  362. public function serviceFansWithoutAdmins ($iContentId = 0, $bAsArray = false)
  363. {
  364. if (!$iContentId)
  365. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  366. if (!$iContentId)
  367. return false;
  368. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  369. if (!$aContentInfo)
  370. return false;
  371. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  372. return false;
  373. $CNF = &$this->_oConfig->CNF;
  374. $aFans = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  375. if(empty($aFans) || !is_array($aFans))
  376. return false;
  377. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  378. if(!empty($aAdmins) && is_array($aAdmins))
  379. $aFans = array_diff($aFans, $aAdmins);
  380. $iStart = (int)bx_get('start');
  381. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  382. if(!$iLimit)
  383. $iLimit = 4;
  384. return $this->_serviceBrowseQuick($aFans, $iStart, $iLimit);
  385. }
  386. public function serviceAdmins ($iContentId = 0)
  387. {
  388. $CNF = &$this->_oConfig->CNF;
  389. if(!$iContentId)
  390. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  391. if(!$iContentId)
  392. return false;
  393. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  394. if(!$oGroupProfile)
  395. return false;
  396. $iStart = (int)bx_get('start');
  397. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  398. if(!$iLimit)
  399. $iLimit = 4;
  400. $aProfiles = $this->_oDb->getAdmins($oGroupProfile->id(), $iStart, $iLimit+1);
  401. if(empty($aProfiles) || !is_array($aProfiles))
  402. return false;
  403. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  404. }
  405. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  406. {
  407. if (!$iProfileId)
  408. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  409. if (!$iProfileId)
  410. return '';
  411. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  412. }
  413. public function serviceBrowseCreatedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  414. {
  415. if (!$iProfileId)
  416. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  417. if (!$iProfileId)
  418. return '';
  419. return $this->_serviceBrowse ('created_entries', array('author' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  420. }
  421. public function serviceEntityPricing($iProfileId = 0)
  422. {
  423. $CNF = &$this->_oConfig->CNF;
  424. if(!$iProfileId)
  425. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  426. if(!$iProfileId)
  427. return '';
  428. if(!$this->_oConfig->isPaidJoin())
  429. return '';
  430. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  431. return MsgBox(_t('_Access denied'));
  432. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_MANAGE']);
  433. if(!$oGrid)
  434. return '';
  435. return $oGrid->getCode();
  436. }
  437. public function serviceEntityJoin($iProfileId = 0)
  438. {
  439. $CNF = &$this->_oConfig->CNF;
  440. if(!$iProfileId)
  441. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  442. if(!$iProfileId)
  443. return '';
  444. if(!$this->_oConfig->isPaidJoin())
  445. return '';
  446. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_VIEW']);
  447. if(!$oGrid)
  448. return '';
  449. return $oGrid->getCode();
  450. }
  451. public function serviceEntityInvite ($iContentId = 0, $bErrorMsg = true)
  452. {
  453. if (isset($this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE']))
  454. return $this->_serviceEntityForm ('editDataForm', $iContentId, $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE'], false, $bErrorMsg);
  455. return false;
  456. }
  457. /**
  458. * Entry social sharing block
  459. */
  460. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  461. {
  462. if(!empty($mixedContent)) {
  463. if(!is_array($mixedContent))
  464. $mixedContent = array((int)$mixedContent, array());
  465. }
  466. else {
  467. $mixedContent = $this->_getContent();
  468. if($mixedContent === false)
  469. return false;
  470. }
  471. list($iContentId, $aContentInfo) = $mixedContent;
  472. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  473. if(!$oGroupProfile)
  474. return false;
  475. return parent::serviceEntitySocialSharing(array($iContentId, $aContentInfo), array(
  476. 'title' => $oGroupProfile->getDisplayName()
  477. ));
  478. }
  479. public function serviceIsPricingAvaliable($iProfileId)
  480. {
  481. if(!$this->_oConfig->isPaidJoin())
  482. return false;
  483. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  484. return false;
  485. return true;
  486. }
  487. public function serviceIsPaidJoinAvaliable($iProfileId)
  488. {
  489. return $this->isPaidJoinByProfile($iProfileId);
  490. }
  491. public function serviceIsPaidJoinAvaliableByContent($iContentId)
  492. {
  493. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  494. if(!$oProfile)
  495. return false;
  496. return $this->isPaidJoinByProfile($oProfile->id());
  497. }
  498. public function serviceIsFreeJoinAvaliable($iProfileId)
  499. {
  500. return !$this->isPaidJoinByProfile($iProfileId);
  501. }
  502. public function serviceIsFreeJoinAvaliableByContent($iContentId)
  503. {
  504. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  505. if(!$oProfile)
  506. return false;
  507. return !$this->isPaidJoinByProfile($oProfile->id());
  508. }
  509. public function isPaidJoinByProfile($iProfileId)
  510. {
  511. if(!$this->_oConfig->isPaidJoin())
  512. return false;
  513. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $iProfileId));
  514. if(empty($aPrices) || !is_array($aPrices))
  515. return false;
  516. return true;
  517. }
  518. /**
  519. * Integration with Payments.
  520. */
  521. public function serviceGetPaymentData()
  522. {
  523. return $this->_aModule;
  524. }
  525. public function serviceGetCartItem($mixedItemId)
  526. {
  527. $CNF = &$this->_oConfig->CNF;
  528. if(!$mixedItemId)
  529. return array();
  530. if(is_numeric($mixedItemId))
  531. $aItem = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => (int)$mixedItemId));
  532. else
  533. $aItem = $this->_oDb->getPrices(array('type' => 'by_name', 'value' => $mixedItemId));
  534. if(empty($aItem) || !is_array($aItem))
  535. return array();
  536. if(!$this->isPaidJoinByProfile($aItem['profile_id']))
  537. return array();
  538. $oGroupProfile = BxDolProfile::getInstance($aItem['profile_id']);
  539. if(!$oGroupProfile)
  540. return array();
  541. $aGroupProfile = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  542. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  543. return array (
  544. 'id' => $aItem['id'],
  545. 'author_id' => $aGroupProfile[$CNF['FIELD_AUTHOR']],
  546. 'name' => $aItem['name'],
  547. 'title' => _t($CNF['T']['txt_cart_item_title'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']], $aItem['period'], $aItem['period_unit']),
  548. 'description' => '',
  549. 'url' => $oGroupProfile->getUrl(),
  550. 'price_single' => $aItem['price'],
  551. 'price_recurring' => $aItem['price'],
  552. 'period_recurring' => $aItem['period'],
  553. 'period_unit_recurring' => $aItem['period_unit'],
  554. 'trial_recurring' => 0
  555. );
  556. }
  557. public function serviceGetCartItems($iSellerId)
  558. {
  559. $CNF = &$this->_oConfig->CNF;
  560. if(empty($iSellerId))
  561. return array();
  562. $sModule = $this->getName();
  563. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  564. $aGroups = $this->_oDb->getEntriesBy(array('type' => 'author', 'author' => $iSellerId));
  565. $aResult = array();
  566. foreach($aGroups as $aGroup) {
  567. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aGroup[$CNF['FIELD_ID']], $sModule);
  568. if(!$oGroupProfile)
  569. continue;
  570. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $oGroupProfile->id()));
  571. if(empty($aPrices) || !is_array($aPrices))
  572. continue;
  573. $sTitle = $oGroupProfile->getDisplayName();
  574. $sUrl = $oGroupProfile->getUrl();
  575. foreach($aPrices as $aPrice)
  576. $aResult[] = array(
  577. 'id' => $aPrice['id'],
  578. 'author_id' => $iSellerId,
  579. 'name' => $aPrice['name'],
  580. 'title' => _t($CNF['T']['txt_cart_item_title'], $sTitle, $aRoles[$aPrice['role_id']], $aPrice['period'], $aPrice['period_unit']),
  581. 'description' => '',
  582. 'url' => $sUrl,
  583. 'price_single' => $aPrice['price'],
  584. 'price_recurring' => $aPrice['price'],
  585. 'period_recurring' => $aPrice['period'],
  586. 'period_unit_recurring' => $aPrice['period_unit']
  587. );
  588. }
  589. return $aResult;
  590. }
  591. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  592. {
  593. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  594. }
  595. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  596. {
  597. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  598. }
  599. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  600. {
  601. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  602. }
  603. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  604. {
  605. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  606. }
  607. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  608. {
  609. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  610. }
  611. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  612. {
  613. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  614. }
  615. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  616. {
  617. return true;
  618. }
  619. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  620. {
  621. $CNF = &$this->_oConfig->CNF;
  622. $aItem = $this->serviceGetCartItem($iItemId);
  623. if(empty($aItem) || !is_array($aItem))
  624. return array();
  625. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  626. $mixedPeriod = false;
  627. if((int)$aItemInfo['period'] != 0)
  628. $mixedPeriod = array(
  629. 'period' => (int)$aItemInfo['period'],
  630. 'period_unit' => $aItemInfo['period_unit'],
  631. 'period_reserve' => $CNF['PARAM_RECURRING_RESERVE']
  632. );
  633. if(!$this->setRole($aItemInfo['profile_id'], $iClientId, $aItemInfo['role_id'], $mixedPeriod, $sOrder))
  634. return array();
  635. return $aItem;
  636. }
  637. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  638. {
  639. $aItem = $this->serviceGetCartItem($iItemIdNew);
  640. if(empty($aItem) || !is_array($aItem))
  641. return array();
  642. $aItemInfoOld = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdOld));
  643. if(empty($aItemInfoOld) || !is_array($aItemInfoOld))
  644. return array();
  645. $aItemInfoNew = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdNew));
  646. if(empty($aItemInfoNew) || !is_array($aItemInfoNew))
  647. return array();
  648. if(!$this->unsetRole($aItemInfoOld['profile_id'], $iClientId))
  649. return array();
  650. $aResult = $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemIdNew, 1, $sOrder);
  651. if(empty($aResult) || !is_array($aResult))
  652. return array();
  653. return $aItem;
  654. }
  655. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  656. {
  657. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  658. if(empty($aItemInfo) || !is_array($aItemInfo))
  659. return false;
  660. return $this->unsetRole($aItemInfo['profile_id'], $iClientId);
  661. }
  662. /**
  663. * Data for Notifications module
  664. */
  665. public function serviceGetNotificationsData()
  666. {
  667. $sModule = $this->_aModule['name'];
  668. $aSettingsTypes = array('follow_member', 'follow_context');
  669. if($this->serviceActAsProfile())
  670. $aSettingsTypes = array('personal', 'follow_member');
  671. return array(
  672. 'handlers' => array(
  673. array('group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'),
  674. array('group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'),
  675. array('group' => $sModule . '_score_up', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteUp', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_up', 'module_class' => 'Module'),
  676. array('group' => $sModule . '_score_down', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteDown', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_down', 'module_class' => 'Module'),
  677. array('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'),
  678. array('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'),
  679. array('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']),
  680. array('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'),
  681. ),
  682. 'settings' => array(
  683. array('group' => 'vote', 'unit' => $sModule, 'action' => 'doVote', 'types' => $aSettingsTypes),
  684. array('group' => 'score_up', 'unit' => $sModule, 'action' => 'doVoteUp', 'types' => $aSettingsTypes),
  685. array('group' => 'score_down', 'unit' => $sModule, 'action' => 'doVoteDown', 'types' => $aSettingsTypes),
  686. array('group' => 'fan', 'unit' => $sModule, 'action' => 'fan_added', 'types' => $aSettingsTypes),
  687. array('group' => 'invite', 'unit' => $sModule, 'action' => 'join_invitation_notif', 'types' => array('personal')),
  688. array('group' => 'join', 'unit' => $sModule, 'action' => 'join_request', 'types' => $aSettingsTypes),
  689. array('group' => 'timeline_post', 'unit' => $sModule, 'action' => 'timeline_post_common', 'types' => $aSettingsTypes)
  690. ),
  691. 'alerts' => array(
  692. array('unit' => $sModule, 'action' => 'doVote'),
  693. array('unit' => $sModule, 'action' => 'undoVote'),
  694. array('unit' => $sModule, 'action' => 'doVoteUp'),
  695. array('unit' => $sModule, 'action' => 'doVoteDown'),
  696. array('unit' => $sModule, 'action' => 'fan_added'),
  697. array('unit' => $sModule, 'action' => 'join_invitation_notif'),
  698. array('unit' => $sModule, 'action' => 'join_request'),
  699. array('unit' => $sModule, 'action' => 'timeline_post_common'),
  700. )
  701. );
  702. }
  703. /**
  704. * Notification about new invitation to join the group
  705. */
  706. public function serviceGetNotificationsJoinInvitation($aEvent)
  707. {
  708. $CNF = &$this->_oConfig->CNF;
  709. $iContentId = (int)$aEvent['object_id'];
  710. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  711. if(!$oGroupProfile)
  712. return array();
  713. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  714. if(empty($aContentInfo) || !is_array($aContentInfo))
  715. return array();
  716. /*
  717. * It's essential that 'object_owner_id' contains invited member profile id.
  718. */
  719. $oProfile = BxDolProfile::getInstance((int)$aEvent['object_owner_id']);
  720. if(!$oProfile)
  721. return array();
  722. /*
  723. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  724. * because Subentry URL has higher display priority and notification
  725. * should be linked to Group Profile (Group Profile -> Members tab)
  726. * instead of Personal Profile of invited member.
  727. */
  728. $sEntryUrl = $oGroupProfile->getUrl();
  729. return array(
  730. 'entry_sample' => $CNF['T']['txt_sample_single'],
  731. 'entry_url' => $sEntryUrl,
  732. 'entry_caption' => $oGroupProfile->getDisplayName(),
  733. 'subentry_sample' => $oProfile->getDisplayName(),
  734. 'subentry_url' => $sEntryUrl,
  735. 'lang_key' => $this->_oConfig->CNF['T']['txt_ntfs_join_invitation']
  736. );
  737. }
  738. /**
  739. * Notification about new member requst in the group
  740. */
  741. public function serviceGetNotificationsJoinRequest($aEvent)
  742. {
  743. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_join_request']);
  744. }
  745. /**
  746. * Notification about new member in the group
  747. */
  748. public function serviceGetNotificationsFanAdded($aEvent)
  749. {
  750. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_fan_added']);
  751. }
  752. protected function _serviceGetNotification($aEvent, $sLangKey)
  753. {
  754. $CNF = &$this->_oConfig->CNF;
  755. $iContentId = (int)$aEvent['object_id'];
  756. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  757. if(!$oGroupProfile)
  758. return array();
  759. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  760. if(empty($aContentInfo) || !is_array($aContentInfo))
  761. return array();
  762. $oProfile = BxDolProfile::getInstance((int)$aEvent['subobject_id']);
  763. if(!$oProfile)
  764. return array();
  765. /*
  766. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  767. * because Subentry URL has higher display priority and notification
  768. * should be linked to Group Profile (Group Profile -> Members tab)
  769. * instead of Personal Profile of a member, who performed an action.
  770. */
  771. $sEntryUrl = $oGroupProfile->getUrl();
  772. if(!empty($CNF['URL_ENTRY_FANS']))
  773. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_FANS'], array(
  774. 'profile_id' => $oGroupProfile->id()
  775. ));
  776. return array(
  777. 'entry_sample' => $CNF['T']['txt_sample_single'],
  778. 'entry_url' => $sEntryUrl,
  779. 'entry_caption' => $oGroupProfile->getDisplayName(),
  780. 'entry_author' => $oGroupProfile->id(),
  781. 'subentry_sample' => $oProfile->getDisplayName(),
  782. 'subentry_url' => $sEntryUrl,
  783. 'lang_key' => $sLangKey
  784. );
  785. }
  786. /**
  787. * Data for Timeline module
  788. */
  789. public function serviceGetTimelineData()
  790. {
  791. return BxBaseModGeneralModule::serviceGetTimelineData();
  792. }
  793. /**
  794. * Entry post for Timeline module
  795. */
  796. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  797. {
  798. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  799. if($a === false)
  800. return false;
  801. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  802. $a['content']['url'] = $oGroupProfile->getUrl();
  803. $a['content']['title'] = $oGroupProfile->getDisplayName();
  804. return $a;
  805. }
  806. // ====== PERMISSION METHODS
  807. /**
  808. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  809. */
  810. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  811. {
  812. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  813. }
  814. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  815. {
  816. $CNF = &$this->_oConfig->CNF;
  817. $bInvited = false;
  818. if(!empty($CNF['TABLE_INVITES']) && bx_get('key')){
  819. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  820. $mixedInvited = $this->isInvited(bx_get('key'), $oGroupProfile->id());
  821. if($mixedInvited === true)
  822. $bInvited = true;
  823. }
  824. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $iProfileId) || $bInvited)
  825. return CHECK_ACTION_RESULT_ALLOWED;
  826. return parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId);
  827. }
  828. /**
  829. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  830. */
  831. public function checkAllowedCompose(&$aDataEntry, $isPerformAction = false)
  832. {
  833. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  834. return _t('_sys_txt_access_denied');
  835. return parent::checkAllowedCompose ($aDataEntry, $isPerformAction);
  836. }
  837. /**
  838. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  839. */
  840. public function checkAllowedFanAdd(&$aDataEntry, $isPerformAction = false)
  841. {
  842. $mixedResult = $this->_modGroupsCheckAllowedFanAdd($aDataEntry, $isPerformAction);
  843. // call alert to allow custom checks
  844. bx_alert('system', 'check_allowed_fan_add', 0, 0, array(
  845. 'module' => $this->getName(),
  846. 'content_info' => $aDataEntry,
  847. 'profile_id' => bx_get_logged_profile_id(),
  848. 'override_result' => &$mixedResult
  849. ));
  850. return $mixedResult;
  851. }
  852. public function _modGroupsCheckAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  853. {
  854. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) || !isLogged())
  855. return _t('_sys_txt_access_denied');
  856. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], true, false);
  857. }
  858. /**
  859. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  860. */
  861. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  862. {
  863. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  864. return CHECK_ACTION_RESULT_ALLOWED;
  865. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  866. }
  867. protected function _checkAllowedActionByFan($sAction, $aDataEntry, $iProfileId = 0)
  868. {
  869. $CNF = &$this->_oConfig->CNF;
  870. $bRoles = $this->_oConfig->isRoles();
  871. if(empty($iProfileId))
  872. $iProfileId = bx_get_logged_profile_id();
  873. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  874. if(!$oGroupProfile)
  875. return $sAction == BX_BASE_MOD_GROUPS_ACTION_DELETE ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_not_found');
  876. $iGroupProfileId = $oGroupProfile->id();
  877. if(!$bRoles && $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aDataEntry))
  878. return CHECK_ACTION_RESULT_ALLOWED;
  879. if($bRoles && $this->isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId))
  880. return CHECK_ACTION_RESULT_ALLOWED;
  881. return _t('_sys_txt_access_denied');
  882. }
  883. public function isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId)
  884. {
  885. $bResult = false;
  886. $iProfileRole = $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  887. if(!empty($iProfileRole))
  888. switch($sAction) {
  889. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  890. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR))
  891. $bResult = true;
  892. break;
  893. default:
  894. $bResult = true;
  895. }
  896. // call alert to allow custom checks
  897. bx_alert('system', 'check_allowed_action_by_role', 0, 0, array(
  898. 'module' => $this->getName(),
  899. 'multi_roles' => $this->_oConfig->isMultiRoles(),
  900. 'action' => $sAction,
  901. 'content_profile_id' => $iGroupProfileId,
  902. 'content_info' => $aDataEntry,
  903. 'profile_id' => $iProfileId,
  904. 'profile_role' => $iProfileRole,
  905. 'override_result' => &$bResult
  906. ));
  907. return $bResult;
  908. }
  909. public function isRole($iProfileRole, $iRole)
  910. {
  911. if(!$this->_oConfig->isMultiRoles())
  912. return $iProfileRole == $iRole;
  913. else
  914. return $iProfileRole & (1 << ($iRole - 1));
  915. }
  916. public function serviceIsRole($iProfileRole, $iRole)
  917. {
  918. return $this->isRole($iProfileRole, $iRole);
  919. }
  920. public function checkAllowedManageFans($mixedDataEntry, $isPerformAction = false)
  921. {
  922. $aDataEntry = array();
  923. if(!is_array($mixedDataEntry)) {
  924. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  925. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  926. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  927. }
  928. else
  929. $aDataEntry = $mixedDataEntry;
  930. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  931. return CHECK_ACTION_RESULT_ALLOWED;
  932. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  933. }
  934. public function checkAllowedManageAdmins($mixedDataEntry, $isPerformAction = false)
  935. {
  936. $aDataEntry = array();
  937. if(!is_array($mixedDataEntry)) {
  938. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  939. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  940. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  941. }
  942. else
  943. $aDataEntry = $mixedDataEntry;
  944. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  945. return CHECK_ACTION_RESULT_ALLOWED;
  946. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  947. }
  948. public function checkAllowedEdit($aDataEntry, $isPerformAction = false)
  949. {
  950. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_EDIT, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  951. return CHECK_ACTION_RESULT_ALLOWED;
  952. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  953. }
  954. public function checkAllowedInvite($aDataEntry, $isPerformAction = false)
  955. {
  956. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_INVITE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  957. return CHECK_ACTION_RESULT_ALLOWED;
  958. return $this->checkAllowedEdit ($aDataEntry, $isPerformAction);
  959. }
  960. public function checkAllowedChangeCover($aDataEntry, $isPerformAction = false)
  961. {
  962. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  963. return CHECK_ACTION_RESULT_ALLOWED;
  964. return parent::checkAllowedChangeCover($aDataEntry, $isPerformAction);
  965. }
  966. public function checkAllowedDelete(&$aDataEntry, $isPerformAction = false)
  967. {
  968. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_DELETE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  969. return CHECK_ACTION_RESULT_ALLOWED;
  970. return parent::checkAllowedDelete($aDataEntry, $isPerformAction);
  971. }
  972. public function checkAllowedJoin(&$aDataEntry, $isPerformAction = false)
  973. {
  974. if (bx_get('key')){
  975. $sKey = bx_get('key');
  976. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  977. $aData = $this->_oDb->getInviteByKey($sKey, $oGroupProfile->id());
  978. if (isset($aData['invited_profile_id']) && $aData['invited_profile_id'] == bx_get_logged_profile_id()){
  979. return CHECK_ACTION_RESULT_ALLOWED;
  980. }
  981. }
  982. return _t('_sys_txt_access_denied');
  983. }
  984. public function checkAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  985. {
  986. $mixedResult = $this->_modGroupsCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  987. // call alert to allow custom checks
  988. bx_alert('system', 'check_allowed_subscribe_add', 0, 0, array(
  989. 'module' => $this->getName(),
  990. 'content_info' => $aDataEntry,
  991. 'profile_id' => bx_get_logged_profile_id(),
  992. 'override_result' => &$mixedResult
  993. ));
  994. return $mixedResult;
  995. }
  996. /**
  997. * Note. Is mainly needed for internal usage. Access level is 'public' to allow outer calls from alerts.
  998. */
  999. public function _modGroupsCheckAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1000. {
  1001. $CNF = &$this->_oConfig->CNF;
  1002. if(!$this->isFan($aDataEntry[$CNF['FIELD_ID']]) && (!isset($CNF['PARAM_SBS_WO_JOIN']) || getParam($CNF['PARAM_SBS_WO_JOIN']) != 'on'))
  1003. return _t('_sys_txt_access_denied');
  1004. return parent::_modProfileCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1005. }
  1006. /**
  1007. * @deprecated since version 11.0.3 and can be removed in the next version.
  1008. */
  1009. public function _checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  1010. {
  1011. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  1012. }
  1013. public function doAudit($iGroupProfileId, $iFanId, $sAction)
  1014. {
  1015. $oProfile = BxDolProfile::getInstance($iFanId);
  1016. $iContentId = $oProfile->getContentId();
  1017. $sModule = $oProfile->getModule();
  1018. $oModule = BxDolModule::getInstance($sModule);
  1019. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile') && $oModule->_oConfig){
  1020. $CNF = $oModule->_oConfig->CNF;
  1021. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iContentId))) : array();
  1022. $AuditParams = array(
  1023. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  1024. 'context_profile_id' => $iGroupProfileId,
  1025. 'context_profile_title' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName()
  1026. );
  1027. bx_audit(
  1028. $iContentId,
  1029. $sModule,
  1030. $sAction,
  1031. $AuditParams
  1032. );
  1033. }
  1034. }
  1035. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  1036. {
  1037. $sResult = $this->checkAllowedView($aDataEntry);
  1038. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  1039. // if profile view isn't allowed but visibility is in partially visible groups
  1040. // then display buttons to connect (befriend, join) to profile,
  1041. // if other conditions (in parent::_checkAllowedConnect) are met as well
  1042. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && !in_array($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), array('s'))))
  1043. return $sResult;
  1044. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  1045. }
  1046. public function addFollower ($iProfileId1, $iProfileId2)
  1047. {
  1048. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  1049. if($oConnectionFollow && !$oConnectionFollow->isConnected($iProfileId1, $iProfileId2)){
  1050. $oConnectionFollow->addConnection($iProfileId1, $iProfileId2);
  1051. return true;
  1052. }
  1053. return false;
  1054. }
  1055. public function isFan ($iContentId, $iProfileId = false)
  1056. {
  1057. $CNF = &$this->_oConfig->CNF;
  1058. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  1059. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1060. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1061. return false;
  1062. }
  1063. public function isFanByGroupProfileId ($iGroupProfileId, $iProfileId = false)
  1064. {
  1065. $CNF = &$this->_oConfig->CNF;
  1066. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1067. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1068. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1069. return false;
  1070. }
  1071. public function isInvited ($sKey, $iProfileId)
  1072. {
  1073. $CNF = &$this->_oConfig->CNF;
  1074. $aData = $this->_oDb->getInviteByKey($sKey, $iProfileId);
  1075. if (!isset($aData['invited_profile_id']))
  1076. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1077. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1078. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1079. return true;
  1080. }
  1081. public function getRole($iGroupProfileId, $iFanProfileId)
  1082. {
  1083. if(!$this->isFanByGroupProfileId($iGroupProfileId, $iFanProfileId))
  1084. return false;
  1085. return $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1086. }
  1087. public function setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  1088. {
  1089. $CNF = &$this->_oConfig->CNF;
  1090. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1091. return false;
  1092. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1093. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1094. if(!$oConnection || !$oGroupProfile)
  1095. return false;
  1096. if(!$oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !($oConnection->addConnection($iFanProfileId, $iGroupProfileId) && $oConnection->addConnection($iGroupProfileId, $iFanProfileId)))
  1097. return false;
  1098. if(!$this->_oDb->setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod, $sOrder))
  1099. return false;
  1100. $this->onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole);
  1101. return true;
  1102. }
  1103. public function onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole)
  1104. {
  1105. $CNF = &$this->_oConfig->CNF;
  1106. $iProfileId = bx_get_logged_profile_id();
  1107. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1108. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1109. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  1110. // notify about admin status
  1111. if(!empty($CNF['EMAIL_FAN_SET_ROLE']) && $iFanProfileId != $iProfileId)
  1112. sendMailTemplate($CNF['EMAIL_FAN_SET_ROLE'], 0, $iFanProfileId, array(
  1113. 'EntryUrl' => $oGroupProfile->getUrl(),
  1114. 'EntryTitle' => $oGroupProfile->getDisplayName(),
  1115. 'Role' => !is_array($mixedRole) ? $aRoles[(int)$mixedRole] : 'TODO: Add List of Roles'
  1116. ), BX_EMAIL_NOTIFY);
  1117. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1118. 'object_author_id' => $iGroupProfileId,
  1119. 'performer_id' => $iProfileId,
  1120. 'fan_id' => $iFanProfileId,
  1121. 'content' => $aGroupProfileInfo,
  1122. 'role' => $mixedRole,
  1123. 'group_profile' => $iGroupProfileId,
  1124. 'profile' => $iProfileId
  1125. ));
  1126. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1127. }
  1128. public function unsetRole($iGroupProfileId, $iFanProfileId)
  1129. {
  1130. $CNF = &$this->_oConfig->CNF;
  1131. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1132. return false;
  1133. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1134. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1135. if(!$oConnection || !$oGroupProfile)
  1136. return false;
  1137. if($oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->removeConnection($iFanProfileId, $iGroupProfileId))
  1138. return false;
  1139. $iRole = $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1140. if(!$this->_oDb->unsetRole($iGroupProfileId, $iFanProfileId))
  1141. return false;
  1142. $this->onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole);
  1143. return true;
  1144. }
  1145. public function onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole)
  1146. {
  1147. $CNF = &$this->_oConfig->CNF;
  1148. $iProfileId = bx_get_logged_profile_id();
  1149. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1150. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1151. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1152. 'object_author_id' => $iGroupProfileId,
  1153. 'performer_id' => $iProfileId,
  1154. 'fan_id' => $iFanProfileId,
  1155. 'content' => $aGroupProfileInfo,
  1156. 'role' => $iRole,
  1157. 'group_profile' => $iGroupProfileId,
  1158. 'profile' => $iProfileId
  1159. ));
  1160. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1161. }
  1162. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  1163. {
  1164. $CNF = &$this->_oConfig->CNF;
  1165. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  1166. $sSrc = '';
  1167. if(isset($CNF['FIELD_COVER']) && !empty($aContentInfo[$CNF['FIELD_COVER']]))
  1168. $sSrc = $oGroupProfile->getCover();
  1169. if(empty($sSrc) && isset($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  1170. $sSrc = $oGroupProfile->getPicture();
  1171. return empty($sSrc) ? array() : array(
  1172. array('id' => $aContentInfo[$CNF['FIELD_PICTURE']], 'url' => $sUrl, 'src' => $sSrc, 'src_orig' => $sSrc),
  1173. );
  1174. }
  1175. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  1176. {
  1177. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  1178. return array(0, 0, null);
  1179. if ($oGroupProfile->getModule() == $this->getName()) {
  1180. $iProfileId = $iInitiatorId;
  1181. $iGroupProfileId = $oGroupProfile->id();
  1182. } else {
  1183. $iProfileId = $oGroupProfile->id();
  1184. $iGroupProfileId = $iInitiatorId;
  1185. }
  1186. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  1187. }
  1188. }
  1189. /** @} */