You are here

class SocialTourController in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  2. 8 modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  3. 8.2 modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  4. 8.3 modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  5. 8.4 modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  6. 8.5 modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  7. 8.6 modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  8. 8.7 modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  9. 8.8 modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  10. 10.3.x modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  11. 10.1.x modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController
  12. 10.2.x modules/custom/social_tour/src/SocialTourController.php \Drupal\social_tour\SocialTourController

Class SocialTourController.

Returns responses for Social Group routes.

@package Drupal\social_tour

Hierarchy

Expanded class hierarchy of SocialTourController

1 string reference to 'SocialTourController'
social_tour.services.yml in modules/custom/social_tour/social_tour.services.yml
modules/custom/social_tour/social_tour.services.yml
1 service uses SocialTourController
social_tour.onboarding in modules/custom/social_tour/social_tour.services.yml
Drupal\social_tour\SocialTourController

File

modules/custom/social_tour/src/SocialTourController.php, line 24

Namespace

Drupal\social_tour
View source
class SocialTourController extends ControllerBase {

  /**
   * The user data.
   *
   * @var \Drupal\user\UserDataInterface
   */
  protected $userData;

  /**
   * The path validator.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

  /**
   * The redirect destination helper.
   *
   * @var \Drupal\Core\Routing\RedirectDestinationInterface
   */
  protected $redirectDestination;

  /**
   * SocialTourController constructor.
   *
   * @param \Drupal\user\UserDataInterface $user_data
   *   The user data.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user service.
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   The path validator.
   * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
   *   The redirect destination helper.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(UserDataInterface $user_data, ConfigFactoryInterface $config_factory, AccountProxyInterface $current_user, PathValidatorInterface $path_validator, RedirectDestinationInterface $redirect_destination, EntityTypeManagerInterface $entity_type_manager) {

    // We needs it.
    $this->userData = $user_data;
    $this->configFactory = $config_factory
      ->get('social_tour.settings');
    $this->currentUser = $current_user;
    $this->pathValidator = $path_validator;
    $this->redirectDestination = $redirect_destination;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('user.data'), $container
      ->get('config.factory'), $container
      ->get('current_user'), $container
      ->get('path.validator'), $container
      ->get('redirect.destination'), $container
      ->get('entity_type.manager'));
  }

  /**
   * Check if onboarding is enabled.
   *
   * @return bool
   *   Returns either TRUE or FALSE.
   */
  public function onboardingEnabled() {

    // Check if tour is enabled by SM setting.
    if (!$this->configFactory
      ->get('social_tour_enabled')) {
      return FALSE;
    }

    // Check permissions.
    if (!$this->currentUser
      ->hasPermission('access tour')) {
      return FALSE;
    }

    // Check if current disabled it.
    if ($this->userData
      ->get('social_tour', $this->currentUser
      ->id(), 'onboarding_disabled')) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Toggle onboarding.
   *
   * @param array $account
   *   Array containing the account.
   */
  public function toggleOnboarding(array $account = NULL) {

    // No user given, then current user.
    $id = $this->currentUser
      ->id();
    if ($account instanceof UserInterface) {
      $id = $account
        ->id();
    }
    $new_value = TRUE;
    if ($this->userData
      ->get('social_tour', $id, 'onboarding_disabled')) {
      $new_value = FALSE;
    }

    // Save the value in the user_data.
    $this
      ->setData($new_value);
  }

  /**
   * Enable onboarding for current_user by Ajax call.
   */
  public function enableOnboarding() {

    // Save the value in the user_data.
    $this
      ->setData(FALSE);

    // Return 200.
    return new JsonResponse([
      'message' => $this
        ->t('Onboarding has been enabled.'),
    ], 200, [
      'Content-Type' => 'application/json',
    ]);
  }

  /**
   * Disable onboarding for current_user by Ajax call.
   */
  public function disableOnboarding() {

    // Save the value in the user_data.
    $this
      ->setData();
    $route_name = $this->pathValidator
      ->getUrlIfValid($this->redirectDestination
      ->get())
      ->getRouteName();
    Cache::invalidateTags($this
      ->getCacheTags($route_name));

    // Set a message that they can be turned on again.
    $this
      ->messenger()
      ->addStatus($this
      ->t('You will not see tips like this anymore.'));

    // Return to previous page.
    return $this
      ->redirect($route_name);
  }

  /**
   * Set onboarding data value.
   *
   * @param bool $disabled
   *   Type of bool, either TRUE or FALSE.
   */
  private function setData($disabled = TRUE) {
    $this->userData
      ->set('social_tour', $this->currentUser
      ->id(), 'onboarding_disabled', $disabled);
  }

  /**
   * Returns tags based on tours of page.
   *
   * @param string $route_name
   *   A route name.
   *
   * @return array
   *   The cache tags.
   */
  public function getCacheTags($route_name) {
    $tours = $this->entityTypeManager
      ->getStorage('tour')
      ->getQuery()
      ->condition('routes.*.route_name', $route_name)
      ->execute();
    return array_map(function ($tour) {
      return 'user:' . $this->currentUser
        ->id() . ':tour:' . $tour;
    }, $tours);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SocialTourController::$pathValidator protected property The path validator.
SocialTourController::$redirectDestination protected property The redirect destination helper. Overrides RedirectDestinationTrait::$redirectDestination
SocialTourController::$userData protected property The user data.
SocialTourController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
SocialTourController::disableOnboarding public function Disable onboarding for current_user by Ajax call.
SocialTourController::enableOnboarding public function Enable onboarding for current_user by Ajax call.
SocialTourController::getCacheTags public function Returns tags based on tours of page.
SocialTourController::onboardingEnabled public function Check if onboarding is enabled.
SocialTourController::setData private function Set onboarding data value.
SocialTourController::toggleOnboarding public function Toggle onboarding.
SocialTourController::__construct public function SocialTourController constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.