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. );
  74. }
  75. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  76. {
  77. if(empty($sUnitTemplate))
  78. $sUnitTemplate = 'unit.html';
  79. return parent::serviceGetSearchResultUnit($iContentId, $sUnitTemplate);
  80. }
  81. /**
  82. * @see BxBaseModProfileModule::serviceGetSpaceTitle
  83. */
  84. public function serviceGetSpaceTitle()
  85. {
  86. return _t($this->_oConfig->CNF['T']['txt_sample_single']);
  87. }
  88. /**
  89. * @see iBxDolProfileService::serviceGetParticipatingProfiles
  90. */
  91. public function serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects = false)
  92. {
  93. if (isset($this->_oConfig->CNF['OBJECT_CONNECTIONS'])){
  94. $aConnectionObjects = array($this->_oConfig->CNF['OBJECT_CONNECTIONS'], 'sys_profiles_subscriptions');
  95. return parent::serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects);
  96. }
  97. return parent::serviceGetParticipatingProfiles($iProfileId);
  98. }
  99. /**
  100. * Check if this module entry can be used as profile
  101. */
  102. public function serviceActAsProfile ()
  103. {
  104. return false;
  105. }
  106. /**
  107. * Check if this module is group profile
  108. */
  109. public function serviceIsGroupProfile ()
  110. {
  111. return true;
  112. }
  113. public function serviceIsEnableForContext($iProfileId = 0)
  114. {
  115. $CNF = &$this->_oConfig->CNF;
  116. $sCnfKey = 'ENABLE_FOR_CONTEXT_IN_MODULES';
  117. if(empty($iProfileId) || empty($CNF[$sCnfKey]) || !is_array($CNF[$sCnfKey]))
  118. return false;
  119. if(in_array(BxDolProfile::getInstance($iProfileId)->getModule(), $CNF[$sCnfKey]))
  120. return true;
  121. return false;
  122. }
  123. /**
  124. * check if provided profile is member of the group
  125. */
  126. public function serviceIsFan ($iGroupProfileId, $iProfileId = false)
  127. {
  128. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  129. return $this->isFan($oGroupProfile->getContentId(), $iProfileId);
  130. }
  131. /**
  132. * check if provided profile is admin of the group
  133. */
  134. public function serviceIsAdmin ($iGroupProfileId, $iProfileId = false)
  135. {
  136. if(!$iProfileId)
  137. $iProfileId = bx_get_logged_profile_id();
  138. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  139. $iGroupContentId = $oGroupProfile->getContentId();
  140. if(!$this->isFan($iGroupContentId, $iProfileId))
  141. return false;
  142. $aGroupContentInfo = $this->_oDb->getContentInfoById($iGroupContentId);
  143. return $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aGroupContentInfo);
  144. }
  145. public function serviceGetAdminRole($iGroupProfileId, $iProfileId = false)
  146. {
  147. if(!$iProfileId)
  148. $iProfileId = bx_get_logged_profile_id();
  149. if(!$this->serviceIsAdmin($iGroupProfileId, $iProfileId))
  150. return 0;
  151. return $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  152. }
  153. /**
  154. * Delete profile from fans and admins tables
  155. * @param $iProfileId profile id
  156. */
  157. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  158. {
  159. $CNF = &$this->_oConfig->CNF;
  160. $this->_oDb->deleteAdminsByProfileId($iProfileId);
  161. if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  162. $oConnection->onDeleteInitiatorAndContent($iProfileId);
  163. }
  164. /**
  165. * Reset group's author for particular group
  166. * @param $iContentId group id
  167. * @return false of error, or number of updated records on success
  168. */
  169. public function serviceReassignEntityAuthor ($iContentId)
  170. {
  171. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  172. if (!$aContentInfo)
  173. return false;
  174. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  175. return false;
  176. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  177. return $this->_oDb->updateAuthorById($iContentId, $aAdmins ? array_pop($aAdmins) : 0);
  178. }
  179. /**
  180. * Entry actions and social sharing block
  181. */
  182. public function serviceEntityAllActions ($mixedContent = false, $aParams = array())
  183. {
  184. $CNF = &$this->_oConfig->CNF;
  185. if(!empty($mixedContent)) {
  186. if(!is_array($mixedContent))
  187. $mixedContent = array((int)$mixedContent, (method_exists($this->_oDb, 'getContentInfoById')) ? $this->_oDb->getContentInfoById((int)$mixedContent) : array());
  188. }
  189. else {
  190. $mixedContent = $this->_getContent();
  191. if($mixedContent === false)
  192. return false;
  193. }
  194. list($iContentId, $aContentInfo) = $mixedContent;
  195. if(!empty($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  196. $aParams = array_merge(array(
  197. 'entry_thumb' => (int)$aContentInfo[$CNF['FIELD_PICTURE']]
  198. ), $aParams);
  199. return parent::serviceEntityAllActions ($mixedContent, $aParams);
  200. }
  201. /**
  202. * Reset group's author when author profile is deleted
  203. * @param $iProfileId profile id
  204. * @return number of changed items
  205. */
  206. public function serviceReassignEntitiesByAuthor ($iProfileId)
  207. {
  208. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  209. if (!$a)
  210. return 0;
  211. $iCount = 0;
  212. foreach ($a as $aContentInfo)
  213. $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']]) ? 1 : 0);
  214. return $iCount;
  215. }
  216. public function servicePrepareFields ($aFieldsProfile)
  217. {
  218. $CNF = &$this->_oConfig->CNF;
  219. $aFieldsProfile[$CNF['FIELD_NAME']] = $aFieldsProfile['name'];
  220. $aFieldsProfile[$CNF['FIELD_TEXT']] = isset($aFieldsProfile['description']) ? $aFieldsProfile['description'] : '';
  221. unset($aFieldsProfile['name']);
  222. unset($aFieldsProfile['description']);
  223. return $aFieldsProfile;
  224. }
  225. public function serviceOnRemoveConnection ($iGroupProfileId, $iInitiatorId)
  226. {
  227. $CNF = &$this->_oConfig->CNF;
  228. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  229. if (!$oGroupProfile)
  230. return false;
  231. $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
  232. if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
  233. return $oConn->removeConnection($iProfileId, $iGroupProfileId);
  234. return false;
  235. }
  236. public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $bSendInviteOnly = false)
  237. {
  238. $CNF = &$this->_oConfig->CNF;
  239. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  240. if (!$oGroupProfile)
  241. return false;
  242. if (!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  243. return false;
  244. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  245. return false;
  246. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  247. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  248. // send invitation to the group
  249. $sModule = $this->getName();
  250. if ($bSendInviteOnly && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  251. bx_alert($sModule, 'join_invitation', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  252. 'content' => $aContentInfo,
  253. 'entry_title' => $sEntryTitle,
  254. 'entry_url' => $sEntryUrl,
  255. 'group_profile' => $iGroupProfileId,
  256. 'profile' => $iProfileId,
  257. 'notification_subobject_id' => $iProfileId,
  258. 'object_author_id' => $iGroupProfileId
  259. ));
  260. /**
  261. * 'Invitation Received' alert for Notifications module.
  262. * Note. It's essential to use Recipient ($iInitiatorId) in 'object_author_id' parameter.
  263. * In this case notification will be received by Recipient profile.
  264. */
  265. bx_alert($sModule, 'join_invitation_notif', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  266. 'object_author_id' => $iInitiatorId,
  267. 'privacy_view' => isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 3,
  268. ));
  269. }
  270. // send notification to group's admins that new connection is pending confirmation
  271. elseif (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  272. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  273. 'object_author_id' => $iGroupProfileId,
  274. 'performer_id' => $iProfileId,
  275. 'content' => $aContentInfo,
  276. 'entry_title' => $sEntryTitle,
  277. 'entry_url' => $sEntryUrl,
  278. 'group_profile' => $iGroupProfileId,
  279. 'profile' => $iProfileId
  280. ));
  281. }
  282. // send notification that join request was accepted
  283. else if (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  284. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  285. 'object_author_id' => $iGroupProfileId,
  286. 'performer_id' => $iProfileId,
  287. 'content' => $aContentInfo,
  288. 'entry_title' => $sEntryTitle,
  289. 'entry_url' => $sEntryUrl,
  290. 'group_profile' => $iGroupProfileId,
  291. 'profile' => $iProfileId
  292. ));
  293. }
  294. // new fan was added
  295. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  296. // follow group on join
  297. if (BxDolService::call($oGroupProfile->getModule(), 'act_as_profile')){
  298. $this->addFollower($oGroupProfile->id(), (int)$iInitiatorId);
  299. }
  300. else{
  301. $this->addFollower((int)$iInitiatorId, $oGroupProfile->id());
  302. }
  303. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  304. 'object_author_id' => $iGroupProfileId,
  305. 'performer_id' => $iProfileId,
  306. 'content' => $aContentInfo,
  307. 'entry_title' => $sEntryTitle,
  308. 'entry_url' => $sEntryUrl,
  309. 'group_profile' => $iGroupProfileId,
  310. 'profile' => $iProfileId,
  311. ));
  312. $this->doAudit($iGroupProfileId, $iInitiatorId, '_sys_audit_action_group_join_request_accepted');
  313. return false;
  314. }
  315. // don't automatically add connection (mutual) if group requires manual join confirmation
  316. if ($bSendInviteOnly || $aContentInfo['join_confirmation'])
  317. return false;
  318. // check if connection already exists
  319. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  320. return false;
  321. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  322. return false;
  323. return true;
  324. }
  325. public function serviceFansTable ()
  326. {
  327. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_CONNECTIONS']);
  328. if (!$oGrid)
  329. return false;
  330. return $oGrid->getCode();
  331. }
  332. public function serviceInvitesTable ()
  333. {
  334. if (!isset($this->_oConfig->CNF['OBJECT_GRID_INVITES']))
  335. return false;
  336. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_INVITES']);
  337. if (!$oGrid)
  338. return false;
  339. return $oGrid->getCode();
  340. }
  341. public function serviceFans ($iContentId = 0, $bAsArray = false)
  342. {
  343. if (!$iContentId)
  344. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  345. if (!$iContentId)
  346. return false;
  347. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  348. if (!$aContentInfo)
  349. return false;
  350. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  351. return false;
  352. if(!$bAsArray) {
  353. bx_import('BxDolConnection');
  354. $mixedResult = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $this->_oConfig->CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  355. if (!$mixedResult)
  356. return MsgBox(_t('_sys_txt_empty'));
  357. }
  358. else
  359. $mixedResult = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  360. return $mixedResult;
  361. }
  362. public function serviceFansWithoutAdmins ($iContentId = 0, $bAsArray = false)
  363. {
  364. if (!$iContentId)
  365. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  366. if (!$iContentId)
  367. return false;
  368. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  369. if (!$aContentInfo)
  370. return false;
  371. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  372. return false;
  373. $CNF = &$this->_oConfig->CNF;
  374. $aFans = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  375. if(empty($aFans) || !is_array($aFans))
  376. return false;
  377. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  378. if(!empty($aAdmins) && is_array($aAdmins))
  379. $aFans = array_diff($aFans, $aAdmins);
  380. $iStart = (int)bx_get('start');
  381. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  382. if(!$iLimit)
  383. $iLimit = 4;
  384. return $this->_serviceBrowseQuick($aFans, $iStart, $iLimit);
  385. }
  386. public function serviceAdmins ($iContentId = 0)
  387. {
  388. $CNF = &$this->_oConfig->CNF;
  389. if(!$iContentId)
  390. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  391. if(!$iContentId)
  392. return false;
  393. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  394. if(!$oGroupProfile)
  395. return false;
  396. $iStart = (int)bx_get('start');
  397. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  398. if(!$iLimit)
  399. $iLimit = 4;
  400. $aProfiles = $this->_oDb->getAdmins($oGroupProfile->id(), $iStart, $iLimit+1);
  401. if(empty($aProfiles) || !is_array($aProfiles))
  402. return false;
  403. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  404. }
  405. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  406. {
  407. if (!$iProfileId)
  408. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  409. if (!$iProfileId)
  410. return '';
  411. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  412. }
  413. public function serviceBrowseCreatedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  414. {
  415. if (!$iProfileId)
  416. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  417. if (!$iProfileId)
  418. return '';
  419. return $this->_serviceBrowse ('created_entries', array('author' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  420. }
  421. public function serviceEntityPricing($iProfileId = 0)
  422. {
  423. $CNF = &$this->_oConfig->CNF;
  424. if(!$iProfileId)
  425. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  426. if(!$iProfileId)
  427. return '';
  428. if(!$this->_oConfig->isPaidJoin())
  429. return '';
  430. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  431. return MsgBox(_t('_Access denied'));
  432. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_MANAGE']);
  433. if(!$oGrid)
  434. return '';
  435. return $oGrid->getCode();
  436. }
  437. public function serviceEntityJoin($iProfileId = 0)
  438. {
  439. $CNF = &$this->_oConfig->CNF;
  440. if(!$iProfileId)
  441. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  442. if(!$iProfileId)
  443. return '';
  444. if(!$this->_oConfig->isPaidJoin())
  445. return '';
  446. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_VIEW']);
  447. if(!$oGrid)
  448. return '';
  449. return $oGrid->getCode();
  450. }
  451. public function serviceEntityInvite ($iContentId = 0, $bErrorMsg = true)
  452. {
  453. if (isset($this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE']))
  454. return $this->_serviceEntityForm ('editDataForm', $iContentId, $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE'], false, $bErrorMsg);
  455. return false;
  456. }
  457. /**
  458. * Entry social sharing block
  459. */
  460. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  461. {
  462. if(!empty($mixedContent)) {
  463. if(!is_array($mixedContent))
  464. $mixedContent = array((int)$mixedContent, array());
  465. }
  466. else {
  467. $mixedContent = $this->_getContent();
  468. if($mixedContent === false)
  469. return false;
  470. }
  471. list($iContentId, $aContentInfo) = $mixedContent;
  472. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  473. if(!$oGroupProfile)
  474. return false;
  475. return parent::serviceEntitySocialSharing(array($iContentId, $aContentInfo), array(
  476. 'title' => $oGroupProfile->getDisplayName()
  477. ));
  478. }
  479. public function serviceIsPricingAvaliable($iProfileId)
  480. {
  481. if(!$this->_oConfig->isPaidJoin())
  482. return false;
  483. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  484. return false;
  485. return true;
  486. }
  487. public function serviceIsPaidJoinAvaliable($iProfileId)
  488. {
  489. return $this->isPaidJoinByProfile($iProfileId);
  490. }
  491. public function serviceIsPaidJoinAvaliableByContent($iContentId)
  492. {
  493. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  494. if(!$oProfile)
  495. return false;
  496. return $this->isPaidJoinByProfile($oProfile->id());
  497. }
  498. public function serviceIsFreeJoinAvaliable($iProfileId)
  499. {
  500. return !$this->isPaidJoinByProfile($iProfileId);
  501. }
  502. public function serviceIsFreeJoinAvaliableByContent($iContentId)
  503. {
  504. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  505. if(!$oProfile)
  506. return false;
  507. return !$this->isPaidJoinByProfile($oProfile->id());
  508. }
  509. public function isPaidJoinByProfile($iProfileId)
  510. {
  511. if(!$this->_oConfig->isPaidJoin())
  512. return false;
  513. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $iProfileId));
  514. if(empty($aPrices) || !is_array($aPrices))
  515. return false;
  516. return true;
  517. }
  518. /**
  519. * Integration with Payments.
  520. */
  521. public function serviceGetPaymentData()
  522. {
  523. return $this->_aModule;
  524. }
  525. public function serviceGetCartItem($mixedItemId)
  526. {
  527. $CNF = &$this->_oConfig->CNF;
  528. if(!$mixedItemId)
  529. return array();
  530. if(is_numeric($mixedItemId))
  531. $aItem = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => (int)$mixedItemId));
  532. else
  533. $aItem = $this->_oDb->getPrices(array('type' => 'by_name', 'value' => $mixedItemId));
  534. if(empty($aItem) || !is_array($aItem))
  535. return array();
  536. if(!$this->isPaidJoinByProfile($aItem['profile_id']))
  537. return array();
  538. $oGroupProfile = BxDolProfile::getInstance($aItem['profile_id']);
  539. if(!$oGroupProfile)
  540. return array();
  541. $aGroupProfile = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  542. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  543. return array (
  544. 'id' => $aItem['id'],
  545. 'author_id' => $aGroupProfile[$CNF['FIELD_AUTHOR']],
  546. 'name' => $aItem['name'],
  547. 'title' => _t($CNF['T']['txt_cart_item_title'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']], $aItem['period'], $aItem['period_unit']),
  548. 'description' => '',
  549. 'url' => $oGroupProfile->getUrl(),
  550. 'price_single' => $aItem['price'],
  551. 'price_recurring' => $aItem['price'],
  552. 'period_recurring' => $aItem['period'],
  553. 'period_unit_recurring' => $aItem['period_unit'],
  554. 'trial_recurring' => 0
  555. );
  556. }
  557. public function serviceGetCartItems($iSellerId)
  558. {
  559. $CNF = &$this->_oConfig->CNF;
  560. if(empty($iSellerId))
  561. return array();
  562. $sModule = $this->getName();
  563. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  564. $aGroups = $this->_oDb->getEntriesBy(array('type' => 'author', 'author' => $iSellerId));
  565. $aResult = array();
  566. foreach($aGroups as $aGroup) {
  567. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aGroup[$CNF['FIELD_ID']], $sModule);
  568. if(!$oGroupProfile)
  569. continue;
  570. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $oGroupProfile->id()));
  571. if(empty($aPrices) || !is_array($aPrices))
  572. continue;
  573. $sTitle = $oGroupProfile->getDisplayName();
  574. $sUrl = $oGroupProfile->getUrl();
  575. foreach($aPrices as $aPrice)
  576. $aResult[] = array(
  577. 'id' => $aPrice['id'],
  578. 'author_id' => $iSellerId,
  579. 'name' => $aPrice['name'],
  580. 'title' => _t($CNF['T']['txt_cart_item_title'], $sTitle, $aRoles[$aPrice['role_id']], $aPrice['period'], $aPrice['period_unit']),
  581. 'description' => '',
  582. 'url' => $sUrl,
  583. 'price_single' => $aPrice['price'],
  584. 'price_recurring' => $aPrice['price'],
  585. 'period_recurring' => $aPrice['period'],
  586. 'period_unit_recurring' => $aPrice['period_unit']
  587. );
  588. }
  589. return $aResult;
  590. }
  591. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  592. {
  593. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  594. }
  595. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  596. {
  597. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  598. }
  599. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  600. {
  601. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  602. }
  603. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  604. {
  605. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  606. }
  607. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  608. {
  609. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  610. }
  611. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  612. {
  613. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  614. }
  615. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  616. {
  617. return true;
  618. }
  619. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  620. {
  621. $CNF = &$this->_oConfig->CNF;
  622. $aItem = $this->serviceGetCartItem($iItemId);
  623. if(empty($aItem) || !is_array($aItem))
  624. return array();
  625. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  626. $mixedPeriod = false;
  627. if((int)$aItemInfo['period'] != 0)
  628. $mixedPeriod = array(
  629. 'period' => (int)$aItemInfo['period'],
  630. 'period_unit' => $aItemInfo['period_unit'],
  631. 'period_reserve' => $CNF['PARAM_RECURRING_RESERVE']
  632. );
  633. if(!$this->setRole($aItemInfo['profile_id'], $iClientId, $aItemInfo['role_id'], $mixedPeriod, $sOrder))
  634. return array();
  635. return $aItem;
  636. }
  637. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  638. {
  639. $aItem = $this->serviceGetCartItem($iItemIdNew);
  640. if(empty($aItem) || !is_array($aItem))
  641. return array();
  642. $aItemInfoOld = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdOld));
  643. if(empty($aItemInfoOld) || !is_array($aItemInfoOld))
  644. return array();
  645. $aItemInfoNew = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdNew));
  646. if(empty($aItemInfoNew) || !is_array($aItemInfoNew))
  647. return array();
  648. if(!$this->unsetRole($aItemInfoOld['profile_id'], $iClientId))
  649. return array();
  650. $aResult = $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemIdNew, 1, $sOrder);
  651. if(empty($aResult) || !is_array($aResult))
  652. return array();
  653. return $aItem;
  654. }
  655. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  656. {
  657. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  658. if(empty($aItemInfo) || !is_array($aItemInfo))
  659. return false;
  660. return $this->unsetRole($aItemInfo['profile_id'], $iClientId);
  661. }
  662. /**
  663. * Data for Notifications module
  664. */
  665. public function serviceGetNotificationsData()
  666. {
  667. $sModule = $this->_aModule['name'];
  668. $aSettingsTypes = array('follow_member', 'follow_context');
  669. if($this->serviceActAsProfile())
  670. $aSettingsTypes = array('personal', 'follow_member');
  671. return array(
  672. 'handlers' => array(
  673. array('group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'),
  674. array('group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'),
  675. array('group' => $sModule . '_score_up', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteUp', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_up', 'module_class' => 'Module'),
  676. array('group' => $sModule . '_score_down', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVoteDown', 'module_name' => $sModule, 'module_method' => 'get_notifications_score_down', 'module_class' => 'Module'),
  677. array('group' => $sModule . '_fan_added', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'fan_added', 'module_name' => $sModule, 'module_method' => 'get_notifications_fan_added', 'module_class' => 'Module'),
  678. array('group' => $sModule . '_join_invitation', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'join_invitation_notif', 'module_name' => $sModule, 'module_method' => 'get_notifications_join_invitation', 'module_class' => 'Module'),
  679. array('group' => $sModule . '_join_request', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'join_request', 'module_name' => $sModule, 'module_method' => 'get_notifications_join_request', 'module_class' => 'Module', 'module_event_privacy' => $this->_oConfig->CNF['OBJECT_PRIVACY_VIEW_NOTIFICATION_EVENT']),
  680. array('group' => $sModule . '_timeline_post_common', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'timeline_post_common', 'module_name' => $sModule, 'module_method' => 'get_notifications_timeline_post_common', 'module_class' => 'Module'),
  681. ),
  682. 'settings' => array(
  683. array('group' => 'vote', 'unit' => $sModule, 'action' => 'doVote', 'types' => $aSettingsTypes),
  684. array('group' => 'score_up', 'unit' => $sModule, 'action' => 'doVoteUp', 'types' => $aSettingsTypes),
  685. array('group' => 'score_down', 'unit' => $sModule, 'action' => 'doVoteDown', 'types' => $aSettingsTypes),
  686. array('group' => 'fan', 'unit' => $sModule, 'action' => 'fan_added', 'types' => $aSettingsTypes),
  687. array('group' => 'invite', 'unit' => $sModule, 'action' => 'join_invitation_notif', 'types' => array('personal')),
  688. array('group' => 'join', 'unit' => $sModule, 'action' => 'join_request', 'types' => $aSettingsTypes),
  689. array('group' => 'timeline_post', 'unit' => $sModule, 'action' => 'timeline_post_common', 'types' => $aSettingsTypes)
  690. ),
  691. 'alerts' => array(
  692. array('unit' => $sModule, 'action' => 'doVote'),
  693. array('unit' => $sModule, 'action' => 'undoVote'),
  694. array('unit' => $sModule, 'action' => 'doVoteUp'),
  695. array('unit' => $sModule, 'action' => 'doVoteDown'),
  696. array('unit' => $sModule, 'action' => 'fan_added'),
  697. array('unit' => $sModule, 'action' => 'join_invitation_notif'),
  698. array('unit' => $sModule, 'action' => 'join_request'),
  699. array('unit' => $sModule, 'action' => 'timeline_post_common'),
  700. )
  701. );
  702. }
  703. /**
  704. * Notification about new invitation to join the group
  705. */
  706. public function serviceGetNotificationsJoinInvitation($aEvent)
  707. {
  708. $CNF = &$this->_oConfig->CNF;
  709. $iContentId = (int)$aEvent['object_id'];
  710. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  711. if(!$oGroupProfile)
  712. return array();
  713. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  714. if(empty($aContentInfo) || !is_array($aContentInfo))
  715. return array();
  716. /*
  717. * It's essential that 'object_owner_id' contains invited member profile id.
  718. */
  719. $oProfile = BxDolProfile::getInstance((int)$aEvent['object_owner_id']);
  720. if(!$oProfile)
  721. return array();
  722. /*
  723. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  724. * because Subentry URL has higher display priority and notification
  725. * should be linked to Group Profile (Group Profile -> Members tab)
  726. * instead of Personal Profile of invited member.
  727. */
  728. $sEntryUrl = $oGroupProfile->getUrl();
  729. return array(
  730. 'entry_sample' => $CNF['T']['txt_sample_single'],
  731. 'entry_url' => $sEntryUrl,
  732. 'entry_caption' => $oGroupProfile->getDisplayName(),
  733. 'subentry_sample' => $oProfile->getDisplayName(),
  734. 'subentry_url' => $sEntryUrl,
  735. 'lang_key' => $this->_oConfig->CNF['T']['txt_ntfs_join_invitation']
  736. );
  737. }
  738. /**
  739. * Notification about new member requst in the group
  740. */
  741. public function serviceGetNotificationsJoinRequest($aEvent)
  742. {
  743. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_join_request']);
  744. }
  745. /**
  746. * Notification about new member in the group
  747. */
  748. public function serviceGetNotificationsFanAdded($aEvent)
  749. {
  750. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_fan_added']);
  751. }
  752. protected function _serviceGetNotification($aEvent, $sLangKey)
  753. {
  754. $CNF = &$this->_oConfig->CNF;
  755. $iContentId = (int)$aEvent['object_id'];
  756. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  757. if(!$oGroupProfile)
  758. return array();
  759. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  760. if(empty($aContentInfo) || !is_array($aContentInfo))
  761. return array();
  762. $oProfile = BxDolProfile::getInstance((int)$aEvent['subobject_id']);
  763. if(!$oProfile)
  764. return array();
  765. /*
  766. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  767. * because Subentry URL has higher display priority and notification
  768. * should be linked to Group Profile (Group Profile -> Members tab)
  769. * instead of Personal Profile of a member, who performed an action.
  770. */
  771. $sEntryUrl = $oGroupProfile->getUrl();
  772. if(!empty($CNF['URL_ENTRY_FANS']))
  773. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_FANS'], array(
  774. 'profile_id' => $oGroupProfile->id()
  775. ));
  776. return array(
  777. 'entry_sample' => $CNF['T']['txt_sample_single'],
  778. 'entry_url' => $sEntryUrl,
  779. 'entry_caption' => $oGroupProfile->getDisplayName(),
  780. 'entry_author' => $oGroupProfile->id(),
  781. 'subentry_sample' => $oProfile->getDisplayName(),
  782. 'subentry_url' => $sEntryUrl,
  783. 'lang_key' => $sLangKey
  784. );
  785. }
  786. /**
  787. * Data for Timeline module
  788. */
  789. public function serviceGetTimelineData()
  790. {
  791. return BxBaseModGeneralModule::serviceGetTimelineData();
  792. }
  793. /**
  794. * Entry post for Timeline module
  795. */
  796. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  797. {
  798. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  799. if($a === false)
  800. return false;
  801. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  802. $a['content']['url'] = $oGroupProfile->getUrl();
  803. $a['content']['title'] = $oGroupProfile->getDisplayName();
  804. return $a;
  805. }
  806. // ====== PERMISSION METHODS
  807. /**
  808. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  809. */
  810. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  811. {
  812. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  813. }
  814. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  815. {
  816. $CNF = &$this->_oConfig->CNF;
  817. $bInvited = false;
  818. if(!empty($CNF['TABLE_INVITES']) && bx_get('key')){
  819. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  820. $mixedInvited = $this->isInvited(bx_get('key'), $oGroupProfile->id());
  821. if($mixedInvited === true)
  822. $bInvited = true;
  823. }
  824. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $iProfileId) || $bInvited)
  825. return CHECK_ACTION_RESULT_ALLOWED;
  826. return parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId);
  827. }
  828. /**
  829. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  830. */
  831. public function checkAllowedCompose(&$aDataEntry, $isPerformAction = false)
  832. {
  833. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  834. return _t('_sys_txt_access_denied');
  835. return parent::checkAllowedCompose ($aDataEntry, $isPerformAction);
  836. }
  837. /**
  838. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  839. */
  840. public function checkAllowedFanAdd(&$aDataEntry, $isPerformAction = false)
  841. {
  842. $mixedResult = $this->_modGroupsCheckAllowedFanAdd($aDataEntry, $isPerformAction);
  843. // call alert to allow custom checks
  844. bx_alert('system', 'check_allowed_fan_add', 0, 0, array(
  845. 'module' => $this->getName(),
  846. 'content_info' => $aDataEntry,
  847. 'profile_id' => bx_get_logged_profile_id(),
  848. 'override_result' => &$mixedResult
  849. ));
  850. return $mixedResult;
  851. }
  852. public function _modGroupsCheckAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  853. {
  854. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) || !isLogged())
  855. return _t('_sys_txt_access_denied');
  856. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], true, false);
  857. }
  858. /**
  859. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  860. */
  861. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  862. {
  863. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  864. return CHECK_ACTION_RESULT_ALLOWED;
  865. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  866. }
  867. protected function _checkAllowedActionByFan($sAction, $aDataEntry, $iProfileId = 0)
  868. {
  869. $CNF = &$this->_oConfig->CNF;
  870. $bRoles = $this->_oConfig->isRoles();
  871. if(empty($iProfileId))
  872. $iProfileId = bx_get_logged_profile_id();
  873. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  874. if(!$oGroupProfile)
  875. return $sAction == BX_BASE_MOD_GROUPS_ACTION_DELETE ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_not_found');
  876. $iGroupProfileId = $oGroupProfile->id();
  877. if(!$bRoles && $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aDataEntry))
  878. return CHECK_ACTION_RESULT_ALLOWED;
  879. if($bRoles && $this->isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId))
  880. return CHECK_ACTION_RESULT_ALLOWED;
  881. return _t('_sys_txt_access_denied');
  882. }
  883. public function isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId)
  884. {
  885. $bResult = false;
  886. $iProfileRole = $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  887. if(!empty($iProfileRole))
  888. switch($sAction) {
  889. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  890. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR))
  891. $bResult = true;
  892. break;
  893. default:
  894. $bResult = true;
  895. }
  896. // call alert to allow custom checks
  897. bx_alert('system', 'check_allowed_action_by_role', 0, 0, array(
  898. 'module' => $this->getName(),
  899. 'multi_roles' => $this->_oConfig->isMultiRoles(),
  900. 'action' => $sAction,
  901. 'content_profile_id' => $iGroupProfileId,
  902. 'content_info' => $aDataEntry,
  903. 'profile_id' => $iProfileId,
  904. 'profile_role' => $iProfileRole,
  905. 'override_result' => &$bResult
  906. ));
  907. return $bResult;
  908. }
  909. public function isRole($iProfileRole, $iRole)
  910. {
  911. if(!$this->_oConfig->isMultiRoles())
  912. return $iProfileRole == $iRole;
  913. else
  914. return $iProfileRole & (1 << ($iRole - 1));
  915. }
  916. public function serviceIsRole($iProfileRole, $iRole)
  917. {
  918. return $this->isRole($iProfileRole, $iRole);
  919. }
  920. public function checkAllowedManageFans($mixedDataEntry, $isPerformAction = false)
  921. {
  922. $aDataEntry = array();
  923. if(!is_array($mixedDataEntry)) {
  924. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  925. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  926. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  927. }
  928. else
  929. $aDataEntry = $mixedDataEntry;
  930. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  931. return CHECK_ACTION_RESULT_ALLOWED;
  932. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  933. }
  934. public function checkAllowedManageAdmins($mixedDataEntry, $isPerformAction = false)
  935. {
  936. $aDataEntry = array();
  937. if(!is_array($mixedDataEntry)) {
  938. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  939. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  940. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  941. }
  942. else
  943. $aDataEntry = $mixedDataEntry;
  944. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  945. return CHECK_ACTION_RESULT_ALLOWED;
  946. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  947. }
  948. public function checkAllowedEdit($aDataEntry, $isPerformAction = false)
  949. {
  950. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_EDIT, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  951. return CHECK_ACTION_RESULT_ALLOWED;
  952. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  953. }
  954. public function checkAllowedInvite($aDataEntry, $isPerformAction = false)
  955. {
  956. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_INVITE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  957. return CHECK_ACTION_RESULT_ALLOWED;
  958. return $this->checkAllowedEdit ($aDataEntry, $isPerformAction);
  959. }
  960. public function checkAllowedChangeCover($aDataEntry, $isPerformAction = false)
  961. {
  962. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  963. return CHECK_ACTION_RESULT_ALLOWED;
  964. return parent::checkAllowedChangeCover($aDataEntry, $isPerformAction);
  965. }
  966. public function checkAllowedDelete(&$aDataEntry, $isPerformAction = false)
  967. {
  968. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_DELETE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  969. return CHECK_ACTION_RESULT_ALLOWED;
  970. return parent::checkAllowedDelete($aDataEntry, $isPerformAction);
  971. }
  972. public function checkAllowedJoin(&$aDataEntry, $isPerformAction = false)
  973. {
  974. if (bx_get('key')){
  975. $sKey = bx_get('key');
  976. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  977. $aData = $this->_oDb->getInviteByKey($sKey, $oGroupProfile->id());
  978. if (isset($aData['invited_profile_id']) && $aData['invited_profile_id'] == bx_get_logged_profile_id()){
  979. return CHECK_ACTION_RESULT_ALLOWED;
  980. }
  981. }
  982. return _t('_sys_txt_access_denied');
  983. }
  984. public function checkAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  985. {
  986. $mixedResult = $this->_modGroupsCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  987. // call alert to allow custom checks
  988. bx_alert('system', 'check_allowed_subscribe_add', 0, 0, array(
  989. 'module' => $this->getName(),
  990. 'content_info' => $aDataEntry,
  991. 'profile_id' => bx_get_logged_profile_id(),
  992. 'override_result' => &$mixedResult
  993. ));
  994. return $mixedResult;
  995. }
  996. /**
  997. * Note. Is mainly needed for internal usage. Access level is 'public' to allow outer calls from alerts.
  998. */
  999. public function _modGroupsCheckAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1000. {
  1001. 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. /** @} */