|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @package Joomla.Administrator |
| 5 | + * @subpackage com_guidedtours |
| 6 | + * |
| 7 | + * @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org> |
| 8 | + * @license GNU General Public License version 2 or later; see LICENSE.txt |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Joomla\Component\Guidedtours\Administrator\Controller; |
| 12 | + |
| 13 | +use Joomla\CMS\Event\AbstractEvent; |
| 14 | +use Joomla\CMS\Language\Text; |
| 15 | +use Joomla\CMS\MVC\Controller\BaseController; |
| 16 | +use Joomla\CMS\Plugin\PluginHelper; |
| 17 | +use Joomla\CMS\Response\JsonResponse; |
| 18 | + |
| 19 | +// phpcs:disable PSR1.Files.SideEffects |
| 20 | +\defined('_JEXEC') or die; |
| 21 | +// phpcs:enable PSR1.Files.SideEffects |
| 22 | + |
| 23 | +/** |
| 24 | + * The guided tours controller for ajax requests. |
| 25 | + * |
| 26 | + * @since __DEPLOY_VERSION__ |
| 27 | + */ |
| 28 | +class AjaxController extends BaseController |
| 29 | +{ |
| 30 | + /** |
| 31 | + * Ajax call used when cancelling, skipping or completing a tour. |
| 32 | + * It allows: |
| 33 | + * - the trigering of before and after events the user state is recorded |
| 34 | + * - the recording of the user behavior in the action logs |
| 35 | + */ |
| 36 | + public function fetchUserState() |
| 37 | + { |
| 38 | + $user = $this->app->getIdentity(); |
| 39 | + |
| 40 | + $tourId = $this->app->input->getInt('tid', 0); |
| 41 | + $stepNumber = $this->app->input->getString('sid', ''); |
| 42 | + $context = $this->app->input->getString('context', ''); |
| 43 | + |
| 44 | + if ($user != null && $user->id > 0) { |
| 45 | + $actionState = ''; |
| 46 | + |
| 47 | + switch ($context) { |
| 48 | + case 'tour.complete': |
| 49 | + $actionState = 'completed'; |
| 50 | + break; |
| 51 | + case 'tour.cancel': |
| 52 | + $actionState = 'delayed'; |
| 53 | + break; |
| 54 | + case 'tour.skip': |
| 55 | + $actionState = 'skipped'; |
| 56 | + break; |
| 57 | + } |
| 58 | + |
| 59 | + PluginHelper::importPlugin('guidedtours'); |
| 60 | + |
| 61 | + // event onBeforeTourSaveUserState before save user tour state |
| 62 | + $beforeEvent = AbstractEvent::create( |
| 63 | + 'onBeforeTourSaveUserState', |
| 64 | + [ |
| 65 | + 'subject' => new \stdClass(), |
| 66 | + 'tourId' => $tourId, |
| 67 | + 'actionState' => $actionState, |
| 68 | + 'stepNumber' => $stepNumber, |
| 69 | + ] |
| 70 | + ); |
| 71 | + |
| 72 | + $this->app->getDispatcher()->dispatch('onBeforeTourSaveUserState', $beforeEvent); |
| 73 | + |
| 74 | + // Save the tour state only when the tour auto-starts. |
| 75 | + $tourModel = $this->getModel('Tour', 'Administrator'); |
| 76 | + if ($tourModel->isAutostart($tourId)) { |
| 77 | + $result = $tourModel->saveTourUserState($tourId, $actionState); |
| 78 | + if ($result) { |
| 79 | + $message = Text::sprintf('COM_GUIDEDTOURS_USERSTATE_STATESAVED', $user->id, $tourId); |
| 80 | + } else { |
| 81 | + $message = Text::sprintf('COM_GUIDEDTOURS_USERSTATE_STATENOTSAVED', $user->id, $tourId); |
| 82 | + } |
| 83 | + } else { |
| 84 | + $result = false; |
| 85 | + $message = Text::sprintf('COM_GUIDEDTOURS_USERSTATE_STATENOTSAVED', $user->id, $tourId); |
| 86 | + } |
| 87 | + |
| 88 | + // event onAfterTourSaveUserState after save user tour state (may override message) |
| 89 | + $afterEvent = AbstractEvent::create( |
| 90 | + 'onAfterTourSaveUserState', |
| 91 | + [ |
| 92 | + 'subject' => new \stdClass(), |
| 93 | + 'tourId' => $tourId, |
| 94 | + 'actionState' => $actionState, |
| 95 | + 'stepNumber' => $stepNumber, |
| 96 | + 'result' => $result, |
| 97 | + 'message' => &$message, |
| 98 | + ] |
| 99 | + ); |
| 100 | + |
| 101 | + $this->app->getDispatcher()->dispatch('onAfterTourSaveUserState', $afterEvent); |
| 102 | + |
| 103 | + // Construct the response data |
| 104 | + $data = [ |
| 105 | + 'tourId' => $tourId, |
| 106 | + 'stepId' => $stepNumber, |
| 107 | + 'context' => $context, |
| 108 | + 'state' => $actionState, |
| 109 | + ]; |
| 110 | + echo new JsonResponse($data, $message); |
| 111 | + $this->app->close(); |
| 112 | + } else { |
| 113 | + // Construct the response data |
| 114 | + $data = [ |
| 115 | + 'success' => false, |
| 116 | + 'tourId' => $tourId, |
| 117 | + ]; |
| 118 | + |
| 119 | + $message = Text::_('COM_GUIDEDTOURS_USERSTATE_CONNECTEDONLY'); |
| 120 | + echo new JsonResponse($data, $message, true); |
| 121 | + $this->app->close(); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments