Created
August 4, 2015 01:23
-
-
Save phptek/0c8448bd869122b6a0ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* In order to tell whether any Fluent-enabled (FLuent augmented) ORM queries | |
* exist on the current object and whether or not they have data, we need to | |
* hook into all ORM queries made on each page. The result of which can be cached | |
* somewhere and consumed accordingly. | |
* | |
* Note: Doesn't augment anything. We're using augmentSQL() as an "entry-point". | |
* | |
* @param SQLQuery $query | |
* @return void | |
*/ | |
public function augmentSQL(SQLQuery &$query) { | |
// Don't run this logic in the CMS | |
if(!Fluent::is_frontend()) { | |
return; | |
} | |
// Get the various query components and for each query, reduce down to a single | |
// limitable query based on the currently selected | |
$locale = Fluent::current_locale(); | |
// Build a "reduced" array of selected fields based only off the current locale | |
$sqlSelect = array_keys($query->getSelect()); | |
$augmentedFields = array_filter($sqlSelect, function($field) use($locale) { | |
// Ignore current-locale's fields and FluentExtension-specific fields | |
if(strstr($field, $locale) !== false && $field !== 'LocaleFilter_' . $locale) { | |
return true; | |
} | |
}); | |
if(!$augmentedFields) { | |
return; | |
} | |
$hasAugmentedFields = count($augmentedFields); | |
if($hasAugmentedFields) { | |
// Don't mess with the original augmented query | |
$newQuery = new SQLQuery(); | |
$newQuery->setSelect($augmentedFields); | |
$newQuery->setFrom($query->getFrom()); | |
$newQuery->setWhere($query->getWhere()); | |
if(!$result = $newQuery->execute()) { | |
return; | |
} | |
while($row = $result->next()) { | |
if(!$row) { | |
continue; | |
} | |
$data = array_values($row); | |
foreach($data as $val) { | |
if(!empty($val)) { | |
Session::set('localeAugmented', $locale); | |
} | |
} | |
} | |
// Clean up | |
unset($newQuery); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment