BxBaseModGroupsModule.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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. if ($bSendInviteOnly && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  251. bx_alert($this->getName(), 'join_invitation', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array('content' => $aContentInfo, 'entry_title' => $sEntryTitle, 'entry_url' => $sEntryUrl, 'group_profile' => $iGroupProfileId, 'profile' => $iProfileId, 'notification_subobject_id' => $iProfileId, 'object_author_id' => $iGroupProfileId));
  252. }
  253. // send notification to group's admins that new connection is pending confirmation
  254. elseif (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  255. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  256. 'object_author_id' => $iGroupProfileId,
  257. 'performer_id' => $iProfileId,
  258. 'content' => $aContentInfo,
  259. 'entry_title' => $sEntryTitle,
  260. 'entry_url' => $sEntryUrl,
  261. 'group_profile' => $iGroupProfileId,
  262. 'profile' => $iProfileId
  263. ));
  264. }
  265. // send notification that join request was accepted
  266. else if (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  267. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  268. 'object_author_id' => $iGroupProfileId,
  269. 'performer_id' => $iProfileId,
  270. 'content' => $aContentInfo,
  271. 'entry_title' => $sEntryTitle,
  272. 'entry_url' => $sEntryUrl,
  273. 'group_profile' => $iGroupProfileId,
  274. 'profile' => $iProfileId
  275. ));
  276. }
  277. // new fan was added
  278. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  279. // follow group on join
  280. if (BxDolService::call($oGroupProfile->getModule(), 'act_as_profile')){
  281. $this->addFollower($oGroupProfile->id(), (int)$iInitiatorId);
  282. }
  283. else{
  284. $this->addFollower((int)$iInitiatorId, $oGroupProfile->id());
  285. }
  286. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  287. 'object_author_id' => $iGroupProfileId,
  288. 'performer_id' => $iProfileId,
  289. 'content' => $aContentInfo,
  290. 'entry_title' => $sEntryTitle,
  291. 'entry_url' => $sEntryUrl,
  292. 'group_profile' => $iGroupProfileId,
  293. 'profile' => $iProfileId,
  294. ));
  295. $this->doAudit($iGroupProfileId, $iInitiatorId, '_sys_audit_action_group_join_request_accepted');
  296. return false;
  297. }
  298. // don't automatically add connection (mutual) if group requires manual join confirmation
  299. if ($bSendInviteOnly || $aContentInfo['join_confirmation'])
  300. return false;
  301. // check if connection already exists
  302. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  303. return false;
  304. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  305. return false;
  306. return true;
  307. }
  308. public function serviceFansTable ()
  309. {
  310. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_CONNECTIONS']);
  311. if (!$oGrid)
  312. return false;
  313. return $oGrid->getCode();
  314. }
  315. public function serviceInvitesTable ()
  316. {
  317. if (!isset($this->_oConfig->CNF['OBJECT_GRID_INVITES']))
  318. return false;
  319. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_INVITES']);
  320. if (!$oGrid)
  321. return false;
  322. return $oGrid->getCode();
  323. }
  324. public function serviceFans ($iContentId = 0, $bAsArray = false)
  325. {
  326. if (!$iContentId)
  327. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  328. if (!$iContentId)
  329. return false;
  330. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  331. if (!$aContentInfo)
  332. return false;
  333. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  334. return false;
  335. if(!$bAsArray) {
  336. bx_import('BxDolConnection');
  337. $mixedResult = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $this->_oConfig->CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  338. if (!$mixedResult)
  339. return MsgBox(_t('_sys_txt_empty'));
  340. }
  341. else
  342. $mixedResult = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  343. return $mixedResult;
  344. }
  345. public function serviceFansWithoutAdmins ($iContentId = 0, $bAsArray = false)
  346. {
  347. if (!$iContentId)
  348. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  349. if (!$iContentId)
  350. return false;
  351. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  352. if (!$aContentInfo)
  353. return false;
  354. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  355. return false;
  356. $CNF = &$this->_oConfig->CNF;
  357. $aFans = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  358. if(empty($aFans) || !is_array($aFans))
  359. return false;
  360. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  361. if(!empty($aAdmins) && is_array($aAdmins))
  362. $aFans = array_diff($aFans, $aAdmins);
  363. $iStart = (int)bx_get('start');
  364. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  365. if(!$iLimit)
  366. $iLimit = 4;
  367. return $this->_serviceBrowseQuick($aFans, $iStart, $iLimit);
  368. }
  369. public function serviceAdmins ($iContentId = 0)
  370. {
  371. $CNF = &$this->_oConfig->CNF;
  372. if(!$iContentId)
  373. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  374. if(!$iContentId)
  375. return false;
  376. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  377. if(!$oGroupProfile)
  378. return false;
  379. $iStart = (int)bx_get('start');
  380. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  381. if(!$iLimit)
  382. $iLimit = 4;
  383. $aProfiles = $this->_oDb->getAdmins($oGroupProfile->id(), $iStart, $iLimit+1);
  384. if(empty($aProfiles) || !is_array($aProfiles))
  385. return false;
  386. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  387. }
  388. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  389. {
  390. if (!$iProfileId)
  391. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  392. if (!$iProfileId)
  393. return '';
  394. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  395. }
  396. public function serviceBrowseCreatedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  397. {
  398. if (!$iProfileId)
  399. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  400. if (!$iProfileId)
  401. return '';
  402. return $this->_serviceBrowse ('created_entries', array('author' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  403. }
  404. public function serviceEntityPricing($iProfileId = 0)
  405. {
  406. $CNF = &$this->_oConfig->CNF;
  407. if(!$iProfileId)
  408. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  409. if(!$iProfileId)
  410. return '';
  411. if(!$this->_oConfig->isPaidJoin())
  412. return '';
  413. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  414. return MsgBox(_t('_Access denied'));
  415. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_MANAGE']);
  416. if(!$oGrid)
  417. return '';
  418. return $oGrid->getCode();
  419. }
  420. public function serviceEntityJoin($iProfileId = 0)
  421. {
  422. $CNF = &$this->_oConfig->CNF;
  423. if(!$iProfileId)
  424. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  425. if(!$iProfileId)
  426. return '';
  427. if(!$this->_oConfig->isPaidJoin())
  428. return '';
  429. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_VIEW']);
  430. if(!$oGrid)
  431. return '';
  432. return $oGrid->getCode();
  433. }
  434. public function serviceEntityInvite ($iContentId = 0, $bErrorMsg = true)
  435. {
  436. if (isset($this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE']))
  437. return $this->_serviceEntityForm ('editDataForm', $iContentId, $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE'], false, $bErrorMsg);
  438. return false;
  439. }
  440. /**
  441. * Entry social sharing block
  442. */
  443. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  444. {
  445. if(!empty($mixedContent)) {
  446. if(!is_array($mixedContent))
  447. $mixedContent = array((int)$mixedContent, array());
  448. }
  449. else {
  450. $mixedContent = $this->_getContent();
  451. if($mixedContent === false)
  452. return false;
  453. }
  454. list($iContentId, $aContentInfo) = $mixedContent;
  455. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  456. if(!$oGroupProfile)
  457. return false;
  458. return parent::serviceEntitySocialSharing(array($iContentId, $aContentInfo), array(
  459. 'title' => $oGroupProfile->getDisplayName()
  460. ));
  461. }
  462. public function serviceIsPricingAvaliable($iProfileId)
  463. {
  464. if(!$this->_oConfig->isPaidJoin())
  465. return false;
  466. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  467. return false;
  468. return true;
  469. }
  470. public function serviceIsPaidJoinAvaliable($iProfileId)
  471. {
  472. return $this->isPaidJoinByProfile($iProfileId);
  473. }
  474. public function serviceIsPaidJoinAvaliableByContent($iContentId)
  475. {
  476. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  477. if(!$oProfile)
  478. return false;
  479. return $this->isPaidJoinByProfile($oProfile->id());
  480. }
  481. public function serviceIsFreeJoinAvaliable($iProfileId)
  482. {
  483. return !$this->isPaidJoinByProfile($iProfileId);
  484. }
  485. public function serviceIsFreeJoinAvaliableByContent($iContentId)
  486. {
  487. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  488. if(!$oProfile)
  489. return false;
  490. return !$this->isPaidJoinByProfile($oProfile->id());
  491. }
  492. public function isPaidJoinByProfile($iProfileId)
  493. {
  494. if(!$this->_oConfig->isPaidJoin())
  495. return false;
  496. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $iProfileId));
  497. if(empty($aPrices) || !is_array($aPrices))
  498. return false;
  499. return true;
  500. }
  501. /**
  502. * Integration with Payments.
  503. */
  504. public function serviceGetPaymentData()
  505. {
  506. return $this->_aModule;
  507. }
  508. public function serviceGetCartItem($mixedItemId)
  509. {
  510. $CNF = &$this->_oConfig->CNF;
  511. if(!$mixedItemId)
  512. return array();
  513. if(is_numeric($mixedItemId))
  514. $aItem = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => (int)$mixedItemId));
  515. else
  516. $aItem = $this->_oDb->getPrices(array('type' => 'by_name', 'value' => $mixedItemId));
  517. if(empty($aItem) || !is_array($aItem))
  518. return array();
  519. if(!$this->isPaidJoinByProfile($aItem['profile_id']))
  520. return array();
  521. $oGroupProfile = BxDolProfile::getInstance($aItem['profile_id']);
  522. if(!$oGroupProfile)
  523. return array();
  524. $aGroupProfile = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  525. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  526. return array (
  527. 'id' => $aItem['id'],
  528. 'author_id' => $aGroupProfile[$CNF['FIELD_AUTHOR']],
  529. 'name' => $aItem['name'],
  530. 'title' => _t($CNF['T']['txt_cart_item_title'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']], $aItem['period'], $aItem['period_unit']),
  531. 'description' => '',
  532. 'url' => $oGroupProfile->getUrl(),
  533. 'price_single' => $aItem['price'],
  534. 'price_recurring' => $aItem['price'],
  535. 'period_recurring' => $aItem['period'],
  536. 'period_unit_recurring' => $aItem['period_unit']
  537. );
  538. }
  539. public function serviceGetCartItems($iSellerId)
  540. {
  541. $CNF = &$this->_oConfig->CNF;
  542. if(empty($iSellerId))
  543. return array();
  544. $sModule = $this->getName();
  545. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  546. $aGroups = $this->_oDb->getEntriesBy(array('type' => 'author', 'author' => $iSellerId));
  547. $aResult = array();
  548. foreach($aGroups as $aGroup) {
  549. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aGroup[$CNF['FIELD_ID']], $sModule);
  550. if(!$oGroupProfile)
  551. continue;
  552. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $oGroupProfile->id()));
  553. if(empty($aPrices) || !is_array($aPrices))
  554. continue;
  555. $sTitle = $oGroupProfile->getDisplayName();
  556. $sUrl = $oGroupProfile->getUrl();
  557. foreach($aPrices as $aPrice)
  558. $aResult[] = array(
  559. 'id' => $aPrice['id'],
  560. 'author_id' => $iSellerId,
  561. 'name' => $aPrice['name'],
  562. 'title' => _t($CNF['T']['txt_cart_item_title'], $sTitle, $aRoles[$aPrice['role_id']], $aPrice['period'], $aPrice['period_unit']),
  563. 'description' => '',
  564. 'url' => $sUrl,
  565. 'price_single' => $aPrice['price'],
  566. 'price_recurring' => $aPrice['price'],
  567. 'period_recurring' => $aPrice['period'],
  568. 'period_unit_recurring' => $aPrice['period_unit']
  569. );
  570. }
  571. return $aResult;
  572. }
  573. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  574. {
  575. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  576. }
  577. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  578. {
  579. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  580. }
  581. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  582. {
  583. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  584. }
  585. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  586. {
  587. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  588. }
  589. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  590. {
  591. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  592. }
  593. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  594. {
  595. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  596. }
  597. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  598. {
  599. return true;
  600. }
  601. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  602. {
  603. $CNF = &$this->_oConfig->CNF;
  604. $aItem = $this->serviceGetCartItem($iItemId);
  605. if(empty($aItem) || !is_array($aItem))
  606. return array();
  607. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  608. $mixedPeriod = false;
  609. if((int)$aItemInfo['period'] != 0)
  610. $mixedPeriod = array(
  611. 'period' => (int)$aItemInfo['period'],
  612. 'period_unit' => $aItemInfo['period_unit'],
  613. 'period_reserve' => $CNF['PARAM_RECURRING_RESERVE']
  614. );
  615. if(!$this->setRole($aItemInfo['profile_id'], $iClientId, $aItemInfo['role_id'], $mixedPeriod, $sOrder))
  616. return array();
  617. return $aItem;
  618. }
  619. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  620. {
  621. $aItem = $this->serviceGetCartItem($iItemIdNew);
  622. if(empty($aItem) || !is_array($aItem))
  623. return array();
  624. $aItemInfoOld = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdOld));
  625. if(empty($aItemInfoOld) || !is_array($aItemInfoOld))
  626. return array();
  627. $aItemInfoNew = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdNew));
  628. if(empty($aItemInfoNew) || !is_array($aItemInfoNew))
  629. return array();
  630. if(!$this->unsetRole($aItemInfoOld['profile_id'], $iClientId))
  631. return array();
  632. $aResult = $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemIdNew, 1, $sOrder);
  633. if(empty($aResult) || !is_array($aResult))
  634. return array();
  635. return $aItem;
  636. }
  637. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  638. {
  639. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  640. if(empty($aItemInfo) || !is_array($aItemInfo))
  641. return false;
  642. return $this->unsetRole($aItemInfo['profile_id'], $iClientId);
  643. }
  644. /**
  645. * Data for Notifications module
  646. */
  647. public function serviceGetNotificationsData()
  648. {
  649. $sModule = $this->_aModule['name'];
  650. $aSettingsTypes = array('follow_member', 'follow_context');
  651. if($this->serviceActAsProfile())
  652. $aSettingsTypes = array('personal', 'follow_member');
  653. return array(
  654. 'handlers' => array(
  655. array('group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'),
  656. array('group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'),
  657. 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'),
  658. 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'),
  659. 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'),
  660. 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']),
  661. 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'),
  662. ),
  663. 'settings' => array(
  664. array('group' => 'vote', 'unit' => $sModule, 'action' => 'doVote', 'types' => $aSettingsTypes),
  665. array('group' => 'score_up', 'unit' => $sModule, 'action' => 'doVoteUp', 'types' => $aSettingsTypes),
  666. array('group' => 'score_down', 'unit' => $sModule, 'action' => 'doVoteDown', 'types' => $aSettingsTypes),
  667. array('group' => 'fan', 'unit' => $sModule, 'action' => 'fan_added', 'types' => $aSettingsTypes),
  668. array('group' => 'join', 'unit' => $sModule, 'action' => 'join_request', 'types' => $aSettingsTypes),
  669. array('group' => 'timeline_post', 'unit' => $sModule, 'action' => 'timeline_post_common', 'types' => $aSettingsTypes)
  670. ),
  671. 'alerts' => array(
  672. array('unit' => $sModule, 'action' => 'doVote'),
  673. array('unit' => $sModule, 'action' => 'undoVote'),
  674. array('unit' => $sModule, 'action' => 'doVoteUp'),
  675. array('unit' => $sModule, 'action' => 'doVoteDown'),
  676. array('unit' => $sModule, 'action' => 'fan_added'),
  677. array('unit' => $sModule, 'action' => 'join_request'),
  678. array('unit' => $sModule, 'action' => 'timeline_post_common'),
  679. )
  680. );
  681. }
  682. /**
  683. * Notification about new member requst in the group
  684. */
  685. public function serviceGetNotificationsJoinRequest($aEvent)
  686. {
  687. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_join_request']);
  688. }
  689. /**
  690. * Notification about new member in the group
  691. */
  692. public function serviceGetNotificationsFanAdded($aEvent)
  693. {
  694. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_fan_added']);
  695. }
  696. protected function _serviceGetNotification($aEvent, $sLangKey)
  697. {
  698. $CNF = &$this->_oConfig->CNF;
  699. $iContentId = (int)$aEvent['object_id'];
  700. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  701. if(!$oGroupProfile)
  702. return array();
  703. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  704. if(empty($aContentInfo) || !is_array($aContentInfo))
  705. return array();
  706. $oProfile = BxDolProfile::getInstance((int)$aEvent['subobject_id']);
  707. if(!$oProfile)
  708. return array();
  709. /*
  710. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  711. * because Subentry URL has higher display priority and notification
  712. * should be linked to Group Profile (Group Profile -> Members tab)
  713. * instead of Personal Profile of a member, who performed an action.
  714. */
  715. $sEntryUrl = $oGroupProfile->getUrl();
  716. if(!empty($CNF['URL_ENTRY_FANS']))
  717. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_FANS'], array(
  718. 'profile_id' => $oGroupProfile->id()
  719. ));
  720. return array(
  721. 'entry_sample' => $CNF['T']['txt_sample_single'],
  722. 'entry_url' => $sEntryUrl,
  723. 'entry_caption' => $oGroupProfile->getDisplayName(),
  724. 'entry_author' => $oGroupProfile->id(),
  725. 'subentry_sample' => $oProfile->getDisplayName(),
  726. 'subentry_url' => $sEntryUrl,
  727. 'lang_key' => $sLangKey
  728. );
  729. }
  730. /**
  731. * Data for Timeline module
  732. */
  733. public function serviceGetTimelineData()
  734. {
  735. return BxBaseModGeneralModule::serviceGetTimelineData();
  736. }
  737. /**
  738. * Entry post for Timeline module
  739. */
  740. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  741. {
  742. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  743. if($a === false)
  744. return false;
  745. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  746. $a['content']['url'] = $oGroupProfile->getUrl();
  747. $a['content']['title'] = $oGroupProfile->getDisplayName();
  748. return $a;
  749. }
  750. // ====== PERMISSION METHODS
  751. /**
  752. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  753. */
  754. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  755. {
  756. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  757. }
  758. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  759. {
  760. $bInvited = false;
  761. if (bx_get('key')){
  762. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  763. $mixedInvited = $this->isInvited(bx_get('key'), $oGroupProfile->id());
  764. if ($mixedInvited === true)
  765. $bInvited = true;
  766. }
  767. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $iProfileId) || $bInvited)
  768. return CHECK_ACTION_RESULT_ALLOWED;
  769. return parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId);
  770. }
  771. /**
  772. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  773. */
  774. public function checkAllowedCompose(&$aDataEntry, $isPerformAction = false)
  775. {
  776. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  777. return _t('_sys_txt_access_denied');
  778. return parent::checkAllowedCompose ($aDataEntry, $isPerformAction);
  779. }
  780. /**
  781. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  782. */
  783. public function checkAllowedFanAdd(&$aDataEntry, $isPerformAction = false)
  784. {
  785. $mixedResult = $this->_modGroupsCheckAllowedFanAdd($aDataEntry, $isPerformAction);
  786. // call alert to allow custom checks
  787. bx_alert('system', 'check_allowed_fan_add', 0, 0, array(
  788. 'module' => $this->getName(),
  789. 'content_info' => $aDataEntry,
  790. 'profile_id' => bx_get_logged_profile_id(),
  791. 'override_result' => &$mixedResult
  792. ));
  793. return $mixedResult;
  794. }
  795. public function _modGroupsCheckAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  796. {
  797. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) || !isLogged())
  798. return _t('_sys_txt_access_denied');
  799. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], true, false);
  800. }
  801. /**
  802. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  803. */
  804. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  805. {
  806. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  807. return CHECK_ACTION_RESULT_ALLOWED;
  808. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  809. }
  810. protected function _checkAllowedActionByFan($sAction, $aDataEntry, $iProfileId = 0)
  811. {
  812. $CNF = &$this->_oConfig->CNF;
  813. $bRoles = $this->_oConfig->isRoles();
  814. if(empty($iProfileId))
  815. $iProfileId = bx_get_logged_profile_id();
  816. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  817. if(!$oGroupProfile)
  818. return $sAction == BX_BASE_MOD_GROUPS_ACTION_DELETE ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_not_found');
  819. $iGroupProfileId = $oGroupProfile->id();
  820. if(!$bRoles && $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aDataEntry))
  821. return CHECK_ACTION_RESULT_ALLOWED;
  822. if($bRoles && $this->isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId))
  823. return CHECK_ACTION_RESULT_ALLOWED;
  824. return _t('_sys_txt_access_denied');
  825. }
  826. public function isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId)
  827. {
  828. $bResult = false;
  829. $iProfileRole = $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  830. if(!empty($iProfileRole))
  831. switch($sAction) {
  832. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  833. if($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR))
  834. $bResult = true;
  835. break;
  836. default:
  837. $bResult = true;
  838. }
  839. // call alert to allow custom checks
  840. bx_alert('system', 'check_allowed_action_by_role', 0, 0, array(
  841. 'module' => $this->getName(),
  842. 'multi_roles' => $this->_oConfig->isMultiRoles(),
  843. 'action' => $sAction,
  844. 'content_profile_id' => $iGroupProfileId,
  845. 'content_info' => $aDataEntry,
  846. 'profile_id' => $iProfileId,
  847. 'profile_role' => $iProfileRole,
  848. 'override_result' => &$bResult
  849. ));
  850. return $bResult;
  851. }
  852. public function isRole($iProfileRole, $iRole)
  853. {
  854. if(!$this->_oConfig->isMultiRoles())
  855. return $iProfileRole == $iRole;
  856. else
  857. return $iProfileRole & (1 << ($iRole - 1));
  858. }
  859. public function serviceIsRole($iProfileRole, $iRole)
  860. {
  861. return $this->isRole($iProfileRole, $iRole);
  862. }
  863. public function checkAllowedManageFans($mixedDataEntry, $isPerformAction = false)
  864. {
  865. $aDataEntry = array();
  866. if(!is_array($mixedDataEntry)) {
  867. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  868. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  869. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  870. }
  871. else
  872. $aDataEntry = $mixedDataEntry;
  873. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  874. return CHECK_ACTION_RESULT_ALLOWED;
  875. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  876. }
  877. public function checkAllowedManageAdmins($mixedDataEntry, $isPerformAction = false)
  878. {
  879. $aDataEntry = array();
  880. if(!is_array($mixedDataEntry)) {
  881. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  882. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  883. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  884. }
  885. else
  886. $aDataEntry = $mixedDataEntry;
  887. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  888. return CHECK_ACTION_RESULT_ALLOWED;
  889. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  890. }
  891. public function checkAllowedEdit($aDataEntry, $isPerformAction = false)
  892. {
  893. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_EDIT, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  894. return CHECK_ACTION_RESULT_ALLOWED;
  895. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  896. }
  897. public function checkAllowedInvite($aDataEntry, $isPerformAction = false)
  898. {
  899. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_INVITE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  900. return CHECK_ACTION_RESULT_ALLOWED;
  901. return $this->checkAllowedEdit ($aDataEntry, $isPerformAction);
  902. }
  903. public function checkAllowedChangeCover($aDataEntry, $isPerformAction = false)
  904. {
  905. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  906. return CHECK_ACTION_RESULT_ALLOWED;
  907. return parent::checkAllowedChangeCover($aDataEntry, $isPerformAction);
  908. }
  909. public function checkAllowedDelete(&$aDataEntry, $isPerformAction = false)
  910. {
  911. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_DELETE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  912. return CHECK_ACTION_RESULT_ALLOWED;
  913. return parent::checkAllowedDelete($aDataEntry, $isPerformAction);
  914. }
  915. public function checkAllowedJoin(&$aDataEntry, $isPerformAction = false)
  916. {
  917. if (bx_get('key')){
  918. $sKey = bx_get('key');
  919. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  920. $aData = $this->_oDb->getInviteByKey($sKey, $oGroupProfile->id());
  921. if (isset($aData['invited_profile_id']) && $aData['invited_profile_id'] == bx_get_logged_profile_id()){
  922. return CHECK_ACTION_RESULT_ALLOWED;
  923. }
  924. }
  925. return _t('_sys_txt_access_denied');
  926. }
  927. public function checkAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  928. {
  929. $mixedResult = $this->_modGroupsCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  930. // call alert to allow custom checks
  931. bx_alert('system', 'check_allowed_subscribe_add', 0, 0, array(
  932. 'module' => $this->getName(),
  933. 'content_info' => $aDataEntry,
  934. 'profile_id' => bx_get_logged_profile_id(),
  935. 'override_result' => &$mixedResult
  936. ));
  937. return $mixedResult;
  938. }
  939. /**
  940. * Note. Is mainly needed for internal usage. Access level is 'public' to allow outer calls from alerts.
  941. */
  942. public function _modGroupsCheckAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  943. {
  944. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  945. return _t('_sys_txt_access_denied');
  946. return parent::_modProfileCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  947. }
  948. /**
  949. * @deprecated since version 11.0.3 and can be removed in the next version.
  950. */
  951. public function _checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  952. {
  953. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  954. }
  955. public function doAudit($iGroupProfileId, $iFanId, $sAction)
  956. {
  957. $oProfile = BxDolProfile::getInstance($iFanId);
  958. $iContentId = $oProfile->getContentId();
  959. $sModule = $oProfile->getModule();
  960. $oModule = BxDolModule::getInstance($sModule);
  961. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile') && $oModule->_oConfig){
  962. $CNF = $oModule->_oConfig->CNF;
  963. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iContentId))) : array();
  964. $AuditParams = array(
  965. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  966. 'context_profile_id' => $iGroupProfileId,
  967. 'context_profile_title' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName()
  968. );
  969. bx_audit(
  970. $iContentId,
  971. $sModule,
  972. $sAction,
  973. $AuditParams
  974. );
  975. }
  976. }
  977. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  978. {
  979. $sResult = $this->checkAllowedView($aDataEntry);
  980. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  981. // if profile view isn't allowed but visibility is in partially visible groups
  982. // then display buttons to connect (befriend, join) to profile,
  983. // if other conditions (in parent::_checkAllowedConnect) are met as well
  984. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && !in_array($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), array('s'))))
  985. return $sResult;
  986. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  987. }
  988. public function addFollower ($iProfileId1, $iProfileId2)
  989. {
  990. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  991. if($oConnectionFollow && !$oConnectionFollow->isConnected($iProfileId1, $iProfileId2)){
  992. $oConnectionFollow->addConnection($iProfileId1, $iProfileId2);
  993. return true;
  994. }
  995. return false;
  996. }
  997. public function isFan ($iContentId, $iProfileId = false)
  998. {
  999. $CNF = &$this->_oConfig->CNF;
  1000. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  1001. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1002. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1003. return false;
  1004. }
  1005. public function isFanByGroupProfileId ($iGroupProfileId, $iProfileId = false)
  1006. {
  1007. $CNF = &$this->_oConfig->CNF;
  1008. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1009. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1010. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1011. return false;
  1012. }
  1013. public function isInvited ($sKey, $iProfileId)
  1014. {
  1015. $CNF = &$this->_oConfig->CNF;
  1016. $aData = $this->_oDb->getInviteByKey($sKey, $iProfileId);
  1017. if (!isset($aData['invited_profile_id']))
  1018. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1019. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1020. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1021. return true;
  1022. }
  1023. public function getRole($iGroupProfileId, $iFanProfileId)
  1024. {
  1025. if(!$this->isFanByGroupProfileId($iGroupProfileId, $iFanProfileId))
  1026. return false;
  1027. return $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1028. }
  1029. public function setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  1030. {
  1031. $CNF = &$this->_oConfig->CNF;
  1032. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1033. return false;
  1034. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1035. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1036. if(!$oConnection || !$oGroupProfile)
  1037. return false;
  1038. if(!$oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !($oConnection->addConnection($iFanProfileId, $iGroupProfileId) && $oConnection->addConnection($iGroupProfileId, $iFanProfileId)))
  1039. return false;
  1040. if(!$this->_oDb->setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod, $sOrder))
  1041. return false;
  1042. $this->onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole);
  1043. return true;
  1044. }
  1045. public function onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole)
  1046. {
  1047. $CNF = &$this->_oConfig->CNF;
  1048. $iProfileId = bx_get_logged_profile_id();
  1049. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1050. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1051. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  1052. // notify about admin status
  1053. if(!empty($CNF['EMAIL_FAN_SET_ROLE']) && $iFanProfileId != $iProfileId)
  1054. sendMailTemplate($CNF['EMAIL_FAN_SET_ROLE'], 0, $iFanProfileId, array(
  1055. 'EntryUrl' => $oGroupProfile->getUrl(),
  1056. 'EntryTitle' => $oGroupProfile->getDisplayName(),
  1057. 'Role' => !is_array($mixedRole) ? $aRoles[(int)$mixedRole] : 'TODO: Add List of Roles'
  1058. ), BX_EMAIL_NOTIFY);
  1059. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1060. 'object_author_id' => $iGroupProfileId,
  1061. 'performer_id' => $iProfileId,
  1062. 'fan_id' => $iFanProfileId,
  1063. 'content' => $aGroupProfileInfo,
  1064. 'role' => $mixedRole,
  1065. 'group_profile' => $iGroupProfileId,
  1066. 'profile' => $iProfileId
  1067. ));
  1068. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1069. }
  1070. public function unsetRole($iGroupProfileId, $iFanProfileId)
  1071. {
  1072. $CNF = &$this->_oConfig->CNF;
  1073. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1074. return false;
  1075. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1076. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1077. if(!$oConnection || !$oGroupProfile)
  1078. return false;
  1079. if($oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->removeConnection($iFanProfileId, $iGroupProfileId))
  1080. return false;
  1081. $iRole = $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1082. if(!$this->_oDb->unsetRole($iGroupProfileId, $iFanProfileId))
  1083. return false;
  1084. $this->onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole);
  1085. return true;
  1086. }
  1087. public function onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole)
  1088. {
  1089. $CNF = &$this->_oConfig->CNF;
  1090. $iProfileId = bx_get_logged_profile_id();
  1091. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1092. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1093. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1094. 'object_author_id' => $iGroupProfileId,
  1095. 'performer_id' => $iProfileId,
  1096. 'fan_id' => $iFanProfileId,
  1097. 'content' => $aGroupProfileInfo,
  1098. 'role' => $iRole,
  1099. 'group_profile' => $iGroupProfileId,
  1100. 'profile' => $iProfileId
  1101. ));
  1102. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1103. }
  1104. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  1105. {
  1106. $CNF = &$this->_oConfig->CNF;
  1107. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  1108. $sSrc = '';
  1109. if(isset($CNF['FIELD_COVER']) && !empty($aContentInfo[$CNF['FIELD_COVER']]))
  1110. $sSrc = $oGroupProfile->getCover();
  1111. if(empty($sSrc) && isset($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  1112. $sSrc = $oGroupProfile->getPicture();
  1113. return empty($sSrc) ? array() : array(
  1114. array('id' => $aContentInfo[$CNF['FIELD_PICTURE']], 'url' => $sUrl, 'src' => $sSrc, 'src_orig' => $sSrc),
  1115. );
  1116. }
  1117. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  1118. {
  1119. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  1120. return array(0, 0, null);
  1121. if ($oGroupProfile->getModule() == $this->getName()) {
  1122. $iProfileId = $iInitiatorId;
  1123. $iGroupProfileId = $oGroupProfile->id();
  1124. } else {
  1125. $iProfileId = $oGroupProfile->id();
  1126. $iGroupProfileId = $iInitiatorId;
  1127. }
  1128. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  1129. }
  1130. }
  1131. /** @} */