BxInvTemplate.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. class BxInvTemplate extends BxBaseModGeneralTemplate
  12. {
  13. function __construct(&$oConfig, &$oDb)
  14. {
  15. parent::__construct($oConfig, $oDb);
  16. }
  17. public function getCssJs()
  18. {
  19. $this->addCss(array(
  20. 'main.css',
  21. ));
  22. $this->addJs(array(
  23. 'clipboard.min.js',
  24. 'jquery.form.min.js',
  25. 'jquery.anim.js',
  26. 'main.js'
  27. ));
  28. }
  29. public function getInclude()
  30. {
  31. $this->getCssJs();
  32. return $this->getJsCode('main');
  33. }
  34. public function getBlockRequestText($aRequest)
  35. {
  36. $sText = bx_process_output(nl2br($aRequest['text']), BX_DATA_TEXT_MULTILINE);
  37. if($this->_bIsApi)
  38. return [['text' => $sText]];
  39. return $this->parseHtmlByName('request_text.html', array(
  40. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  41. 'text' => $sText
  42. ));
  43. }
  44. public function getBlockInviteInfo($aRequest)
  45. {
  46. $aVars = array(
  47. 'date_sent_title' => _t('_bx_invites_title_date_invite_sent'),
  48. 'date_seen_title' => _t('_bx_invites_title_date_invite_seen'),
  49. 'bx_repeat:items' => array()
  50. );
  51. foreach ($aRequest as $r) {
  52. $aVars['bx_repeat:items'][] = array (
  53. 'date_sent' => bx_time_js($r['date'], BX_FORMAT_DATE_TIME, true),
  54. 'date_seen' => ($r['date_seen'] != '' ? bx_time_js($r['date_seen'], BX_FORMAT_DATE_TIME, true) : ' - ')
  55. );
  56. }
  57. return $this->parseHtmlByName('invite_info.html', $aVars);
  58. }
  59. public function getBlockInvite($iAccountId, $iProfileId, $aParams = [])
  60. {
  61. $aRequestParams = [];
  62. $sInviteUrl = BxDolPermalinks::getInstance()->permalink($this->_oConfig->CNF['URL_INVITE']);
  63. //-- Process 'redirect to URL'.
  64. if(isset($aParams['redirect']) && $aParams['redirect'] === true) {
  65. list($sPageLink, $aPageParams) = bx_get_base_url_inline();
  66. if(!$this->_bIsApi) {
  67. if(isset($aPageParams['_q']))
  68. unset($aPageParams['_q']);
  69. $sRedirect = bx_append_url_params($sPageLink, $aPageParams);
  70. }
  71. else
  72. $sRedirect = $aPageParams['params'][0];
  73. $aParamsAdd = [
  74. 'aja' => 'redirect',
  75. 'ajp' => $this->_oConfig->urlEncode($sRedirect)
  76. ];
  77. $sInviteUrl = bx_append_url_params($sInviteUrl, $aParamsAdd);
  78. $aRequestParams = array_merge($aRequestParams, $aParamsAdd);
  79. }
  80. $sJsObject = $this->_oConfig->getJsObject('main');
  81. $mInvitesRemain = $this->_oConfig->getCountPerUser();
  82. $aTmplVarsShowByCode = isAdmin($iAccountId) ? ['js_object' => $sJsObject] : [];
  83. //-- Process 'invite to context'.
  84. if(isset($aParams['context']) && ($iContext = (int)$aParams['context']) != 0) {
  85. $sContext = BxDolProfile::getInstance($iContext)->getModule();
  86. if(!$aTmplVarsShowByCode && bx_srv($sContext, 'is_admin', [$iContext, $iProfileId])) {
  87. $mInvitesRemain = true;
  88. $aTmplVarsShowByCode = ['js_object' => $sJsObject];
  89. }
  90. $aParamsAdd = [
  91. 'aja' => 'invite_to_context',
  92. 'ajp' => $iContext
  93. ];
  94. $sInviteUrl = bx_append_url_params($sInviteUrl, $aParamsAdd);
  95. $aRequestParams = array_merge($aRequestParams, $aParamsAdd);
  96. }
  97. if($mInvitesRemain === true)
  98. $mInvitesRemain = _t('_bx_invites_txt_unlimited');
  99. if($this->_bIsApi)
  100. return [bx_api_get_block('invite', ['remain' => $mInvitesRemain, 'request_url' => 'bx_invites/get_link/'])];
  101. $this->getCssJs();
  102. return $this->parseHtmlByName('block_invite.html', [
  103. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  104. 'js_object' => $sJsObject,
  105. 'text' => _t('_bx_invites_txt_invite_block_text', $mInvitesRemain),
  106. 'url' => bx_absolute_url($sInviteUrl),
  107. 'bx_if:show_by_code' => [
  108. 'condition' => !empty($aTmplVarsShowByCode),
  109. 'content' => $aTmplVarsShowByCode
  110. ],
  111. 'js_code' => $this->getJsCode('main', [
  112. 'oRequestParams' => $aRequestParams
  113. ])
  114. ]);
  115. }
  116. public function getBlockAcceptByCode()
  117. {
  118. $CNF = &$this->_oConfig->CNF;
  119. $oModule = $this->getModule();
  120. $sJsObject = $this->_oConfig->getJsObject('main');
  121. $oForm = $oModule->getFormObjectInvite($this->_oConfig->getObject('form_display_invite_accept_by_code'));
  122. $sFormId = $oForm->getId();
  123. $oForm->initChecker();
  124. if(!$oForm->isSubmittedAndValid()) {
  125. $sForm = $oForm->getCode();
  126. if(!$oForm->isSubmitted()) {
  127. $this->getCssJs();
  128. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['request' => ['url' => '/api.php?r=' . $this->_oModule->getName() . '/get_block_accept_by_code', 'immutable' => true]]])] : $this->parseHtmlByName('block_accept_form.html', [
  129. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  130. 'js_object' => $sJsObject,
  131. 'js_code' => $this->getJsCode('main'),
  132. 'form' => $sForm,
  133. 'form_id' => $sFormId,
  134. ]);
  135. }
  136. if(!$oForm->isValid())
  137. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['request' => ['url' => '/api.php?r=' . $this->_oModule->getName() . '/get_block_accept_by_code', 'immutable' => true]]])] : ['form' => $sForm, 'form_id' => $sFormId];
  138. }
  139. $sKey = $oForm->getCleanValue('key');
  140. $aInvite = $this->_oDb->getInvites(['type' => 'by_key', 'key' => $sKey]);
  141. if(empty($aInvite) || !is_array($aInvite))
  142. return ($sMsg = _t('_bx_invites_err_code_not_found')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['msg' => $sMsg];
  143. if(!isLogged())
  144. return ($sJoinLink = $oModule->getJoinLink($sKey)) && $this->_bIsApi ? [bx_api_get_msg('TODO: Need redirect to join.')] : ['redirect' => $sJoinLink];
  145. $iProfileId = bx_get_logged_profile_id();
  146. $sRedirectUrl = '';
  147. if(!empty($aInvite['aj_action']))
  148. switch($aInvite['aj_action']) {
  149. case 'redirect':
  150. $sRedirectUrl = $aInvite['aj_params'];
  151. break;
  152. case 'invite_to_context':
  153. if(($iContextPid = (int)$aInvite['aj_params']) && ($oContext = BxDolProfile::getInstanceMagic($iContextPid)) && $oModule->processInviteToContext($iProfileId, $iContextPid))
  154. $sRedirectUrl = $oContext->getUrl();
  155. break;
  156. }
  157. if(!$sRedirectUrl)
  158. return ($sMsg = _t('_bx_invites_err_code_not_applicable')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : ['msg' => $sMsg];
  159. return $this->_bIsApi ? [bx_api_get_msg('TODO: Need redirect to ' . $sRedirectUrl)] : ['redirect' => $sRedirectUrl];
  160. }
  161. public function getBlockRequest()
  162. {
  163. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($this->_oConfig->CNF['URL_REQUEST']));
  164. $this->getCssJs();
  165. return $this->parseHtmlByName('block_request.html', array(
  166. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  167. 'text' => _t('_bx_invites_txt_request_block_text'),
  168. 'bx_if:show_button_request' => array(
  169. 'condition' => $this->_oConfig->isRequestInvite(),
  170. 'content' => array(
  171. 'url' => $sUrl
  172. )
  173. )
  174. ));
  175. }
  176. /**
  177. * API version of the join form replacement: the message and, when registration is by invitation only and requests are on, the request form.
  178. * @param string $sText the message from BxInvModule::serviceAccountAddFormCheck
  179. */
  180. public function getBlockRequestAPI($sText)
  181. {
  182. $aResult = [bx_api_get_msg($sText)];
  183. if($this->_oConfig->isRegistrationByInvitation() && $this->_oConfig->isRequestInvite())
  184. $aResult = array_merge($aResult, $this->getBlockFormRequest());
  185. return $aResult;
  186. }
  187. public function getBlockFormRequest()
  188. {
  189. if(!$this->_oConfig->isRequestInvite())
  190. return $this->_bIsApi ? [bx_api_get_msg(_t('_bx_invites_err_not_available'))] : MsgBox(_t('_bx_invites_err_not_available'));
  191. $mixedAllowed = $this->getModule()->isAllowedRequest(0);
  192. if($mixedAllowed !== true)
  193. return $this->_bIsApi ? [bx_api_get_msg($mixedAllowed)] : MsgBox($mixedAllowed);
  194. $CNF = &$this->_oConfig->CNF;
  195. $sJsObject = $this->_oConfig->getJsObject('main');
  196. $oPermalink = BxDolPermalinks::getInstance();
  197. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_request'), $this->_oConfig->getObject('form_display_request_send'));
  198. $oForm->aFormAttrs['action'] = bx_absolute_url($oPermalink->permalink($CNF['URL_REQUEST']));
  199. $sFormId = $oForm->getId();
  200. $sEval = $sJsObject . '.onRequestFormSubmit(oData)';
  201. $oForm->initChecker();
  202. if(!$oForm->isSubmittedAndValid()) {
  203. $sForm = $oForm->getCode();
  204. if(!$oForm->isSubmitted()) {
  205. $this->getCssJs();
  206. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['request' => ['url' => '/api.php?r=' . $this->_oModule->getName() . '/get_block_form_request', 'immutable' => true]]])] : $this->parseHtmlByName('block_request_form.html', array(
  207. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  208. 'js_object' => $sJsObject,
  209. 'js_code' => $this->getJsCode('main'),
  210. 'form' => $sForm,
  211. 'form_id' => $sFormId,
  212. ));
  213. }
  214. if(!$oForm->isValid())
  215. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['request' => ['url' => '/api.php?r=' . $this->_oModule->getName() . '/get_block_form_request', 'immutable' => true]]])] : array('content' => $sForm, 'content_id' => $sFormId, 'eval' => $sEval);
  216. }
  217. $sEmail = $oForm->getCleanValue('email');
  218. $oAccountQuery = BxDolAccountQuery::getInstance();
  219. $iAccount = $oAccountQuery->getIdByEmail($sEmail);
  220. if ($iAccount > 0)
  221. return $this->_bIsApi ? [bx_api_get_msg(_t('_bx_invites_err_already_registed', bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password'))))] : array('content' => MsgBox(_t('_bx_invites_err_already_registed', bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password')))), 'content_id' => $sFormId, 'eval' => $sEval);
  222. $iCountByEmail = $this->_oDb->getRequests(array('type' => 'count_by_email', 'value' => $sEmail));
  223. if ($iCountByEmail > 0)
  224. return $this->_bIsApi ? [bx_api_get_msg(_t('_bx_invites_err_already_send'))] : array('content' => MsgBox(_t('_bx_invites_err_already_send')), 'content_id' => $sFormId, 'eval' => $sEval);
  225. $sIp = getVisitorIP();
  226. $iId = (int)$oForm->insert(array('nip' => ip2long($sIp),'date' => time()));
  227. if(!$iId)
  228. return $this->_bIsApi ? [bx_api_get_msg(_t('_bx_invites_err_cannot_perform'))] : array('content' => MsgBox(_t('_bx_invites_err_cannot_perform')), 'content_id' => $sFormId, 'eval' => $sEval);
  229. $this->getModule()->onRequest($iId);
  230. $sRequestsEmail = $this->_oConfig->getRequestsEmail();
  231. if(!empty($sRequestsEmail)) {
  232. $sManageUrl = bx_absolute_url($oPermalink->permalink($CNF['URL_REQUESTS']));
  233. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_invites_request_form_message', array(
  234. 'sender_name' => bx_process_output($oForm->getCleanValue('name')),
  235. 'sender_email' => bx_process_output($oForm->getCleanValue('email')),
  236. 'sender_ip' => $sIp,
  237. 'manage_url' => $sManageUrl
  238. ));
  239. $aRequestsEmails = explode(',', $sRequestsEmail);
  240. foreach ($aRequestsEmails as $s) {
  241. sendMail(trim($s), $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM);
  242. }
  243. }
  244. return $this->_bIsApi ? [bx_api_get_msg(_t('_bx_invites_msg_request_sent'))] : array('content' => MsgBox(_t('_bx_invites_msg_request_sent')), 'content_id' => $sFormId, 'eval' => $sEval);
  245. }
  246. public function getCodePopup($sKey, $sLink)
  247. {
  248. $sId = $this->_oConfig->getHtmlIds('code_popup');
  249. $sTitle = _t('_bx_invites_txt_code_popup_title');
  250. $sContent = $this->parseHtmlByName('popup_code.html', [
  251. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  252. 'html_id_code' => $this->_oConfig->getHtmlIds('code_input'),
  253. 'html_id_code_link' => $this->_oConfig->getHtmlIds('code_link_input'),
  254. 'code' => $sKey,
  255. 'link' => $sLink
  256. ]);
  257. return BxTemplFunctions::getInstance()->popupBox($sId, $sTitle, $sContent, true);
  258. }
  259. public function getLinkPopup($sLink)
  260. {
  261. $sId = $this->_oConfig->getHtmlIds('link_popup');
  262. $sTitle = _t('_bx_invites_txt_link_popup_title');
  263. $sContent = $this->parseHtmlByName('popup_link.html', array(
  264. 'style_prefix' => $this->_oConfig->getPrefix('style'),
  265. 'html_id_link' => $this->_oConfig->getHtmlIds('link_input'),
  266. 'link' => $sLink
  267. ));
  268. return BxTemplFunctions::getInstance()->popupBox($sId, $sTitle, $sContent, true);
  269. }
  270. public function getJsCode($sType, $aParams = array(), $mixedWrap = true)
  271. {
  272. $sGrid = 'requests';
  273. if(!empty($aParams['grid'])) {
  274. $sGrid = $aParams['grid'];
  275. unset($aParams['grid']);
  276. }
  277. $aParams = array_merge(array(
  278. 'aHtmlIds' => $this->_oConfig->getHtmlIds(),
  279. 'sObjNameGrid' => $this->_oConfig->getObject('grid_' . $sGrid),
  280. ), $aParams);
  281. return parent::getJsCode($sType, $aParams);
  282. }
  283. public function getMenuForManageBlocks($sType)
  284. {
  285. $CNF = &$this->_oConfig->CNF;
  286. $sMenu = '';
  287. $oPermalink = BxDolPermalinks::getInstance();
  288. $aMenuItems = array();
  289. $aMenuItems[] = array('id' => 'invites-requests', 'name' => 'invites-requests', 'class' => '', 'link' => $oPermalink->permalink($CNF['URL_REQUESTS']), 'target' => '_self', 'title' => _t('_bx_invites_menu_item_title_manage_requests'), 'active' => 1);
  290. $aMenuItems[] = array('id' => 'invites-invites', 'name' => 'invites-invites', 'class' => '', 'link' => $oPermalink->permalink($CNF['URL_INVITES']), 'target' => '_self', 'title' => _t('_bx_invites_menu_item_title_manage_invites'), 'active' => 1);
  291. if(count($aMenuItems) > 1) {
  292. $oMenu = new BxTemplMenu(array(
  293. 'template' => 'menu_vertical.html',
  294. 'menu_items' => $aMenuItems
  295. ), $this);
  296. $oMenu->setSelected($this->getModule()->_aModule['name'], 'invites-' . $sType);
  297. $sMenu = $oMenu->getCode();
  298. }
  299. return $sMenu;
  300. }
  301. public function getProfilesByAccount($iAccountId, $iMaxVisible = 2)
  302. {
  303. $aProfiles = BxDolAccount::getInstance($iAccountId)->getProfiles();
  304. $iProfiles = count($aProfiles);
  305. $aTmplVars = array (
  306. 'class_cnt' => '',
  307. 'bx_repeat:profiles' => array(),
  308. 'bx_if:profiles_more' => array(
  309. 'condition' => $iProfiles > $iMaxVisible,
  310. 'content' => array(
  311. 'html_id' => $this->_oConfig->getHtmlIds('profile_more_popup') . $iAccountId,
  312. 'more' => _t('_bx_accnt_txt_more', $iProfiles - $iMaxVisible),
  313. 'more_attr' => bx_html_attribute(_t('_bx_accnt_txt_see_more')),
  314. 'popup' => '',
  315. ),
  316. ),
  317. );
  318. $aTmplVarsPopup = array (
  319. 'class_cnt' => ' bx-def-padding',
  320. 'bx_repeat:profiles' => array(),
  321. 'bx_if:profiles_more' => array(
  322. 'condition' => false,
  323. 'content' => array(),
  324. ),
  325. );
  326. $i = 0;
  327. foreach ($aProfiles as $iProfileId => $aProfile) {
  328. $oProfile = BxDolProfile::getInstance($iProfileId);
  329. if(!$oProfile)
  330. continue;
  331. $sName = $oProfile->getDisplayName();
  332. $aTmplVarsProfile = array (
  333. 'html_id' => $this->_oConfig->getHtmlIds('profile') . $aProfile['id'],
  334. 'id' => $oProfile->id(),
  335. 'url' => $oProfile->getUrl(),
  336. 'name' => $sName,
  337. 'name_attr' => bx_html_attribute($sName)
  338. );
  339. if($i < $iMaxVisible)
  340. $aTmplVars['bx_repeat:profiles'][] = $aTmplVarsProfile;
  341. if($i >= $iMaxVisible)
  342. $aTmplVarsPopup['bx_repeat:profiles'][] = $aTmplVarsProfile;
  343. ++$i;
  344. }
  345. if($aTmplVarsPopup['bx_repeat:profiles']) {
  346. $aTmplVars['bx_if:profiles_more']['content']['popup'] = BxTemplFunctions::getInstance()->transBox('', $this->parseHtmlByName('profiles.html', $aTmplVarsPopup));
  347. }
  348. return $this->parseHtmlByName('profiles.html', $aTmplVars);
  349. }
  350. }
  351. /** @} */