BxAntispamGridIpTable.php 5.3 KB

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