BxBaseModGroupsModule.php 54 KB

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