Просмотр исходного кода

#3393 Help Tours: Added an ability to control tours' visibility by ACLs.
Various enhancements.

Alexander 5 лет назад
Родитель
Сommit
06078658a3

+ 36 - 7
modules/boonex/help_tours/classes/BxHelpToursDb.php

@@ -66,14 +66,17 @@ class BxHelpToursDb extends BxDolModuleDb
         return $aData;
     }
 
-    public function putHelpTourOnPage($iTourId, $sOldPage) {
+    public function putHelpTourOnPage($iTourId, $sOldPage, $iPageBlockVisibility) {
         $aTour = $this->getTourDetails($iTourId);
         $sPage = $aTour['page'];
 
+        $sServiceCall = str_replace('__tour_id__', $iTourId, $this->_sTourBlockTemplate);
+
         if ($sOldPage != $sPage) {
             if (!empty($sOldPage)) {
-                $this->query("DELETE FROM `sys_pages_blocks` WHERE `object` = :page AND `module` = 'bx_help_tours'", [
+                $this->query("DELETE FROM `sys_pages_blocks` WHERE `object` = :page AND `module` = 'bx_help_tours' AND `content` = :service_call", [
                     'page' => $sOldPage,
+                    'service_call' => $sServiceCall,
                 ]);
             }
 
@@ -81,24 +84,39 @@ class BxHelpToursDb extends BxDolModuleDb
 
             $this->query("
                 INSERT INTO `sys_pages_blocks` (`object`, `cell_id`, `module`, `title`, `designbox_id`, `visible_for_levels`, `type`, `content`, `deletable`, `copyable`, `order`) VALUES
-                (:page, 1, 'bx_help_tours', '_bx_help_tours_page_block_caption', 0, 2147483647, 'service', :service_call, 0, 0, :order);
+                (:page, 1, 'bx_help_tours', '_bx_help_tours_page_block_caption', 0, :visibility, 'service', :service_call, 0, 0, :order);
             ", [
                 'page' => $sPage,
-                'service_call' => str_replace('__tour_id__', $iTourId, $this->_sTourBlockTemplate),
+                'service_call' => $sServiceCall,
                 'order' => $iOrder,
+                'visibility' => $iPageBlockVisibility,
+            ]);
+
+            // disable the built-in site tour on homepage to avoid tours conflicts
+            if ($sPage == 'sys_home') setParam('site_tour_home', '');
+        } else {
+            $this->query("UPDATE `sys_pages_blocks` SET `visible_for_levels` = :visibility WHERE `object` = :page AND `module` = 'bx_help_tours' AND `content` = :service_call", [
+                'page' => $sPage,
+                'service_call' => $sServiceCall,
+                'visibility' => $iPageBlockVisibility,
             ]);
         }
     }
 
-    function deleteHelpTourBlock($iTourId) {
+    public function deleteHelpTourBlock($iTourId) {
         $aTour = $this->getTourDetails($iTourId);
 
-        $this->query("DELETE FROM `sys_pages_blocks` WHERE `module` = 'bx_help_tours' AND `object` = :page", [
+        $this->query("DELETE FROM `sys_pages_blocks` WHERE `module` = 'bx_help_tours' AND `object` = :page AND `content` = :call", [
             'page' => $aTour['page'],
+            'call' => str_replace('__tour_id__', $iTourId, $this->_sTourBlockTemplate),
         ]);
     }
 
-    function getHelpTourItemsCount($iTourId) {
+    public function deleteHelpTourTrackingData($iTourId) {
+        $this->query("DELETE FROM `bx_help_tours_track_views` WHERE `tour` = :tour", ['tour' => $iTourId]);
+    }
+
+    public function getHelpTourItemsCount($iTourId) {
         return $this->getOne("SELECT COUNT(*) FROM `bx_help_tours_items` WHERE `tour` = :tour", [
             'tour' => $iTourId,
         ]);
@@ -143,8 +161,19 @@ class BxHelpToursDb extends BxDolModuleDb
     }
 
     public function isHelpTourSeen($iAccount, $iTour) {
+        if (isset($_COOKIE['bx-help-tours-seen-'.$iTour])) return true;
+        if (!$iAccount) return false;
         return $this->getOne("SELECT COUNT(*) FROM `bx_help_tours_track_views` WHERE `account` = :account AND `tour` = :tour", ['account' => $iAccount, 'tour' => $iTour]);
     }
+
+    public function getTourVisibility($iTour) {
+        $aTour = $this->getTourDetails($iTour);
+
+        return $this->getOne("SELECT `visible_for_levels` FROM `sys_pages_blocks` WHERE `module` = 'bx_help_tours' AND `object` = :page AND `content` = :call", [
+            'page' => $aTour['page'],
+            'call' => str_replace('__tour_id__', $iTour, $this->_sTourBlockTemplate),
+        ]);
+    }
 }
 
 /** @} */

+ 35 - 3
modules/boonex/help_tours/classes/BxHelpToursGridTours.php

@@ -9,6 +9,8 @@
  * @{
  */
 
+bx_import('BxDolStudioUtils');
+
 class BxHelpToursGridTours extends BxTemplGrid
 {
     public function __construct ($aOptions, $oTemplate = false)
@@ -60,7 +62,10 @@ class BxHelpToursGridTours extends BxTemplGrid
             }
         }
 
-        $oForm = $this->getAddEditFormObject($iEntry);
+        $iTourVisibility = !$iEntry ? BX_DOL_INT_MAX : $this->_oModule->_oDb->getTourVisibility($iEntry);
+        if (bx_get('visible_for'))
+            $iTourVisibility = BxDolStudioUtils::getVisibilityValue(bx_get('visible_for'), bx_get('visible_for_levels'));
+        $oForm = $this->getAddEditFormObject($iEntry, $iTourVisibility);
 
         $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT.'grid.php?o=' . $this->_sObject . '&a='.($aEntry ? 'edit' : 'add');
 
@@ -75,7 +80,7 @@ class BxHelpToursGridTours extends BxTemplGrid
             }
 
             if ($iResult) {
-                $this->_oModule->_oDb->putHelpTourOnPage($iResult, $aEntry ? $aEntry['page'] : '');
+                $this->_oModule->_oDb->putHelpTourOnPage($iResult, $aEntry ? $aEntry['page'] : '', $iTourVisibility);
                 $aRes = ['grid' => $this->getCode(false), 'blink' => $iResult];
             } else {
                 $aRes = ['msg' => "Error occured"]; // if record adding failed, display error message
@@ -102,6 +107,7 @@ class BxHelpToursGridTours extends BxTemplGrid
             echoJson(['popup' => [
                 'html' => BxTemplStudioFunctions::getInstance()->popupBox('bx-tour-details-popup', _t('_bx_help_tours_cpt_tour_details'), $s),
                 'options' => [
+                    'onBeforeShow' => empty($aEntry) ? "$('#grid-popup-bx_help_tours_tours-edit').remove();" : "$('#grid-popup-bx_help_tours_tours-add').remove();",
                     'closeOnOuterClick' => false,
                 ]]]
             );
@@ -149,6 +155,7 @@ class BxHelpToursGridTours extends BxTemplGrid
 
         foreach ($aIds as $iEntry) {
             $this->_oModule->_oDb->deleteHelpTourBlock($iEntry);
+            $this->_oModule->_oDb->deleteHelpTourTrackingData($iEntry);
 
             $aItems = $this->_oModule->_oDb->getHelpTourItems($iEntry);
             if ($aItems) foreach ($aItems as $aItem) {
@@ -165,7 +172,7 @@ class BxHelpToursGridTours extends BxTemplGrid
         ));
     }
 
-    private function getAddEditFormObject($iEntry) {
+    private function getAddEditFormObject($iEntry, $iVisibility = BX_DOL_INT_MAX) {
         $aForm = [
             'form_attrs' => [
                 'method' => 'post',
@@ -211,6 +218,29 @@ class BxHelpToursGridTours extends BxTemplGrid
                         'pass' => 'Int',
                     ],
                 ],
+                'visible_for' => [
+                    'type' => 'select',
+                    'name' => 'visible_for',
+                    'caption' => _t('_bx_help_tours_form_field_visible_for'),
+                    'value' => $iVisibility == BX_DOL_INT_MAX ? BX_DOL_STUDIO_VISIBLE_ALL : BX_DOL_STUDIO_VISIBLE_SELECTED,
+                    'values' => [
+                        ['key' => BX_DOL_STUDIO_VISIBLE_ALL, 'value' => _t('_bx_help_tours_form_field_visible_for_all')],
+                        ['key' => BX_DOL_STUDIO_VISIBLE_SELECTED, 'value' => _t('_bx_help_tours_form_field_visible_for_selected')],
+                    ],
+                    'attrs' => [
+                        'onchange' => "$('#bx-form-element-visible_for_levels').bx_anim(this.value == 'all' ? 'hide' : 'show')",
+                    ],
+                ],
+                'visible_for_levels' => [
+                    'type' => 'checkbox_set',
+                    'name' => 'visible_for_levels',
+                    'caption' => _t('_bx_help_tours_form_field_visible_for_levels'),
+                    'value' => '',
+                    'values' => [],
+                    'tr_attrs' => [
+                        'style' => $iVisibility == BX_DOL_INT_MAX ? 'display:none' : ''
+                    ],
+                ],
                 'actions' => [
                     'type' => 'input_set',
                     0 => [
@@ -230,6 +260,8 @@ class BxHelpToursGridTours extends BxTemplGrid
                 ],
             ],
         ];
+        BxDolStudioUtils::getVisibilityValues($iVisibility, $aForm['inputs']['visible_for_levels']['values'], $aForm['inputs']['visible_for_levels']['value']);
+
         return new BxTemplFormView($aForm);
     }
 }

+ 7 - 1
modules/boonex/help_tours/classes/BxHelpToursModule.php

@@ -17,7 +17,12 @@ class BxHelpToursModule extends BxDolModule
     }
 
     public function serviceGetHelpTour($iTourId) {
-        if (!isLogged() || !$iTourId) return;
+        static $_bAlreadyPlacedATour;
+
+        // avoid running multiple tours at once on a single page
+        if ($_bAlreadyPlacedATour) return;
+
+        if (!$iTourId) return;
 
         $aTour = $this->_oDb->getTourDetails($iTourId);
         if (!$aTour) return;
@@ -27,6 +32,7 @@ class BxHelpToursModule extends BxDolModule
 
         if ((!isAdmin() || !bx_get('help_tour_preview')) && $this->_oDb->isHelpTourSeen(getLoggedId(), $iTourId)) return;
 
+        $_bAlreadyPlacedATour = true;
         return $this->_oTemplate->getHelpTourCode($aTour, $aHelpTourItems);
     }
 

+ 0 - 1
modules/boonex/help_tours/classes/BxHelpToursTemplate.php

@@ -29,7 +29,6 @@ class BxHelpToursTemplate extends BxBaseModGeneralTemplate
             $aStep = [
                 'id' => 'bx-help-tour-step-'.$aItem['id'],
                 'text' => _t($aItem['text']),
-                'attachTo' => ['element' => $aItem['element'], 'on' => $aItem['arrow']],
             ];
 
             if ($aItem['title']) $aStep['title'] = _t($aItem['title']);

+ 4 - 0
modules/boonex/help_tours/install/langs/en.xml

@@ -49,4 +49,8 @@
 	<string name="_bx_help_tours_form_field_option_custom"><![CDATA[Custom]]></string>
 
 	<string name="_bx_help_tours_page_block_caption"><![CDATA[Help Tour]]></string>
+	<string name="_bx_help_tours_form_field_visible_for"><![CDATA[Visible For]]></string>
+	<string name="_bx_help_tours_form_field_visible_for_levels"><![CDATA[Select Levels]]></string>
+	<string name="_bx_help_tours_form_field_visible_for_all"><![CDATA[Anyone]]></string>
+	<string name="_bx_help_tours_form_field_visible_for_selected"><![CDATA[Selected levels]]></string>
 </resources>

+ 11 - 11
modules/boonex/help_tours/template/images/icons/std-icon.svg

@@ -1,16 +1,16 @@
-<svg width="150" height="150" viewBox="0 0 150 150" fill="none" xmlns="http://www.w3.org/2000/svg">
-<path d="M0 75C0 18.75 18.75 0 75 0C131.25 0 150 18.75 150 75C150 131.25 131.25 150 75 150C18.75 150 0 131.25 0 75Z" fill="url(#paint0_linear)"/>
+<svg width="512" height="512" viewBox="0 0 512 512" fill="none" xmlns="http://www.w3.org/2000/svg">
+<path d="M0 256C0 64 64 0 256 0C448 0 512 64 512 256C512 448 448 512 256 512C64 512 0 448 0 256Z" fill="url(#paint0_linear)"/>
 <g filter="url(#filter0_d)">
-<path d="M101.46 79.5407C108.165 86.246 107.442 97.5539 99.3006 103.272C94.1194 106.909 86.8823 106.909 81.7011 103.272C73.5549 97.5591 72.8368 86.246 79.5419 79.5407C85.5961 73.4864 95.4108 73.4864 101.46 79.5407" stroke="white" stroke-width="6" stroke-linecap="round" stroke-linejoin="round"/>
-<path d="M112 112L102 102" stroke="white" stroke-width="6" stroke-linecap="round" stroke-linejoin="round"/>
-<path d="M50.26 48.96C50.248 48.96 50.24 48.968 50.24 48.98C50.24 48.992 50.248 49 50.26 49C50.272 49 50.28 48.992 50.28 48.98C50.28 48.968 50.272 48.96 50.26 48.96" stroke="#F3F4F6" stroke-width="6" stroke-linecap="round" stroke-linejoin="round"/>
-<path d="M60.432 48.96C60.42 48.96 60.412 48.968 60.412 48.98C60.412 48.992 60.424 49 60.432 49C60.444 49 60.452 48.992 60.452 48.98C60.452 48.968 60.444 48.96 60.432 48.96" stroke="#F3F4F6" stroke-width="6" stroke-linecap="round" stroke-linejoin="round"/>
-<path d="M70.588 48.96C70.576 48.96 70.568 48.968 70.568 48.98C70.568 48.992 70.576 49 70.588 49C70.6 49 70.608 48.992 70.608 48.98C70.608 48.968 70.6 48.96 70.588 48.96" stroke="#F3F4F6" stroke-width="6" stroke-linecap="round" stroke-linejoin="round"/>
-<path d="M111 71V47C111 42.58 107.42 39 103 39H47C42.58 39 39 42.58 39 47V103C39 107.42 42.58 111 47 111H71" stroke="#F3F4F6" stroke-width="6" stroke-linecap="round" stroke-linejoin="round"/>
-<path d="M39.144 59H111" stroke="#F3F4F6" stroke-width="6" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M373.599 276.181C403.4 305.982 400.185 356.24 364.003 381.655C340.975 397.819 308.81 397.819 285.783 381.655C249.577 356.263 246.386 305.982 276.186 276.181C303.094 249.273 346.715 249.273 373.599 276.181" stroke="white" stroke-width="32" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M408.444 408.444L376 376" stroke="white" stroke-width="32" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M146.044 140.267C145.991 140.267 145.956 140.302 145.956 140.356C145.956 140.409 145.991 140.444 146.044 140.444C146.098 140.444 146.133 140.409 146.133 140.356C146.133 140.302 146.098 140.267 146.044 140.267" stroke="#D1FAE5" stroke-width="32" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M191.253 140.267C191.2 140.267 191.164 140.302 191.164 140.356C191.164 140.409 191.218 140.444 191.253 140.444C191.307 140.444 191.342 140.409 191.342 140.356C191.342 140.302 191.307 140.267 191.253 140.267" stroke="#D1FAE5" stroke-width="32" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M236.391 140.267C236.338 140.267 236.302 140.302 236.302 140.356C236.302 140.409 236.338 140.444 236.391 140.444C236.445 140.444 236.48 140.409 236.48 140.356C236.48 140.302 236.445 140.267 236.391 140.267" stroke="#D1FAE5" stroke-width="32" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M416 238.222V131.556C416 111.911 400.089 96 380.444 96H131.556C111.911 96 96 111.911 96 131.556V380.444C96 400.089 111.911 416 131.556 416H238.222" stroke="#D1FAE5" stroke-width="32" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M96.6401 184.889H416" stroke="#D1FAE5" stroke-width="32" stroke-linecap="round" stroke-linejoin="round"/>
 </g>
 <defs>
-<filter id="filter0_d" x="34" y="35" width="83" height="83" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
+<filter id="filter0_d" x="78" y="79" width="356" height="356" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
 <feFlood flood-opacity="0" result="BackgroundImageFix"/>
 <feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"/>
 <feOffset dy="1"/>
@@ -19,7 +19,7 @@
 <feBlend mode="normal" in2="BackgroundImageFix" result="effect1_dropShadow"/>
 <feBlend mode="normal" in="SourceGraphic" in2="effect1_dropShadow" result="shape"/>
 </filter>
-<linearGradient id="paint0_linear" x1="75" y1="0" x2="75" y2="150" gradientUnits="userSpaceOnUse">
+<linearGradient id="paint0_linear" x1="256" y1="0" x2="256" y2="512" gradientUnits="userSpaceOnUse">
 <stop stop-color="#10B981"/>
 <stop offset="1" stop-color="#059669"/>
 </linearGradient>

+ 16 - 13
modules/boonex/help_tours/template/tour.html

@@ -10,6 +10,7 @@
 
     <bx_if:not_preview>
     Shepherd.on('cancel', function () {
+        $.cookie('bx-help-tours-seen-__tour_id__', 1, {expires: 9999});
         $.post('__finish_callback_url__', {tour: __tour_id__});
     })
     </bx_if:not_preview>
@@ -17,22 +18,24 @@
     var bx_help_tour_steps =  __steps__;
     var bx_help_tour_steps_filtered = [];
     for (i in bx_help_tour_steps) {
-        //do not show help items for missing elements.
-        if (bx_help_tour_steps[i].attachTo.element.length && !$(bx_help_tour_steps[i].attachTo.element).length) continue;
+        if (typeof bx_help_tour_steps[i].attachTo !== 'undefined' && bx_help_tour_steps[i].attachTo.element.length) {
+            //do not show help items for missing elements.
+            if (!$(bx_help_tour_steps[i].attachTo.element).length) continue;
 
-        // if this is a help for a field that is sitting inside a collapsed section then
-        // we are `uncollapsing` it on this page load
-        if (!$(bx_help_tour_steps[i].attachTo.element).is(':visible')) {
-            var sect = $(bx_help_tour_steps[i].attachTo.element).closest('.bx-form-section-hidden');
-            if (sect.length && sect.hasClass('bx-form-section-hidden')) {
-                $(sect).children('legend').trigger('click');
-                sect.removeClass('bx-form-collapsed');
-                sect.removeClass('bx-form-section-hidden');
+            // if this is a help for a field that is sitting inside a collapsed section then
+            // we are `uncollapsing` it on this page load
+            if (!$(bx_help_tour_steps[i].attachTo.element).is(':visible')) {
+                var sect = $(bx_help_tour_steps[i].attachTo.element).closest('.bx-form-section-hidden');
+                if (sect.length && sect.hasClass('bx-form-section-hidden')) {
+                    $(sect).children('legend').trigger('click');
+                    sect.removeClass('bx-form-collapsed');
+                    sect.removeClass('bx-form-section-hidden');
+                }
             }
-        }
 
-        //do not process hidden elements
-        if (!$(bx_help_tour_steps[i].attachTo.element).is(':visible')) continue;
+            //do not process hidden elements
+            if (!$(bx_help_tour_steps[i].attachTo.element).is(':visible')) continue;
+        }
 
         bx_help_tour_steps_filtered.push(bx_help_tour_steps[i]);
     }

+ 4 - 0
modules/boonex/russian/data/langs/bx_help_tours/ru.xml

@@ -49,4 +49,8 @@
 	<string name="_bx_help_tours_form_field_option_custom"><![CDATA[Пользовательский]]></string>
 
 	<string name="_bx_help_tours_page_block_caption"><![CDATA[Тур Помощи]]></string>
+	<string name="_bx_help_tours_form_field_visible_for"><![CDATA[Видимо для]]></string>
+	<string name="_bx_help_tours_form_field_visible_for_levels"><![CDATA[Выбрать уровни]]></string>
+	<string name="_bx_help_tours_form_field_visible_for_all"><![CDATA[Всех]]></string>
+	<string name="_bx_help_tours_form_field_visible_for_selected"><![CDATA[Выбранные уровни]]></string>
 </resources>