1
0

BxInvModule.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. /**
  17. * Constructor
  18. */
  19. function __construct($aModule)
  20. {
  21. parent::__construct($aModule);
  22. $this->_oConfig->init($this->_oDb);
  23. }
  24. /**
  25. * ACTION METHODS
  26. */
  27. function actionGetLink()
  28. {
  29. $iProfileId = $this->getProfileId();
  30. $iAccountId = $this->getAccountId($iProfileId);
  31. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  32. if($mixedAllowed !== true)
  33. return echoJson(array('message' => $mixedAllowed));
  34. if(!isAdmin($iAccountId)) {
  35. if($this->_oConfig->getCountPerUser() <= 0)
  36. return echoJson(array('message' => _t('_bx_invites_err_limit_reached')));
  37. }
  38. $oKeys = BxDolKey::getInstance();
  39. if(!$oKeys)
  40. return echoJson(array('message' => _t('_bx_invites_err_not_available')));
  41. $sKey = $oKeys->getNewKey(false, $this->_oConfig->getKeyLifetime());
  42. $oForm = $this->getFormObjectInvite();
  43. $oForm->insert(array(
  44. 'account_id' => $iAccountId,
  45. 'profile_id' => $iProfileId,
  46. 'key' => $sKey,
  47. 'email' => '',
  48. 'date' => time()
  49. ));
  50. $this->onInvite($iProfileId);
  51. echoJson(array('popup' => $this->_oTemplate->getLinkPopup(
  52. $this->getJoinLink($sKey)
  53. )));
  54. }
  55. public function actionSetSeenMark($Code)
  56. {
  57. header('Content-Type: image/png');
  58. if (isset($Code) && trim($Code) != "")
  59. $this->_oDb->updateDateSeenForInvite($Code);
  60. }
  61. /**
  62. * SERVICE METHODS
  63. */
  64. public function serviceGetSafeServices()
  65. {
  66. return array (
  67. 'GetBlockInvite' => '',
  68. 'GetBlockFormInvite' => '',
  69. 'GetBlockFormRequest' => '',
  70. );
  71. }
  72. /**
  73. * @page service Service Calls
  74. * @section bx_invites Invitations
  75. * @subsection bx_invites-other Other
  76. * @subsubsection bx_invites-get_include get_include
  77. *
  78. * @code bx_srv('bx_invites', 'get_include', [...]); @endcode
  79. *
  80. * Get all necessary CSS and JS files to include in a page.
  81. *
  82. * @return string with all necessary CSS and JS files.
  83. *
  84. * @see BxInvModule::serviceGetInclude
  85. */
  86. /**
  87. * @ref bx_invites-get_include "get_include"
  88. */
  89. public function serviceGetInclude()
  90. {
  91. return $this->_oTemplate->getInclude();
  92. }
  93. /**
  94. * @page service Service Calls
  95. * @section bx_invites Invitations
  96. * @subsection bx_invites-page_blocks Page Blocks
  97. * @subsubsection bx_invites-get_block_invite get_block_invite
  98. *
  99. * @code bx_srv('bx_invites', 'get_block_invite', [...]); @endcode
  100. *
  101. * Get page block for member's Dashboard which displays invitations related info and action(s).
  102. *
  103. * @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.
  104. *
  105. * @see BxInvModule::serviceGetBlockInvite
  106. */
  107. /**
  108. * @ref bx_invites-get_block_invite "get_block_invite"
  109. */
  110. public function serviceGetBlockInvite()
  111. {
  112. $iProfileId = $this->getProfileId();
  113. $iAccountId = $this->getAccountId($iProfileId);
  114. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  115. if($mixedAllowed !== true)
  116. return '';
  117. if(!isAdmin($iAccountId) && $this->_oConfig->getCountPerUser() <= 0)
  118. return '';
  119. return array(
  120. 'content' => $this->_oTemplate->getBlockInvite($iAccountId, $iProfileId)
  121. );
  122. }
  123. /**
  124. * @page service Service Calls
  125. * @section bx_invites Invitations
  126. * @subsection bx_invites-page_blocks Page Blocks
  127. * @subsubsection bx_invites-get_block_form_invite get_block_form_invite
  128. *
  129. * @code bx_srv('bx_invites', 'get_block_form_invite', [...]); @endcode
  130. *
  131. * Get page block with invite form.
  132. *
  133. * @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.
  134. *
  135. * @see BxInvModule::serviceGetBlockFormInvite
  136. */
  137. /**
  138. * @ref bx_invites-get_block_form_invite "get_block_form_invite"
  139. */
  140. public function serviceGetBlockFormInvite()
  141. {
  142. $oForm = $this->getFormObjectInvite();
  143. $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
  144. $sResult = '';
  145. $oForm->initChecker();
  146. if($oForm->isSubmittedAndValid()) {
  147. $sResult = $this->processFormObjectInvite($oForm);
  148. $sResult = MsgBox($sResult);
  149. }
  150. return array(
  151. 'content' => $sResult . $oForm->getCode()
  152. );
  153. }
  154. /**
  155. * @page service Service Calls
  156. * @section bx_invites Invitations
  157. * @subsection bx_invites-page_blocks Page Blocks
  158. * @subsubsection bx_invites-get_block_form_request get_block_form_request
  159. *
  160. * @code bx_srv('bx_invites', 'get_block_form_request', [...]); @endcode
  161. *
  162. * Get page block with request invitation form.
  163. *
  164. * @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.
  165. *
  166. * @see BxInvModule::serviceGetBlockFormRequest
  167. */
  168. /**
  169. * @ref bx_invites-get_block_form_request "get_block_form_request"
  170. */
  171. public function serviceGetBlockFormRequest()
  172. {
  173. $mixedResult = $this->_oTemplate->getBlockFormRequest();
  174. if(is_array($mixedResult)) {
  175. echoJson($mixedResult);
  176. exit;
  177. }
  178. return array(
  179. 'content' => $mixedResult
  180. );
  181. }
  182. /**
  183. * @page service Service Calls
  184. * @section bx_invites Invitations
  185. * @subsection bx_invites-page_blocks Page Blocks
  186. * @subsubsection bx_invites-get_block_manage_requests get_block_manage_requests
  187. *
  188. * @code bx_srv('bx_invites', 'get_block_manage_requests', [...]); @endcode
  189. *
  190. * Get page block with manage invitation requests table.
  191. *
  192. * @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.
  193. *
  194. * @see BxInvModule::serviceGetBlockManageRequests
  195. */
  196. /**
  197. * @ref bx_invites-get_block_manage_requests "get_block_manage_requests"
  198. */
  199. public function serviceGetBlockManageRequests()
  200. {
  201. return $this->getBlockManage('requests');
  202. }
  203. /**
  204. * @page service Service Calls
  205. * @section bx_invites Invitations
  206. * @subsection bx_invites-page_blocks Page Blocks
  207. * @subsubsection bx_invites-get_block_manage_invites get_block_manage_requests
  208. *
  209. * @code bx_srv('bx_invites', 'get_block_manage_invites', [...]); @endcode
  210. *
  211. * Get page block with manage invitations table.
  212. *
  213. * @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.
  214. *
  215. * @see BxInvModule::serviceGetBlockManageInvites
  216. */
  217. /**
  218. * @ref bx_invites-get_block_manage_invites "get_block_manage_invites"
  219. */
  220. public function serviceGetBlockManageInvites()
  221. {
  222. return $this->getBlockManage('invites');
  223. }
  224. /**
  225. * @page service Service Calls
  226. * @section bx_invites Invitations
  227. * @subsection bx_invites-other Other
  228. * @subsubsection bx_invites-get_menu_addon_requests get_menu_addon_requests
  229. *
  230. * @code bx_srv('bx_invites', 'get_menu_addon_requests', [...]); @endcode
  231. *
  232. * Get number or invitation requests.
  233. *
  234. * @return integer value with number of invitation requests.
  235. *
  236. * @see BxInvModule::serviceGetMenuAddonRequests
  237. */
  238. /**
  239. * @ref bx_invites-get_menu_addon_requests "get_menu_addon_requests"
  240. */
  241. public function serviceGetMenuAddonRequests()
  242. {
  243. return array('counter3_value' => $this->_oDb->getRequests(array('type' => 'count_all')));
  244. }
  245. /**
  246. * @page service Service Calls
  247. * @section bx_invites Invitations
  248. * @subsection bx_invites-other Other
  249. * @subsubsection bx_invites-account_add_form_check account_add_form_check
  250. *
  251. * @code bx_srv('bx_invites', 'account_add_form_check', [...]); @endcode
  252. *
  253. * Perform neccessary checking on join form.
  254. *
  255. * @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.
  256. *
  257. * @see BxInvModule::serviceAccountAddFormCheck
  258. */
  259. /**
  260. * @ref bx_invites-account_add_form_check "account_add_form_check"
  261. */
  262. public function serviceAccountAddFormCheck()
  263. {
  264. $sReturn = '';
  265. $oSession = BxDolSession::getInstance();
  266. $sKeyCode = $this->_oConfig->getKeyCode();
  267. $bKey = bx_get($sKeyCode) !== false;
  268. if($bKey) {
  269. $sKey = bx_process_input(bx_get($sKeyCode));
  270. $oKeys = BxDolKey::getInstance();
  271. if($oKeys && $oKeys->isKeyExists($sKey))
  272. $oSession->setValue($sKeyCode, $sKey);
  273. }
  274. $sKey = $oSession->getValue($sKeyCode);
  275. if($sKey !== false)
  276. return $sReturn;
  277. if($bKey)
  278. $sReturn .= MsgBox(_t('_bx_invites_err_used'));
  279. if ($this->_oConfig->isRegistrationByInvitation())
  280. $sReturn .= $this->_oTemplate->getBlockRequest();
  281. return $sReturn;
  282. }
  283. /**
  284. * Data for Notifications module
  285. */
  286. public function serviceGetNotificationsData()
  287. {
  288. $sModule = $this->_aModule['name'];
  289. $sEventPrivacy = $sModule . '_allow_view_event_to';
  290. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  291. $sEventPrivacy = '';
  292. return array(
  293. 'handlers' => array(
  294. 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),
  295. ),
  296. 'settings' => array(
  297. array('group' => 'request', 'unit' => $sModule, 'action' => 'request_notify', 'types' => array('personal')),
  298. ),
  299. 'alerts' => array(
  300. array('unit' => $sModule, 'action' => 'request_notify'),
  301. )
  302. );
  303. }
  304. /**
  305. * Entry post for Notifications module
  306. */
  307. public function serviceGetNotificationsRequest($aEvent)
  308. {
  309. if (getParam('bx_invites_requests_notifications') != 'on')
  310. return array();
  311. $aRequest = $this->_oDb->getRequests(array('type' => 'by_id', 'value' => $aEvent['object_id']));
  312. if (!$aRequest)
  313. return array();
  314. $CNF = &$this->_oConfig->CNF;
  315. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_REQUESTS']);
  316. return array(
  317. 'entry_sample' => $aRequest['name'] . '(' . $aRequest['email'] . ')',
  318. 'entry_url' => $sEntryUrl,
  319. 'entry_caption' => $aRequest['text'],
  320. 'entry_author' => '',
  321. 'entry_privacy' => '', //may be empty or not specified. In this case Public privacy will be used.
  322. '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.
  323. );
  324. }
  325. public function attachAccountIdToInvite($iAccountId, $sKey)
  326. {
  327. $this->_oDb->attachAccountIdToInvite($iAccountId, $sKey);
  328. }
  329. public function invite($sType, $sEmails, $sText, $mixedLimit = false, $oForm = null)
  330. {
  331. $iProfileId = $this->getProfileId();
  332. $iAccountId = $this->getAccountId($iProfileId);
  333. $oKeys = BxDolKey::getInstance();
  334. if(!$oKeys || !in_array($sType, array(BX_INV_TYPE_FROM_MEMBER, BX_INV_TYPE_FROM_SYSTEM)))
  335. return false;
  336. $iKeyLifetime = $this->_oConfig->getKeyLifetime();
  337. $sEmailTemplate = '';
  338. switch($sType) {
  339. case BX_INV_TYPE_FROM_MEMBER:
  340. $sEmailTemplate = 'bx_invites_invite_form_message';
  341. break;
  342. case BX_INV_TYPE_FROM_SYSTEM:
  343. $sEmailTemplate = 'bx_invites_invite_by_request_message';
  344. break;
  345. }
  346. if(empty($oForm))
  347. $oForm = $this->getFormObjectInvite();
  348. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate($sEmailTemplate, array(
  349. 'text' => $sText
  350. ), $iAccountId, $iProfileId);
  351. $aAccountIds = array();
  352. $iDate = time();
  353. $aEmails = preg_split("/[\s\n,;]+/", $sEmails);
  354. if(is_array($aEmails) && !empty($aEmails)){
  355. foreach($aEmails as $sEmail) {
  356. if($mixedLimit !== false && (int)$mixedLimit <= 0)
  357. break;
  358. $sEmail = trim($sEmail);
  359. if(empty($sEmail))
  360. continue;
  361. $sKey = $oKeys->getNewKey(false, $iKeyLifetime);
  362. if(sendMail($sEmail, $aMessage['Subject'], $aMessage['Body'], 0, array('join_url' => $this->getJoinLink($sKey), 'seen_image_url' => $this->getSeenImageUrl($sKey)), BX_EMAIL_SYSTEM)) {
  363. $iInviteId = (int)$this->_oDb->insertInvite($iAccountId, $iProfileId, $sKey, $sEmail, $iDate);
  364. array_push($aAccountIds, $iInviteId);
  365. $this->onInvite($iProfileId);
  366. if($mixedLimit !== false)
  367. $mixedLimit -= 1;
  368. }
  369. }
  370. }
  371. return $aAccountIds;
  372. }
  373. public function processFormObjectInvite($oForm)
  374. {
  375. $iProfileId = $this->getProfileId();
  376. $iAccountId = $this->getAccountId($iProfileId);
  377. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  378. if($mixedAllowed !== true)
  379. return $mixedAllowed;
  380. $mixedInvites = false;
  381. if(!isAdmin($iAccountId)) {
  382. if( $this->_oConfig->getCountPerUser() <= 0)
  383. return _t('_bx_invites_err_limit_reached');
  384. }
  385. $sEmails = bx_process_input($oForm->getCleanValue('emails'));
  386. $sText = bx_process_pass($oForm->getCleanValue('text'));
  387. $mixedResult = $this->invite(BX_INV_TYPE_FROM_MEMBER, $sEmails, $sText, $mixedInvites, $oForm);
  388. if($mixedResult !== false)
  389. $sResult = _t('_bx_invites_msg_invitation_sent', count($mixedResult));
  390. else
  391. $sResult = _t('_bx_invites_err_not_available');
  392. return $sResult;
  393. }
  394. public function isAllowedInvite($iProfileId, $bPerform = false)
  395. {
  396. $aCheckResult = checkActionModule($iProfileId, 'invite', $this->getName(), $bPerform);
  397. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  398. }
  399. public function isAllowedRequest($iProfileId, $bPerform = false)
  400. {
  401. $aCheckResult = checkActionModule($iProfileId, 'request', $this->getName(), $bPerform);
  402. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  403. }
  404. public function isAllowedDeleteRequest($iProfileId, $bPerform = false)
  405. {
  406. $aCheckResult = checkActionModule($iProfileId, 'delete request', $this->getName(), $bPerform);
  407. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  408. }
  409. public function isAllowedDeleteInvite($iProfileId, $bPerform = false)
  410. {
  411. $aCheckResult = checkActionModule($iProfileId, 'delete invite', $this->getName(), $bPerform);
  412. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  413. }
  414. public function getProfileId()
  415. {
  416. return isLogged() ? bx_get_logged_profile_id() : 0;
  417. }
  418. public function getProfileObject($iProfileId = 0)
  419. {
  420. $oProfile = BxDolProfile::getInstance($iProfileId);
  421. if (!$oProfile)
  422. $oProfile = BxDolProfileUndefined::getInstance();
  423. return $oProfile;
  424. }
  425. public function getAccountId($iProfileId)
  426. {
  427. $oProfile = $this->getProfileObject($iProfileId);
  428. if($oProfile->id() == 0)
  429. return 0;
  430. return $oProfile->getAccountId();
  431. }
  432. public function onRequest($iRequestId)
  433. {
  434. //--- Event -> Request for Alerts Engine ---//
  435. bx_alert($this->_oConfig->getObject('alert'), 'request', $iRequestId);
  436. $aProfiles = BxDolAclQuery::getInstance()->getProfilesByMembership(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR));
  437. foreach($aProfiles as $aProfile) {
  438. bx_alert($this->_oConfig->getObject('alert'), 'request_notify', $iRequestId, 0, array('parent_author_id' => $aProfile['id']));
  439. }
  440. //--- Event -> Request for Alerts Engine ---//
  441. }
  442. public function getFormObjectInvite($sDisplay = '')
  443. {
  444. if(empty($sDisplay))
  445. $sDisplay = $this->_oConfig->getObject('form_display_invite_send');
  446. bx_import('FormCheckerHelper', $this->_aModule);
  447. return BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $sDisplay);
  448. }
  449. protected function getBlockManage($sType)
  450. {
  451. $this->_oTemplate->addJs('jquery.form.min.js');
  452. $this->_oTemplate->addJs('main.js');
  453. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->getObject('grid_' . $sType));
  454. if(!$oGrid)
  455. return '';
  456. $this->_oTemplate->addCss(array('main.css'));
  457. $this->_oTemplate->addJsTranslation(array('_sys_grid_search'));
  458. return array(
  459. 'menu' => $this->_oTemplate->getMenuForManageBlocks($sType),
  460. 'content' => $this->_oTemplate->getJsCode('main', array('grid' => $sType)) . $oGrid->getCode()
  461. );
  462. }
  463. public function onInvite($iProfileId)
  464. {
  465. $this->isAllowedInvite($iProfileId, true);
  466. //--- Event -> Invite for Alerts Engine ---//
  467. bx_alert($this->_oConfig->getObject('alert'), 'invite', 0, $iProfileId);
  468. //--- Event -> Invite for Alerts Engine ---//
  469. }
  470. public function getJoinLink($sKey)
  471. {
  472. $sKeyCode = $this->_oConfig->getKeyCode();
  473. $sJoinUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account');
  474. return bx_append_url_params($sJoinUrl, array($sKeyCode => $sKey));
  475. }
  476. public function getSeenImageUrl($sKey)
  477. {
  478. return BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'SetSeenMark/' . $sKey . "/";
  479. }
  480. }
  481. /** @} */