BxBaseModGroupsModule.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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. array('key' => BX_BASE_MOD_GROUPS_MMODE_PAID_JOIN, 'value' => _t($CNF['T']['option_members_mode_' . BX_BASE_MOD_GROUPS_MMODE_PAID_JOIN]))
  74. );
  75. }
  76. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  77. {
  78. if(empty($sUnitTemplate))
  79. $sUnitTemplate = 'unit.html';
  80. return parent::serviceGetSearchResultUnit($iContentId, $sUnitTemplate);
  81. }
  82. /**
  83. * @see BxBaseModProfileModule::serviceGetSpaceTitle
  84. */
  85. public function serviceGetSpaceTitle()
  86. {
  87. return _t($this->_oConfig->CNF['T']['txt_sample_single']);
  88. }
  89. /**
  90. * @see iBxDolProfileService::serviceGetParticipatingProfiles
  91. */
  92. public function serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects = false)
  93. {
  94. if (isset($this->_oConfig->CNF['OBJECT_CONNECTIONS'])){
  95. $aConnectionObjects = array($this->_oConfig->CNF['OBJECT_CONNECTIONS'], 'sys_profiles_subscriptions');
  96. return parent::serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects);
  97. }
  98. return parent::serviceGetParticipatingProfiles($iProfileId);
  99. }
  100. /**
  101. * Check if this module entry can be used as profile
  102. */
  103. public function serviceActAsProfile ()
  104. {
  105. return false;
  106. }
  107. /**
  108. * Check if this module is group profile
  109. */
  110. public function serviceIsGroupProfile ()
  111. {
  112. return true;
  113. }
  114. public function serviceIsEnableForContext($iProfileId = 0)
  115. {
  116. $CNF = &$this->_oConfig->CNF;
  117. $sCnfKey = 'ENABLE_FOR_CONTEXT_IN_MODULES';
  118. if(empty($iProfileId) || empty($CNF[$sCnfKey]) || !is_array($CNF[$sCnfKey]))
  119. return false;
  120. if(in_array(BxDolProfile::getInstance($iProfileId)->getModule(), $CNF[$sCnfKey]))
  121. return true;
  122. return false;
  123. }
  124. /**
  125. * check if provided profile is member of the group
  126. */
  127. public function serviceIsFan ($iGroupProfileId, $iProfileId = false)
  128. {
  129. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  130. return $this->isFan($oGroupProfile->getContentId(), $iProfileId);
  131. }
  132. /**
  133. * check if provided profile is admin of the group
  134. */
  135. public function serviceIsAdmin ($iGroupProfileId, $iProfileId = false)
  136. {
  137. if(!$iProfileId)
  138. $iProfileId = bx_get_logged_profile_id();
  139. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  140. $iGroupContentId = $oGroupProfile->getContentId();
  141. if(!$this->isFan($iGroupContentId, $iProfileId))
  142. return false;
  143. $aGroupContentInfo = $this->_oDb->getContentInfoById($iGroupContentId);
  144. return $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aGroupContentInfo);
  145. }
  146. public function serviceGetAdminRole($iGroupProfileId, $iProfileId = false)
  147. {
  148. if(!$iProfileId)
  149. $iProfileId = bx_get_logged_profile_id();
  150. if(!$this->serviceIsAdmin($iGroupProfileId, $iProfileId))
  151. return 0;
  152. return $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  153. }
  154. /**
  155. * Delete profile from fans and admins tables
  156. * @param $iProfileId profile id
  157. */
  158. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  159. {
  160. $CNF = &$this->_oConfig->CNF;
  161. $this->_oDb->deleteAdminsByProfileId($iProfileId);
  162. if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  163. $oConnection->onDeleteInitiatorAndContent($iProfileId);
  164. }
  165. /**
  166. * Reset group's author for particular group
  167. * @param $iContentId group id
  168. * @return false of error, or number of updated records on success
  169. */
  170. public function serviceReassignEntityAuthor ($iContentId)
  171. {
  172. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  173. if (!$aContentInfo)
  174. return false;
  175. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  176. return false;
  177. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  178. return $this->_oDb->updateAuthorById($iContentId, $aAdmins ? array_pop($aAdmins) : 0);
  179. }
  180. /**
  181. * Entry actions and social sharing block
  182. */
  183. public function serviceEntityAllActions ($mixedContent = false, $aParams = array())
  184. {
  185. $CNF = &$this->_oConfig->CNF;
  186. if(!empty($mixedContent)) {
  187. if(!is_array($mixedContent))
  188. $mixedContent = array((int)$mixedContent, (method_exists($this->_oDb, 'getContentInfoById')) ? $this->_oDb->getContentInfoById((int)$mixedContent) : array());
  189. }
  190. else {
  191. $mixedContent = $this->_getContent();
  192. if($mixedContent === false)
  193. return false;
  194. }
  195. list($iContentId, $aContentInfo) = $mixedContent;
  196. if(!empty($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  197. $aParams = array_merge(array(
  198. 'entry_thumb' => (int)$aContentInfo[$CNF['FIELD_PICTURE']]
  199. ), $aParams);
  200. return parent::serviceEntityAllActions ($mixedContent, $aParams);
  201. }
  202. /**
  203. * Reset group's author when author profile is deleted
  204. * @param $iProfileId profile id
  205. * @return number of changed items
  206. */
  207. public function serviceReassignEntitiesByAuthor ($iProfileId)
  208. {
  209. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  210. if (!$a)
  211. return 0;
  212. $iCount = 0;
  213. foreach ($a as $aContentInfo)
  214. $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']]) ? 1 : 0);
  215. return $iCount;
  216. }
  217. public function servicePrepareFields ($aFieldsProfile)
  218. {
  219. $CNF = &$this->_oConfig->CNF;
  220. $aFieldsProfile[$CNF['FIELD_NAME']] = $aFieldsProfile['name'];
  221. $aFieldsProfile[$CNF['FIELD_TEXT']] = isset($aFieldsProfile['description']) ? $aFieldsProfile['description'] : '';
  222. unset($aFieldsProfile['name']);
  223. unset($aFieldsProfile['description']);
  224. return $aFieldsProfile;
  225. }
  226. public function serviceOnRemoveConnection ($iGroupProfileId, $iInitiatorId)
  227. {
  228. $CNF = &$this->_oConfig->CNF;
  229. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  230. if (!$oGroupProfile)
  231. return false;
  232. $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
  233. if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
  234. return $oConn->removeConnection($iProfileId, $iGroupProfileId);
  235. return false;
  236. }
  237. public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $bSendInviteOnly = false)
  238. {
  239. $CNF = &$this->_oConfig->CNF;
  240. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  241. if (!$oGroupProfile)
  242. return false;
  243. if (!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  244. return false;
  245. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  246. return false;
  247. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  248. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  249. // send invitation to the group
  250. $sModule = $this->getName();
  251. if ($bSendInviteOnly && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  252. bx_alert($sModule, 'join_invitation', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  253. 'content' => $aContentInfo,
  254. 'entry_title' => $sEntryTitle,
  255. 'entry_url' => $sEntryUrl,
  256. 'group_profile' => $iGroupProfileId,
  257. 'profile' => $iProfileId,
  258. 'notification_subobject_id' => $iProfileId,
  259. 'object_author_id' => $iGroupProfileId
  260. ));
  261. /**
  262. * 'Invitation Received' alert for Notifications module.
  263. * Note. It's essential to use Recipient ($iInitiatorId) in 'object_author_id' parameter.
  264. * In this case notification will be received by Recipient profile.
  265. */
  266. bx_alert($sModule, 'join_invitation_notif', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  267. 'object_author_id' => $iInitiatorId,
  268. 'privacy_view' => isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 3,
  269. ));
  270. }
  271. // send notification to group's admins that new connection is pending confirmation
  272. elseif (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  273. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  274. 'object_author_id' => $iGroupProfileId,
  275. 'performer_id' => $iProfileId,
  276. 'content' => $aContentInfo,
  277. 'entry_title' => $sEntryTitle,
  278. 'entry_url' => $sEntryUrl,
  279. 'group_profile' => $iGroupProfileId,
  280. 'profile' => $iProfileId
  281. ));
  282. }
  283. // send notification that join request was accepted
  284. else if (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  285. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  286. 'object_author_id' => $iGroupProfileId,
  287. 'performer_id' => $iProfileId,
  288. 'content' => $aContentInfo,
  289. 'entry_title' => $sEntryTitle,
  290. 'entry_url' => $sEntryUrl,
  291. 'group_profile' => $iGroupProfileId,
  292. 'profile' => $iProfileId
  293. ));
  294. }
  295. // new fan was added
  296. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  297. // follow group on join
  298. if (BxDolService::call($oGroupProfile->getModule(), 'act_as_profile')){
  299. $this->addFollower($oGroupProfile->id(), (int)$iInitiatorId);
  300. }
  301. else{
  302. $this->addFollower((int)$iInitiatorId, $oGroupProfile->id());
  303. }
  304. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  305. 'object_author_id' => $iGroupProfileId,
  306. 'performer_id' => $iProfileId,
  307. 'content' => $aContentInfo,
  308. 'entry_title' => $sEntryTitle,
  309. 'entry_url' => $sEntryUrl,
  310. 'group_profile' => $iGroupProfileId,
  311. 'profile' => $iProfileId,
  312. ));
  313. $this->doAudit($iGroupProfileId, $iInitiatorId, '_sys_audit_action_group_join_request_accepted');
  314. return false;
  315. }
  316. // don't automatically add connection (mutual) if group requires manual join confirmation
  317. if ($bSendInviteOnly || $aContentInfo['join_confirmation'])
  318. return false;
  319. // check if connection already exists
  320. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  321. return false;
  322. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  323. return false;
  324. return true;
  325. }
  326. public function serviceFansTable ()
  327. {
  328. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_CONNECTIONS']);
  329. if (!$oGrid)
  330. return false;
  331. return $oGrid->getCode();
  332. }
  333. public function serviceInvitesTable ()
  334. {
  335. if (!isset($this->_oConfig->CNF['OBJECT_GRID_INVITES']))
  336. return false;
  337. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_INVITES']);
  338. if (!$oGrid)
  339. return false;
  340. return $oGrid->getCode();
  341. }
  342. public function serviceFans ($iContentId = 0, $bAsArray = false)
  343. {
  344. if (!$iContentId)
  345. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  346. if (!$iContentId)
  347. return false;
  348. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  349. if (!$aContentInfo)
  350. return false;
  351. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  352. return false;
  353. if(!$bAsArray) {
  354. bx_import('BxDolConnection');
  355. $mixedResult = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $this->_oConfig->CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  356. if (!$mixedResult)
  357. return MsgBox(_t('_sys_txt_empty'));
  358. }
  359. else
  360. $mixedResult = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  361. return $mixedResult;
  362. }
  363. public function serviceFansWithoutAdmins ($iContentId = 0, $bAsArray = false)
  364. {
  365. if (!$iContentId)
  366. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  367. if (!$iContentId)
  368. return false;
  369. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  370. if (!$aContentInfo)
  371. return false;
  372. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  373. return false;
  374. $CNF = &$this->_oConfig->CNF;
  375. $aFans = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  376. if(empty($aFans) || !is_array($aFans))
  377. return false;
  378. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  379. if(!empty($aAdmins) && is_array($aAdmins))
  380. $aFans = array_diff($aFans, $aAdmins);
  381. $iStart = (int)bx_get('start');
  382. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  383. if(!$iLimit)
  384. $iLimit = 4;
  385. return $this->_serviceBrowseQuick($aFans, $iStart, $iLimit);
  386. }
  387. public function serviceAdmins ($iContentId = 0)
  388. {
  389. $CNF = &$this->_oConfig->CNF;
  390. if(!$iContentId)
  391. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  392. if(!$iContentId)
  393. return false;
  394. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  395. if(!$oGroupProfile)
  396. return false;
  397. $iStart = (int)bx_get('start');
  398. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  399. if(!$iLimit)
  400. $iLimit = 4;
  401. $aProfiles = $this->_oDb->getAdmins($oGroupProfile->id(), $iStart, $iLimit+1);
  402. if(empty($aProfiles) || !is_array($aProfiles))
  403. return false;
  404. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  405. }
  406. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  407. {
  408. if (!$iProfileId)
  409. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  410. if (!$iProfileId)
  411. return '';
  412. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  413. }
  414. public function serviceBrowseCreatedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  415. {
  416. if (!$iProfileId)
  417. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  418. if (!$iProfileId)
  419. return '';
  420. return $this->_serviceBrowse ('created_entries', array('author' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  421. }
  422. public function serviceEntityPricing($iProfileId = 0)
  423. {
  424. $CNF = &$this->_oConfig->CNF;
  425. if(!$iProfileId)
  426. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  427. if(!$iProfileId)
  428. return '';
  429. if(!$this->_oConfig->isPaidJoin())
  430. return '';
  431. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  432. return MsgBox(_t('_Access denied'));
  433. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_MANAGE']);
  434. if(!$oGrid)
  435. return '';
  436. return $oGrid->getCode();
  437. }
  438. public function serviceEntityJoin($iProfileId = 0)
  439. {
  440. $CNF = &$this->_oConfig->CNF;
  441. if(!$iProfileId)
  442. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  443. if(!$iProfileId)
  444. return '';
  445. if(!$this->_oConfig->isPaidJoin())
  446. return '';
  447. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_VIEW']);
  448. if(!$oGrid)
  449. return '';
  450. return $oGrid->getCode();
  451. }
  452. public function serviceEntityInvite ($iContentId = 0, $bErrorMsg = true)
  453. {
  454. if (isset($this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE']))
  455. return $this->_serviceEntityForm ('editDataForm', $iContentId, $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE'], false, $bErrorMsg);
  456. return false;
  457. }
  458. /**
  459. * Entry social sharing block
  460. */
  461. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  462. {
  463. if(!empty($mixedContent)) {
  464. if(!is_array($mixedContent))
  465. $mixedContent = array((int)$mixedContent, array());
  466. }
  467. else {
  468. $mixedContent = $this->_getContent();
  469. if($mixedContent === false)
  470. return false;
  471. }
  472. list($iContentId, $aContentInfo) = $mixedContent;
  473. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  474. if(!$oGroupProfile)
  475. return false;
  476. return parent::serviceEntitySocialSharing(array($iContentId, $aContentInfo), array(
  477. 'title' => $oGroupProfile->getDisplayName()
  478. ));
  479. }
  480. public function serviceIsPricingAvaliable($iProfileId)
  481. {
  482. if(!$this->_oConfig->isPaidJoin())
  483. return false;
  484. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  485. return false;
  486. return true;
  487. }
  488. public function serviceIsPaidJoinAvaliable($iProfileId)
  489. {
  490. return $this->isPaidJoinByProfile($iProfileId);
  491. }
  492. public function serviceIsPaidJoinAvaliableByContent($iContentId)
  493. {
  494. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  495. if(!$oProfile)
  496. return false;
  497. return $this->isPaidJoinByProfile($oProfile->id());
  498. }
  499. public function serviceIsFreeJoinAvaliable($iProfileId)
  500. {
  501. return !$this->isPaidJoinByProfile($iProfileId);
  502. }
  503. public function serviceIsFreeJoinAvaliableByContent($iContentId)
  504. {
  505. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  506. if(!$oProfile)
  507. return false;
  508. return !$this->isPaidJoinByProfile($oProfile->id());
  509. }
  510. public function isPaidJoinByProfile($iProfileId)
  511. {
  512. if(!$this->_oConfig->isPaidJoin())
  513. return false;
  514. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $iProfileId));
  515. if(empty($aPrices) || !is_array($aPrices))
  516. return false;
  517. return true;
  518. }
  519. /**
  520. * Integration with Payments.
  521. */
  522. public function serviceGetPaymentData()
  523. {
  524. return $this->_aModule;
  525. }
  526. public function serviceGetCartItem($mixedItemId)
  527. {
  528. $CNF = &$this->_oConfig->CNF;
  529. if(!$mixedItemId)
  530. return array();
  531. if(is_numeric($mixedItemId))
  532. $aItem = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => (int)$mixedItemId));
  533. else
  534. $aItem = $this->_oDb->getPrices(array('type' => 'by_name', 'value' => $mixedItemId));
  535. if(empty($aItem) || !is_array($aItem))
  536. return array();
  537. if(!$this->isPaidJoinByProfile($aItem['profile_id']))
  538. return array();
  539. $oGroupProfile = BxDolProfile::getInstance($aItem['profile_id']);
  540. if(!$oGroupProfile)
  541. return array();
  542. $aGroupProfile = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  543. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  544. return array (
  545. 'id' => $aItem['id'],
  546. 'author_id' => $aGroupProfile[$CNF['FIELD_AUTHOR']],
  547. 'name' => $aItem['name'],
  548. 'title' => _t($CNF['T']['txt_cart_item_title'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']], $aItem['period'], $aItem['period_unit']),
  549. 'description' => '',
  550. 'url' => $oGroupProfile->getUrl(),
  551. 'price_single' => $aItem['price'],
  552. 'price_recurring' => $aItem['price'],
  553. 'period_recurring' => $aItem['period'],
  554. 'period_unit_recurring' => $aItem['period_unit']
  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. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  1002. return _t('_sys_txt_access_denied');
  1003. return parent::_modProfileCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1004. }
  1005. /**
  1006. * @deprecated since version 11.0.3 and can be removed in the next version.
  1007. */
  1008. public function _checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  1009. {
  1010. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  1011. }
  1012. public function doAudit($iGroupProfileId, $iFanId, $sAction)
  1013. {
  1014. $oProfile = BxDolProfile::getInstance($iFanId);
  1015. $iContentId = $oProfile->getContentId();
  1016. $sModule = $oProfile->getModule();
  1017. $oModule = BxDolModule::getInstance($sModule);
  1018. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile') && $oModule->_oConfig){
  1019. $CNF = $oModule->_oConfig->CNF;
  1020. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iContentId))) : array();
  1021. $AuditParams = array(
  1022. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  1023. 'context_profile_id' => $iGroupProfileId,
  1024. 'context_profile_title' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName()
  1025. );
  1026. bx_audit(
  1027. $iContentId,
  1028. $sModule,
  1029. $sAction,
  1030. $AuditParams
  1031. );
  1032. }
  1033. }
  1034. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  1035. {
  1036. $sResult = $this->checkAllowedView($aDataEntry);
  1037. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  1038. // if profile view isn't allowed but visibility is in partially visible groups
  1039. // then display buttons to connect (befriend, join) to profile,
  1040. // if other conditions (in parent::_checkAllowedConnect) are met as well
  1041. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && !in_array($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), array('s'))))
  1042. return $sResult;
  1043. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  1044. }
  1045. public function addFollower ($iProfileId1, $iProfileId2)
  1046. {
  1047. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  1048. if($oConnectionFollow && !$oConnectionFollow->isConnected($iProfileId1, $iProfileId2)){
  1049. $oConnectionFollow->addConnection($iProfileId1, $iProfileId2);
  1050. return true;
  1051. }
  1052. return false;
  1053. }
  1054. public function isFan ($iContentId, $iProfileId = false)
  1055. {
  1056. $CNF = &$this->_oConfig->CNF;
  1057. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  1058. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1059. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1060. return false;
  1061. }
  1062. public function isFanByGroupProfileId ($iGroupProfileId, $iProfileId = false)
  1063. {
  1064. $CNF = &$this->_oConfig->CNF;
  1065. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1066. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1067. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1068. return false;
  1069. }
  1070. public function isInvited ($sKey, $iProfileId)
  1071. {
  1072. $CNF = &$this->_oConfig->CNF;
  1073. $aData = $this->_oDb->getInviteByKey($sKey, $iProfileId);
  1074. if (!isset($aData['invited_profile_id']))
  1075. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1076. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1077. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1078. return true;
  1079. }
  1080. public function getRole($iGroupProfileId, $iFanProfileId)
  1081. {
  1082. if(!$this->isFanByGroupProfileId($iGroupProfileId, $iFanProfileId))
  1083. return false;
  1084. return $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1085. }
  1086. public function setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  1087. {
  1088. $CNF = &$this->_oConfig->CNF;
  1089. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1090. return false;
  1091. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1092. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1093. if(!$oConnection || !$oGroupProfile)
  1094. return false;
  1095. if(!$oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !($oConnection->addConnection($iFanProfileId, $iGroupProfileId) && $oConnection->addConnection($iGroupProfileId, $iFanProfileId)))
  1096. return false;
  1097. if(!$this->_oDb->setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod, $sOrder))
  1098. return false;
  1099. $this->onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole);
  1100. return true;
  1101. }
  1102. public function onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole)
  1103. {
  1104. $CNF = &$this->_oConfig->CNF;
  1105. $iProfileId = bx_get_logged_profile_id();
  1106. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1107. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1108. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  1109. // notify about admin status
  1110. if(!empty($CNF['EMAIL_FAN_SET_ROLE']) && $iFanProfileId != $iProfileId)
  1111. sendMailTemplate($CNF['EMAIL_FAN_SET_ROLE'], 0, $iFanProfileId, array(
  1112. 'EntryUrl' => $oGroupProfile->getUrl(),
  1113. 'EntryTitle' => $oGroupProfile->getDisplayName(),
  1114. 'Role' => !is_array($mixedRole) ? $aRoles[(int)$mixedRole] : 'TODO: Add List of Roles'
  1115. ), BX_EMAIL_NOTIFY);
  1116. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1117. 'object_author_id' => $iGroupProfileId,
  1118. 'performer_id' => $iProfileId,
  1119. 'fan_id' => $iFanProfileId,
  1120. 'content' => $aGroupProfileInfo,
  1121. 'role' => $mixedRole,
  1122. 'group_profile' => $iGroupProfileId,
  1123. 'profile' => $iProfileId
  1124. ));
  1125. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1126. }
  1127. public function unsetRole($iGroupProfileId, $iFanProfileId)
  1128. {
  1129. $CNF = &$this->_oConfig->CNF;
  1130. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1131. return false;
  1132. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1133. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1134. if(!$oConnection || !$oGroupProfile)
  1135. return false;
  1136. if($oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->removeConnection($iFanProfileId, $iGroupProfileId))
  1137. return false;
  1138. $iRole = $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1139. if(!$this->_oDb->unsetRole($iGroupProfileId, $iFanProfileId))
  1140. return false;
  1141. $this->onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole);
  1142. return true;
  1143. }
  1144. public function onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole)
  1145. {
  1146. $CNF = &$this->_oConfig->CNF;
  1147. $iProfileId = bx_get_logged_profile_id();
  1148. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1149. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1150. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1151. 'object_author_id' => $iGroupProfileId,
  1152. 'performer_id' => $iProfileId,
  1153. 'fan_id' => $iFanProfileId,
  1154. 'content' => $aGroupProfileInfo,
  1155. 'role' => $iRole,
  1156. 'group_profile' => $iGroupProfileId,
  1157. 'profile' => $iProfileId
  1158. ));
  1159. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1160. }
  1161. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  1162. {
  1163. $CNF = &$this->_oConfig->CNF;
  1164. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  1165. $sSrc = '';
  1166. if(isset($CNF['FIELD_COVER']) && !empty($aContentInfo[$CNF['FIELD_COVER']]))
  1167. $sSrc = $oGroupProfile->getCover();
  1168. if(empty($sSrc) && isset($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  1169. $sSrc = $oGroupProfile->getPicture();
  1170. return empty($sSrc) ? array() : array(
  1171. array('id' => $aContentInfo[$CNF['FIELD_PICTURE']], 'url' => $sUrl, 'src' => $sSrc, 'src_orig' => $sSrc),
  1172. );
  1173. }
  1174. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  1175. {
  1176. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  1177. return array(0, 0, null);
  1178. if ($oGroupProfile->getModule() == $this->getName()) {
  1179. $iProfileId = $iInitiatorId;
  1180. $iGroupProfileId = $oGroupProfile->id();
  1181. } else {
  1182. $iProfileId = $oGroupProfile->id();
  1183. $iGroupProfileId = $iInitiatorId;
  1184. }
  1185. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  1186. }
  1187. }
  1188. /** @} */