BxNtfsGridEtemplates.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Notifications Notifications
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxNtfsGridEtemplates extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. public function __construct ($aOptions, $oTemplate = false)
  16. {
  17. parent::__construct ($aOptions, $oTemplate);
  18. $this->_sModule = 'bx_notifications';
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. }
  21. public function getCode($isDisplayHeader = true)
  22. {
  23. $sResult = parent::getCode($isDisplayHeader);
  24. if(!$sResult)
  25. return $sResult;
  26. return $sResult . $this->_oModule->_oTemplate->getJsCode('main');
  27. }
  28. public function performActionAdd()
  29. {
  30. $sAction = 'add';
  31. $oForm = $this->_getFormObject($sAction);
  32. $oForm->initChecker();
  33. if($oForm->isSubmittedAndValid()) {
  34. if(($iId = $oForm->insert()) !== false)
  35. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  36. else
  37. $aRes = ['msg' => _t('_bx_ntfs_txt_err_cannot_perform')];
  38. echoJson($aRes);
  39. }
  40. else {
  41. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('view', 'add_etemplate_popup'), _t('_bx_ntfs_grid_popup_title_ett_add'), $this->_oModule->_oTemplate->parseHtmlByName('etemplate_form.html', [
  42. 'style_prefix' => $this->_oModule->_oConfig->getPrefix('style'),
  43. 'form_id' => $oForm->aFormAttrs['id'],
  44. 'form' => $oForm->getCode(true),
  45. 'object' => $this->_sObject,
  46. 'action' => $sAction
  47. ]));
  48. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false, 'removeOnClose' => true]]]);
  49. }
  50. }
  51. public function performActionEdit()
  52. {
  53. $sAction = 'edit';
  54. $iId = $this->_getId();
  55. $aEtemplate = $this->_oModule->_oDb->getEtemplates(['sample' => 'id', 'id' => $iId]);
  56. if(empty($aEtemplate) || !is_array($aEtemplate))
  57. return echoJson([]);
  58. $oForm = $this->_getFormObject($sAction, $aEtemplate);
  59. $oForm->initChecker($aEtemplate);
  60. if($oForm->isSubmittedAndValid()) {
  61. if($oForm->update($iId) !== false)
  62. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  63. else
  64. $aRes = ['msg' => _t('_bx_ntfs_txt_err_cannot_perform')];
  65. echoJson($aRes);
  66. }
  67. else {
  68. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('view', 'edit_etemplate_popup'), _t('_bx_ntfs_grid_popup_title_ett_edit'), $this->_oModule->_oTemplate->parseHtmlByName('etemplate_form.html', [
  69. 'style_prefix' => $this->_oModule->_oConfig->getPrefix('style'),
  70. 'form_id' => $oForm->aFormAttrs['id'],
  71. 'form' => $oForm->getCode(true),
  72. 'object' => $this->_sObject,
  73. 'action' => $sAction
  74. ]));
  75. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false, 'removeOnClose' => true]]]);
  76. }
  77. }
  78. public function performActionPreview()
  79. {
  80. $iId = $this->_getId();
  81. $aEtemplate = $this->_oModule->_oDb->getEtemplates(['sample' => 'id', 'id' => $iId]);
  82. if(empty($aEtemplate) || !is_array($aEtemplate))
  83. return echoJson([]);
  84. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('bx_notifications_new_event', [
  85. 'subject' => !empty($aEtemplate['subject']) ? $aEtemplate['subject'] : _t('_bx_ntfs_txt_new_event_subject', getParam('site_title')),
  86. 'content' => $aEtemplate['body']
  87. ]);
  88. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('view', 'preview_etemplate_popup'), _t('_bx_ntfs_grid_popup_title_ett_preview'), $this->_oModule->_oTemplate->parseHtmlByName('etemplate_preview.html', [
  89. 'style_prefix' => $this->_oModule->_oConfig->getPrefix('style'),
  90. 'subject' => $aTemplate['Subject'],
  91. 'body' => $aTemplate['Body']
  92. ]));
  93. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => true, 'removeOnClose' => true]]]);
  94. }
  95. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  96. {
  97. return parent::_getCellDefault($this->_oModule->_oTemplate->getTypeTitle($mixedValue), $sKey, $aField, $aRow);
  98. }
  99. protected function _getCellAction($mixedValue, $sKey, $aField, $aRow)
  100. {
  101. return parent::_getCellDefault($this->_oModule->_oTemplate->getActionTitle($mixedValue), $sKey, $aField, $aRow);
  102. }
  103. protected function _getActionsDisabledBehavior($aRow)
  104. {
  105. return false;
  106. }
  107. protected function _getFormObject($sAction, $aItem = [])
  108. {
  109. $CNF = &$this->_oModule->_oConfig->CNF;
  110. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ETEMPLATE'], $CNF['OBJECT_FORM_ETEMPLATE_DISPLAY_' . strtoupper($sAction)]);
  111. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction;
  112. if(!empty($aItem['id']))
  113. $oForm->aFormAttrs['action'] .= '&id=' . $aItem['id'];
  114. if(($sK = 'type') && isset($oForm->aInputs[$sK])) {
  115. $oForm->aInputs[$sK] = array_merge($oForm->aInputs[$sK], [
  116. 'attrs' => [
  117. 'onchange' => $this->_oModule->_oConfig->getJsObject('main') . '.onChangeType(this)',
  118. ],
  119. 'values' => [
  120. ['key' => '', 'value' => _t('_sys_please_select')]
  121. ]
  122. ]);
  123. $aTypes = $this->_oModule->_oDb->getHandlerTypes();
  124. foreach($aTypes as $sType)
  125. $oForm->aInputs[$sK]['values'][] = ['key' => $sType, 'value' => $this->_oModule->_oTemplate->getTypeTitle($sType)];
  126. }
  127. if(($sK = 'action') && isset($oForm->aInputs[$sK])) {
  128. $oForm->aInputs[$sK] = array_merge($oForm->aInputs[$sK], [
  129. 'attrs' => [
  130. 'id' => $this->_oModule->_oConfig->getHtmlIds('main', 'field_action'),
  131. 'disabled' => 'disabled'
  132. ],
  133. 'values' => [
  134. ['key' => '', 'value' => _t('_sys_please_select')]
  135. ]
  136. ]);
  137. }
  138. if(($sK = 'body') && isset($oForm->aInputs[$sK]))
  139. $oForm->aInputs[$sK]['code'] = 1;
  140. return $oForm;
  141. }
  142. protected function _getId()
  143. {
  144. if(($aIds = bx_get('ids')) && is_array($aIds))
  145. return array_shift($aIds);
  146. return ($iId = bx_get('id')) !== false ? (int)$iId : false;
  147. }
  148. }
  149. /** @} */