BxHelpToursDb.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 BxHelpToursDb extends BxDolModuleDb
  12. {
  13. private $_sTourBlockTemplate = 'a:3:{s:6:"module";s:13:"bx_help_tours";s:6:"method";s:13:"get_help_tour";s:6:"params";a:1:{i:0;i:__tour_id__;}}';
  14. private $_sBlockSelectorTemplate = '#bx-page-block-__block_id__ > div';
  15. public function __construct(&$oConfig)
  16. {
  17. parent::__construct($oConfig);
  18. }
  19. public function getTourDetails($iTourId) {
  20. return $this->getRow("SELECT * FROM `bx_help_tours` WHERE `id` = :id", ['id' => $iTourId]);
  21. }
  22. public function getTourPageUrl($iTourId) {
  23. $aTour = $this->getTourDetails($iTourId);
  24. if (!$aTour) return false;
  25. if ($aTour['page'] == 'sys_home') return '/';
  26. return $this->getOne("SELECT `url` FROM `sys_objects_page` WHERE `object` = :page", ['page' => $aTour['page']]);
  27. }
  28. public function getSitePages() {
  29. $aPages = $this->getAll("SELECT `object`, `module`, `title_system` AS `name` FROM `sys_objects_page` WHERE `title_system` <> '' ORDER BY `title_system` ASC");
  30. $aPagesByModules = [];
  31. foreach ($aPages as $aPage) {
  32. $aPagesByModules[$aPage['module']][$aPage['object']] = _t($aPage['name']);
  33. }
  34. $aResult = [];
  35. foreach ($aPagesByModules as $sModule => $aPages) {
  36. $oModule = BxDolModule::getInstance($sModule);
  37. $sTitle = $oModule && isset($oModule->_aModule) ? $oModule->_aModule['title'] : ucfirst($sModule);
  38. $aResult[$sModule.'_start'] = ['type' => 'group_header', 'value' => $sTitle];
  39. $aResult = $aResult + $aPages;
  40. $aResult[$sModule.'_end'] = ['type' => 'group_end'];
  41. }
  42. return $aResult;
  43. }
  44. public function deleteHelpTourItems($iEntry) {
  45. $this->query("DELETE FROM `bx_help_tours_items` WHERE `tour` = :tour", ['tour' => $iEntry]);
  46. }
  47. public function getPageTitleDetails($sPageObject) {
  48. $aData = $this->getRow("SELECT `module`, `title_system` FROM `sys_objects_page` WHERE `object` = :page", ['page' => $sPageObject]);
  49. if (!$aData) return false;
  50. $oModule = BxDolModule::getInstance($aData['module']);
  51. $aData['module_title'] = $oModule && isset($oModule->_aModule) ? $oModule->_aModule['title'] : ucfirst($aData['module']);
  52. return $aData;
  53. }
  54. public function putHelpTourOnPage($iTourId, $sOldPage, $iPageBlockVisibility) {
  55. $aTour = $this->getTourDetails($iTourId);
  56. $sPage = $aTour['page'];
  57. $sServiceCall = str_replace('__tour_id__', $iTourId, $this->_sTourBlockTemplate);
  58. if ($sOldPage != $sPage) {
  59. if (!empty($sOldPage)) {
  60. $this->query("DELETE FROM `sys_pages_blocks` WHERE `object` = :page AND `module` = 'bx_help_tours' AND `content` = :service_call", [
  61. 'page' => $sOldPage,
  62. 'service_call' => $sServiceCall,
  63. ]);
  64. }
  65. $iOrder = intval($this->getOne("SELECT MAX(`order`) FROM `sys_pages_blocks` WHERE `object` = :page and `cell_id` = 1", ['page' => $sPage])) + 1;
  66. $this->query("
  67. INSERT INTO `sys_pages_blocks` (`object`, `cell_id`, `module`, `title`, `designbox_id`, `visible_for_levels`, `type`, `content`, `deletable`, `copyable`, `order`) VALUES
  68. (:page, 1, 'bx_help_tours', '_bx_help_tours_page_block_caption', 0, :visibility, 'service', :service_call, 0, 0, :order);
  69. ", [
  70. 'page' => $sPage,
  71. 'service_call' => $sServiceCall,
  72. 'order' => $iOrder,
  73. 'visibility' => $iPageBlockVisibility,
  74. ]);
  75. // disable the built-in site tour on homepage to avoid tours conflicts
  76. if ($sPage == 'sys_home') setParam('site_tour_home', '');
  77. } else {
  78. $this->query("UPDATE `sys_pages_blocks` SET `visible_for_levels` = :visibility WHERE `object` = :page AND `module` = 'bx_help_tours' AND `content` = :service_call", [
  79. 'page' => $sPage,
  80. 'service_call' => $sServiceCall,
  81. 'visibility' => $iPageBlockVisibility,
  82. ]);
  83. }
  84. }
  85. public function deleteHelpTourBlock($iTourId) {
  86. $aTour = $this->getTourDetails($iTourId);
  87. $this->query("DELETE FROM `sys_pages_blocks` WHERE `module` = 'bx_help_tours' AND `object` = :page AND `content` = :call", [
  88. 'page' => $aTour['page'],
  89. 'call' => str_replace('__tour_id__', $iTourId, $this->_sTourBlockTemplate),
  90. ]);
  91. }
  92. public function deleteHelpTourTrackingData($iTourId) {
  93. $this->query("DELETE FROM `bx_help_tours_track_views` WHERE `tour` = :tour", ['tour' => $iTourId]);
  94. }
  95. public function getHelpTourItemsCount($iTourId) {
  96. return $this->getOne("SELECT COUNT(*) FROM `bx_help_tours_items` WHERE `tour` = :tour", [
  97. 'tour' => $iTourId,
  98. ]);
  99. }
  100. public function getHelpTourItems($iTourId) {
  101. return $this->getAll("SELECT * FROM `bx_help_tours_items` WHERE `tour` = :tour ORDER BY `order` ASC", [
  102. 'tour' => $iTourId,
  103. ]);
  104. }
  105. public function getTourItemDetails($iItemId) {
  106. return $this->getRow("SELECT * FROM `bx_help_tours_items` WHERE `id` = :id", ['id' => $iItemId]);
  107. }
  108. public function getPageBlocksForHelpTour($iTourId) {
  109. $aTour = $this->getTourDetails($iTourId);
  110. if (!$aTour) return [];
  111. $aResult = [];
  112. $aRes = $this->getAll("SELECT `id`, `title_system`, `title` FROM `sys_pages_blocks` WHERE `object` = :page AND `cell_id` > 0 AND `active` AND `module` <> 'bx_help_tours' AND (`title_system` <> '' OR `title` <> '') ORDER BY `cell_id` ASC, `order` ASC", ['page' => $aTour['page']]);
  113. if ($aRes) {
  114. foreach ($aRes as $aRow) {
  115. $aResult[str_replace('__block_id__', $aRow['id'], $this->_sBlockSelectorTemplate)] = !empty($aRow['title_system']) ? _t($aRow['title_system']) : _t($aRow['title']);
  116. }
  117. }
  118. return $aResult;
  119. }
  120. public function getHelpToursOptions() {
  121. return $this->getPairs("SELECT `id`, `page` FROM `bx_help_tours` ORDER BY `order` ASC", 'id', 'page');
  122. }
  123. public function deleteAccountData($iAccount) {
  124. $this->query("DELETE FROM `bx_help_tours_track_views` WHERE `account` = :account", ['account' => $iAccount]);
  125. }
  126. public function trackTourSeen($iAccount, $iTour) {
  127. $this->query("INSERT IGNORE INTO `bx_help_tours_track_views` (`account`, `tour`) VALUES(:account, :tour)", ['account' => $iAccount, 'tour' => $iTour]);
  128. }
  129. public function isHelpTourSeen($iAccount, $iTour) {
  130. if (isset($_COOKIE['bx-help-tours-seen-'.$iTour])) return true;
  131. if (!$iAccount) return false;
  132. return $this->getOne("SELECT COUNT(*) FROM `bx_help_tours_track_views` WHERE `account` = :account AND `tour` = :tour", ['account' => $iAccount, 'tour' => $iTour]);
  133. }
  134. public function getTourVisibility($iTour) {
  135. $aTour = $this->getTourDetails($iTour);
  136. return $this->getOne("SELECT `visible_for_levels` FROM `sys_pages_blocks` WHERE `module` = 'bx_help_tours' AND `object` = :page AND `content` = :call", [
  137. 'page' => $aTour['page'],
  138. 'call' => str_replace('__tour_id__', $iTour, $this->_sTourBlockTemplate),
  139. ]);
  140. }
  141. }
  142. /** @} */