BxBaseModFilesFormsEntryHelper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. protected function addDataFormAction ($sDisplay = false, $sCheckFunction = false)
  34. {
  35. $CNF = &$this->_oModule->_oConfig->CNF;
  36. $bAsJson = false;
  37. // get form object
  38. $oForm = $this->getObjectFormAdd();
  39. if (!$oForm)
  40. return $this->prepareResponse(MsgBox(_t('_sys_txt_error_occured')), $bAsJson, 'msg');
  41. $bAsJson = $this->_bAjaxMode && $oForm->isSubmitted();
  42. // check access
  43. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedAdd()))
  44. return $this->prepareResponse(MsgBox($sMsg), $bAsJson, 'msg');
  45. // check and display form
  46. $oForm->initChecker();
  47. if (!$oForm->isSubmittedAndValid())
  48. return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
  49. 'form_id' => $oForm->getId()
  50. ));
  51. // insert data into database
  52. $aValsToAdd = array ();
  53. $aContentIds = $oForm->insert ($aValsToAdd);
  54. if (false === $aContentIds || !is_array($aContentIds)) {
  55. if (!$oForm->isValid() || !is_array($aContentIds))
  56. return $this->prepareResponse($oForm->getCode($this->_bDynamicMode), $bAsJson, 'form', array(
  57. 'form_id' => $oForm->getId()
  58. ));
  59. else
  60. return $this->prepareResponse(MsgBox(_t('_sys_txt_error_entry_creation')), $bAsJson, 'msg');
  61. }
  62. foreach ($aContentIds as $iContentId) {
  63. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  64. if ($sResult)
  65. return $sResult;
  66. if (!($aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId)))
  67. return MsgBox(_t('_sys_txt_error_occured'));
  68. // Create alert about the completed action.
  69. $this->_oModule->alertAfterAdd($aContentInfo);
  70. }
  71. return array('need_redirect_after_action' => true, 'content_ids_array' => $aContentIds);
  72. }
  73. protected function getRedirectUrlAfterAdd($mixedContent)
  74. {
  75. $CNF = &$this->_oModule->_oConfig->CNF;
  76. $iProfileId = false;
  77. $sUri = $CNF['URI_AUTHOR_ENTRIES'];
  78. $sKeyIds = 'content_ids_array';
  79. if(!empty($mixedContent[$sKeyIds]) && is_array($mixedContent[$sKeyIds])) {
  80. $aContentInfo = $this->_oModule->_oDb->getContentInfoById(array_pop($mixedContent[$sKeyIds]));
  81. if(($sKey = 'FIELD_AUTHOR') && isset($CNF[$sKey]) && ($iAuthor = (int)$aContentInfo[$CNF[$sKey]])) {
  82. $iProfileId = $iAuthor;
  83. }
  84. if(($sKey = 'FIELD_ALLOW_VIEW_TO') && isset($CNF[$sKey]) && ($iVisibility = (int)$aContentInfo[$CNF[$sKey]]) < 0) {
  85. $iProfileId = abs($iVisibility);
  86. $sUri = $CNF['URI_ENTRIES_BY_CONTEXT'];
  87. }
  88. }
  89. return 'page.php?i=' . $sUri . '&profile_id=' . (($oProfile = BxDolProfile::getInstance($iProfileId)) !== false ? $oProfile->id() : bx_get_logged_profile_id());
  90. }
  91. }
  92. /** @} */