BxBaseModGroupsModule.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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. );
  555. }
  556. public function serviceGetCartItems($iSellerId)
  557. {
  558. $CNF = &$this->_oConfig->CNF;
  559. if(empty($iSellerId))
  560. return array();
  561. $sModule = $this->getName();
  562. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  563. $aGroups = $this->_oDb->getEntriesBy(array('type' => 'author', 'author' => $iSellerId));
  564. $aResult = array();
  565. foreach($aGroups as $aGroup) {
  566. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aGroup[$CNF['FIELD_ID']], $sModule);
  567. if(!$oGroupProfile)
  568. continue;
  569. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $oGroupProfile->id()));
  570. if(empty($aPrices) || !is_array($aPrices))
  571. continue;
  572. $sTitle = $oGroupProfile->getDisplayName();
  573. $sUrl = $oGroupProfile->getUrl();
  574. foreach($aPrices as $aPrice)
  575. $aResult[] = array(
  576. 'id' => $aPrice['id'],
  577. 'author_id' => $iSellerId,
  578. 'name' => $aPrice['name'],
  579. 'title' => _t($CNF['T']['txt_cart_item_title'], $sTitle, $aRoles[$aPrice['role_id']], $aPrice['period'], $aPrice['period_unit']),
  580. 'description' => '',
  581. 'url' => $sUrl,
  582. 'price_single' => $aPrice['price'],
  583. 'price_recurring' => $aPrice['price'],
  584. 'period_recurring' => $aPrice['period'],
  585. 'period_unit_recurring' => $aPrice['period_unit']
  586. );
  587. }
  588. return $aResult;
  589. }
  590. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  591. {
  592. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  593. }
  594. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  595. {
  596. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  597. }
  598. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  599. {
  600. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  601. }
  602. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  603. {
  604. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  605. }
  606. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  607. {
  608. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  609. }
  610. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  611. {
  612. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  613. }
  614. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  615. {
  616. return true;
  617. }
  618. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  619. {
  620. $CNF = &$this->_oConfig->CNF;
  621. $aItem = $this->serviceGetCartItem($iItemId);
  622. if(empty($aItem) || !is_array($aItem))
  623. return array();
  624. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  625. $mixedPeriod = false;
  626. if((int)$aItemInfo['period'] != 0)
  627. $mixedPeriod = array(
  628. 'period' => (int)$aItemInfo['period'],
  629. 'period_unit' => $aItemInfo['period_unit'],
  630. 'period_reserve' => $CNF['PARAM_RECURRING_RESERVE']
  631. );
  632. if(!$this->setRole($aItemInfo['profile_id'], $iClientId, $aItemInfo['role_id'], $mixedPeriod, $sOrder))
  633. return array();
  634. return $aItem;
  635. }
  636. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  637. {
  638. $aItem = $this->serviceGetCartItem($iItemIdNew);
  639. if(empty($aItem) || !is_array($aItem))
  640. return array();
  641. $aItemInfoOld = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdOld));
  642. if(empty($aItemInfoOld) || !is_array($aItemInfoOld))
  643. return array();
  644. $aItemInfoNew = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdNew));
  645. if(empty($aItemInfoNew) || !is_array($aItemInfoNew))
  646. return array();
  647. if(!$this->unsetRole($aItemInfoOld['profile_id'], $iClientId))
  648. return array();
  649. $aResult = $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemIdNew, 1, $sOrder);
  650. if(empty($aResult) || !is_array($aResult))
  651. return array();
  652. return $aItem;
  653. }
  654. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  655. {
  656. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  657. if(empty($aItemInfo) || !is_array($aItemInfo))
  658. return false;
  659. return $this->unsetRole($aItemInfo['profile_id'], $iClientId);
  660. }
  661. /**
  662. * Data for Notifications module
  663. */
  664. public function serviceGetNotificationsData()
  665. {
  666. $sModule = $this->_aModule['name'];
  667. $aSettingsTypes = array('follow_member', 'follow_context');
  668. if($this->serviceActAsProfile())
  669. $aSettingsTypes = array('personal', 'follow_member');
  670. return array(
  671. 'handlers' => array(
  672. array('group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'),
  673. array('group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'),
  674. 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'),
  675. 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'),
  676. 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'),
  677. 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'),
  678. 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']),
  679. 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'),
  680. ),
  681. 'settings' => array(
  682. array('group' => 'vote', 'unit' => $sModule, 'action' => 'doVote', 'types' => $aSettingsTypes),
  683. array('group' => 'score_up', 'unit' => $sModule, 'action' => 'doVoteUp', 'types' => $aSettingsTypes),
  684. array('group' => 'score_down', 'unit' => $sModule, 'action' => 'doVoteDown', 'types' => $aSettingsTypes),
  685. array('group' => 'fan', 'unit' => $sModule, 'action' => 'fan_added', 'types' => $aSettingsTypes),
  686. array('group' => 'invite', 'unit' => $sModule, 'action' => 'join_invitation_notif', 'types' => array('personal')),
  687. array('group' => 'join', 'unit' => $sModule, 'action' => 'join_request', 'types' => $aSettingsTypes),
  688. array('group' => 'timeline_post', 'unit' => $sModule, 'action' => 'timeline_post_common', 'types' => $aSettingsTypes)
  689. ),
  690. 'alerts' => array(
  691. array('unit' => $sModule, 'action' => 'doVote'),
  692. array('unit' => $sModule, 'action' => 'undoVote'),
  693. array('unit' => $sModule, 'action' => 'doVoteUp'),
  694. array('unit' => $sModule, 'action' => 'doVoteDown'),
  695. array('unit' => $sModule, 'action' => 'fan_added'),
  696. array('unit' => $sModule, 'action' => 'join_invitation_notif'),
  697. array('unit' => $sModule, 'action' => 'join_request'),
  698. array('unit' => $sModule, 'action' => 'timeline_post_common'),
  699. )
  700. );
  701. }
  702. /**
  703. * Notification about new invitation to join the group
  704. */
  705. public function serviceGetNotificationsJoinInvitation($aEvent)
  706. {
  707. $CNF = &$this->_oConfig->CNF;
  708. $iContentId = (int)$aEvent['object_id'];
  709. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  710. if(!$oGroupProfile)
  711. return array();
  712. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  713. if(empty($aContentInfo) || !is_array($aContentInfo))
  714. return array();
  715. /*
  716. * It's essential that 'object_owner_id' contains invited member profile id.
  717. */
  718. $oProfile = BxDolProfile::getInstance((int)$aEvent['object_owner_id']);
  719. if(!$oProfile)
  720. return array();
  721. /*
  722. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  723. * because Subentry URL has higher display priority and notification
  724. * should be linked to Group Profile (Group Profile -> Members tab)
  725. * instead of Personal Profile of invited member.
  726. */
  727. $sEntryUrl = $oGroupProfile->getUrl();
  728. return array(
  729. 'entry_sample' => $CNF['T']['txt_sample_single'],
  730. 'entry_url' => $sEntryUrl,
  731. 'entry_caption' => $oGroupProfile->getDisplayName(),
  732. 'subentry_sample' => $oProfile->getDisplayName(),
  733. 'subentry_url' => $sEntryUrl,
  734. 'lang_key' => $this->_oConfig->CNF['T']['txt_ntfs_join_invitation']
  735. );
  736. }
  737. /**
  738. * Notification about new member requst in the group
  739. */
  740. public function serviceGetNotificationsJoinRequest($aEvent)
  741. {
  742. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_join_request']);
  743. }
  744. /**
  745. * Notification about new member in the group
  746. */
  747. public function serviceGetNotificationsFanAdded($aEvent)
  748. {
  749. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_fan_added']);
  750. }
  751. protected function _serviceGetNotification($aEvent, $sLangKey)
  752. {
  753. $CNF = &$this->_oConfig->CNF;
  754. $iContentId = (int)$aEvent['object_id'];
  755. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  756. if(!$oGroupProfile)
  757. return array();
  758. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  759. if(empty($aContentInfo) || !is_array($aContentInfo))
  760. return array();
  761. $oProfile = BxDolProfile::getInstance((int)$aEvent['subobject_id']);
  762. if(!$oProfile)
  763. return array();
  764. /*
  765. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  766. * because Subentry URL has higher display priority and notification
  767. * should be linked to Group Profile (Group Profile -> Members tab)
  768. * instead of Personal Profile of a member, who performed an action.
  769. */
  770. $sEntryUrl = $oGroupProfile->getUrl();
  771. if(!empty($CNF['URL_ENTRY_FANS']))
  772. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_FANS'], array(
  773. 'profile_id' => $oGroupProfile->id()
  774. ));
  775. return array(
  776. 'entry_sample' => $CNF['T']['txt_sample_single'],
  777. 'entry_url' => $sEntryUrl,
  778. 'entry_caption' => $oGroupProfile->getDisplayName(),
  779. 'entry_author' => $oGroupProfile->id(),
  780. 'subentry_sample' => $oProfile->getDisplayName(),
  781. 'subentry_url' => $sEntryUrl,
  782. 'lang_key' => $sLangKey
  783. );
  784. }
  785. /**
  786. * Data for Timeline module
  787. */
  788. public function serviceGetTimelineData()
  789. {
  790. return BxBaseModGeneralModule::serviceGetTimelineData();
  791. }
  792. /**
  793. * Entry post for Timeline module
  794. */
  795. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  796. {
  797. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  798. if($a === false)
  799. return false;
  800. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  801. $a['content']['url'] = $oGroupProfile->getUrl();
  802. $a['content']['title'] = $oGroupProfile->getDisplayName();
  803. return $a;
  804. }
  805. // ====== PERMISSION METHODS
  806. /**
  807. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  808. */
  809. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  810. {
  811. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  812. }
  813. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  814. {
  815. $CNF = &$this->_oConfig->CNF;
  816. $bInvited = false;
  817. if(!empty($CNF['TABLE_INVITES']) && bx_get('key')){
  818. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  819. $mixedInvited = $this->isInvited(bx_get('key'), $oGroupProfile->id());
  820. if($mixedInvited === true)
  821. $bInvited = true;
  822. }
  823. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $iProfileId) || $bInvited)
  824. return CHECK_ACTION_RESULT_ALLOWED;
  825. return parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId);
  826. }
  827. /**
  828. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  829. */
  830. public function checkAllowedCompose(&$aDataEntry, $isPerformAction = false)
  831. {
  832. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  833. return _t('_sys_txt_access_denied');
  834. return parent::checkAllowedCompose ($aDataEntry, $isPerformAction);
  835. }
  836. /**
  837. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  838. */
  839. public function checkAllowedFanAdd(&$aDataEntry, $isPerformAction = false)
  840. {
  841. $mixedResult = $this->_modGroupsCheckAllowedFanAdd($aDataEntry, $isPerformAction);
  842. // call alert to allow custom checks
  843. bx_alert('system', 'check_allowed_fan_add', 0, 0, array(
  844. 'module' => $this->getName(),
  845. 'content_info' => $aDataEntry,
  846. 'profile_id' => bx_get_logged_profile_id(),
  847. 'override_result' => &$mixedResult
  848. ));
  849. return $mixedResult;
  850. }
  851. public function _modGroupsCheckAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  852. {
  853. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) || !isLogged())
  854. return _t('_sys_txt_access_denied');
  855. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], true, false);
  856. }
  857. /**
  858. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  859. */
  860. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  861. {
  862. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  863. return CHECK_ACTION_RESULT_ALLOWED;
  864. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  865. }
  866. protected function _checkAllowedActionByFan($sAction, $aDataEntry, $iProfileId = 0)
  867. {
  868. $CNF = &$this->_oConfig->CNF;
  869. $bRoles = $this->_oConfig->isRoles();
  870. if(empty($iProfileId))
  871. $iProfileId = bx_get_logged_profile_id();
  872. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  873. if(!$oGroupProfile)
  874. return $sAction == BX_BASE_MOD_GROUPS_ACTION_DELETE ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_not_found');
  875. $iGroupProfileId = $oGroupProfile->id();
  876. if(!$bRoles && $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aDataEntry))
  877. return CHECK_ACTION_RESULT_ALLOWED;
  878. if($bRoles && $this->isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId))
  879. return CHECK_ACTION_RESULT_ALLOWED;
  880. return _t('_sys_txt_access_denied');
  881. }
  882. public function isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId)
  883. {
  884. $bResult = false;
  885. $iProfileRole = $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  886. if(!empty($iProfileRole))
  887. switch($sAction) {
  888. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  889. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR))
  890. $bResult = true;
  891. break;
  892. default:
  893. $bResult = true;
  894. }
  895. // call alert to allow custom checks
  896. bx_alert('system', 'check_allowed_action_by_role', 0, 0, array(
  897. 'module' => $this->getName(),
  898. 'multi_roles' => $this->_oConfig->isMultiRoles(),
  899. 'action' => $sAction,
  900. 'content_profile_id' => $iGroupProfileId,
  901. 'content_info' => $aDataEntry,
  902. 'profile_id' => $iProfileId,
  903. 'profile_role' => $iProfileRole,
  904. 'override_result' => &$bResult
  905. ));
  906. return $bResult;
  907. }
  908. public function isRole($iProfileRole, $iRole)
  909. {
  910. if(!$this->_oConfig->isMultiRoles())
  911. return $iProfileRole == $iRole;
  912. else
  913. return $iProfileRole & (1 << ($iRole - 1));
  914. }
  915. public function serviceIsRole($iProfileRole, $iRole)
  916. {
  917. return $this->isRole($iProfileRole, $iRole);
  918. }
  919. public function checkAllowedManageFans($mixedDataEntry, $isPerformAction = false)
  920. {
  921. $aDataEntry = array();
  922. if(!is_array($mixedDataEntry)) {
  923. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  924. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  925. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  926. }
  927. else
  928. $aDataEntry = $mixedDataEntry;
  929. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  930. return CHECK_ACTION_RESULT_ALLOWED;
  931. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  932. }
  933. public function checkAllowedManageAdmins($mixedDataEntry, $isPerformAction = false)
  934. {
  935. $aDataEntry = array();
  936. if(!is_array($mixedDataEntry)) {
  937. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  938. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  939. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  940. }
  941. else
  942. $aDataEntry = $mixedDataEntry;
  943. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  944. return CHECK_ACTION_RESULT_ALLOWED;
  945. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  946. }
  947. public function checkAllowedEdit($aDataEntry, $isPerformAction = false)
  948. {
  949. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_EDIT, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  950. return CHECK_ACTION_RESULT_ALLOWED;
  951. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  952. }
  953. public function checkAllowedInvite($aDataEntry, $isPerformAction = false)
  954. {
  955. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_INVITE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  956. return CHECK_ACTION_RESULT_ALLOWED;
  957. return $this->checkAllowedEdit ($aDataEntry, $isPerformAction);
  958. }
  959. public function checkAllowedChangeCover($aDataEntry, $isPerformAction = false)
  960. {
  961. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  962. return CHECK_ACTION_RESULT_ALLOWED;
  963. return parent::checkAllowedChangeCover($aDataEntry, $isPerformAction);
  964. }
  965. public function checkAllowedDelete(&$aDataEntry, $isPerformAction = false)
  966. {
  967. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_DELETE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  968. return CHECK_ACTION_RESULT_ALLOWED;
  969. return parent::checkAllowedDelete($aDataEntry, $isPerformAction);
  970. }
  971. public function checkAllowedJoin(&$aDataEntry, $isPerformAction = false)
  972. {
  973. if (bx_get('key')){
  974. $sKey = bx_get('key');
  975. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  976. $aData = $this->_oDb->getInviteByKey($sKey, $oGroupProfile->id());
  977. if (isset($aData['invited_profile_id']) && $aData['invited_profile_id'] == bx_get_logged_profile_id()){
  978. return CHECK_ACTION_RESULT_ALLOWED;
  979. }
  980. }
  981. return _t('_sys_txt_access_denied');
  982. }
  983. public function checkAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  984. {
  985. $mixedResult = $this->_modGroupsCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  986. // call alert to allow custom checks
  987. bx_alert('system', 'check_allowed_subscribe_add', 0, 0, array(
  988. 'module' => $this->getName(),
  989. 'content_info' => $aDataEntry,
  990. 'profile_id' => bx_get_logged_profile_id(),
  991. 'override_result' => &$mixedResult
  992. ));
  993. return $mixedResult;
  994. }
  995. /**
  996. * Note. Is mainly needed for internal usage. Access level is 'public' to allow outer calls from alerts.
  997. */
  998. public function _modGroupsCheckAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  999. {
  1000. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  1001. return _t('_sys_txt_access_denied');
  1002. return parent::_modProfileCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1003. }
  1004. /**
  1005. * @deprecated since version 11.0.3 and can be removed in the next version.
  1006. */
  1007. public function _checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  1008. {
  1009. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  1010. }
  1011. public function doAudit($iGroupProfileId, $iFanId, $sAction)
  1012. {
  1013. $oProfile = BxDolProfile::getInstance($iFanId);
  1014. $iContentId = $oProfile->getContentId();
  1015. $sModule = $oProfile->getModule();
  1016. $oModule = BxDolModule::getInstance($sModule);
  1017. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile') && $oModule->_oConfig){
  1018. $CNF = $oModule->_oConfig->CNF;
  1019. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iContentId))) : array();
  1020. $AuditParams = array(
  1021. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  1022. 'context_profile_id' => $iGroupProfileId,
  1023. 'context_profile_title' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName()
  1024. );
  1025. bx_audit(
  1026. $iContentId,
  1027. $sModule,
  1028. $sAction,
  1029. $AuditParams
  1030. );
  1031. }
  1032. }
  1033. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  1034. {
  1035. $sResult = $this->checkAllowedView($aDataEntry);
  1036. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  1037. // if profile view isn't allowed but visibility is in partially visible groups
  1038. // then display buttons to connect (befriend, join) to profile,
  1039. // if other conditions (in parent::_checkAllowedConnect) are met as well
  1040. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && !in_array($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), array('s'))))
  1041. return $sResult;
  1042. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  1043. }
  1044. public function addFollower ($iProfileId1, $iProfileId2)
  1045. {
  1046. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  1047. if($oConnectionFollow && !$oConnectionFollow->isConnected($iProfileId1, $iProfileId2)){
  1048. $oConnectionFollow->addConnection($iProfileId1, $iProfileId2);
  1049. return true;
  1050. }
  1051. return false;
  1052. }
  1053. public function isFan ($iContentId, $iProfileId = false)
  1054. {
  1055. $CNF = &$this->_oConfig->CNF;
  1056. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  1057. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1058. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1059. return false;
  1060. }
  1061. public function isFanByGroupProfileId ($iGroupProfileId, $iProfileId = false)
  1062. {
  1063. $CNF = &$this->_oConfig->CNF;
  1064. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1065. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1066. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1067. return false;
  1068. }
  1069. public function isInvited ($sKey, $iProfileId)
  1070. {
  1071. $CNF = &$this->_oConfig->CNF;
  1072. $aData = $this->_oDb->getInviteByKey($sKey, $iProfileId);
  1073. if (!isset($aData['invited_profile_id']))
  1074. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1075. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1076. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1077. return true;
  1078. }
  1079. public function getRole($iGroupProfileId, $iFanProfileId)
  1080. {
  1081. if(!$this->isFanByGroupProfileId($iGroupProfileId, $iFanProfileId))
  1082. return false;
  1083. return $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1084. }
  1085. public function setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  1086. {
  1087. $CNF = &$this->_oConfig->CNF;
  1088. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1089. return false;
  1090. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1091. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1092. if(!$oConnection || !$oGroupProfile)
  1093. return false;
  1094. if(!$oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !($oConnection->addConnection($iFanProfileId, $iGroupProfileId) && $oConnection->addConnection($iGroupProfileId, $iFanProfileId)))
  1095. return false;
  1096. if(!$this->_oDb->setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod, $sOrder))
  1097. return false;
  1098. $this->onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole);
  1099. return true;
  1100. }
  1101. public function onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole)
  1102. {
  1103. $CNF = &$this->_oConfig->CNF;
  1104. $iProfileId = bx_get_logged_profile_id();
  1105. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1106. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1107. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  1108. // notify about admin status
  1109. if(!empty($CNF['EMAIL_FAN_SET_ROLE']) && $iFanProfileId != $iProfileId)
  1110. sendMailTemplate($CNF['EMAIL_FAN_SET_ROLE'], 0, $iFanProfileId, array(
  1111. 'EntryUrl' => $oGroupProfile->getUrl(),
  1112. 'EntryTitle' => $oGroupProfile->getDisplayName(),
  1113. 'Role' => !is_array($mixedRole) ? $aRoles[(int)$mixedRole] : 'TODO: Add List of Roles'
  1114. ), BX_EMAIL_NOTIFY);
  1115. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1116. 'object_author_id' => $iGroupProfileId,
  1117. 'performer_id' => $iProfileId,
  1118. 'fan_id' => $iFanProfileId,
  1119. 'content' => $aGroupProfileInfo,
  1120. 'role' => $mixedRole,
  1121. 'group_profile' => $iGroupProfileId,
  1122. 'profile' => $iProfileId
  1123. ));
  1124. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1125. }
  1126. public function unsetRole($iGroupProfileId, $iFanProfileId)
  1127. {
  1128. $CNF = &$this->_oConfig->CNF;
  1129. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1130. return false;
  1131. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1132. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1133. if(!$oConnection || !$oGroupProfile)
  1134. return false;
  1135. if($oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->removeConnection($iFanProfileId, $iGroupProfileId))
  1136. return false;
  1137. $iRole = $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1138. if(!$this->_oDb->unsetRole($iGroupProfileId, $iFanProfileId))
  1139. return false;
  1140. $this->onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole);
  1141. return true;
  1142. }
  1143. public function onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole)
  1144. {
  1145. $CNF = &$this->_oConfig->CNF;
  1146. $iProfileId = bx_get_logged_profile_id();
  1147. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1148. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1149. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1150. 'object_author_id' => $iGroupProfileId,
  1151. 'performer_id' => $iProfileId,
  1152. 'fan_id' => $iFanProfileId,
  1153. 'content' => $aGroupProfileInfo,
  1154. 'role' => $iRole,
  1155. 'group_profile' => $iGroupProfileId,
  1156. 'profile' => $iProfileId
  1157. ));
  1158. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1159. }
  1160. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  1161. {
  1162. $CNF = &$this->_oConfig->CNF;
  1163. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  1164. $sSrc = '';
  1165. if(isset($CNF['FIELD_COVER']) && !empty($aContentInfo[$CNF['FIELD_COVER']]))
  1166. $sSrc = $oGroupProfile->getCover();
  1167. if(empty($sSrc) && isset($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  1168. $sSrc = $oGroupProfile->getPicture();
  1169. return empty($sSrc) ? array() : array(
  1170. array('id' => $aContentInfo[$CNF['FIELD_PICTURE']], 'url' => $sUrl, 'src' => $sSrc, 'src_orig' => $sSrc),
  1171. );
  1172. }
  1173. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  1174. {
  1175. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  1176. return array(0, 0, null);
  1177. if ($oGroupProfile->getModule() == $this->getName()) {
  1178. $iProfileId = $iInitiatorId;
  1179. $iGroupProfileId = $oGroupProfile->id();
  1180. } else {
  1181. $iProfileId = $oGroupProfile->id();
  1182. $iGroupProfileId = $iInitiatorId;
  1183. }
  1184. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  1185. }
  1186. }
  1187. /** @} */