BxBaseModGroupsModule.php 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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_ROLE_COMMON', 0);
  13. define('BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR', 1);
  14. define('BX_BASE_MOD_GROUPS_ROLE_MODERATOR', 2);
  15. define('BX_BASE_MOD_GROUPS_ACTION_EDIT', 'edit');
  16. define('BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER', 'change_cover');
  17. define('BX_BASE_MOD_GROUPS_ACTION_INVITE', 'invite');
  18. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS', 'manage_fans');
  19. define('BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES', 'manage_roles');
  20. define('BX_BASE_MOD_GROUPS_ACTION_DELETE', 'delete');
  21. define('BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT', 'edit_any');
  22. define('BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT', 'delete_any');
  23. define('BX_BASE_MOD_GROUPS_ACTION_TIMELINE_POST_PIN', 'pin'); //for timeline posts only
  24. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY', 'day');
  25. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK', 'week');
  26. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_MONTH', 'month');
  27. define('BX_BASE_MOD_GROUPS_PERIOD_UNIT_YEAR', 'year');
  28. /**
  29. * Groups profiles module.
  30. */
  31. class BxBaseModGroupsModule extends BxBaseModProfileModule
  32. {
  33. function __construct(&$aModule)
  34. {
  35. parent::__construct($aModule);
  36. $CNF = &$this->_oConfig->CNF;
  37. if(isset($CNF['FIELD_PUBLISHED']))
  38. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  39. $CNF['FIELD_PUBLISHED'],
  40. ));
  41. }
  42. /**
  43. * Get possible recipients for start conversation form
  44. */
  45. public function actionAjaxGetInitialMembers ()
  46. {
  47. $sTerm = bx_get('term');
  48. $a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  49. header('Content-Type:text/javascript; charset=utf-8');
  50. echo(json_encode($a));
  51. }
  52. /**
  53. * Process Process Invitation
  54. */
  55. public function actionProcessInvite ($sKey, $iGroupProfileId, $bAccept)
  56. {
  57. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  58. if (isset($aData['invited_profile_id'])){
  59. $CNF = &$this->_oConfig->CNF;
  60. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  61. return '';
  62. $iInvitedProfileId = $aData['invited_profile_id'];
  63. if ($iInvitedProfileId != bx_get_logged_profile_id())
  64. return '';
  65. if ($bAccept){
  66. if($oConnection && !$oConnection->isConnected($iInvitedProfileId, $iGroupProfileId)){
  67. $oConnection->addConnection($iInvitedProfileId, $iGroupProfileId);
  68. $oConnection->addConnection($iGroupProfileId, $iInvitedProfileId);
  69. }
  70. }
  71. $this->_oDb->deleteInviteByKey($sKey, $iGroupProfileId);
  72. }
  73. }
  74. public function actionCheckName()
  75. {
  76. $CNF = &$this->_oConfig->CNF;
  77. $sName = bx_process_input(bx_get('name'));
  78. if(empty($sName))
  79. return echoJson(array());
  80. $sResult = '';
  81. $iId = (int)bx_get('id');
  82. if(!empty($iId)) {
  83. $aPrice = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iId));
  84. if(strcmp($sName, $aPrice[$CNF['FIELD_PRICE_NAME']]) == 0)
  85. $sResult = $sName;
  86. }
  87. echoJson(array(
  88. 'name' => !empty($sResult) ? $sResult : $this->_oConfig->getPriceName($sName)
  89. ));
  90. }
  91. public function serviceGetOptionsMembersMode()
  92. {
  93. $CNF = &$this->_oConfig->CNF;
  94. return array(
  95. array('key' => '', 'value' => _t('_None')),
  96. array('key' => BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES, 'value' => _t($CNF['T']['option_members_mode_' . BX_BASE_MOD_GROUPS_MMODE_MULTI_ROLES])),
  97. );
  98. }
  99. public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '')
  100. {
  101. if(empty($sUnitTemplate))
  102. $sUnitTemplate = 'unit.html';
  103. return parent::serviceGetSearchResultUnit($iContentId, $sUnitTemplate);
  104. }
  105. /**
  106. * @see BxBaseModProfileModule::serviceGetSpaceTitle
  107. */
  108. public function serviceGetSpaceTitle()
  109. {
  110. return _t($this->_oConfig->CNF['T']['txt_sample_single']);
  111. }
  112. /**
  113. * @see iBxDolProfileService::serviceGetParticipatingProfiles
  114. */
  115. public function serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects = false)
  116. {
  117. if (isset($this->_oConfig->CNF['OBJECT_CONNECTIONS'])){
  118. $aConnectionObjects = array($this->_oConfig->CNF['OBJECT_CONNECTIONS'], 'sys_profiles_subscriptions');
  119. return parent::serviceGetParticipatingProfiles($iProfileId, $aConnectionObjects);
  120. }
  121. return parent::serviceGetParticipatingProfiles($iProfileId);
  122. }
  123. /**
  124. * Check if this module entry can be used as profile
  125. */
  126. public function serviceActAsProfile ()
  127. {
  128. return false;
  129. }
  130. /**
  131. * Check if this module is group profile
  132. */
  133. public function serviceIsGroupProfile ()
  134. {
  135. return true;
  136. }
  137. public function serviceIsEnableForContext($iProfileId = 0)
  138. {
  139. $CNF = &$this->_oConfig->CNF;
  140. $sCnfKey = 'ENABLE_FOR_CONTEXT_IN_MODULES';
  141. if(empty($iProfileId) || empty($CNF[$sCnfKey]) || !is_array($CNF[$sCnfKey]))
  142. return false;
  143. if(in_array(BxDolProfile::getInstance($iProfileId)->getModule(), $CNF[$sCnfKey]))
  144. return true;
  145. return false;
  146. }
  147. /**
  148. * check if provided profile is member of the group
  149. */
  150. public function serviceIsFan ($iGroupProfileId, $iProfileId = false)
  151. {
  152. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  153. return $this->isFan($oGroupProfile->getContentId(), $iProfileId);
  154. }
  155. /**
  156. * check if provided profile is admin of the group
  157. */
  158. public function serviceIsAdmin ($iGroupProfileId, $iProfileId = false)
  159. {
  160. if(!$iProfileId)
  161. $iProfileId = bx_get_logged_profile_id();
  162. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  163. $iGroupContentId = $oGroupProfile->getContentId();
  164. if(!$this->isFan($iGroupContentId, $iProfileId))
  165. return false;
  166. $aGroupContentInfo = $this->_oDb->getContentInfoById($iGroupContentId);
  167. return $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aGroupContentInfo);
  168. }
  169. public function serviceGetAdminRole($iGroupProfileId, $iProfileId = false)
  170. {
  171. if(!$iProfileId)
  172. $iProfileId = bx_get_logged_profile_id();
  173. if(!$this->serviceIsAdmin($iGroupProfileId, $iProfileId))
  174. return 0;
  175. return $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  176. }
  177. /**
  178. * Delete profile from fans and admins tables
  179. * @param $iProfileId profile id
  180. */
  181. public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
  182. {
  183. $CNF = &$this->_oConfig->CNF;
  184. $this->_oDb->deleteAdminsByProfileId($iProfileId);
  185. if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  186. $oConnection->onDeleteInitiatorAndContent($iProfileId);
  187. }
  188. /**
  189. * Reset group's author for particular group
  190. * @param $iContentId group id
  191. * @parem $iAuthorId new author profile ID
  192. * @return false of error, or number of updated records on success
  193. */
  194. public function serviceReassignEntityAuthor ($iContentId, $iAuthorId = 0)
  195. {
  196. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  197. if (!$aContentInfo)
  198. return false;
  199. if (empty($iAuthorId)) {
  200. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  201. if (!$oGroupProfile)
  202. return false;
  203. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  204. if($aAdmins)
  205. $iAuthorId = array_pop($aAdmins);
  206. }
  207. return $this->_oDb->updateAuthorById($iContentId, $iAuthorId);
  208. }
  209. /**
  210. * Entry actions and social sharing block
  211. */
  212. public function serviceEntityAllActions ($mixedContent = false, $aParams = array())
  213. {
  214. $CNF = &$this->_oConfig->CNF;
  215. if(!empty($mixedContent)) {
  216. if(!is_array($mixedContent))
  217. $mixedContent = array((int)$mixedContent, (method_exists($this->_oDb, 'getContentInfoById')) ? $this->_oDb->getContentInfoById((int)$mixedContent) : array());
  218. }
  219. else {
  220. $mixedContent = $this->_getContent();
  221. if($mixedContent === false)
  222. return false;
  223. }
  224. list($iContentId, $aContentInfo) = $mixedContent;
  225. if(!empty($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  226. $aParams = array_merge(array(
  227. 'entry_thumb' => (int)$aContentInfo[$CNF['FIELD_PICTURE']]
  228. ), $aParams);
  229. return parent::serviceEntityAllActions ($mixedContent, $aParams);
  230. }
  231. /**
  232. * Reset group's author when author profile is deleted
  233. * @param $iProfileId author profile id
  234. * @param $iAuthorId new author profile id
  235. * @return number of changed items
  236. */
  237. public function serviceReassignEntitiesByAuthor ($iProfileId, $iAuthorId = 0)
  238. {
  239. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  240. if (!$a)
  241. return 0;
  242. $iCount = 0;
  243. foreach ($a as $aContentInfo)
  244. $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']], $iAuthorId) ? 1 : 0);
  245. return $iCount;
  246. }
  247. public function servicePrepareFields ($aFieldsProfile)
  248. {
  249. $CNF = &$this->_oConfig->CNF;
  250. $aFieldsProfile[$CNF['FIELD_NAME']] = $aFieldsProfile['name'];
  251. $aFieldsProfile[$CNF['FIELD_TEXT']] = isset($aFieldsProfile['description']) ? $aFieldsProfile['description'] : '';
  252. unset($aFieldsProfile['name']);
  253. unset($aFieldsProfile['description']);
  254. return $aFieldsProfile;
  255. }
  256. public function serviceOnRemoveConnection ($iGroupProfileId, $iInitiatorId)
  257. {
  258. $CNF = &$this->_oConfig->CNF;
  259. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  260. if (!$oGroupProfile)
  261. return false;
  262. $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
  263. if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
  264. return $oConn->removeConnection($iProfileId, $iGroupProfileId);
  265. return false;
  266. }
  267. public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $bSendInviteOnly = false)
  268. {
  269. $CNF = &$this->_oConfig->CNF;
  270. list ($iProfileId, $iGroupProfileId, $oGroupProfile) = $this->_prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId);
  271. if (!$oGroupProfile)
  272. return false;
  273. if (!($aContentInfo = $this->_oDb->getContentInfoById((int)BxDolProfile::getInstance($iGroupProfileId)->getContentId())))
  274. return false;
  275. if (!isset($CNF['OBJECT_CONNECTIONS']) || !($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
  276. return false;
  277. $sEntryTitle = $aContentInfo[$CNF['FIELD_NAME']];
  278. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  279. // send invitation to the group
  280. $sModule = $this->getName();
  281. if ($bSendInviteOnly && !$oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && bx_get_logged_profile_id() != $iProfileId) {
  282. bx_alert($sModule, 'join_invitation', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  283. 'content' => $aContentInfo,
  284. 'entry_title' => $sEntryTitle,
  285. 'entry_url' => $sEntryUrl,
  286. 'group_profile' => $iGroupProfileId,
  287. 'profile' => $iProfileId,
  288. 'notification_subobject_id' => $iProfileId,
  289. 'object_author_id' => $iGroupProfileId
  290. ));
  291. /**
  292. * 'Invitation Received' alert for Notifications module.
  293. * Note. It's essential to use Recipient ($iInitiatorId) in 'object_author_id' parameter.
  294. * In this case notification will be received by Recipient profile.
  295. */
  296. bx_alert($sModule, 'join_invitation_notif', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  297. 'object_author_id' => $iInitiatorId,
  298. 'privacy_view' => isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) ? $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 3,
  299. ));
  300. }
  301. // send notification to group's admins that new connection is pending confirmation
  302. elseif (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id()) && !$oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId) && $aContentInfo['join_confirmation']) {
  303. bx_alert($this->getName(), 'join_request', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  304. 'object_author_id' => $iGroupProfileId,
  305. 'performer_id' => $iProfileId,
  306. 'content' => $aContentInfo,
  307. 'entry_title' => $sEntryTitle,
  308. 'entry_url' => $sEntryUrl,
  309. 'group_profile' => $iGroupProfileId,
  310. 'profile' => $iProfileId
  311. ));
  312. }
  313. // send notification that join request was accepted
  314. else if (!$bSendInviteOnly && $oConnection->isConnected((int)$iInitiatorId, $oGroupProfile->id(), true) && $oGroupProfile->getModule() != $this->getName() && bx_get_logged_profile_id() != $iProfileId) {
  315. bx_alert($this->getName(), 'join_request_accepted', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  316. 'object_author_id' => $iGroupProfileId,
  317. 'performer_id' => $iProfileId,
  318. 'content' => $aContentInfo,
  319. 'entry_title' => $sEntryTitle,
  320. 'entry_url' => $sEntryUrl,
  321. 'group_profile' => $iGroupProfileId,
  322. 'profile' => $iProfileId
  323. ));
  324. }
  325. // new fan was added
  326. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true)) {
  327. // follow group on join
  328. if (BxDolService::call($oGroupProfile->getModule(), 'act_as_profile')){
  329. $this->addFollower($oGroupProfile->id(), (int)$iInitiatorId);
  330. }
  331. else{
  332. $this->addFollower((int)$iInitiatorId, $oGroupProfile->id());
  333. }
  334. bx_alert($this->getName(), 'fan_added', $aContentInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  335. 'object_author_id' => $iGroupProfileId,
  336. 'performer_id' => $iProfileId,
  337. 'content' => $aContentInfo,
  338. 'entry_title' => $sEntryTitle,
  339. 'entry_url' => $sEntryUrl,
  340. 'group_profile' => $iGroupProfileId,
  341. 'profile' => $iProfileId,
  342. ));
  343. $this->doAudit($iGroupProfileId, $iInitiatorId, '_sys_audit_action_group_join_request_accepted');
  344. return false;
  345. }
  346. // don't automatically add connection (mutual) if group requires manual join confirmation
  347. if ($bSendInviteOnly || $aContentInfo['join_confirmation'])
  348. return false;
  349. // check if connection already exists
  350. if ($oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId, true) || $oConnection->isConnected($oGroupProfile->id(), (int)$iInitiatorId))
  351. return false;
  352. if (!$oConnection->addConnection($oGroupProfile->id(), (int)$iInitiatorId))
  353. return false;
  354. return true;
  355. }
  356. public function serviceFansTable ()
  357. {
  358. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_CONNECTIONS']);
  359. if (!$oGrid)
  360. return false;
  361. return $oGrid->getCode();
  362. }
  363. public function serviceInvitesTable ()
  364. {
  365. if (!isset($this->_oConfig->CNF['OBJECT_GRID_INVITES']))
  366. return false;
  367. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID_INVITES']);
  368. if (!$oGrid)
  369. return false;
  370. return $oGrid->getCode();
  371. }
  372. public function serviceFans ($iContentId = 0, $bAsArray = false)
  373. {
  374. if (!$iContentId)
  375. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  376. if (!$iContentId)
  377. return false;
  378. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  379. if (!$aContentInfo)
  380. return false;
  381. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  382. return false;
  383. if(!$bAsArray) {
  384. bx_import('BxDolConnection');
  385. $mixedResult = $this->serviceBrowseConnectionsQuick ($oGroupProfile->id(), $this->_oConfig->CNF['OBJECT_CONNECTIONS'], BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  386. if (!$mixedResult)
  387. return MsgBox(_t('_sys_txt_empty'));
  388. }
  389. else
  390. $mixedResult = BxDolConnection::getObjectInstance($this->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  391. return $mixedResult;
  392. }
  393. public function serviceFansWithoutAdmins ($iContentId = 0, $bAsArray = false)
  394. {
  395. if (!$iContentId)
  396. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  397. if (!$iContentId)
  398. return false;
  399. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  400. if (!$aContentInfo)
  401. return false;
  402. if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
  403. return false;
  404. $CNF = &$this->_oConfig->CNF;
  405. $aFans = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->getConnectedContent($oGroupProfile->id(), true);
  406. if(empty($aFans) || !is_array($aFans))
  407. return false;
  408. $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
  409. if(!empty($aAdmins) && is_array($aAdmins))
  410. $aFans = array_diff($aFans, $aAdmins);
  411. $iStart = (int)bx_get('start');
  412. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  413. if(!$iLimit)
  414. $iLimit = 4;
  415. return $this->_serviceBrowseQuick($aFans, $iStart, $iLimit);
  416. }
  417. public function serviceAdmins ($iContentId = 0)
  418. {
  419. $CNF = &$this->_oConfig->CNF;
  420. if(!$iContentId)
  421. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  422. if(!$iContentId)
  423. return false;
  424. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  425. if(!$oGroupProfile)
  426. return false;
  427. $iStart = (int)bx_get('start');
  428. $iLimit = !empty($CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? getParam($CNF['PARAM_NUM_CONNECTIONS_QUICK']) : 4;
  429. if(!$iLimit)
  430. $iLimit = 4;
  431. $aProfiles = $this->_oDb->getAdmins($oGroupProfile->id(), $iStart, $iLimit+1);
  432. if(empty($aProfiles) || !is_array($aProfiles))
  433. return false;
  434. return $this->_serviceBrowseQuick($aProfiles, $iStart, $iLimit);
  435. }
  436. public function serviceBrowseJoinedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  437. {
  438. if (!$iProfileId)
  439. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  440. if (!$iProfileId)
  441. return '';
  442. return $this->_serviceBrowse ('joined_entries', array('joined_profile' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  443. }
  444. public function serviceBrowseCreatedEntries ($iProfileId = 0, $bDisplayEmptyMsg = false)
  445. {
  446. if (!$iProfileId)
  447. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  448. if (!$iProfileId)
  449. return '';
  450. return $this->_serviceBrowse ('created_entries', array('author' => $iProfileId), BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  451. }
  452. public function serviceEntityPricing($iProfileId = 0)
  453. {
  454. $CNF = &$this->_oConfig->CNF;
  455. if(!$iProfileId)
  456. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  457. if(!$iProfileId)
  458. return '';
  459. if(!$this->_oConfig->isPaidJoin())
  460. return '';
  461. if($this->checkAllowedUsePaidJoin() !== CHECK_ACTION_RESULT_ALLOWED)
  462. return MsgBox(_t('_Access denied'));
  463. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  464. return MsgBox(_t('_Access denied'));
  465. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_MANAGE']);
  466. if(!$oGrid)
  467. return '';
  468. return $oGrid->getCode();
  469. }
  470. public function serviceEntityJoin($iProfileId = 0)
  471. {
  472. $CNF = &$this->_oConfig->CNF;
  473. if(!$iProfileId)
  474. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  475. if(!$iProfileId)
  476. return '';
  477. if(!$this->_oConfig->isPaidJoin())
  478. return '';
  479. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_PRICES_VIEW']);
  480. if(!$oGrid)
  481. return '';
  482. return $oGrid->getCode();
  483. }
  484. public function serviceEntityInvite ($iContentId = 0, $bErrorMsg = true)
  485. {
  486. if (isset($this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE']))
  487. return $this->_serviceEntityForm ('editDataForm', $iContentId, $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_INVITE'], false, $bErrorMsg);
  488. return false;
  489. }
  490. /**
  491. * Entry social sharing block
  492. */
  493. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  494. {
  495. if(!empty($mixedContent)) {
  496. if(!is_array($mixedContent))
  497. $mixedContent = array((int)$mixedContent, array());
  498. }
  499. else {
  500. $mixedContent = $this->_getContent();
  501. if($mixedContent === false)
  502. return false;
  503. }
  504. list($iContentId, $aContentInfo) = $mixedContent;
  505. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  506. if(!$oGroupProfile)
  507. return false;
  508. return parent::serviceEntitySocialSharing(array($iContentId, $aContentInfo), array(
  509. 'title' => $oGroupProfile->getDisplayName()
  510. ));
  511. }
  512. public function serviceIsPricingAvaliable($iProfileId)
  513. {
  514. if(!$this->_oConfig->isPaidJoin())
  515. return false;
  516. if($this->checkAllowedUsePaidJoin() !== CHECK_ACTION_RESULT_ALLOWED)
  517. return false;
  518. if($this->checkAllowedManageAdmins($iProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  519. return false;
  520. return true;
  521. }
  522. public function serviceIsPaidJoinAvaliable($iGroupProfileId, $iProfileId = 0)
  523. {
  524. return $this->isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId);
  525. }
  526. public function serviceIsPaidJoinAvaliableByContent($iGroupContentId, $iProfileId = 0)
  527. {
  528. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName());
  529. if(!$oGroupProfile)
  530. return false;
  531. return $this->isPaidJoinByProfileForProfile($oGroupProfile->id(), $iProfileId);
  532. }
  533. public function serviceIsFreeJoinAvaliable($iGroupProfileId, $iProfileId = 0)
  534. {
  535. return !$this->isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId);
  536. }
  537. public function serviceIsFreeJoinAvaliableByContent($iGroupContentId, $iProfileId = 0)
  538. {
  539. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iGroupContentId, $this->getName());
  540. if(!$oGroupProfile)
  541. return false;
  542. return !$this->isPaidJoinByProfileForProfile($oGroupProfile->id(), $iProfileId);
  543. }
  544. /**
  545. * Is Paid Join enabled in the group and whether a profile can use it.
  546. *
  547. * @param type $iGroupProfileId - Group profile ID.
  548. * @param type $iProfileId - Profile ID of the user who wants to join.
  549. * @return boolean
  550. */
  551. public function isPaidJoinByProfileForProfile($iGroupProfileId, $iProfileId = 0)
  552. {
  553. $CNF = &$this->_oConfig->CNF;
  554. if(empty($iProfileId))
  555. $iProfileId = $this->_iProfileId;
  556. if(BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])->isConnected($iProfileId, $iGroupProfileId))
  557. return false;
  558. return $this->isPaidJoinByProfile($iGroupProfileId);
  559. }
  560. /**
  561. * Is Paid Join enabled as is and whether a group has pricing plans added.
  562. *
  563. * @param type $iGroupProfileId - Group profile ID.
  564. * @return boolean
  565. */
  566. public function isPaidJoinByProfile($iGroupProfileId)
  567. {
  568. if(!$this->_oConfig->isPaidJoin())
  569. return false;
  570. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $iGroupProfileId));
  571. if(empty($aPrices) || !is_array($aPrices))
  572. return false;
  573. return true;
  574. }
  575. /**
  576. * Integration with Payments.
  577. */
  578. public function serviceGetPaymentData()
  579. {
  580. return $this->_aModule;
  581. }
  582. public function serviceGetCartItem($mixedItemId)
  583. {
  584. $CNF = &$this->_oConfig->CNF;
  585. if(!$mixedItemId)
  586. return array();
  587. if(is_numeric($mixedItemId))
  588. $aItem = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => (int)$mixedItemId));
  589. else
  590. $aItem = $this->_oDb->getPrices(array('type' => 'by_name', 'value' => $mixedItemId));
  591. if(empty($aItem) || !is_array($aItem))
  592. return array();
  593. if(!$this->isPaidJoinByProfile($aItem['profile_id']))
  594. return array();
  595. $oGroupProfile = BxDolProfile::getInstance($aItem['profile_id']);
  596. if(!$oGroupProfile)
  597. return array();
  598. $aGroupProfile = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  599. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  600. return array (
  601. 'id' => $aItem['id'],
  602. 'author_id' => $aGroupProfile[$CNF['FIELD_AUTHOR']],
  603. 'name' => $aItem['name'],
  604. 'title' => _t($CNF['T']['txt_cart_item_title'], $oGroupProfile->getDisplayName(), $aRoles[$aItem['role_id']], $aItem['period'], $aItem['period_unit']),
  605. 'description' => '',
  606. 'url' => $oGroupProfile->getUrl(),
  607. 'price_single' => $aItem['price'],
  608. 'price_recurring' => $aItem['price'],
  609. 'period_recurring' => $aItem['period'],
  610. 'period_unit_recurring' => $aItem['period_unit'],
  611. 'trial_recurring' => 0
  612. );
  613. }
  614. public function serviceGetCartItems($iSellerId)
  615. {
  616. $CNF = &$this->_oConfig->CNF;
  617. if(empty($iSellerId))
  618. return array();
  619. $sModule = $this->getName();
  620. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  621. $aGroups = $this->_oDb->getEntriesBy(array('type' => 'author', 'author' => $iSellerId));
  622. $aResult = array();
  623. foreach($aGroups as $aGroup) {
  624. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aGroup[$CNF['FIELD_ID']], $sModule);
  625. if(!$oGroupProfile)
  626. continue;
  627. $aPrices = $this->_oDb->getPrices(array('type' => 'by_profile_id', 'profile_id' => $oGroupProfile->id()));
  628. if(empty($aPrices) || !is_array($aPrices))
  629. continue;
  630. $sTitle = $oGroupProfile->getDisplayName();
  631. $sUrl = $oGroupProfile->getUrl();
  632. foreach($aPrices as $aPrice)
  633. $aResult[] = array(
  634. 'id' => $aPrice['id'],
  635. 'author_id' => $iSellerId,
  636. 'name' => $aPrice['name'],
  637. 'title' => _t($CNF['T']['txt_cart_item_title'], $sTitle, $aRoles[$aPrice['role_id']], $aPrice['period'], $aPrice['period_unit']),
  638. 'description' => '',
  639. 'url' => $sUrl,
  640. 'price_single' => $aPrice['price'],
  641. 'price_recurring' => $aPrice['price'],
  642. 'period_recurring' => $aPrice['period'],
  643. 'period_unit_recurring' => $aPrice['period_unit']
  644. );
  645. }
  646. return $aResult;
  647. }
  648. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  649. {
  650. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  651. }
  652. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  653. {
  654. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  655. }
  656. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  657. {
  658. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  659. }
  660. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  661. {
  662. return $this->_serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder);
  663. }
  664. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  665. {
  666. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  667. }
  668. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  669. {
  670. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder);
  671. }
  672. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  673. {
  674. return true;
  675. }
  676. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  677. {
  678. $CNF = &$this->_oConfig->CNF;
  679. $aItem = $this->serviceGetCartItem($iItemId);
  680. if(empty($aItem) || !is_array($aItem))
  681. return array();
  682. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  683. $mixedPeriod = false;
  684. if((int)$aItemInfo['period'] != 0)
  685. $mixedPeriod = array(
  686. 'period' => (int)$aItemInfo['period'],
  687. 'period_unit' => $aItemInfo['period_unit'],
  688. 'period_reserve' => $CNF['PARAM_RECURRING_RESERVE']
  689. );
  690. if(!$this->setRole($aItemInfo['profile_id'], $iClientId, $aItemInfo['role_id'], $mixedPeriod, $sOrder))
  691. return array();
  692. return $aItem;
  693. }
  694. protected function _serviceReregisterItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  695. {
  696. $aItem = $this->serviceGetCartItem($iItemIdNew);
  697. if(empty($aItem) || !is_array($aItem))
  698. return array();
  699. $aItemInfoOld = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdOld));
  700. if(empty($aItemInfoOld) || !is_array($aItemInfoOld))
  701. return array();
  702. $aItemInfoNew = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemIdNew));
  703. if(empty($aItemInfoNew) || !is_array($aItemInfoNew))
  704. return array();
  705. if(!$this->unsetRole($aItemInfoOld['profile_id'], $iClientId))
  706. return array();
  707. $aResult = $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemIdNew, 1, $sOrder);
  708. if(empty($aResult) || !is_array($aResult))
  709. return array();
  710. return $aItem;
  711. }
  712. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  713. {
  714. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  715. if(empty($aItemInfo) || !is_array($aItemInfo))
  716. return false;
  717. return $this->unsetRole($aItemInfo['profile_id'], $iClientId);
  718. }
  719. /**
  720. * Data for Notifications module
  721. */
  722. public function serviceGetNotificationsData()
  723. {
  724. $sModule = $this->_aModule['name'];
  725. $aSettingsTypes = array('follow_member', 'follow_context');
  726. if($this->serviceActAsProfile())
  727. $aSettingsTypes = array('personal', 'follow_member');
  728. return array(
  729. 'handlers' => array(
  730. array('group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'),
  731. array('group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'),
  732. 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'),
  733. 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'),
  734. 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'),
  735. array('group' => $sModule . '_join_invitation', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'join_invitation_notif', 'module_name' => $sModule, 'module_method' => 'get_notifications_join_invitation', 'module_class' => 'Module'),
  736. 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']),
  737. 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'),
  738. ),
  739. 'settings' => array(
  740. array('group' => 'vote', 'unit' => $sModule, 'action' => 'doVote', 'types' => $aSettingsTypes),
  741. array('group' => 'score_up', 'unit' => $sModule, 'action' => 'doVoteUp', 'types' => $aSettingsTypes),
  742. array('group' => 'score_down', 'unit' => $sModule, 'action' => 'doVoteDown', 'types' => $aSettingsTypes),
  743. array('group' => 'fan', 'unit' => $sModule, 'action' => 'fan_added', 'types' => $aSettingsTypes),
  744. array('group' => 'invite', 'unit' => $sModule, 'action' => 'join_invitation_notif', 'types' => array('personal')),
  745. array('group' => 'join', 'unit' => $sModule, 'action' => 'join_request', 'types' => $aSettingsTypes),
  746. array('group' => 'timeline_post', 'unit' => $sModule, 'action' => 'timeline_post_common', 'types' => $aSettingsTypes)
  747. ),
  748. 'alerts' => array(
  749. array('unit' => $sModule, 'action' => 'doVote'),
  750. array('unit' => $sModule, 'action' => 'undoVote'),
  751. array('unit' => $sModule, 'action' => 'doVoteUp'),
  752. array('unit' => $sModule, 'action' => 'doVoteDown'),
  753. array('unit' => $sModule, 'action' => 'fan_added'),
  754. array('unit' => $sModule, 'action' => 'join_invitation_notif'),
  755. array('unit' => $sModule, 'action' => 'join_request'),
  756. array('unit' => $sModule, 'action' => 'timeline_post_common'),
  757. )
  758. );
  759. }
  760. /**
  761. * Notification about new invitation to join the group
  762. */
  763. public function serviceGetNotificationsJoinInvitation($aEvent)
  764. {
  765. $CNF = &$this->_oConfig->CNF;
  766. $iContentId = (int)$aEvent['object_id'];
  767. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  768. if(!$oGroupProfile)
  769. return array();
  770. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  771. if(empty($aContentInfo) || !is_array($aContentInfo))
  772. return array();
  773. /*
  774. * It's essential that 'object_owner_id' contains invited member profile id.
  775. */
  776. $oProfile = BxDolProfile::getInstance((int)$aEvent['object_owner_id']);
  777. if(!$oProfile)
  778. return array();
  779. /*
  780. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  781. * because Subentry URL has higher display priority and notification
  782. * should be linked to Group Profile (Group Profile -> Members tab)
  783. * instead of Personal Profile of invited member.
  784. */
  785. $sEntryUrl = $oGroupProfile->getUrl();
  786. return array(
  787. 'entry_sample' => $CNF['T']['txt_sample_single'],
  788. 'entry_url' => $sEntryUrl,
  789. 'entry_caption' => $oGroupProfile->getDisplayName(),
  790. 'subentry_sample' => $oProfile->getDisplayName(),
  791. 'subentry_url' => $sEntryUrl,
  792. 'lang_key' => $this->_oConfig->CNF['T']['txt_ntfs_join_invitation']
  793. );
  794. }
  795. /**
  796. * Notification about new member requst in the group
  797. */
  798. public function serviceGetNotificationsJoinRequest($aEvent)
  799. {
  800. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_join_request']);
  801. }
  802. /**
  803. * Notification about new member in the group
  804. */
  805. public function serviceGetNotificationsFanAdded($aEvent)
  806. {
  807. return $this->_serviceGetNotification($aEvent, $this->_oConfig->CNF['T']['txt_ntfs_fan_added']);
  808. }
  809. protected function _serviceGetNotification($aEvent, $sLangKey)
  810. {
  811. $CNF = &$this->_oConfig->CNF;
  812. $iContentId = (int)$aEvent['object_id'];
  813. $oGroupProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->getName());
  814. if(!$oGroupProfile)
  815. return array();
  816. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  817. if(empty($aContentInfo) || !is_array($aContentInfo))
  818. return array();
  819. $oProfile = BxDolProfile::getInstance((int)$aEvent['subobject_id']);
  820. if(!$oProfile)
  821. return array();
  822. /*
  823. * Note. Group Profile URL is used for both Entry and Subentry URLs,
  824. * because Subentry URL has higher display priority and notification
  825. * should be linked to Group Profile (Group Profile -> Members tab)
  826. * instead of Personal Profile of a member, who performed an action.
  827. */
  828. $sEntryUrl = $oGroupProfile->getUrl();
  829. if(!empty($CNF['URL_ENTRY_FANS']))
  830. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_ENTRY_FANS'], array(
  831. 'profile_id' => $oGroupProfile->id()
  832. ));
  833. return array(
  834. 'entry_sample' => $CNF['T']['txt_sample_single'],
  835. 'entry_url' => $sEntryUrl,
  836. 'entry_caption' => $oGroupProfile->getDisplayName(),
  837. 'entry_author' => $oGroupProfile->id(),
  838. 'subentry_sample' => $oProfile->getDisplayName(),
  839. 'subentry_url' => $sEntryUrl,
  840. 'lang_key' => $sLangKey
  841. );
  842. }
  843. /**
  844. * Data for Timeline module
  845. */
  846. public function serviceGetTimelineData()
  847. {
  848. return BxBaseModGeneralModule::serviceGetTimelineData();
  849. }
  850. /**
  851. * Entry post for Timeline module
  852. */
  853. public function serviceGetTimelinePost($aEvent, $aBrowseParams = array())
  854. {
  855. $CNF = &$this->_oConfig->CNF;
  856. $a = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
  857. if($a === false)
  858. return false;
  859. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  860. $a['content']['url'] = $oGroupProfile->getUrl();
  861. $a['content']['title'] = $oGroupProfile->getDisplayName();
  862. if(isset($CNF['FIELD_PUBLISHED'])) {
  863. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  864. if($aContentInfo[$CNF['FIELD_PUBLISHED']] > $a['date'])
  865. $a['date'] = $aContentInfo[$CNF['FIELD_PUBLISHED']];
  866. }
  867. return $a;
  868. }
  869. // ====== PERMISSION METHODS
  870. /**
  871. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  872. */
  873. public function checkAllowedUsePaidJoin($isPerformAction = false)
  874. {
  875. // check ACL
  876. $aCheck = checkActionModule($this->_iProfileId, 'use paid join', $this->getName(), $isPerformAction);
  877. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  878. return $aCheck[CHECK_ACTION_MESSAGE];
  879. return CHECK_ACTION_RESULT_ALLOWED;
  880. }
  881. /**
  882. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  883. */
  884. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  885. {
  886. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  887. }
  888. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  889. {
  890. $CNF = &$this->_oConfig->CNF;
  891. $bInvited = false;
  892. if(!empty($CNF['TABLE_INVITES']) && bx_get('key')){
  893. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  894. $mixedInvited = $this->isInvited(bx_get('key'), $oGroupProfile->id());
  895. if($mixedInvited === true)
  896. $bInvited = true;
  897. }
  898. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $iProfileId) || $bInvited)
  899. return CHECK_ACTION_RESULT_ALLOWED;
  900. return parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId);
  901. }
  902. /**
  903. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  904. */
  905. public function checkAllowedCompose(&$aDataEntry, $isPerformAction = false)
  906. {
  907. if(!$this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]))
  908. return _t('_sys_txt_access_denied');
  909. return parent::checkAllowedCompose ($aDataEntry, $isPerformAction);
  910. }
  911. /**
  912. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  913. */
  914. public function checkAllowedFanAdd(&$aDataEntry, $isPerformAction = false)
  915. {
  916. $mixedResult = $this->_modGroupsCheckAllowedFanAdd($aDataEntry, $isPerformAction);
  917. // call alert to allow custom checks
  918. bx_alert('system', 'check_allowed_fan_add', 0, 0, array(
  919. 'module' => $this->getName(),
  920. 'content_info' => $aDataEntry,
  921. 'profile_id' => bx_get_logged_profile_id(),
  922. 'override_result' => &$mixedResult
  923. ));
  924. return $mixedResult;
  925. }
  926. public function _modGroupsCheckAllowedFanAdd (&$aDataEntry, $isPerformAction = false)
  927. {
  928. if ($this->isFan($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]) || !isLogged())
  929. return _t('_sys_txt_access_denied');
  930. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], true, false);
  931. }
  932. /**
  933. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  934. */
  935. public function checkAllowedFanRemove (&$aDataEntry, $isPerformAction = false)
  936. {
  937. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, true))
  938. return CHECK_ACTION_RESULT_ALLOWED;
  939. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, $this->_oConfig->CNF['OBJECT_CONNECTIONS'], false, true, false);
  940. }
  941. protected function _checkAllowedActionByFan($sAction, $aDataEntry, $iProfileId = 0)
  942. {
  943. $CNF = &$this->_oConfig->CNF;
  944. $bRoles = $this->_oConfig->isRoles();
  945. if(empty($iProfileId))
  946. $iProfileId = bx_get_logged_profile_id();
  947. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$CNF['FIELD_ID']], $this->getName());
  948. if(!$oGroupProfile)
  949. return $sAction == BX_BASE_MOD_GROUPS_ACTION_DELETE ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_not_found');
  950. $iGroupProfileId = $oGroupProfile->id();
  951. if(!$bRoles && $this->_oDb->isAdmin($iGroupProfileId, $iProfileId, $aDataEntry))
  952. return CHECK_ACTION_RESULT_ALLOWED;
  953. if($bRoles && $this->isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId))
  954. return CHECK_ACTION_RESULT_ALLOWED;
  955. return _t('_sys_txt_access_denied');
  956. }
  957. public function isAllowedActionByRole($sAction, $aDataEntry, $iGroupProfileId, $iProfileId)
  958. {
  959. $iProfileRole = $this->_oDb->getRole($iGroupProfileId, $iProfileId);
  960. $bResult = $iProfileId ? $this->isAllowedModuleActionByRole($this->getName(), $sAction, $iProfileRole) : false;
  961. // in case neithe of the profile's roles are having permissions set explicitly
  962. if ($bResult === NULL) {
  963. $bResult = false;
  964. if(!empty($iProfileRole)) {
  965. switch ($sAction) {
  966. case BX_BASE_MOD_GROUPS_ACTION_DELETE:
  967. case BX_BASE_MOD_GROUPS_ACTION_EDIT:
  968. case BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER:
  969. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  970. if ($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR)) $bResult = true;
  971. break;
  972. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS:
  973. case BX_BASE_MOD_GROUPS_ACTION_INVITE:
  974. case BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT:
  975. case BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT:
  976. if ($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR) || $this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_MODERATOR)) $bResult = true;
  977. break;
  978. default:
  979. $bResult = true;
  980. }
  981. }
  982. }
  983. // call alert to allow custom checks
  984. bx_alert('system', 'check_allowed_action_by_role', 0, 0, array(
  985. 'module' => $this->getName(),
  986. 'multi_roles' => $this->_oConfig->isMultiRoles(),
  987. 'action' => $sAction,
  988. 'content_profile_id' => $iGroupProfileId,
  989. 'content_info' => $aDataEntry,
  990. 'profile_id' => $iProfileId,
  991. 'profile_role' => $iProfileRole,
  992. 'override_result' => &$bResult
  993. ));
  994. return $bResult;
  995. }
  996. public function isAllowedModuleActionByRole($sModule, $sAction, $iProfileRole) {
  997. static $aRoles;
  998. if (!$aRoles && isset($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES']) && !empty($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES']))
  999. $aRoles = BxBaseFormView::getDataItems($this->_oConfig->CNF['OBJECT_PRE_LIST_ROLES'], true, BX_DATA_VALUES_ALL);
  1000. if ($aRoles) {
  1001. foreach ($aRoles as $iRole => $aRoleData) {
  1002. if ($iRole == 0 && $iProfileRole == 0 || $iRole > 0 && $this->isRole($iProfileRole, $iRole)) {
  1003. $mPermissions = isset($aRoles[$iRole]) && isset($aRoles[$iRole]['Data']) && !empty($aRoles[$iRole]['Data']) ? unserialize($aRoles[$iRole]['Data']) : false;
  1004. if ($mPermissions && isset($mPermissions[$sModule])) {
  1005. return isset($mPermissions[$sModule][$sAction]) && $mPermissions[$sModule][$sAction];
  1006. } else {
  1007. // default values in case a role has not been specified explicitly
  1008. switch ($sAction) {
  1009. case BX_BASE_MOD_GROUPS_ACTION_EDIT:
  1010. case BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER:
  1011. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES:
  1012. if ($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR)) return true;
  1013. break;
  1014. case BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS:
  1015. case BX_BASE_MOD_GROUPS_ACTION_INVITE:
  1016. case BX_BASE_MOD_GROUPS_ACTION_EDIT_CONTENT:
  1017. case BX_BASE_MOD_GROUPS_ACTION_DELETE_CONTENT:
  1018. case BX_BASE_MOD_GROUPS_ACTION_TIMELINE_POST_PIN:
  1019. if ($this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR) || $this->isRole($iProfileRole, BX_BASE_MOD_GROUPS_ROLE_MODERATOR)) return true;
  1020. break;
  1021. // for the rest of the actions like "Post" still return NULL
  1022. // to let the code go down the stack of checkAllowed* function calls for a default behavior
  1023. }
  1024. }
  1025. }
  1026. }
  1027. }
  1028. return NULL;
  1029. }
  1030. public function isAllowedModuleActionByProfile($iContentId, $sPostModule, $sAction, $iProfileId = 0) {
  1031. if (!$iProfileId) $iProfileId = bx_get_logged_profile_id();
  1032. if ($iProfileId && $this->isFan($iContentId, $iProfileId)) {
  1033. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  1034. $iProfileRole = $this->_oDb->getRole($oGroupProfile->id(), $iProfileId);
  1035. $bResult = $this->isAllowedModuleActionByRole($sPostModule, $sAction, $iProfileRole);
  1036. if ($bResult === true) return CHECK_ACTION_RESULT_ALLOWED;
  1037. if ($bResult === false) return _t('_sys_txt_access_denied');
  1038. }
  1039. return NULL; //undefined, because the profile is either not a fan or his role is not having permissions defined. So process the default way then.
  1040. }
  1041. public function isRole($iProfileRole, $iRole)
  1042. {
  1043. if(!$this->_oConfig->isMultiRoles())
  1044. return $iProfileRole == $iRole;
  1045. else
  1046. return $iProfileRole & (1 << ($iRole - 1));
  1047. }
  1048. public function serviceIsRole($iProfileRole, $iRole)
  1049. {
  1050. return $this->isRole($iProfileRole, $iRole);
  1051. }
  1052. public function checkAllowedManageFans($mixedDataEntry, $isPerformAction = false)
  1053. {
  1054. $aDataEntry = array();
  1055. if(!is_array($mixedDataEntry)) {
  1056. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  1057. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  1058. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1059. }
  1060. else
  1061. $aDataEntry = $mixedDataEntry;
  1062. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_FANS, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1063. return CHECK_ACTION_RESULT_ALLOWED;
  1064. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1065. }
  1066. public function checkAllowedManageAdmins($mixedDataEntry, $isPerformAction = false)
  1067. {
  1068. $aDataEntry = array();
  1069. if(!is_array($mixedDataEntry)) {
  1070. $oGroupProfile = BxDolProfile::getInstance((int)$mixedDataEntry);
  1071. if($oGroupProfile && $this->getName() == $oGroupProfile->getModule())
  1072. $aDataEntry = $this->_oDb->getContentInfoById($oGroupProfile->getContentId());
  1073. }
  1074. else
  1075. $aDataEntry = $mixedDataEntry;
  1076. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_MANAGE_ROLES, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1077. return CHECK_ACTION_RESULT_ALLOWED;
  1078. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1079. }
  1080. public function checkAllowedEdit($aDataEntry, $isPerformAction = false)
  1081. {
  1082. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_EDIT, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1083. return CHECK_ACTION_RESULT_ALLOWED;
  1084. return parent::checkAllowedEdit($aDataEntry, $isPerformAction);
  1085. }
  1086. public function checkAllowedInvite($aDataEntry, $isPerformAction = false)
  1087. {
  1088. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_INVITE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1089. return CHECK_ACTION_RESULT_ALLOWED;
  1090. return $this->checkAllowedEdit ($aDataEntry, $isPerformAction);
  1091. }
  1092. public function checkAllowedChangeCover($aDataEntry, $isPerformAction = false)
  1093. {
  1094. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_CHANGE_COVER, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1095. return CHECK_ACTION_RESULT_ALLOWED;
  1096. return parent::checkAllowedChangeCover($aDataEntry, $isPerformAction);
  1097. }
  1098. public function checkAllowedDelete(&$aDataEntry, $isPerformAction = false)
  1099. {
  1100. if($this->_checkAllowedActionByFan(BX_BASE_MOD_GROUPS_ACTION_DELETE, $aDataEntry) === CHECK_ACTION_RESULT_ALLOWED)
  1101. return CHECK_ACTION_RESULT_ALLOWED;
  1102. return parent::checkAllowedDelete($aDataEntry, $isPerformAction);
  1103. }
  1104. public function checkAllowedJoin(&$aDataEntry, $isPerformAction = false)
  1105. {
  1106. if (bx_get('key')){
  1107. $sKey = bx_get('key');
  1108. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->getName());
  1109. $aData = $this->_oDb->getInviteByKey($sKey, $oGroupProfile->id());
  1110. if (isset($aData['invited_profile_id']) && $aData['invited_profile_id'] == bx_get_logged_profile_id()){
  1111. return CHECK_ACTION_RESULT_ALLOWED;
  1112. }
  1113. }
  1114. return _t('_sys_txt_access_denied');
  1115. }
  1116. public function checkAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1117. {
  1118. $mixedResult = $this->_modGroupsCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1119. // call alert to allow custom checks
  1120. bx_alert('system', 'check_allowed_subscribe_add', 0, 0, array(
  1121. 'module' => $this->getName(),
  1122. 'content_info' => $aDataEntry,
  1123. 'profile_id' => bx_get_logged_profile_id(),
  1124. 'override_result' => &$mixedResult
  1125. ));
  1126. return $mixedResult;
  1127. }
  1128. /**
  1129. * Note. Is mainly needed for internal usage. Access level is 'public' to allow outer calls from alerts.
  1130. */
  1131. public function _modGroupsCheckAllowedSubscribeAdd(&$aDataEntry, $isPerformAction = false)
  1132. {
  1133. $CNF = &$this->_oConfig->CNF;
  1134. if(!$this->isFan($aDataEntry[$CNF['FIELD_ID']]) && (!isset($CNF['PARAM_SBS_WO_JOIN']) || getParam($CNF['PARAM_SBS_WO_JOIN']) != 'on'))
  1135. return _t('_sys_txt_access_denied');
  1136. return parent::_modProfileCheckAllowedSubscribeAdd($aDataEntry, $isPerformAction);
  1137. }
  1138. /**
  1139. * @deprecated since version 11.0.3 and can be removed in the next version.
  1140. */
  1141. public function _checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  1142. {
  1143. return parent::checkAllowedSubscribeAdd ($aDataEntry, $isPerformAction);
  1144. }
  1145. public function doAudit($iGroupProfileId, $iFanId, $sAction)
  1146. {
  1147. $oProfile = BxDolProfile::getInstance($iFanId);
  1148. $iContentId = $oProfile->getContentId();
  1149. $sModule = $oProfile->getModule();
  1150. $oModule = BxDolModule::getInstance($sModule);
  1151. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile') && $oModule->_oConfig){
  1152. $CNF = $oModule->_oConfig->CNF;
  1153. $aContentInfo = BxDolRequest::serviceExists($sModule, 'get_all') ? BxDolService::call($sModule, 'get_all', array(array('type' => 'id', 'id' => $iContentId))) : array();
  1154. $AuditParams = array(
  1155. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  1156. 'context_profile_id' => $iGroupProfileId,
  1157. 'context_profile_title' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName()
  1158. );
  1159. bx_audit(
  1160. $iContentId,
  1161. $sModule,
  1162. $sAction,
  1163. $AuditParams
  1164. );
  1165. }
  1166. }
  1167. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  1168. {
  1169. $sResult = $this->checkAllowedView($aDataEntry);
  1170. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->CNF['OBJECT_PRIVACY_VIEW']);
  1171. // if profile view isn't allowed but visibility is in partially visible groups
  1172. // then display buttons to connect (befriend, join) to profile,
  1173. // if other conditions (in parent::_checkAllowedConnect) are met as well
  1174. if (CHECK_ACTION_RESULT_ALLOWED !== $sResult && !in_array($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), array('s'))))
  1175. return $sResult;
  1176. return parent::_checkAllowedConnect ($aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap);
  1177. }
  1178. // ====== COMMON METHODS
  1179. /**
  1180. * Get array of params to be passed in Add/Edit Alert.
  1181. */
  1182. protected function _alertParams($aContentInfo)
  1183. {
  1184. $aParams = parent::_alertParams($aContentInfo);
  1185. $CNF = &$this->_oConfig->CNF;
  1186. if(!empty($CNF['FIELD_ALLOW_VIEW_TO']) && isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]))
  1187. $aParams['privacy_view'] = $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  1188. return $aParams;
  1189. }
  1190. public function addFollower ($iProfileId1, $iProfileId2)
  1191. {
  1192. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  1193. if($oConnectionFollow && !$oConnectionFollow->isConnected($iProfileId1, $iProfileId2)){
  1194. $oConnectionFollow->addConnection($iProfileId1, $iProfileId2);
  1195. return true;
  1196. }
  1197. return false;
  1198. }
  1199. public function isFan ($iContentId, $iProfileId = false)
  1200. {
  1201. $CNF = &$this->_oConfig->CNF;
  1202. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
  1203. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1204. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1205. return false;
  1206. }
  1207. public function isFanByGroupProfileId ($iGroupProfileId, $iProfileId = false)
  1208. {
  1209. $CNF = &$this->_oConfig->CNF;
  1210. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1211. if($oGroupProfile && isset($CNF['OBJECT_CONNECTIONS']))
  1212. return ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) && $oConnection->isConnected($iProfileId ? $iProfileId : bx_get_logged_profile_id(), $oGroupProfile->id(), true);
  1213. return false;
  1214. }
  1215. public function isInvited ($sKey, $iGroupProfileId)
  1216. {
  1217. $CNF = &$this->_oConfig->CNF;
  1218. $aData = $this->_oDb->getInviteByKey($sKey, $iGroupProfileId);
  1219. if (!isset($aData['invited_profile_id']))
  1220. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1221. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1222. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1223. return true;
  1224. }
  1225. public function isInvitedByProfileId ($iProfileId, $iGroupProfileId)
  1226. {
  1227. $CNF = &$this->_oConfig->CNF;
  1228. $aData = $this->_oDb->getInviteByInvited($iProfileId, $iGroupProfileId);
  1229. if (!isset($aData['invited_profile_id']))
  1230. return _t($CNF['T']['txt_invitation_popup_error_invitation_absent']);
  1231. if ($aData['invited_profile_id'] != bx_get_logged_profile_id())
  1232. return _t($CNF['T']['txt_invitation_popup_error_wrong_user']);
  1233. return true;
  1234. }
  1235. public function serviceIsInvited($iGroupProfileId, $iProfileId = false, $sKey = '')
  1236. {
  1237. if(!$iProfileId)
  1238. $iProfileId = bx_get_logged_profile_id();
  1239. if(empty($sKey) && ($sKey = bx_get('key')) !== false)
  1240. $sKey = bx_process_input($sKey);
  1241. if(!empty($sKey))
  1242. $mixedInvited = $this->isInvited($sKey, $iGroupProfileId);
  1243. else if($iProfileId !== false)
  1244. $mixedInvited = $this->isInvitedByProfileId($iProfileId, $iGroupProfileId);
  1245. return $mixedInvited === true;
  1246. }
  1247. public function serviceIsNotInvited($iGroupProfileId, $iProfileId = false, $sKey = '')
  1248. {
  1249. return !$this->serviceIsInvited($iGroupProfileId, $iProfileId, $sKey);
  1250. }
  1251. public function serviceGetInvitedKey($iGroupProfileId, $iProfileId = false)
  1252. {
  1253. $sKey = '';
  1254. if(($sKey = bx_get('key')) !== false)
  1255. $sKey = bx_process_input($sKey);
  1256. if(!$sKey) {
  1257. if(!$iProfileId)
  1258. $iProfileId = bx_get_logged_profile_id();
  1259. if($iProfileId !== false) {
  1260. $aInvite = $this->_oDb->getInviteByInvited($iProfileId, $iGroupProfileId);
  1261. if(!empty($aInvite) && is_array($aInvite))
  1262. $sKey = $aInvite['key'];
  1263. }
  1264. }
  1265. return $sKey;
  1266. }
  1267. public function getRole($iGroupProfileId, $iFanProfileId)
  1268. {
  1269. if(!$this->isFanByGroupProfileId($iGroupProfileId, $iFanProfileId))
  1270. return false;
  1271. return $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1272. }
  1273. public function setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  1274. {
  1275. $CNF = &$this->_oConfig->CNF;
  1276. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1277. return false;
  1278. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1279. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1280. if(!$oConnection || !$oGroupProfile)
  1281. return false;
  1282. if(!$oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !($oConnection->addConnection($iFanProfileId, $iGroupProfileId) && $oConnection->addConnection($iGroupProfileId, $iFanProfileId)))
  1283. return false;
  1284. if(!$this->_oDb->setRole($iGroupProfileId, $iFanProfileId, $mixedRole, $mixedPeriod, $sOrder))
  1285. return false;
  1286. $this->onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole);
  1287. return true;
  1288. }
  1289. public function onSetRole($iGroupProfileId, $iFanProfileId, $mixedRole)
  1290. {
  1291. $CNF = &$this->_oConfig->CNF;
  1292. $iProfileId = bx_get_logged_profile_id();
  1293. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1294. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1295. $aRoles = BxDolFormQuery::getDataItems($CNF['OBJECT_PRE_LIST_ROLES']);
  1296. // notify about admin status
  1297. if(!empty($CNF['EMAIL_FAN_SET_ROLE']) && $iFanProfileId != $iProfileId) {
  1298. $aSetRoles = is_array($mixedRole) ? $mixedRole : [$mixedRole];
  1299. $aRolesNames = [];
  1300. foreach ($aSetRoles as $iRole)
  1301. $aRolesNames[] = $aRoles[(int)$iRole];
  1302. sendMailTemplate($CNF['EMAIL_FAN_SET_ROLE'], 0, $iFanProfileId, array(
  1303. 'EntryUrl' => $oGroupProfile->getUrl(),
  1304. 'EntryTitle' => $oGroupProfile->getDisplayName(),
  1305. 'Role' => implode(', ', $aRolesNames),
  1306. ), BX_EMAIL_NOTIFY);
  1307. }
  1308. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1309. 'object_author_id' => $iGroupProfileId,
  1310. 'performer_id' => $iProfileId,
  1311. 'fan_id' => $iFanProfileId,
  1312. 'content' => $aGroupProfileInfo,
  1313. 'role' => $mixedRole,
  1314. 'group_profile' => $iGroupProfileId,
  1315. 'profile' => $iProfileId
  1316. ));
  1317. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1318. }
  1319. public function unsetRole($iGroupProfileId, $iFanProfileId)
  1320. {
  1321. $CNF = &$this->_oConfig->CNF;
  1322. if(!isset($CNF['OBJECT_CONNECTIONS']))
  1323. return false;
  1324. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  1325. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1326. if(!$oConnection || !$oGroupProfile)
  1327. return false;
  1328. if($oConnection->isConnected($iFanProfileId, $iGroupProfileId, true) && !$oConnection->removeConnection($iFanProfileId, $iGroupProfileId))
  1329. return false;
  1330. $iRole = $this->_oDb->getRole($iGroupProfileId, $iFanProfileId);
  1331. if(!$this->_oDb->unsetRole($iGroupProfileId, $iFanProfileId))
  1332. return false;
  1333. $this->onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole);
  1334. return true;
  1335. }
  1336. public function onUnsetRole($iGroupProfileId, $iFanProfileId, $iRole)
  1337. {
  1338. $CNF = &$this->_oConfig->CNF;
  1339. $iProfileId = bx_get_logged_profile_id();
  1340. $oGroupProfile = BxDolProfile::getInstance($iGroupProfileId);
  1341. $aGroupProfileInfo = $this->_oDb->getContentInfoById((int)$oGroupProfile->getContentId());
  1342. bx_alert($this->getName(), 'set_role', $aGroupProfileInfo[$CNF['FIELD_ID']], $iGroupProfileId, array(
  1343. 'object_author_id' => $iGroupProfileId,
  1344. 'performer_id' => $iProfileId,
  1345. 'fan_id' => $iFanProfileId,
  1346. 'content' => $aGroupProfileInfo,
  1347. 'role' => $iRole,
  1348. 'group_profile' => $iGroupProfileId,
  1349. 'profile' => $iProfileId
  1350. ));
  1351. $this->doAudit($iGroupProfileId, $iFanProfileId, '_sys_audit_action_group_role_changed');
  1352. }
  1353. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  1354. {
  1355. $CNF = &$this->_oConfig->CNF;
  1356. $oGroupProfile = BxDolProfile::getInstanceByContentAndType($aEvent['object_id'], $this->getName());
  1357. $sSrc = '';
  1358. if(isset($CNF['FIELD_COVER']) && !empty($aContentInfo[$CNF['FIELD_COVER']]))
  1359. $sSrc = $oGroupProfile->getCover();
  1360. if(empty($sSrc) && isset($CNF['FIELD_PICTURE']) && !empty($aContentInfo[$CNF['FIELD_PICTURE']]))
  1361. $sSrc = $oGroupProfile->getPicture();
  1362. return empty($sSrc) ? array() : array(
  1363. array('id' => $aContentInfo[$CNF['FIELD_PICTURE']], 'url' => $sUrl, 'src' => $sSrc, 'src_orig' => $sSrc),
  1364. );
  1365. }
  1366. protected function _prepareProfileAndGroupProfile($iGroupProfileId, $iInitiatorId)
  1367. {
  1368. if (!($oGroupProfile = BxDolProfile::getInstance($iGroupProfileId)))
  1369. return array(0, 0, null);
  1370. if ($oGroupProfile->getModule() == $this->getName()) {
  1371. $iProfileId = $iInitiatorId;
  1372. $iGroupProfileId = $oGroupProfile->id();
  1373. } else {
  1374. $iProfileId = $oGroupProfile->id();
  1375. $iGroupProfileId = $iInitiatorId;
  1376. }
  1377. return array($iProfileId, $iGroupProfileId, $oGroupProfile);
  1378. }
  1379. }
  1380. /** @} */