BxAntispamGridIpTable.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Antispam Antispam
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  12. class BxAntispamGridIpTable extends BxTemplGrid
  13. {
  14. public function __construct ($aOptions, $oTemplate = false)
  15. {
  16. parent::__construct ($aOptions, $oTemplate);
  17. }
  18. /**
  19. * add js file for AJAX form submission
  20. */
  21. protected function _addJsCss()
  22. {
  23. parent::_addJsCss();
  24. $this->_oTemplate->addJs('jquery.form.min.js');
  25. $oForm = new BxTemplFormView(array());
  26. $oForm->addCssJs();
  27. }
  28. /**
  29. * 'add' action handler
  30. */
  31. public function performActionAdd()
  32. {
  33. $this->_performAction('add', 'bx_antispam_ip_table_form_add');
  34. }
  35. /**
  36. * 'edit' action handler
  37. */
  38. public function performActionEdit()
  39. {
  40. $iId = 0;
  41. $aIds = bx_get('ids');
  42. if ($aIds && is_array($aIds))
  43. $iId = (int)array_pop($aIds);
  44. if (!$iId)
  45. $iId = (int)bx_get('ID');
  46. if (!$iId) {
  47. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  48. exit;
  49. }
  50. $this->_performAction('edit', 'bx_antispam_ip_table_form_edit', $iId);
  51. }
  52. protected function _performAction($sAction, $sDisplay, $iId = 0)
  53. {
  54. $oForm = BxDolForm::getObjectInstance('bx_antispam_ip_table_form', $sDisplay); // get form instance for specified form object and display
  55. if (!$oForm) {
  56. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  57. exit;
  58. }
  59. $oForm->addMarkers(array(
  60. 'grid_object' => $this->_sObject,
  61. 'grid_action' => $sAction,
  62. ));
  63. $aIpTableDirective = array();
  64. if ($iId) {
  65. $oModule = BxDolModule::getInstance('bx_antispam');
  66. $oAntispamIp = bx_instance('BxAntispamIP', array(), $oModule->_aModule);
  67. $aIpTableDirective = $oAntispamIp->getIpTableDirective($iId);
  68. $aIpTableDirective['From'] = long2ip($aIpTableDirective['From']);
  69. $aIpTableDirective['To'] = long2ip($aIpTableDirective['To']);
  70. }
  71. $oForm->initChecker($aIpTableDirective);
  72. if ($oForm->isSubmittedAndValid()) { // if form is submitted and all fields are valid
  73. $aCustomValues = array(
  74. 'From' => sprintf("%u", ip2long($oForm->getCleanValue('From'))),
  75. 'To' => sprintf("%u", ip2long($oForm->getCleanValue('To'))),
  76. );
  77. if ($iId) {
  78. if ($oForm->update ($iId, $aCustomValues)) // update record
  79. $iRecentId = $iId;
  80. } else {
  81. $iRecentId = $oForm->insert ($aCustomValues, true); // insert new record
  82. }
  83. if ($iRecentId)
  84. $aRes = array('grid' => $this->getCode(false), 'blink' => $iRecentId); // if record is successfully added, reload grid and highlight added row
  85. else
  86. $aRes = array('msg' => _t('_sys_txt_error_occured')); // if record adding failed, display error message
  87. echoJson($aRes);
  88. } else { // if form is not submitted or some fields are invalid, display popup with form
  89. $s = BxTemplFunctions::getInstance()->popupBox($oForm->getId() . '_form', _t('_bx_antispam_form_ip_table_add'), $oForm->getCode() . '
  90. <script>
  91. $(document).ready(function () {
  92. $("#' . $oForm->getId() . '").ajaxForm({
  93. dataType: "json",
  94. beforeSubmit: function (formData, jqForm, options) {
  95. bx_loading($("#' . $oForm->getId() . '"), true);
  96. },
  97. success: function (data) {
  98. $(".bx-popup-applied:visible").dolPopupHide();
  99. glGrids.' . $this->_sObject . '.processJson(data, "' . $sAction . '");
  100. }
  101. });
  102. });
  103. </script>');
  104. echoJson(array('popup' => array('html' => $s, 'options' => array('closeOnOuterClick' => false))));
  105. }
  106. }
  107. protected function _getCellType ($mixedValue, $sKey, $aField, $aRow)
  108. {
  109. return parent::_getCellDefault ('allow' == $mixedValue ? _t('_bx_antispam_ip_allow') : _t('_bx_antispam_ip_deny'), $sKey, $aField, $aRow);
  110. }
  111. protected function _getCellLastDT ($mixedValue, $sKey, $aField, $aRow)
  112. {
  113. return parent::_getCellDefault (bx_time_js($mixedValue, BX_FORMAT_DATE), $sKey, $aField, $aRow);
  114. }
  115. protected function _getCellFrom ($mixedValue, $sKey, $aField, $aRow)
  116. {
  117. return parent::_getCellDefault (long2ip($mixedValue), $sKey, $aField, $aRow);
  118. }
  119. protected function _getCellTo ($mixedValue, $sKey, $aField, $aRow)
  120. {
  121. return parent::_getCellDefault (long2ip($mixedValue), $sKey, $aField, $aRow);
  122. }
  123. }
  124. /** @} */