BxHelpToursGridItems.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. class BxHelpToursGridItems extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_iTourId;
  16. protected $_aTourPageBlocks;
  17. public function __construct ($aOptions, $oTemplate = false)
  18. {
  19. $this->_sModule = 'bx_help_tours';
  20. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  21. if(!$oTemplate)
  22. $oTemplate = $this->_oModule->_oTemplate;
  23. parent::__construct ($aOptions, $oTemplate);
  24. $this->_iTourId = bx_get('tour');
  25. if ($this->_iTourId) {
  26. $this->_aQueryAppend['tour'] = $this->_iTourId;
  27. $this->_aTourPageBlocks = $this->_oModule->_oDb->getPageBlocksForHelpTour($this->_iTourId);
  28. }
  29. }
  30. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  31. {
  32. if(!empty($this->_iTourId)) {
  33. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tour` = ? ", $this->_iTourId);
  34. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  35. }
  36. return false;
  37. }
  38. protected function _getCellElement($mixedValue, $sKey, $aField, $aRow)
  39. {
  40. if (isset($this->_aTourPageBlocks[$aRow['element']])) $mixedValue = htmlspecialchars($this->_aTourPageBlocks[$aRow['element']]);
  41. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  42. }
  43. private function getAddEditForm($iEntry) {
  44. $aEntry = false;
  45. if ($iEntry) {
  46. $aEntry = $this->_oModule->_oDb->getTourItemDetails($iEntry);
  47. if (!$aEntry) {
  48. echoJson([]);
  49. exit;
  50. }
  51. }
  52. $oForm = $this->getAddEditFormObject($iEntry);
  53. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT.'grid.php?o=' . $this->_sObject . '&a='.($aEntry ? 'edit' : 'add');
  54. if ($aEntry) {
  55. $aEntry['element_helper'] = $aEntry['element'];
  56. if ($aEntry['element'] && !isset($this->_aTourPageBlocks[$aEntry['element']])) $aEntry['element_helper'] = 'custom';
  57. }
  58. $oForm->initChecker($aEntry);
  59. if (isset($oForm->aInputs['element_helper']['value']) && $oForm->aInputs['element_helper']['value'] == 'custom')
  60. unset($oForm->aInputs['element']['tr_attrs']);
  61. if ($oForm->isSubmittedAndValid()) {
  62. if ($aEntry) {
  63. $oForm->update($aEntry['id']);
  64. $iResult = $aEntry['id'];
  65. } else {
  66. $iResult = $oForm->insert();
  67. }
  68. if ($iResult) {
  69. $aRes = ['grid' => $this->getCode(false), 'blink' => $iResult];
  70. } else {
  71. $aRes = ['msg' => "Error occured"]; // if record adding failed, display error message
  72. }
  73. echoJson($aRes);
  74. } else {
  75. $s = '
  76. <div>' . $oForm->getCode(true) . '</div>
  77. <script>
  78. $(document).ready(function () {
  79. $("#'.$oForm->aFormAttrs['id'].'").ajaxForm({
  80. dataType: "json",
  81. beforeSubmit: function (formData, jqForm, options) {
  82. bx_loading($("#' . $oForm->aFormAttrs['id'] . '"), true);
  83. },
  84. success: function (data) {
  85. $(".bx-popup-active:visible").dolPopupHide();
  86. glGrids.' . $this->_sObject . '.processJson(data, "'.($aEntry ? 'edit' : 'add').'");
  87. }
  88. });
  89. });
  90. </script>';
  91. echoJson(['popup' => [
  92. 'html' => BxTemplStudioFunctions::getInstance()->popupBox('bx-tour-item-details-popup', _t('_bx_help_tours_cpt_tour_item_details'), $s),
  93. 'options' => [
  94. 'onBeforeShow' => empty($aEntry) ? "$('#grid-popup-bx_help_tours_items-edit').remove();" : "$('#grid-popup-bx_help_tours_items-add').remove();",
  95. 'closeOnOuterClick' => false,
  96. ]]]
  97. );
  98. }
  99. }
  100. public function performActionAdd()
  101. {
  102. if(!$this->canAdd()) {
  103. echoJson([]);
  104. exit;
  105. }
  106. return $this->getAddEditForm(0);
  107. }
  108. public function performActionEdit()
  109. {
  110. $iEntry = !is_null($_REQUEST) && isset($_REQUEST['id']) ? intval($_REQUEST['id']) : intval($_REQUEST['ids'][0]);
  111. if(!$iEntry) {
  112. echoJson([]);
  113. exit;
  114. }
  115. return $this->getAddEditForm($iEntry);
  116. }
  117. private function getAddEditFormObject($iEntry) {
  118. $aArrowOptionsSrc = ['auto', 'auto-start', 'auto-end', 'top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'right', 'right-start', 'right-end', 'left', 'left-start', 'left-end'];
  119. $aArrowOptions = ['' => _t('_bx_help_tours_arrow_option_none')];
  120. foreach ($aArrowOptionsSrc as $sOption) {
  121. $aArrowOptions[$sOption] = $sOption;
  122. }
  123. $aForm = [
  124. 'form_attrs' => [
  125. 'method' => 'post',
  126. 'action' => '',
  127. 'id' => 'bx-help-tour-item-details-form',
  128. ],
  129. 'params' => [
  130. 'db' => [
  131. 'submit_name' => 'submit',
  132. 'table' => 'bx_help_tours_items',
  133. 'key' => 'id',
  134. 'uri' => '',
  135. 'uri_title' => '',
  136. ],
  137. ],
  138. 'inputs' => [
  139. 'id' => [
  140. 'type' => 'hidden',
  141. 'name' => 'id',
  142. 'value' => $iEntry,
  143. ],
  144. 'tour' => [
  145. 'type' => 'hidden',
  146. 'name' => 'tour',
  147. 'value' => $this->_iTourId,
  148. 'db' => [
  149. 'pass' => 'Int',
  150. ],
  151. ],
  152. 'name' => [
  153. 'type' => 'text',
  154. 'name' => 'name',
  155. 'required' => true,
  156. 'caption' => _t('_bx_help_tours_form_field_name'),
  157. 'info' => _t('_bx_help_tours_form_field_name_info'),
  158. 'checker' => [
  159. 'func' => 'Avail',
  160. 'error' => _t('_bx_help_tours_form_field_name_error'),
  161. ],
  162. 'db' => [
  163. 'pass' => 'Xss',
  164. ],
  165. ],
  166. 'element_helper' => [
  167. 'type' => 'select',
  168. 'name' => 'element_helper',
  169. 'caption' => _t('_bx_help_tours_form_field_element'),
  170. 'info' => _t('_bx_help_tours_form_field_element_info'),
  171. 'values' =>
  172. ['' => _t('_bx_help_tours_form_field_option_none')] +
  173. $this->_aTourPageBlocks +
  174. ['custom' => _t('_bx_help_tours_form_field_option_custom')],
  175. 'attrs' => [
  176. 'onchange' => "
  177. $('#bx-help-tours-input-element').val(this.value == 'custom' ? '' : this.value);
  178. if (this.value == 'custom') $('#bx-form-element-element').fadeIn(); else $('#bx-form-element-element').fadeOut();
  179. ",
  180. ],
  181. ],
  182. 'element' => [
  183. 'type' => 'text',
  184. 'name' => 'element',
  185. 'caption' => _t('_bx_help_tours_form_field_element_custom'),
  186. 'info' => _t('_bx_help_tours_form_field_element_custom_info'),
  187. 'attrs' => [
  188. 'id' => 'bx-help-tours-input-element',
  189. ],
  190. 'tr_attrs' => [
  191. 'style' => 'display:none;',
  192. ],
  193. 'db' => array (
  194. 'pass' => 'Xss',
  195. ),
  196. ],
  197. 'title' => [
  198. 'type' => 'text_translatable',
  199. 'name' => 'title',
  200. 'caption' => _t('_bx_help_tours_form_field_title'),
  201. 'db' => [
  202. 'pass' => 'Xss',
  203. ],
  204. ],
  205. 'text' => [
  206. 'type' => 'textarea_translatable',
  207. 'name' => 'text',
  208. 'required' => true,
  209. 'caption' => _t('_bx_help_tours_form_field_text'),
  210. 'checker' => [
  211. 'func' => 'AvailTranslatable',
  212. 'params' => ['text'],
  213. 'error' => _t('_bx_help_tours_form_field_text_error'),
  214. ],
  215. 'db' => [
  216. 'pass' => 'Xss',
  217. ],
  218. 'code' => true, //to avoid loosing HTML formatting
  219. ],
  220. 'arrow' => [
  221. 'type' => 'select',
  222. 'name' => 'arrow',
  223. 'caption' => _t('_bx_help_tours_form_field_arrow'),
  224. 'values' => $aArrowOptions,
  225. 'value' => 'auto',
  226. 'db' => array (
  227. 'pass' => 'Xss',
  228. ),
  229. ],
  230. 'actions' => [
  231. 'type' => 'input_set',
  232. 0 => [
  233. 'type' => 'submit',
  234. 'name' => 'submit',
  235. 'value' => _t('_Submit'),
  236. ],
  237. 1 => [
  238. 'type' => 'button',
  239. 'name' => 'cancel',
  240. 'value' => _t('_Cancel'),
  241. 'attrs' => [
  242. 'style' => 'margin-left: 20px;',
  243. 'onclick' => '$(".bx-popup-active:visible").dolPopupHide();',
  244. ],
  245. ],
  246. ],
  247. ],
  248. ];
  249. return new BxTemplStudioFormView($aForm);
  250. }
  251. public function performActionDelete() {
  252. $aIds = bx_get('ids');
  253. if (!$aIds || !is_array($aIds)) {
  254. echoJson(array());
  255. exit;
  256. }
  257. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  258. foreach ($aIds as $iEntry) {
  259. $aItem = $this->_oModule->_oDb->getTourItemDetails($iEntry);
  260. $oLanguage->deleteLanguageString($aItem['title']);
  261. $oLanguage->deleteLanguageString($aItem['text']);
  262. $this->_delete($iEntry);
  263. }
  264. echoJson(array(
  265. 'grid' => $this->getCode(false),
  266. ));
  267. }
  268. public function _getActionPreview($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array()) {
  269. $sUrl = $this->_oModule->_oDb->getTourPageUrl($this->_iTourId);
  270. if (!$sUrl) return '';
  271. $sUrl = trim($sUrl, '/');
  272. $a['attr']['href'] = bx_absolute_url(bx_append_url_params(BxDolPermalinks::getInstance()->permalink($sUrl), ['help_tour_preview' => $this->_iTourId, 'help_tour_item' => $aRow['id']]));
  273. $a['attr']['target'] = '_blank';
  274. $sButton = $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  275. $sA = str_replace('button', 'a', $sButton);
  276. return $sA;
  277. }
  278. protected function canAdd()
  279. {
  280. return !empty($this->_iTourId);
  281. }
  282. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  283. {
  284. if(!$this->canAdd())
  285. $isDisabled = true;
  286. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  287. }
  288. protected function _getFilterControls ()
  289. {
  290. $aTourOptions = $this->_oModule->_oDb->getHelpToursOptions();
  291. if ($aTourOptions) foreach ($aTourOptions as $iTour => $sPage) {
  292. $aPageTitle = $this->_oModule->_oDb->getPageTitleDetails($sPage);
  293. if ($aPageTitle)
  294. $aTourOptions[$iTour] = $aPageTitle['module_title'].': '._t($aPageTitle['title_system']);
  295. }
  296. $aInputTour = [
  297. 'type' => 'select',
  298. 'name' => 'tour',
  299. 'value' => $this->_iTourId,
  300. 'values' => ['' => _t('_sys_please_select')] + $aTourOptions,
  301. 'attrs' => array(
  302. 'id' => 'bx-grid-tour',
  303. 'onChange' => 'javascript:' . $this->getJsObject() . '.onChangeTour(this.value);',
  304. ),
  305. ];
  306. $oForm = new BxTemplStudioFormView(array());
  307. return $oForm->genRow($aInputTour).parent::_getFilterControls();
  308. }
  309. protected function _addJsCss()
  310. {
  311. parent::_addJsCss();
  312. $this->_oModule->_oTemplate->addStudioCss('admin.css');
  313. $this->_oModule->_oTemplate->addStudioJs('grid_items.js');
  314. $oForm = new BxTemplStudioFormView(array());
  315. $oForm->addCssJs();
  316. }
  317. function getJsObject()
  318. {
  319. return 'oBxHelpToursGridItems';
  320. }
  321. function getCode($isDisplayHeader = true)
  322. {
  323. return $this->_oModule->_oTemplate->parseHtmlByName('grid_items.html', [
  324. 'module_url' => BX_DOL_URL_ROOT.$this->_oModule->_oConfig->getBaseUri(),
  325. 'content' => parent::getCode($isDisplayHeader),
  326. 'js_object' => $this->getJsObject(),
  327. 'grid_object' => $this->_sObject,
  328. 'tour' => $this->_iTourId,
  329. 'page_url' => BX_DOL_URL_STUDIO.'module.php?name=' . $this->_sModule . '&page=items',
  330. ]);
  331. }
  332. }
  333. /** @} */