You are here

public function PopupMessageStatus::check in Popup message 8

Check and send popup status to js.

Parameters

\Symfony\Component\HttpFoundation\Request $request: Request.

Return value

\Symfony\Component\HttpFoundation\Response Popup status in json.

1 string reference to 'PopupMessageStatus::check'
popup_message.routing.yml in ./popup_message.routing.yml
popup_message.routing.yml

File

src/Controller/PopupMessageStatus.php, line 66

Class

PopupMessageStatus
Class PopupMessageStatus.

Namespace

Drupal\popup_message\Controller

Code

public function check(Request $request) {
  $config = $this->configFactory
    ->get('popup_message.settings');

  // Get popup message visibility settings.
  $visibility = $config
    ->get('visibility') ? $config
    ->get('visibility') : 0;

  // Get popup message visibility pages settings.
  $visibility_pages = $config
    ->get('visibility_pages') ? $config
    ->get('visibility_pages') : '';

  // Predefine value.
  $page_match = TRUE;

  // Limited visibility popup message must list at least one page.
  $status = TRUE;
  if ($visibility == 1 && empty($visibility_pages)) {
    $status = FALSE;
  }

  // Match path if necessary.
  if ($visibility_pages && $status) {

    // Convert path to lowercase. This allows comparison of the same path
    // with different case. Ex: /Page, /page, /PAGE.
    $real_path = $request
      ->get('popup_path');
    if ($real_path == '/') {
      $real_path = $this->configFactory
        ->get('system.site')
        ->get('page.front');
    }
    else {
      $real_path = substr($real_path, 1);
    }
    $pages = mb_strtolower($visibility_pages);
    if ($visibility < 2) {

      // Convert the Drupal path to lowercase.
      $path = mb_strtolower($real_path);

      // Compare the lowercase internal and lowercase path alias (if any).
      $page_match = $this->pathMatcher
        ->matchPath($path, $pages);
      $page_match = !($visibility xor $page_match);
    }
    else {
      $page_match = FALSE;
    }
  }
  $show_popup = (int) $page_match;
  $response = new Response();
  $response
    ->setContent(json_encode([
    'status' => $show_popup,
  ]));
  $response->headers
    ->set('Content-Type', 'application/json');
  return $response;
}