BxReviewsGridVotingOptions.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 Reviews Reviews
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxReviewsGridVotingOptions extends BxTemplGrid
  12. {
  13. public function __construct ($aOptions, $oTemplate = false)
  14. {
  15. $this->MODULE = 'bx_reviews';
  16. parent::__construct ($aOptions, $oTemplate);
  17. }
  18. private function getVotingOptionForm($iVotingOptionId, $sLKey, $sActionurl) {
  19. $aForm = [
  20. 'form_attrs' => [
  21. 'id' => 'bx-reviews-voting-option-form',
  22. 'action' => bx_append_url_params($sActionurl, 'ids[]='.$iVotingOptionId),
  23. 'method' => 'post'
  24. ],
  25. 'params' => [
  26. 'db' => [
  27. 'table' => 'bx_reviews_voting_options',
  28. 'key' => 'id',
  29. 'uri' => '',
  30. 'uri_title' => '',
  31. 'submit_name' => 'do_submit'
  32. ],
  33. ],
  34. 'inputs' => [
  35. 'id' => [
  36. 'type' => 'hidden',
  37. 'name' => 'id',
  38. 'value' => $iVotingOptionId,
  39. 'db' => [
  40. 'pass' => 'Int',
  41. ],
  42. ],
  43. 'lkey' => [
  44. 'type' => 'text_translatable',
  45. 'name' => 'lkey',
  46. 'caption' => _t('_bx_reviews_form_entry_input_option_name'),
  47. 'value' => $sLKey,
  48. 'required' => '1',
  49. 'db' => [
  50. 'pass' => 'Xss',
  51. ],
  52. 'checker' => [
  53. 'func' => 'LengthTranslatable',
  54. 'params' => [3, 100, 'lkey'],
  55. 'error' => _t('_bx_reviews_form_entry_input_option_name_err'),
  56. ],
  57. ],
  58. 'controls' => [
  59. 'name' => 'controls',
  60. 'type' => 'input_set',
  61. [
  62. 'type' => 'submit',
  63. 'name' => 'do_submit',
  64. 'value' => _t('_bx_reviews_form_entry_input_do_submit')
  65. ],
  66. [
  67. 'type' => 'reset',
  68. 'name' => 'close',
  69. 'value' => _t('_bx_reviews_txt_cancel'),
  70. 'attrs' => [
  71. 'onclick' => '$(this).closest(\'.bx-popup-responsive\').dolPopupHide();',
  72. 'class' => 'bx-def-margin-sec-left',
  73. ],
  74. ]
  75. ]
  76. ]
  77. ];
  78. $oForm = new BxTemplStudioFormView($aForm);
  79. $oForm->initChecker();
  80. return $oForm;
  81. }
  82. public function performActionAdd() {
  83. $this->performActionAddEdit('add', 0, '');
  84. }
  85. public function performActionEdit() {
  86. $aIds = bx_get('ids');
  87. if (!$aIds || !is_array($aIds)) {
  88. echoJson(array());
  89. exit;
  90. }
  91. $iOptionId = $aIds[0];
  92. $oModule = BxDolModule::getInstance($this->MODULE);
  93. $sLkey = $oModule->_oDb->getVotingOptionLKey($iOptionId);
  94. $this->performActionAddEdit('edit', $iOptionId, $sLkey);
  95. }
  96. public function performActionDelete() {
  97. $aIds = bx_get('ids');
  98. if (!$aIds || !is_array($aIds)) {
  99. echoJson(array());
  100. exit;
  101. }
  102. $iOptionId = $aIds[0];
  103. $oModule = BxDolModule::getInstance($this->MODULE);
  104. $sLkey = $oModule->_oDb->getVotingOptionLKey($iOptionId);
  105. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  106. $oLanguage->deleteLanguageString($sLkey);
  107. return parent::performActionDelete();
  108. }
  109. private function performActionAddEdit($sAction, $iVotingOptionId, $sLKey) {
  110. $oForm = $this->getVotingOptionForm($iVotingOptionId, $sLKey, BX_DOL_URL_ROOT.'grid.php?o=' . $this->_sObject . '&a=' . $sAction);
  111. if ($oForm->isSubmittedAndValid()) {
  112. if ($sAction == 'add') {
  113. $iSuccessId = $oForm->insert();
  114. }
  115. if ($sAction == 'edit') {
  116. $oForm->update($iVotingOptionId);
  117. $iSuccessId = $iVotingOptionId;
  118. }
  119. if ($iSuccessId) {
  120. $aRes = array('grid' => $this->getCode(false), 'blink' => $iSuccessId);
  121. } else
  122. $aRes = array('msg' => "Error occured"); // if record adding failed, display error message
  123. echoJson($aRes);
  124. } else {
  125. $s = PopupBox('bx-reviews-popup', _t('_bx_reviews_popup_voting_option_title'), $oForm->getCode().'
  126. <script>
  127. function bx_grid_ajax_form() {
  128. $("#'.$oForm->aFormAttrs['id'].'").ajaxForm({
  129. dataType: "json",
  130. beforeSubmit: function (formData, jqForm, options) {
  131. bx_loading($("#' . $oForm->aFormAttrs['id'] . '"), true);
  132. },
  133. success: function (data) {
  134. $(".bx-popup-active:visible").dolPopupHide();
  135. glGrids.' . $this->_sObject . '.processJson(data, "add");
  136. }
  137. });
  138. }
  139. </script>');
  140. echoJson([
  141. 'popup' => [
  142. 'html' => $s,
  143. 'options' => [
  144. 'onShow' => 'bx_grid_ajax_form();'
  145. ]
  146. ]
  147. ]);
  148. }
  149. }
  150. }
  151. /** @} */