View source
<?php
namespace Drupal\splashify\Service;
use Drupal\Component\Utility\Unicode;
use Drupal\splashify\Entity\SplashifyEntity;
use Drupal\splashify\Entity\SplashifyGroupEntity;
use Symfony\Component\HttpFoundation\Request;
class SplashifyInjection {
protected $splash = NULL;
protected $timeLine = [
'once' => 31536000,
'daily' => 86400,
'weekly' => 604800,
'always' => 0,
];
public function isSplashExist(Request $request = NULL) {
if (is_object($request) && !$request
->hasSession()) {
return TRUE;
}
if (!empty($this->splash)) {
return TRUE;
}
$this->splash = $this
->findSplash();
return !empty($this->splash);
}
private function getPageGroups() {
$groups = SplashifyGroupEntity::loadMultiple();
$page_groups = [];
foreach ($groups as $group) {
if ($this
->checkRole($group) && $this
->checkWhere($group)) {
$page_groups[$group
->id()] = $group;
}
}
return $page_groups;
}
private function getSplashes() {
$page_groups = $this
->getPageGroups();
$page_groups_id = array_keys($page_groups);
if (empty($page_groups_id)) {
return [];
}
$splashes_id = \Drupal::entityQuery('splashify_entity')
->condition('status', 1)
->sort('field_weight', 'DESC')
->sort('id', 'DESC')
->condition('field_group', $page_groups_id, 'IN')
->execute();
if (empty($splashes_id)) {
return [];
}
$splashes = SplashifyEntity::loadMultiple($splashes_id);
return $splashes;
}
private function findSplash() {
$splashes = $this
->getSplashes();
foreach ($splashes as $splash) {
if ($this
->checkFrequency($splash)) {
return $splash;
}
}
return NULL;
}
private function getRenderElement($splash) {
$build = [];
$mode = $splash
->getGroup()
->getMode();
switch ($mode) {
case 'redirect':
$build = [
'#attached' => [
'drupalSettings' => [
'splashify' => [
'mode' => 'redirect',
'url' => '/splashify/' . $splash
->id(),
],
],
'library' => [
'splashify/redirect',
],
],
];
break;
case 'window':
$size = explode('x', $splash
->getGroup()
->getSize());
$width = is_numeric($size[0]) ? $size[0] : 800;
$height = is_numeric($size[1]) ? $size[1] : 600;
$build = [
'#attached' => [
'drupalSettings' => [
'splashify' => [
'mode' => 'window',
'url' => '/splashify/' . $splash
->id(),
'size' => "width={$width}, height={$height}",
],
],
'library' => [
'splashify/window',
],
],
];
break;
case 'full_screen':
$build = [
'#theme' => 'splashify',
'#splashify_content' => $splash
->getContent(),
'#attached' => [
'drupalSettings' => [
'splashify' => [
'mode' => 'full_screen',
],
],
'library' => [
'splashify/full_screen',
],
],
];
break;
case 'lightbox':
$size = explode('x', $splash
->getGroup()
->getSize());
\Drupal::service('colorbox.attachment')
->attach($build);
$build['#attached']['drupalSettings']['splashify'] = [
'mode' => 'lightbox',
'url' => '/splashify/' . $splash
->id(),
'width' => is_numeric($size[0]) ? $size[0] : 800,
'height' => is_numeric($size[1]) ? $size[1] : 600,
];
$build['#attached']['library'][] = 'splashify/lightbox';
break;
}
$build['#cache']['max-age'] = 0;
$build['#attached']['drupalSettings']['splashify']['refferer_check'] = !$splash
->getGroup()
->isDisableReferrerCheck();
array_unshift($build['#attached']['library'], 'splashify/splash_init');
return $build;
}
public function getAttach() {
if (!$this
->isSplashExist()) {
return [];
}
setcookie("splashify[" . $this->splash
->id() . "]", REQUEST_TIME, NULL, '/');
return $this
->getRenderElement($this->splash);
}
private function checkRole($group) {
$account = \Drupal::currentUser()
->getAccount();
if ($group
->isRestrictRoles()) {
$account_roles = $account
->getRoles();
$group_roles = $group
->getRoles();
return !empty(array_intersect($account_roles, $group_roles));
}
return TRUE;
}
private function checkWhere($group) {
$where = $group
->getWhere();
switch ($where) {
case 'all':
return TRUE;
case 'home':
$is_front = \Drupal::service('path.matcher')
->isFrontPage();
return $group
->isOpposite() ? !$is_front : $is_front;
case 'list':
$pages = Unicode::strtolower($group
->getListPages());
$path = \Drupal::service('path.current')
->getPath();
$path = $path === '/' ? $path : rtrim($path, '/');
$path_alias = Unicode::strtolower(\Drupal::service('path.alias_manager')
->getAliasByPath($path));
$path_matcher = \Drupal::service('path.matcher');
$is_match = $path_matcher
->matchPath($path_alias, $pages) || $path != $path_alias && $path_matcher
->matchPath($path, $pages);
return $group
->isOpposite() ? !$is_match : $is_match;
}
return TRUE;
}
private function checkFrequency($splash) {
$frequency = $splash
->getGroup()
->getOften();
if ($frequency == 'never') {
return FALSE;
}
$cookie = \Drupal::request()->cookies;
$splashify_cookies = $cookie
->get('splashify');
if (!is_array($splashify_cookies) || !array_key_exists($splash
->id(), $splashify_cookies)) {
return TRUE;
}
$expired_time = REQUEST_TIME - $splashify_cookies[$splash
->id()];
return $expired_time >= $this->timeLine[$frequency];
}
}