BxInvModule.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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 Invites Invites
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolAcl');
  12. define('BX_INV_TYPE_FROM_MEMBER', 'from_member');
  13. define('BX_INV_TYPE_FROM_SYSTEM', 'from_system');
  14. class BxInvModule extends BxDolModule
  15. {
  16. protected $_bIsApi;
  17. /**
  18. * Constructor
  19. */
  20. public function __construct($aModule)
  21. {
  22. parent::__construct($aModule);
  23. $this->_bIsApi = bx_is_api();
  24. $this->_oConfig->init($this->_oDb);
  25. }
  26. /**
  27. * ACTION METHODS
  28. */
  29. public function serviceGetCode($aParams)
  30. {
  31. $iProfileId = $this->getProfileId();
  32. $iAccountId = $this->getAccountId($iProfileId);
  33. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  34. if($mixedAllowed !== true)
  35. return echoJson(['message' => $mixedAllowed]);
  36. $bContextAdmin = false;
  37. if(isset($aParams['aj_action'], $aParams['aj_params']) && $aParams['aj_action'] == 'invite_to_context' && ($iContextPid = (int)$aParams['aj_params']) != 0)
  38. $bContextAdmin = ($oContext = BxDolProfile::getInstance($iContextPid)) !== false && bx_srv($oContext->getModule(), 'is_admin', [$iContextPid, $iProfileId]);
  39. if(!isAdmin($iAccountId) && !$bContextAdmin && $this->_oConfig->getCountPerUser() <= 0)
  40. return $this->_bIsApi ? [bx_api_get_msg(_t('_bx_invites_err_limit_reached'))] : echoJson(['message' => _t('_bx_invites_err_limit_reached')]);
  41. $oKeys = BxDolKey::getInstance();
  42. if(!$oKeys)
  43. return $this->_bIsApi ? [bx_api_get_msg(_t('_bx_invites_err_not_available'))] : echoJson(['message' => _t('_bx_invites_err_not_available')]);
  44. $sKey = $oKeys->getNewKey(false, $this->_oConfig->getKeyLifetime());
  45. $this->getFormObjectInvite()->insert(array_merge([
  46. 'account_id' => $iAccountId,
  47. 'profile_id' => $iProfileId,
  48. 'key' => $sKey,
  49. 'multi' => 1,
  50. 'date' => time()
  51. ], $aParams));
  52. $this->onInvite($iProfileId);
  53. $sLink = $this->getJoinLink($sKey);
  54. if($this->_bIsApi)
  55. return [
  56. 'code' => $sKey,
  57. 'link' => bx_api_get_relative_url($sLink)
  58. ];
  59. echoJson(['popup' => $this->_oTemplate->getCodePopup($sKey, $sLink)]);
  60. }
  61. function serviceGetLink($aParams)
  62. {
  63. $iProfileId = $this->getProfileId();
  64. $iAccountId = $this->getAccountId($iProfileId);
  65. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  66. if($mixedAllowed !== true)
  67. return echoJson(array('message' => $mixedAllowed));
  68. if(!isAdmin($iAccountId)) {
  69. if($this->_oConfig->getCountPerUser() <= 0)
  70. return $this->_bIsApi ? [bx_api_get_msg(_t('_bx_invites_err_limit_reached'))] : echoJson(array('message' => _t('_bx_invites_err_limit_reached')));
  71. }
  72. $oKeys = BxDolKey::getInstance();
  73. if(!$oKeys)
  74. return $this->_bIsApi ? [bx_api_get_msg(_t('_bx_invites_err_not_available'))] : echoJson(array('message' => _t('_bx_invites_err_not_available')));
  75. $sKey = $oKeys->getNewKey(false, $this->_oConfig->getKeyLifetime());
  76. $this->getFormObjectInvite()->insert(array_merge([
  77. 'account_id' => $iAccountId,
  78. 'profile_id' => $iProfileId,
  79. 'key' => $sKey,
  80. 'date' => time()
  81. ], $aParams));
  82. $this->onInvite($iProfileId);
  83. $sLink = $this->getJoinLink($sKey);
  84. if($this->_bIsApi)
  85. return ['link' => bx_api_get_relative_url($sLink)];
  86. echoJson(['popup' => $this->_oTemplate->getLinkPopup($sLink)]);
  87. }
  88. public function actionGetCode()
  89. {
  90. $aParams = $this->_prepareParamsGet();
  91. $this->serviceGetCode($aParams);
  92. }
  93. public function actionGetLink()
  94. {
  95. $aParams = $this->_prepareParamsGet();
  96. $this->serviceGetLink($aParams);
  97. }
  98. public function actionSetSeenMark($Code)
  99. {
  100. header('Content-Type: image/png');
  101. if (isset($Code) && trim($Code) != "")
  102. $this->_oDb->updateDateSeenForInvite($Code);
  103. }
  104. /**
  105. * SERVICE METHODS
  106. */
  107. public function serviceGetSafeServices()
  108. {
  109. return array (
  110. 'GetBlockInvite' => '',
  111. 'GetBlockInviteToContext' => '',
  112. 'GetBlockFormInvite' => '',
  113. 'GetBlockFormRequest' => '',
  114. 'GetLink' => '',
  115. );
  116. }
  117. /**
  118. * @page service Service Calls
  119. * @section bx_invites Invitations
  120. * @subsection bx_invites-other Other
  121. * @subsubsection bx_invites-get_include get_include
  122. *
  123. * @code bx_srv('bx_invites', 'get_include', [...]); @endcode
  124. *
  125. * Get all necessary CSS and JS files to include in a page.
  126. *
  127. * @return string with all necessary CSS and JS files.
  128. *
  129. * @see BxInvModule::serviceGetInclude
  130. */
  131. /**
  132. * @ref bx_invites-get_include "get_include"
  133. */
  134. public function serviceGetInclude()
  135. {
  136. return $this->_oTemplate->getInclude();
  137. }
  138. /**
  139. * @page service Service Calls
  140. * @section bx_invites Invitations
  141. * @subsection bx_invites-page_blocks Page Blocks
  142. * @subsubsection bx_invites-get_block_invite get_block_invite
  143. *
  144. * @code bx_srv('bx_invites', 'get_block_invite', [...]); @endcode
  145. *
  146. * Get page block which displays invitations related info and action(s).
  147. *
  148. * @return an array describing a block to display on the site or empty string if something is wrong. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  149. *
  150. * @see BxInvModule::serviceGetBlockInvite
  151. */
  152. /**
  153. * @ref bx_invites-get_block_invite "get_block_invite"
  154. */
  155. public function serviceGetBlockInvite($bRedirect = false)
  156. {
  157. $iProfileId = $this->getProfileId();
  158. if(($mixedAllowed = $this->isAllowedInvite($iProfileId)) !== true)
  159. return '';
  160. $iAccountId = $this->getAccountId($iProfileId);
  161. if(!isAdmin($iAccountId) && $this->_oConfig->getCountPerUser() <= 0)
  162. return '';
  163. return [
  164. 'content' => $this->_oTemplate->getBlockInvite($iAccountId, $iProfileId, [
  165. 'redirect' => (bool)$bRedirect
  166. ])
  167. ];
  168. }
  169. /**
  170. * @page service Service Calls
  171. * @section bx_invites Invitations
  172. * @subsection bx_invites-page_blocks Page Blocks
  173. * @subsubsection bx_invites-get_block_invite get_block_invite_to_context
  174. *
  175. * @code bx_srv('bx_invites', 'get_block_invite_to_context', [...]); @endcode
  176. *
  177. * Get page block which displays invitations related info and action(s). It's similar to BxInvModule::serviceGetBlockInvite
  178. * but should be used on View Context page (should receive Context Profile ID). It invites to the context after joining the site.
  179. *
  180. * @return an array describing a block to display on the site or empty string if something is wrong.
  181. * All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  182. *
  183. * @see BxInvModule::serviceGetBlockInviteToContext
  184. */
  185. /**
  186. * @ref bx_invites-get_block_invite_to_context "get_block_invite_to_context"
  187. */
  188. public function serviceGetBlockInviteToContext($iContextPid)
  189. {
  190. $oContext = false;
  191. if(!$iContextPid || !($oContext = BxDolProfile::getInstance($iContextPid)))
  192. return '';
  193. $sContext = $oContext->getModule();
  194. if(!bx_srv('system', 'is_module_context', [$sContext]))
  195. return '';
  196. $iProfileId = $this->getProfileId();
  197. if(($mixedAllowed = $this->isAllowedInvite($iProfileId)) !== true)
  198. return '';
  199. $iAccountId = $this->getAccountId($iProfileId);
  200. if(!isAdmin($iAccountId) && !bx_srv($sContext, 'is_admin', [$iContextPid, $iProfileId]) && $this->_oConfig->getCountPerUser() <= 0)
  201. return '';
  202. return [
  203. 'content' => $this->_oTemplate->getBlockInvite($iAccountId, $iProfileId, [
  204. 'context' => (int)$iContextPid
  205. ])
  206. ];
  207. }
  208. /**
  209. * @page service Service Calls
  210. * @section bx_invites Invitations
  211. * @subsection bx_invites-page_blocks Page Blocks
  212. * @subsubsection bx_invites-get_block_form_invite get_block_form_invite
  213. *
  214. * @code bx_srv('bx_invites', 'get_block_form_invite', [...]); @endcode
  215. *
  216. * Get page block with invite form.
  217. *
  218. * @return an array describing a block to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  219. *
  220. * @see BxInvModule::serviceGetBlockFormInvite
  221. */
  222. /**
  223. * @ref bx_invites-get_block_form_invite "get_block_form_invite"
  224. */
  225. public function serviceGetBlockFormInvite($aParams = [])
  226. {
  227. if(!isset($aParams['aj_action'], $aParams['aj_params']))
  228. $aParams = array_merge($aParams, $this->_prepareParamsGet());
  229. $oForm = $this->getFormObjectInvite();
  230. $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
  231. foreach(['aj_action', 'aj_params'] as $sKey)
  232. if(!empty($aParams[$sKey]))
  233. $oForm->aInputs[$sKey] = [
  234. 'name' => $sKey,
  235. 'type' => 'hidden',
  236. 'value' => $aParams[$sKey]
  237. ];
  238. $sResult = '';
  239. $oForm->initChecker();
  240. if($oForm->isSubmittedAndValid()){
  241. $sResult = MsgBox($this->processFormObjectInvite($oForm));
  242. if($this->_bIsApi)
  243. return [bx_api_get_msg($sResult)];
  244. }
  245. if($this->_bIsApi)
  246. return [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['name' => $this->getName(), 'request' => ['url' => '/api.php?r=' . $this->getName() . '/get_block_form_invite', 'immutable' => true]]])];
  247. return [
  248. 'content' => $sResult . $oForm->getCode()
  249. ];
  250. }
  251. /**
  252. * @page service Service Calls
  253. * @section bx_invites Invitations
  254. * @subsection bx_invites-page_blocks Page Blocks
  255. * @subsubsection bx_invites-get_block_form_request get_block_form_request
  256. *
  257. * @code bx_srv('bx_invites', 'get_block_form_request', [...]); @endcode
  258. *
  259. * Get page block with request invitation form.
  260. *
  261. * @return an array describing a block to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  262. *
  263. * @see BxInvModule::serviceGetBlockFormRequest
  264. */
  265. /**
  266. * @ref bx_invites-get_block_form_request "get_block_form_request"
  267. */
  268. public function serviceGetBlockFormRequest()
  269. {
  270. $mixedResult = $this->_oTemplate->getBlockFormRequest();
  271. if ($this->_bIsApi)
  272. return $mixedResult;
  273. if(is_array($mixedResult)) {
  274. echoJson($mixedResult);
  275. exit;
  276. }
  277. return array(
  278. 'content' => $mixedResult
  279. );
  280. }
  281. /**
  282. * @page service Service Calls
  283. * @section bx_invites Invitations
  284. * @subsection bx_invites-page_blocks Page Blocks
  285. * @subsubsection bx_invites-get_block_manage_requests get_block_manage_requests
  286. *
  287. * @code bx_srv('bx_invites', 'get_block_manage_requests', [...]); @endcode
  288. *
  289. * Get page block with manage invitation requests table.
  290. *
  291. * @return HTML string with block content to display on the site or empty string if something is wrong. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  292. *
  293. * @see BxInvModule::serviceGetBlockManageRequests
  294. */
  295. /**
  296. * @ref bx_invites-get_block_manage_requests "get_block_manage_requests"
  297. */
  298. public function serviceGetBlockManageRequests()
  299. {
  300. return $this->getBlockManage('requests');
  301. }
  302. /**
  303. * @page service Service Calls
  304. * @section bx_invites Invitations
  305. * @subsection bx_invites-page_blocks Page Blocks
  306. * @subsubsection bx_invites-get_block_manage_invites get_block_manage_requests
  307. *
  308. * @code bx_srv('bx_invites', 'get_block_manage_invites', [...]); @endcode
  309. *
  310. * Get page block with manage invitations table.
  311. *
  312. * @return HTML string with block content to display on the site or empty string if something is wrong. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  313. *
  314. * @see BxInvModule::serviceGetBlockManageInvites
  315. */
  316. /**
  317. * @ref bx_invites-get_block_manage_invites "get_block_manage_invites"
  318. */
  319. public function serviceGetBlockManageInvites()
  320. {
  321. return $this->getBlockManage('invites');
  322. }
  323. /**
  324. * @page service Service Calls
  325. * @section bx_invites Invitations
  326. * @subsection bx_invites-other Other
  327. * @subsubsection bx_invites-get_menu_addon_requests get_menu_addon_requests
  328. *
  329. * @code bx_srv('bx_invites', 'get_menu_addon_requests', [...]); @endcode
  330. *
  331. * Get number or invitation requests.
  332. *
  333. * @return integer value with number of invitation requests.
  334. *
  335. * @see BxInvModule::serviceGetMenuAddonRequests
  336. */
  337. /**
  338. * @ref bx_invites-get_menu_addon_requests "get_menu_addon_requests"
  339. */
  340. public function serviceGetMenuAddonRequests()
  341. {
  342. return array('counter3_value' => $this->_oDb->getRequests(array('type' => 'count_all')));
  343. }
  344. /**
  345. * @page service Service Calls
  346. * @section bx_invites Invitations
  347. * @subsection bx_invites-other Other
  348. * @subsubsection bx_invites-account_add_form_check account_add_form_check
  349. *
  350. * @code bx_srv('bx_invites', 'account_add_form_check', [...]); @endcode
  351. *
  352. * Perform neccessary checking on join form.
  353. *
  354. * @return empty string - if join is allowed and should be processed as usual, non-empty string - if join form need to be replaced with this code.
  355. *
  356. * @see BxInvModule::serviceAccountAddFormCheck
  357. */
  358. /**
  359. * @ref bx_invites-account_add_form_check "account_add_form_check"
  360. */
  361. public function serviceAccountAddFormCheck()
  362. {
  363. $sReturn = '';
  364. $sKey = bx_get($this->_oConfig->getKeyCode());
  365. $bKey = $sKey !== false;
  366. if($bKey) {
  367. $sKey = bx_process_input($sKey);
  368. $oKeys = BxDolKey::getInstance();
  369. if($oKeys->isKeyExists($sKey))
  370. $this->_oConfig->setKey($sKey);
  371. else
  372. $this->_oConfig->unsetKey();
  373. }
  374. if($this->_oConfig->getKey() !== false)
  375. return $sReturn;
  376. if($bKey)
  377. $sReturn .= MsgBox(_t('_bx_invites_err_used'));
  378. if($this->_oConfig->isRegistrationByInvitation())
  379. $sReturn .= $this->_oTemplate->getBlockRequest();
  380. return $sReturn;
  381. }
  382. /**
  383. * Data for Notifications module
  384. */
  385. public function serviceGetNotificationsData()
  386. {
  387. $sModule = $this->_aModule['name'];
  388. $sEventPrivacy = $sModule . '_allow_view_event_to';
  389. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  390. $sEventPrivacy = '';
  391. return array(
  392. 'handlers' => array(
  393. array('group' => $sModule . '_object', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'request_notify', 'module_name' => $sModule, 'module_method' => 'get_notifications_request', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  394. ),
  395. 'settings' => array(
  396. array('group' => 'request', 'unit' => $sModule, 'action' => 'request_notify', 'types' => array('personal')),
  397. ),
  398. 'alerts' => array(
  399. array('unit' => $sModule, 'action' => 'request_notify'),
  400. )
  401. );
  402. }
  403. /**
  404. * Entry post for Notifications module
  405. */
  406. public function serviceGetNotificationsRequest($aEvent)
  407. {
  408. if (getParam('bx_invites_requests_notifications') != 'on')
  409. return [];
  410. $aRequest = $this->_oDb->getRequests(array('type' => 'by_id', 'value' => $aEvent['object_id']));
  411. if (!$aRequest)
  412. return [];
  413. $CNF = &$this->_oConfig->CNF;
  414. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_REQUESTS']), '{bx_url_root}');
  415. return [
  416. 'entry_sample' => $aRequest['name'] . '(' . $aRequest['email'] . ')',
  417. 'entry_url' => $sEntryUrl,
  418. 'entry_caption' => $aRequest['text'],
  419. 'entry_author' => '',
  420. 'entry_privacy' => '', //may be empty or not specified. In this case Public privacy will be used.
  421. 'lang_key' => '_bx_invites_alert_action_request', //may be empty or not specified. In this case the default one from Notification module will be used.
  422. ];
  423. }
  424. public function attachAccountIdToInvite($iAccountId, $sKey)
  425. {
  426. $this->_oDb->attachAccountIdToInvite($iAccountId, $sKey);
  427. }
  428. public function invite($sType, $aEmails, $sText, $aParams = [])
  429. {
  430. if(empty($aEmails) || !is_array($aEmails))
  431. return false;
  432. $oKeys = BxDolKey::getInstance();
  433. if(!$oKeys || !in_array($sType, [BX_INV_TYPE_FROM_MEMBER, BX_INV_TYPE_FROM_SYSTEM]))
  434. return false;
  435. $iKeyLifetime = $this->_oConfig->getKeyLifetime();
  436. $sEmailTemplate = '';
  437. switch($sType) {
  438. case BX_INV_TYPE_FROM_MEMBER:
  439. $sEmailTemplate = 'bx_invites_invite_form_message';
  440. break;
  441. case BX_INV_TYPE_FROM_SYSTEM:
  442. $sEmailTemplate = 'bx_invites_invite_by_request_message';
  443. break;
  444. }
  445. $iDate = time();
  446. $iProfileId = $this->getProfileId();
  447. $iAccountId = $this->getAccountId($iProfileId);
  448. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate($sEmailTemplate, array(
  449. 'text' => $sText
  450. ), $iAccountId, $iProfileId);
  451. $iEmailUse = !empty($aParams['email_use']) ? (int)$aParams['email_use'] : 0;
  452. $bLimit = !empty($aParams['limit']) && is_numeric($aParams['limit']);
  453. $aResults = [];
  454. foreach($aEmails as $sEmail) {
  455. $sEmail = trim($sEmail);
  456. if(empty($sEmail))
  457. continue;
  458. $sKey = $oKeys->getNewKey(false, $iKeyLifetime);
  459. $aMarkers = [
  460. 'join_url' => $this->getJoinLink($sKey),
  461. 'seen_image_url' => $this->getSeenImageUrl($sKey)
  462. ];
  463. if(sendMail($sEmail, $aMessage['Subject'], $aMessage['Body'], 0, $aMarkers, BX_EMAIL_SYSTEM, 'html', false, [], true)) {
  464. $iInviteId = (int)$this->_oDb->insertInvite([
  465. 'account_id' => $iAccountId,
  466. 'profile_id' => $iProfileId,
  467. 'key' => $sKey,
  468. 'email' => $sEmail,
  469. 'email_use' => $iEmailUse,
  470. 'aj_action' => !empty($aParams['aj_action']) ? $aParams['aj_action'] : '',
  471. 'aj_params' => !empty($aParams['aj_params']) ? $aParams['aj_params'] : '',
  472. 'date' => $iDate
  473. ]);
  474. $this->onInvite($iProfileId);
  475. $aResults[$iInviteId] = $sEmail;
  476. if($bLimit && count($aResults) == (int)$aParams['limit'])
  477. break;
  478. }
  479. }
  480. return $aResults;
  481. }
  482. public function processFormObjectInvite(&$oForm)
  483. {
  484. $iProfileId = $this->getProfileId();
  485. $iAccountId = $this->getAccountId($iProfileId);
  486. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  487. if($mixedAllowed !== true)
  488. return $mixedAllowed;
  489. $aInviteParams = [];
  490. if(!isAdmin($iAccountId) && ($aInviteParams['limit'] = $this->_oConfig->getCountPerUser()) <= 0)
  491. return _t('_bx_invites_err_limit_reached');
  492. $sEmails = bx_process_input($oForm->getCleanValue('emails'));
  493. $aEmails = preg_split("/[\s\n,;]+/", $sEmails);
  494. $aInviteParams['email_use'] = (int)$oForm->getCleanValue('email_use');
  495. if(($sA = 'aj_action') && ($$sA = $oForm->getCleanValue($sA)) && ($sP = 'aj_params') && ($$sP = $oForm->getCleanValue($sP)))
  496. $aInviteParams = array_merge($aInviteParams, [
  497. $sA => $$sA,
  498. $sP => $$sP
  499. ]);
  500. $sText = bx_process_pass($oForm->getCleanValue('text'));
  501. $sResult = _t('_bx_invites_err_not_available');
  502. if(($aEmailsSent = $this->invite(BX_INV_TYPE_FROM_MEMBER, $aEmails, $sText, $aInviteParams)) !== false) {
  503. $sEmailsResult = '';
  504. if(count($aEmails) != count($aEmailsSent))
  505. $sEmailsResult = implode('; ', array_diff($aEmails, $aEmailsSent));
  506. $oForm->aInputs['emails']['value'] = $sEmailsResult;
  507. $sResult = _t('_bx_invites_msg_invitation_sent', count($aEmailsSent));
  508. }
  509. return $sResult;
  510. }
  511. public function isAllowedInvite($iProfileId, $bPerform = false)
  512. {
  513. $aCheckResult = checkActionModule($iProfileId, 'invite', $this->getName(), $bPerform);
  514. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  515. }
  516. public function isAllowedRequest($iProfileId, $bPerform = false)
  517. {
  518. $aCheckResult = checkActionModule($iProfileId, 'request', $this->getName(), $bPerform);
  519. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  520. }
  521. public function isAllowedDeleteRequest($iProfileId, $bPerform = false)
  522. {
  523. $aCheckResult = checkActionModule($iProfileId, 'delete request', $this->getName(), $bPerform);
  524. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  525. }
  526. public function isAllowedDeleteInvite($iProfileId, $bPerform = false)
  527. {
  528. $aCheckResult = checkActionModule($iProfileId, 'delete invite', $this->getName(), $bPerform);
  529. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  530. }
  531. public function getProfileId()
  532. {
  533. return isLogged() ? bx_get_logged_profile_id() : 0;
  534. }
  535. public function getProfileObject($iProfileId = 0)
  536. {
  537. $oProfile = BxDolProfile::getInstance($iProfileId);
  538. if (!$oProfile)
  539. $oProfile = BxDolProfileUndefined::getInstance();
  540. return $oProfile;
  541. }
  542. public function getAccountId($iProfileId)
  543. {
  544. $oProfile = $this->getProfileObject($iProfileId);
  545. if($oProfile->id() == 0)
  546. return 0;
  547. return $oProfile->getAccountId();
  548. }
  549. public function onRequest($iRequestId)
  550. {
  551. /**
  552. * @hooks
  553. * @hookdef hook-bx_invites-deleted_answer_notif 'bx_invites', 'request' - hook on create new request on invite
  554. * - $unit_name - equals `bx_invites`
  555. * - $action - equals `request`
  556. * - $object_id - request id
  557. * - $sender_id - not used
  558. * @hook @ref hook-bx_invites-request
  559. */
  560. bx_alert($this->_oConfig->getObject('alert'), 'request', $iRequestId);
  561. $aProfiles = BxDolAclQuery::getInstance()->getProfilesByMembership(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR));
  562. foreach($aProfiles as $aProfile) {
  563. /**
  564. * @hooks
  565. * @hookdef hook-bx_invites-request_notify 'bx_invites', 'request_notify' - hook on notify all admins&moderators about new request
  566. * - $unit_name - equals `bx_invites`
  567. * - $action - equals `request_notify`
  568. * - $object_id - request id
  569. * - $sender_id - not used
  570. * - $extra_params - array of additional params with the following array keys:
  571. * - `parent_author_id` - [int] profile_id of notified profile
  572. * @hook @ref hook-bx_invites-request_notify
  573. */
  574. bx_alert($this->_oConfig->getObject('alert'), 'request_notify', $iRequestId, 0, array('parent_author_id' => $aProfile['id']));
  575. }
  576. //--- Event -> Request for Alerts Engine ---//
  577. }
  578. public function getFormObjectInvite($sDisplay = '')
  579. {
  580. if(empty($sDisplay))
  581. $sDisplay = $this->_oConfig->getObject('form_display_invite_send');
  582. bx_import('FormCheckerHelper', $this->_aModule);
  583. return BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $sDisplay);
  584. }
  585. protected function getBlockManage($sType)
  586. {
  587. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->getObject('grid_' . $sType));
  588. if(!$oGrid)
  589. return '';
  590. if($this->_bIsApi)
  591. return [
  592. bx_api_get_block('grid', $oGrid->getCodeAPI())
  593. ];
  594. $this->_oTemplate->addCss(['main.css']);
  595. $this->_oTemplate->addJs(['jquery.form.min.js', 'main.js']);
  596. $this->_oTemplate->addJsTranslation(['_sys_grid_search']);
  597. return [
  598. 'menu' => $this->_oTemplate->getMenuForManageBlocks($sType),
  599. 'content' => $this->_oTemplate->getJsCode('main', ['grid' => $sType]) . $oGrid->getCode()
  600. ];
  601. }
  602. public function onInvite($iProfileId)
  603. {
  604. $this->isAllowedInvite($iProfileId, true);
  605. /**
  606. * @hooks
  607. * @hookdef hook-bx_invites-invite 'bx_invites', 'invite' - hook on new invite
  608. * - $unit_name - equals `bx_invites`
  609. * - $action - equals `invite`
  610. * - $object_id - not used
  611. * - $sender_id - invited profile_id
  612. * @hook @ref hook-bx_invites-invite
  613. */
  614. bx_alert($this->_oConfig->getObject('alert'), 'invite', 0, $iProfileId);
  615. }
  616. public function getJoinLink($sKey)
  617. {
  618. $sKeyCode = $this->_oConfig->getKeyCode();
  619. $sJoinUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=create-account'));
  620. return bx_append_url_params($sJoinUrl, array($sKeyCode => $sKey));
  621. }
  622. public function getSeenImageUrl($sKey)
  623. {
  624. return BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'SetSeenMark/' . $sKey . "/";
  625. }
  626. protected function _prepareParamsGet()
  627. {
  628. $sAjAction = bx_get('aja');
  629. if(!$sAjAction || !($sAjAction = bx_process_input($sAjAction)) || !in_array($sAjAction, ['redirect', 'invite_to_context']))
  630. return [];
  631. $sAjParams = bx_get('ajp');
  632. if(!$sAjParams)
  633. return [];
  634. switch($sAjAction) {
  635. case 'redirect':
  636. $sAjParams = bx_process_input($sAjParams);
  637. break;
  638. case 'invite_to_context':
  639. $sAjParams = bx_process_input($sAjParams, BX_DATA_INT);
  640. break;
  641. }
  642. return [
  643. 'aj_action' => $sAjAction,
  644. 'aj_params' => $sAjParams
  645. ];
  646. }
  647. }
  648. /** @} */