BxHelpToursGridTours.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 Help Tours Help Tours
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolStudioUtils');
  12. class BxHelpToursGridTours extends BxTemplGrid
  13. {
  14. protected $_sModule;
  15. protected $_oModule;
  16. public function __construct ($aOptions, $oTemplate = false)
  17. {
  18. $this->_sModule = 'bx_help_tours';
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. if(!$oTemplate)
  21. $oTemplate = $this->_oModule->_oTemplate;
  22. parent::__construct ($aOptions, $oTemplate);
  23. }
  24. protected function _getCellPage($mixedValue, $sKey, $aField, $aRow)
  25. {
  26. $aPageTitle = $this->_oModule->_oDb->getPageTitleDetails($mixedValue);
  27. if ($aPageTitle)
  28. $mixedValue = $aPageTitle['module_title'].': '._t($aPageTitle['title_system']);
  29. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  30. }
  31. protected function _getCellItems($mixedValue, $sKey, $aField, $aRow)
  32. {
  33. $sLink = 'javascript: glGrids.'.$this->_sObject.'.action(\'edit_items\', {id: '.$aRow['id'].'});';
  34. $mixedValue = $this->_oTemplate->parseLink($sLink, _t('_bx_help_tours_cpt_items_cnt', $this->_oModule->_oDb->getHelpTourItemsCount($aRow['id'])));
  35. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  36. }
  37. public function _getActionPreview($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array()) {
  38. $sUrl = $this->_oModule->_oDb->getTourPageUrl($aRow['id']);
  39. if (!$sUrl) return '';
  40. $sUrl = trim($sUrl, '/');
  41. $a['attr']['href'] = bx_absolute_url(bx_append_url_params(BxDolPermalinks::getInstance()->permalink($sUrl), ['help_tour_preview' => $aRow['id']]));
  42. $a['attr']['target'] = '_blank';
  43. $sButton = $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  44. $sA = str_replace('button', 'a', $sButton);
  45. return $sA;
  46. }
  47. private function getAddEditForm($iEntry) {
  48. $aEntry = false;
  49. if ($iEntry) {
  50. $aEntry = $this->_oModule->_oDb->getTourDetails($iEntry);
  51. if (!$aEntry) {
  52. echoJson([]);
  53. exit;
  54. }
  55. }
  56. $iTourVisibility = !$iEntry ? BX_DOL_INT_MAX : $this->_oModule->_oDb->getTourVisibility($iEntry);
  57. if (bx_get('visible_for'))
  58. $iTourVisibility = BxDolStudioUtils::getVisibilityValue(bx_get('visible_for'), bx_get('visible_for_levels'));
  59. $oForm = $this->getAddEditFormObject($iEntry, $iTourVisibility);
  60. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT.'grid.php?o=' . $this->_sObject . '&a='.($aEntry ? 'edit' : 'add');
  61. $oForm->initChecker($aEntry);
  62. if ($oForm->isSubmittedAndValid()) {
  63. if ($aEntry) {
  64. $oForm->update($aEntry['id']);
  65. $iResult = $aEntry['id'];
  66. } else {
  67. $iResult = $oForm->insert();
  68. }
  69. if ($iResult) {
  70. $this->_oModule->_oDb->putHelpTourOnPage($iResult, $aEntry ? $aEntry['page'] : '', $iTourVisibility);
  71. $aRes = ['grid' => $this->getCode(false), 'blink' => $iResult];
  72. } else {
  73. $aRes = ['msg' => "Error occured"]; // if record adding failed, display error message
  74. }
  75. echoJson($aRes);
  76. } else {
  77. $s = '
  78. <div>' . $oForm->getCode(true) . '</div>
  79. <script>
  80. $(document).ready(function () {
  81. $("#'.$oForm->aFormAttrs['id'].'").ajaxForm({
  82. dataType: "json",
  83. beforeSubmit: function (formData, jqForm, options) {
  84. bx_loading($("#' . $oForm->aFormAttrs['id'] . '"), true);
  85. },
  86. success: function (data) {
  87. $(".bx-popup-active:visible").dolPopupHide();
  88. glGrids.' . $this->_sObject . '.processJson(data, "'.($aEntry ? 'edit' : 'add').'");
  89. }
  90. });
  91. });
  92. </script>';
  93. echoJson(['popup' => [
  94. 'html' => BxTemplStudioFunctions::getInstance()->popupBox('bx-tour-details-popup', _t('_bx_help_tours_cpt_tour_details'), $s),
  95. 'options' => [
  96. 'onBeforeShow' => empty($aEntry) ? "$('#grid-popup-bx_help_tours_tours-edit').remove();" : "$('#grid-popup-bx_help_tours_tours-add').remove();",
  97. 'closeOnOuterClick' => false,
  98. ]]]
  99. );
  100. }
  101. }
  102. public function performActionAdd()
  103. {
  104. return $this->getAddEditForm(0);
  105. }
  106. public function performActionEdit()
  107. {
  108. $iEntry = !is_null($_REQUEST) && isset($_REQUEST['id']) ? intval($_REQUEST['id']) : intval($_REQUEST['ids'][0]);
  109. if(!$iEntry) {
  110. echoJson([]);
  111. exit;
  112. }
  113. return $this->getAddEditForm($iEntry);
  114. }
  115. public function performActionEditItems()
  116. {
  117. $iTourId = !is_null($_REQUEST) && isset($_REQUEST['id']) ? intval($_REQUEST['id']) : intval($_REQUEST['ids'][0]);
  118. if (!$iTourId) {
  119. echoJson(array());
  120. exit;
  121. }
  122. echoJson(array(
  123. 'redirect' => BX_DOL_URL_STUDIO.'module.php?name=' . $this->_sModule . '&tour=' . $iTourId . '&page=items',
  124. ));
  125. }
  126. public function performActionDelete() {
  127. $aIds = bx_get('ids');
  128. if (!$aIds || !is_array($aIds)) {
  129. echoJson(array());
  130. exit;
  131. }
  132. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  133. foreach ($aIds as $iEntry) {
  134. $this->_oModule->_oDb->deleteHelpTourBlock($iEntry);
  135. $this->_oModule->_oDb->deleteHelpTourTrackingData($iEntry);
  136. $aItems = $this->_oModule->_oDb->getHelpTourItems($iEntry);
  137. if ($aItems) foreach ($aItems as $aItem) {
  138. $oLanguage->deleteLanguageString($aItem['title']);
  139. $oLanguage->deleteLanguageString($aItem['text']);
  140. }
  141. $this->_oModule->_oDb->deleteHelpTourItems($iEntry);
  142. $this->_delete($iEntry);
  143. }
  144. echoJson(array(
  145. 'grid' => $this->getCode(false),
  146. ));
  147. }
  148. private function getAddEditFormObject($iEntry, $iVisibility = BX_DOL_INT_MAX) {
  149. $aForm = [
  150. 'form_attrs' => [
  151. 'method' => 'post',
  152. 'action' => '',
  153. 'id' => 'bx-help-tour-details-form',
  154. ],
  155. 'params' => [
  156. 'db' => [
  157. 'submit_name' => 'submit',
  158. 'table' => 'bx_help_tours',
  159. 'key' => 'id',
  160. 'uri' => '',
  161. 'uri_title' => '',
  162. ],
  163. ],
  164. 'inputs' => [
  165. 'id' => [
  166. 'type' => 'hidden',
  167. 'name' => 'id',
  168. 'value' => $iEntry,
  169. ],
  170. 'page' => [
  171. 'type' => 'select',
  172. 'name' => 'page',
  173. 'required' => true,
  174. 'caption' => _t('_bx_help_tours_form_field_page'),
  175. 'values' => ['' => _t('_sys_please_select')] + $this->_oModule->_oDb->getSitePages(),
  176. 'checker' => [
  177. 'func' => 'Avail',
  178. 'error' => _t('_bx_help_tours_form_field_page_error'),
  179. ],
  180. 'db' => [
  181. 'pass' => 'Xss',
  182. ],
  183. ],
  184. 'overlay' => [
  185. 'type' => 'switcher',
  186. 'name' => 'overlay',
  187. 'caption' => _t('_bx_help_tours_form_field_overlay'),
  188. 'info' => _t('_bx_help_tours_form_field_overlay_info'),
  189. 'value' => 1,
  190. 'db' => [
  191. 'pass' => 'Int',
  192. ],
  193. ],
  194. 'visible_for' => [
  195. 'type' => 'select',
  196. 'name' => 'visible_for',
  197. 'caption' => _t('_bx_help_tours_form_field_visible_for'),
  198. 'value' => $iVisibility == BX_DOL_INT_MAX ? BX_DOL_STUDIO_VISIBLE_ALL : BX_DOL_STUDIO_VISIBLE_SELECTED,
  199. 'values' => [
  200. ['key' => BX_DOL_STUDIO_VISIBLE_ALL, 'value' => _t('_bx_help_tours_form_field_visible_for_all')],
  201. ['key' => BX_DOL_STUDIO_VISIBLE_SELECTED, 'value' => _t('_bx_help_tours_form_field_visible_for_selected')],
  202. ],
  203. 'attrs' => [
  204. 'onchange' => "$('#bx-form-element-visible_for_levels').bx_anim(this.value == 'all' ? 'hide' : 'show')",
  205. ],
  206. ],
  207. 'visible_for_levels' => [
  208. 'type' => 'checkbox_set',
  209. 'name' => 'visible_for_levels',
  210. 'caption' => _t('_bx_help_tours_form_field_visible_for_levels'),
  211. 'value' => '',
  212. 'values' => [],
  213. 'tr_attrs' => [
  214. 'style' => $iVisibility == BX_DOL_INT_MAX ? 'display:none' : ''
  215. ],
  216. ],
  217. 'actions' => [
  218. 'type' => 'input_set',
  219. 0 => [
  220. 'type' => 'submit',
  221. 'name' => 'submit',
  222. 'value' => _t('_Submit'),
  223. ],
  224. 1 => [
  225. 'type' => 'button',
  226. 'name' => 'cancel',
  227. 'value' => _t('_Cancel'),
  228. 'attrs' => [
  229. 'style' => 'margin-left: 20px;',
  230. 'onclick' => '$(".bx-popup-active:visible").dolPopupHide();',
  231. ],
  232. ],
  233. ],
  234. ],
  235. ];
  236. BxDolStudioUtils::getVisibilityValues($iVisibility, $aForm['inputs']['visible_for_levels']['values'], $aForm['inputs']['visible_for_levels']['value']);
  237. return new BxTemplFormView($aForm);
  238. }
  239. }
  240. /** @} */