| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup Help Tours Help Tours
- * @ingroup UnaModules
- *
- * @{
- */
- class BxHelpToursGridItems extends BxTemplGrid
- {
- protected $_sModule;
- protected $_oModule;
- protected $_iTourId;
- protected $_aTourPageBlocks;
- public function __construct ($aOptions, $oTemplate = false)
- {
- $this->_sModule = 'bx_help_tours';
- $this->_oModule = BxDolModule::getInstance($this->_sModule);
- if(!$oTemplate)
- $oTemplate = $this->_oModule->_oTemplate;
- parent::__construct ($aOptions, $oTemplate);
- $this->_iTourId = bx_get('tour');
- if ($this->_iTourId) {
- $this->_aQueryAppend['tour'] = $this->_iTourId;
- $this->_aTourPageBlocks = $this->_oModule->_oDb->getPageBlocksForHelpTour($this->_iTourId);
- }
- }
- protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
- {
- if(!empty($this->_iTourId)) {
- $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tour` = ? ", $this->_iTourId);
- return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
- }
- return false;
- }
- protected function _getCellElement($mixedValue, $sKey, $aField, $aRow)
- {
- if (isset($this->_aTourPageBlocks[$aRow['element']])) $mixedValue = htmlspecialchars($this->_aTourPageBlocks[$aRow['element']]);
- return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
- }
- private function getAddEditForm($iEntry) {
- $aEntry = false;
- if ($iEntry) {
- $aEntry = $this->_oModule->_oDb->getTourItemDetails($iEntry);
- if (!$aEntry) {
- echoJson([]);
- exit;
- }
- }
- $oForm = $this->getAddEditFormObject($iEntry);
- $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT.'grid.php?o=' . $this->_sObject . '&a='.($aEntry ? 'edit' : 'add');
- if ($aEntry) {
- $aEntry['element_helper'] = $aEntry['element'];
- if ($aEntry['element'] && !isset($this->_aTourPageBlocks[$aEntry['element']])) $aEntry['element_helper'] = 'custom';
- }
- $oForm->initChecker($aEntry);
- if (isset($oForm->aInputs['element_helper']['value']) && $oForm->aInputs['element_helper']['value'] == 'custom')
- unset($oForm->aInputs['element']['tr_attrs']);
- if ($oForm->isSubmittedAndValid()) {
- if ($aEntry) {
- $oForm->update($aEntry['id']);
- $iResult = $aEntry['id'];
- } else {
- $iResult = $oForm->insert();
- }
- if ($iResult) {
- $aRes = ['grid' => $this->getCode(false), 'blink' => $iResult];
- } else {
- $aRes = ['msg' => "Error occured"]; // if record adding failed, display error message
- }
- echoJson($aRes);
- } else {
- $s = '
- <div>' . $oForm->getCode(true) . '</div>
- <script>
- $(document).ready(function () {
- $("#'.$oForm->aFormAttrs['id'].'").ajaxForm({
- dataType: "json",
- beforeSubmit: function (formData, jqForm, options) {
- bx_loading($("#' . $oForm->aFormAttrs['id'] . '"), true);
- },
- success: function (data) {
- $(".bx-popup-active:visible").dolPopupHide();
- glGrids.' . $this->_sObject . '.processJson(data, "'.($aEntry ? 'edit' : 'add').'");
- }
- });
- });
- </script>';
- echoJson(['popup' => [
- 'html' => BxTemplStudioFunctions::getInstance()->popupBox('bx-tour-item-details-popup', _t('_bx_help_tours_cpt_tour_item_details'), $s),
- 'options' => [
- 'onBeforeShow' => empty($aEntry) ? "$('#grid-popup-bx_help_tours_items-edit').remove();" : "$('#grid-popup-bx_help_tours_items-add').remove();",
- 'closeOnOuterClick' => false,
- ]]]
- );
- }
- }
- public function performActionAdd()
- {
- if(!$this->canAdd()) {
- echoJson([]);
- exit;
- }
- return $this->getAddEditForm(0);
- }
- public function performActionEdit()
- {
- $iEntry = !is_null($_REQUEST) && isset($_REQUEST['id']) ? intval($_REQUEST['id']) : intval($_REQUEST['ids'][0]);
- if(!$iEntry) {
- echoJson([]);
- exit;
- }
- return $this->getAddEditForm($iEntry);
- }
- private function getAddEditFormObject($iEntry) {
- $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'];
- $aArrowOptions = ['' => _t('_bx_help_tours_arrow_option_none')];
- foreach ($aArrowOptionsSrc as $sOption) {
- $aArrowOptions[$sOption] = $sOption;
- }
- $aForm = [
- 'form_attrs' => [
- 'method' => 'post',
- 'action' => '',
- 'id' => 'bx-help-tour-item-details-form',
- ],
- 'params' => [
- 'db' => [
- 'submit_name' => 'submit',
- 'table' => 'bx_help_tours_items',
- 'key' => 'id',
- 'uri' => '',
- 'uri_title' => '',
- ],
- ],
- 'inputs' => [
- 'id' => [
- 'type' => 'hidden',
- 'name' => 'id',
- 'value' => $iEntry,
- ],
- 'tour' => [
- 'type' => 'hidden',
- 'name' => 'tour',
- 'value' => $this->_iTourId,
- 'db' => [
- 'pass' => 'Int',
- ],
- ],
- 'name' => [
- 'type' => 'text',
- 'name' => 'name',
- 'required' => true,
- 'caption' => _t('_bx_help_tours_form_field_name'),
- 'info' => _t('_bx_help_tours_form_field_name_info'),
- 'checker' => [
- 'func' => 'Avail',
- 'error' => _t('_bx_help_tours_form_field_name_error'),
- ],
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'element_helper' => [
- 'type' => 'select',
- 'name' => 'element_helper',
- 'caption' => _t('_bx_help_tours_form_field_element'),
- 'info' => _t('_bx_help_tours_form_field_element_info'),
- 'values' =>
- ['' => _t('_bx_help_tours_form_field_option_none')] +
- $this->_aTourPageBlocks +
- ['custom' => _t('_bx_help_tours_form_field_option_custom')],
- 'attrs' => [
- 'onchange' => "
- $('#bx-help-tours-input-element').val(this.value == 'custom' ? '' : this.value);
- if (this.value == 'custom') $('#bx-form-element-element').fadeIn(); else $('#bx-form-element-element').fadeOut();
- ",
- ],
- ],
- 'element' => [
- 'type' => 'text',
- 'name' => 'element',
- 'caption' => _t('_bx_help_tours_form_field_element_custom'),
- 'info' => _t('_bx_help_tours_form_field_element_custom_info'),
- 'attrs' => [
- 'id' => 'bx-help-tours-input-element',
- ],
- 'tr_attrs' => [
- 'style' => 'display:none;',
- ],
- 'db' => array (
- 'pass' => 'Xss',
- ),
- ],
- 'title' => [
- 'type' => 'text_translatable',
- 'name' => 'title',
- 'caption' => _t('_bx_help_tours_form_field_title'),
- 'db' => [
- 'pass' => 'Xss',
- ],
- ],
- 'text' => [
- 'type' => 'textarea_translatable',
- 'name' => 'text',
- 'required' => true,
- 'caption' => _t('_bx_help_tours_form_field_text'),
- 'checker' => [
- 'func' => 'AvailTranslatable',
- 'params' => ['text'],
- 'error' => _t('_bx_help_tours_form_field_text_error'),
- ],
- 'db' => [
- 'pass' => 'Xss',
- ],
- 'code' => true, //to avoid loosing HTML formatting
- ],
- 'arrow' => [
- 'type' => 'select',
- 'name' => 'arrow',
- 'caption' => _t('_bx_help_tours_form_field_arrow'),
- 'values' => $aArrowOptions,
- 'value' => 'auto',
- 'db' => array (
- 'pass' => 'Xss',
- ),
- ],
- 'actions' => [
- 'type' => 'input_set',
- 0 => [
- 'type' => 'submit',
- 'name' => 'submit',
- 'value' => _t('_Submit'),
- ],
- 1 => [
- 'type' => 'button',
- 'name' => 'cancel',
- 'value' => _t('_Cancel'),
- 'attrs' => [
- 'style' => 'margin-left: 20px;',
- 'onclick' => '$(".bx-popup-active:visible").dolPopupHide();',
- ],
- ],
- ],
- ],
- ];
- return new BxTemplStudioFormView($aForm);
- }
- public function performActionDelete() {
- $aIds = bx_get('ids');
- if (!$aIds || !is_array($aIds)) {
- echoJson(array());
- exit;
- }
- $oLanguage = BxDolStudioLanguagesUtils::getInstance();
- foreach ($aIds as $iEntry) {
- $aItem = $this->_oModule->_oDb->getTourItemDetails($iEntry);
- $oLanguage->deleteLanguageString($aItem['title']);
- $oLanguage->deleteLanguageString($aItem['text']);
- $this->_delete($iEntry);
- }
- echoJson(array(
- 'grid' => $this->getCode(false),
- ));
- }
- public function _getActionPreview($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array()) {
- $sUrl = $this->_oModule->_oDb->getTourPageUrl($this->_iTourId);
- if (!$sUrl) return '';
- $sUrl = trim($sUrl, '/');
- $a['attr']['href'] = bx_absolute_url(bx_append_url_params(BxDolPermalinks::getInstance()->permalink($sUrl), ['help_tour_preview' => $this->_iTourId, 'help_tour_item' => $aRow['id']]));
- $a['attr']['target'] = '_blank';
- $sButton = $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
- $sA = str_replace('button', 'a', $sButton);
- return $sA;
- }
- protected function canAdd()
- {
- return !empty($this->_iTourId);
- }
- protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
- {
- if(!$this->canAdd())
- $isDisabled = true;
- return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
- }
- protected function _getFilterControls ()
- {
- $aTourOptions = $this->_oModule->_oDb->getHelpToursOptions();
- if ($aTourOptions) foreach ($aTourOptions as $iTour => $sPage) {
- $aPageTitle = $this->_oModule->_oDb->getPageTitleDetails($sPage);
- if ($aPageTitle)
- $aTourOptions[$iTour] = $aPageTitle['module_title'].': '._t($aPageTitle['title_system']);
- }
- $aInputTour = [
- 'type' => 'select',
- 'name' => 'tour',
- 'value' => $this->_iTourId,
- 'values' => ['' => _t('_sys_please_select')] + $aTourOptions,
- 'attrs' => array(
- 'id' => 'bx-grid-tour',
- 'onChange' => 'javascript:' . $this->getJsObject() . '.onChangeTour(this.value);',
- ),
- ];
- $oForm = new BxTemplStudioFormView(array());
- return $oForm->genRow($aInputTour).parent::_getFilterControls();
- }
- protected function _addJsCss()
- {
- parent::_addJsCss();
- $this->_oModule->_oTemplate->addStudioCss('admin.css');
- $this->_oModule->_oTemplate->addStudioJs('grid_items.js');
- $oForm = new BxTemplStudioFormView(array());
- $oForm->addCssJs();
- }
- function getJsObject()
- {
- return 'oBxHelpToursGridItems';
- }
- function getCode($isDisplayHeader = true)
- {
- return $this->_oModule->_oTemplate->parseHtmlByName('grid_items.html', [
- 'module_url' => BX_DOL_URL_ROOT.$this->_oModule->_oConfig->getBaseUri(),
- 'content' => parent::getCode($isDisplayHeader),
- 'js_object' => $this->getJsObject(),
- 'grid_object' => $this->_sObject,
- 'tour' => $this->_iTourId,
- 'page_url' => BX_DOL_URL_STUDIO.'module.php?name=' . $this->_sModule . '&page=items',
- ]);
- }
- }
- /** @} */
|