1
0

BxBaseModFilesFormsEntryHelper.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 BaseFile Base classes for files modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Entry forms helper functions
  13. */
  14. class BxBaseModFilesFormsEntryHelper extends BxBaseModTextFormsEntryHelper
  15. {
  16. protected $_sDisplayForFormAdd;
  17. protected $_sObjectNameForFormAdd;
  18. public function __construct($oModule)
  19. {
  20. parent::__construct($oModule);
  21. }
  22. public function getObjectFormAdd ($sDisplay = false)
  23. {
  24. if (false === $sDisplay)
  25. $sDisplay = $this->_sDisplayForFormAdd;
  26. $oForm = BxDolForm::getObjectInstance($this->_sObjectNameForFormAdd, $sDisplay, $this->_oModule->_oTemplate);
  27. if($this->_bAjaxMode)
  28. $oForm->setAjaxMode($this->_bAjaxMode);
  29. if($this->_bAbsoluteActionUrl)
  30. $this->_setAbsoluteActionUrl('add', $oForm);
  31. return $oForm;
  32. }
  33. public function addDataForm ($sDisplay = false, $sCheckFunction = false)
  34. {
  35. $mixedContent = $this->addDataFormAction($sDisplay, $sCheckFunction);
  36. if(!is_array($mixedContent) || empty($mixedContent['need_redirect_after_action']))
  37. return $mixedContent;
  38. $sUrl = $this->getRedirectUrlAfterAdd($mixedContent);
  39. if($this->_bIsApi)
  40. return [bx_api_get_block('redirect', ['uri' => BxDolPermalinks::getInstance()->permalink($sUrl), 'timeout' => 1000])];
  41. if($this->_bAjaxMode) {
  42. echoJson($this->prepareResponse($sUrl, $this->_bAjaxMode, 'redirect'));
  43. exit;
  44. }
  45. else
  46. $this->_redirectAndExit($sUrl);
  47. }
  48. protected function addDataFormAction ($sDisplay = false, $sCheckFunction = false)
  49. {
  50. $CNF = &$this->_oModule->_oConfig->CNF;
  51. $bAsJson = false;
  52. // get form object
  53. $oForm = $this->getObjectFormAdd();
  54. if (!$oForm)
  55. return ($sKey = '_sys_txt_error_occured') && $this->_bIsApi ? [bx_api_get_msg($sKey)] : $this->prepareResponse(MsgBox(_t($sKey)), $bAsJson, 'msg');
  56. $bAsJson = $this->_bAjaxMode && $oForm->isSubmitted();
  57. $sModule = $this->_oModule->getName();
  58. // check access
  59. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedAdd()))
  60. return $this->_bIsApi ? [bx_api_get_msg($sMsg)] : $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');
  61. // check and display form
  62. $oForm->initChecker();
  63. if (!$oForm->isSubmittedAndValid())
  64. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['name' => $sModule, 'request' => ['url' => '/api.php?r=' . $sModule . '/entity_create', 'immutable' => true]]])] : $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', [
  65. 'form_id' => $oForm->getId()
  66. ]);
  67. // insert data into database
  68. $aValsToAdd = [];
  69. $aContentIds = $oForm->insert ($aValsToAdd);
  70. if (false === $aContentIds || !is_array($aContentIds)) {
  71. if (!$oForm->isValid() || !is_array($aContentIds))
  72. return $this->_bIsApi ? [bx_api_get_block('form', $oForm->getCodeAPI(), ['ext' => ['name' => $sModule, 'request' => ['url' => '/api.php?r=' . $sModule . '/entity_create', 'immutable' => true]]])] : $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', [
  73. 'form_id' => $oForm->getId()
  74. ]);
  75. else
  76. return ($sKey = '_sys_txt_error_entry_creation') && $this->_bIsApi ? [bx_api_get_msg($sKey)] : $this->prepareResponse(MsgBox(_t($sKey)), $bAsJson, 'msg');
  77. }
  78. foreach ($aContentIds as $iContentId) {
  79. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  80. if ($sResult)
  81. return $this->_bIsApi ? [bx_api_get_msg($sResult)] : $this->prepareResponse($sResult, $bAsJson, 'msg');
  82. if (!($aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId)))
  83. return ($sKey = '_sys_txt_error_occured') && $this->_bIsApi ? [bx_api_get_msg($sKey)] : $this->prepareResponse(MsgBox(_t($sKey)), $bAsJson, 'msg');
  84. // Create alert about the completed action.
  85. $this->_oModule->alertAfterAdd($aContentInfo);
  86. }
  87. return ['need_redirect_after_action' => true, 'content_ids_array' => $aContentIds];
  88. }
  89. protected function getRedirectUrlAfterAdd($mixedContent)
  90. {
  91. $CNF = &$this->_oModule->_oConfig->CNF;
  92. $iProfileId = false;
  93. $sUri = $CNF['URI_AUTHOR_ENTRIES'];
  94. $sKeyIds = 'content_ids_array';
  95. if(!empty($mixedContent[$sKeyIds]) && is_array($mixedContent[$sKeyIds])) {
  96. $aContentInfo = $this->_oModule->_oDb->getContentInfoById(array_pop($mixedContent[$sKeyIds]));
  97. if(($sKey = 'FIELD_AUTHOR') && isset($CNF[$sKey]) && ($iAuthor = (int)$aContentInfo[$CNF[$sKey]])) {
  98. $iProfileId = $iAuthor;
  99. }
  100. if(($sKey = 'FIELD_ALLOW_VIEW_TO') && isset($CNF[$sKey]) && ($iVisibility = (int)$aContentInfo[$CNF[$sKey]]) < 0) {
  101. $iProfileId = abs($iVisibility);
  102. $sUri = $CNF['URI_ENTRIES_BY_CONTEXT'];
  103. }
  104. }
  105. $sUrl = 'page.php?i=' . $sUri . '&profile_id=' . (($oProfile = BxDolProfile::getInstance($iProfileId)) !== false ? $oProfile->id() : bx_get_logged_profile_id());
  106. if(($mixedUrl = $this->_getRedirectFromContext('add', $aContentInfo)) !== false)
  107. $sUrl = $mixedUrl;
  108. return $sUrl;
  109. }
  110. }
  111. /** @} */