'PageDomainRepository', 'page_region' => "PageRegionRepository", 'page_render' => "PageRenderRepository" ]; foreach($tables as $table=>$migration){ $exists = $this->checkTableExists($table); if(!$exists){ $namespace = "Drupal\\funpodium_core\\Repository\\"; $className = $namespace.$migration; $repo = new $className(); $repo->migrationTable(); // $repo->seeder(); if($table == 'page_render'){ $new = true; } } if($new){ $this->oldMigrations(); } } } private function checkTableExists($tName){ return Database::getConnection()->schema()->tableExists($tName); } private function setCodes($queryObj){ $session = \Drupal::request()->getSession(); if($session->get('affiliate') == ""){ if($queryObj->affiliate_code != null){ $session->set('affiliate',$queryObj->affiliate_code); } } $trackCodes = json_decode($queryObj->track_code, true); if(is_array($trackCodes)){ if(count($trackCodes) > 0){ if($session->get('ga') == ""){ $session->set('ga',$trackCodes['ga']); } if($session->get('piwik') == ""){ $session->set('piwik',$trackCodes['piwik']); } if($session->get('la51') == ""){ $session->set('la51',$trackCodes['la51']); } } } // if($GLOBALS['qrpCodes']['affiliate'] == ""){ // if($queryObj->affiliate_code != null){ // $GLOBALS['qrpCodes']['affiliate'] = $queryObj->affiliate_code; // } // } // $trackCodes = json_decode($queryObj->track_code, true); // if(is_array($trackCodes)){ // if(count($trackCodes) > 0){ // if($GLOBALS['qrpCodes']['ga'] == ""){ // $GLOBALS['qrpCodes']['ga'] = $trackCodes['ga']; // } // if($GLOBALS['qrpCodes']['piwik'] == ""){ // $GLOBALS['qrpCodes']['piwik'] = $trackCodes['piwik']; // } // if($GLOBALS['qrpCodes']['la51'] == ""){ // $GLOBALS['qrpCodes']['la51'] = $trackCodes['la51']; // } // } // } } private function setHomepage($queryObj){ // $config = \Drupal::configFactory()->getEditable('system.site'); // $homepage = $queryObj->homepage; // if (!empty(\Drupal::entityTypeManager()->getStorage('node')->load($homepage))) { // $config->set('page.front', "/node/".\Drupal::entityTypeManager()->getStorage('node')->load($homepage)->get("nid")->getString())->save(); // } //$_SESSION['homepage_node'] = \Drupal::entityTypeManager()->getStorage('node')->load($queryObj->homepage); $session = \Drupal::request()->getSession(); $session->set('homepage_node', \Drupal::entityTypeManager()->getStorage('node')->load($queryObj->homepage)); $session->set('page_type', "QRP"); } // private function initCodes(){ // $GLOBALS['qrpCodes']['affiliate'] = ""; // $GLOBALS['qrpCodes']['ga'] = ""; // $GLOBALS['qrpCodes']['piwik'] = ""; // $GLOBALS['qrpCodes']['la51'] = ""; // } public function mapDomain(){ //$this->initCodes(); $session = \Drupal::request()->getSession(); $session->remove('homepage_node'); $session->remove('affiliate'); $session->remove('ga'); $session->remove('piwik'); $session->remove('la51'); $session->remove('page_type'); try{ $IS_USE_NEW_CODE = isset($_GET['use_new_code_202104']); if ($IS_USE_NEW_CODE) { $DEFINED_SPECIAL_FOLDERS = ['euro-2021','admin','login','node_view']; $languages = array_keys($languages = \Drupal::service('language_manager')->getLanguages()); $host = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : ''; $uri = strtok( $_SERVER["REQUEST_URI"], "?"); $uri_without_final_slash = substr($uri, -1) === "/" ? substr($uri, 0, -1) : $uri; $langcode_regexp = array_map(function($l){ return '\/'.$l; }, $languages); $langcode_regexp = implode('|', $langcode_regexp); $is_special_uri = array_reduce($DEFINED_SPECIAL_FOLDERS, function ($b, $folder) use ($langcode_regexp, $uri_without_final_slash) { // Tip from https://stackoverflow.com/questions/39875691/ $re = '/^(' . $langcode_regexp . ')?\/' . $folder . '/'; return $b || preg_match($re, $uri_without_final_slash . '/'); }); if ($is_special_uri) { return; } $HomepageSettingRepository = new PageDomainRepository; $Obj = $HomepageSettingRepository->getByDomainUsingRegexp('https://' . $host . $uri , $languages); if (!$Obj){ $Obj = $HomepageSettingRepository->getByDomainUsingRegexp($uri , $languages); } if (!$Obj) { return; } $this->setCodes($Obj); $this->setHomepage($Obj); } else { // $IS_USE_NEW_CODE else $hostName = str_replace("www.", "", $_SERVER['HTTP_HOST']); $uri = strtok( $_SERVER["REQUEST_URI"], "?"); $language = explode('/', $uri)[1]; $HomepageSettingRepository = new PageDomainRepository; $setHomepageFlag = 0; $special = 0; $specialCheck = explode('/', $uri); $DEFINED_SPECIAL_FOLDERS = ['euro-2021','admin','login','node_view']; foreach($DEFINED_SPECIAL_FOLDERS as $SPECIAL_FOLDER){ if (in_array($SPECIAL_FOLDER, $specialCheck)) { // Success! $special = 1; break; } } $languagesIndex = array("zh-hans","vi","th"); if(!in_array($language,$languagesIndex)){ $uri = '/' . \Drupal::languageManager()->getCurrentLanguage()->getId() . $uri; } $uri = substr($uri,-1) == "/" ? substr($uri,0,-1):$uri; //get full path $fullPathObj = $HomepageSettingRepository->getByDomain($hostName.$uri)->fetch(); $uriObj = $HomepageSettingRepository->getByDomain($uri)->fetch(); $domainObj = $HomepageSettingRepository->getByDomain($hostName)->fetch(); if(!$domainObj){ $domainObj = $HomepageSettingRepository->getByDomain($hostName."/")->fetch(); } $elseObj = $HomepageSettingRepository->getByDomain("/")->fetch(); $languages = array_keys($languages = \Drupal::service('language_manager')->getLanguages()); if(in_array($language, $languages)){ $uri = str_replace('/' . $language, '', $uri); } if($uri != "/"){ if(gettype($fullPathObj) == "object"){ $this->setCodes($fullPathObj); //if Full Path detected then we just need it as a homepage $this->setHomepage($fullPathObj); $setHomepageFlag = 1; } if(gettype($uriObj) == "object"){ $this->setCodes($uriObj); } } //This is to check if the setHomepage hasn't set then we will make homepage (if any) as a homepage $IS_NOT_SPECIAL_FOLDER = $special == 0; $IS_NOT_SET_HOMEPAGE = $setHomepageFlag == 0; if(gettype($domainObj) == "object" && $IS_NOT_SET_HOMEPAGE && $IS_NOT_SPECIAL_FOLDER){ $this->setCodes($domainObj); if($uri != "/"){ $this->setHomepage($domainObj); } } if(gettype($elseObj) == "object"){ $this->setCodes($elseObj); if(gettype($domainObj) != "object" && $uri != "/"){ $this->setHomepage($elseObj); } } } // end of $IS_USE_NEW_CODE if-else }catch (Exception $e){ } // end of try-catch } // end of mapDomain function public function kernel_request($event){ if($event->getRequestType() == 1){ $this->mapDomain(); } } /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events['kernel.request'] = ['kernel_request', '35']; return $events; } public static function getHttpHost(){ return self::$httpHost; } /* SOME FUNCTIONS THAT WILL SUPPORT TO CREATE SOME MIGRATIONS AT THE FIRST TIME WHERE THE TABLE CREATED */ public function oldMigrations(){ $page_render_id = []; //GET DATA FROM LANDING PAGE KIND $connection = \Drupal::database(); $sql = sprintf("SELECT name FROM landing_page_kind WHERE name != 'example1' "); $query = $connection->query($sql); $pages = $query->fetchAll(); $pagesNames = []; foreach ($pages as $key => $value) { $pagesNames[] = $value->name; } //GET DATA FROM LANDING PAGE SETTING $LandingPageSettingRepository = new LandingPageSettingRepository; $LandingPageList = $LandingPageSettingRepository->all()->fetchAll(); $LandingPageList = json_decode($LandingPageSettingRepository->paserSQLResultsToObject($LandingPageList, false)); $idx=0; foreach($LandingPageList as $key => $value){ if($key == $pagesNames[$idx]){ db_insert('page_render')->fields([ 'nid' => \Drupal::entityManager()->loadEntityByUuid('node', $value->landing_page_id->parameter_value)->id(), 'name' => $key, 'style' => $value->landing_page_style->parameter_value, 'created_uid' => 1, 'updated_uid' => 1, 'created_time' => REQUEST_TIME, 'updated_time' => REQUEST_TIME, ])->execute(); $sql = sprintf("SELECT id FROM page_render WHERE name = '$key' "); $query = $connection->query($sql); $pages = $query->fetchAll(); $tempId = ""; foreach ($pages as $pagesKey => $pagesValue) { $tempId = $pagesValue->id; } //$tempId; //HOMEPAGE SETTINGS $uuid = $value->landing_page_id->parameter_value; $sql = sprintf("SELECT domain,aff_code FROM homepage_setting WHERE homepage = '$uuid' "); $query = $connection->query($sql); $pages = $query->fetchAll(); foreach ($pages as $pagesKey => $pagesValue) { db_insert('page_domain')->fields([ 'page_id' => $tempId, 'domain' => $pagesValue->domain, 'affiliate_code' => $pagesValue->aff_code, 'track_code' => '', 'created_uid' => 1, 'updated_uid' => 1, 'created_time' => REQUEST_TIME, 'updated_time' => REQUEST_TIME, ])->execute(); } // PAGE REGION //Styling $themeName = \Drupal::config('system.theme')->get('default'); $themeNameDir = dirname(drupal_get_filename('theme', $themeName)); $styleFilePath = '/templates/static/initial.json'; $stylePath = DRUPAL_ROOT . '/' . $themeNameDir . '/' . $styleFilePath; $components = json_decode(file_get_contents($stylePath), true); foreach ($components[$uuid] as $val) { //$usedComponents[$val['nid']] = $val['region']; db_insert('page_region')->fields([ 'page_id' => $tempId, 'nid' => $val['nid'], 'region' => $val['region'], 'created_uid' => 1, 'updated_uid' => 1, 'created_time' => REQUEST_TIME, 'updated_time' => REQUEST_TIME, ])->execute(); } if($key != "example1"){ $idx++; } } } return true; } }