Merge pull request #186 from tractorcow/pulls/3.2-compat-on-2.x

Compatibility with 3.2
This commit is contained in:
Ingo Schommer 2015-02-23 21:57:32 +13:00
commit c741a0bdf9
4 changed files with 46 additions and 16 deletions

View File

@ -8,6 +8,11 @@ php:
env:
- DB=MYSQL CORE_RELEASE=3.1
matrix:
include:
- php: 5.4
env: DB=MYSQL CORE_RELEASE=3
before_script:
- pear -q install --onlyreqdeps pear/PHP_CodeSniffer
- phpenv rehash
@ -16,5 +21,5 @@ before_script:
- cd ~/builds/ss
script:
- phpunit translatable/tests/
- vendor/bin/phpunit translatable/tests/
- phpcs --encoding=utf-8 --tab-width=4 --standard=translatable/tests/phpcs -np translatable

View File

@ -14,7 +14,6 @@ Note: This module was originally part of the SilverStripe CMS 2.x codebase.
## Requirements ##
* SilverStripe Framework 3.1+ and CMS 3.1+
* Note: For SilverStripe 2.3/2.4 support, please use the core built-in version (no module required)
## Maintainers ##

View File

@ -582,6 +582,30 @@ class Translatable extends DataExtension implements PermissionProvider {
return $config;
}
/**
* Check if a given SQLQuery filters on the Locale field
*
* @param SQLQuery $query
* @return boolean
*/
protected function filtersOnLocale($query) {
foreach($query->getWhere() as $condition) {
// Compat for 3.1/3.2 where syntax
if(is_array($condition)) {
// In >=3.2 each $condition is a single length array('condition' => array('params'))
reset($condition);
$condition = key($condition);
}
// >=3.2 allows conditions to be expressed as evaluatable objects
if(interface_exists('SQLConditionGroup') && ($condition instanceof SQLConditionGroup)) {
$condition = $condition->conditionSQL($params);
}
if(preg_match('/("|\'|`)Locale("|\'|`)/', $condition)) return true;
}
}
/**
* Changes any SELECT query thats not filtering on an ID
* to limit by the current language defined in {@link get_current_locale()}.
@ -590,7 +614,7 @@ class Translatable extends DataExtension implements PermissionProvider {
*
* Use {@link disable_locale_filter()} to temporarily disable this "auto-filtering".
*/
function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) {
public function augmentSQL(SQLQuery &$query, DataQuery $dataQuery = null) {
// If the record is saved (and not a singleton), and has a locale,
// limit the current call to its locale. This fixes a lot of problems
// with other extensions like Versioned
@ -614,7 +638,7 @@ class Translatable extends DataExtension implements PermissionProvider {
&& array_search($baseTable, array_keys($query->getFrom())) !== false
// or we're already filtering by Lang (either from an earlier augmentSQL()
// call or through custom SQL filters)
&& !preg_match('/("|\'|`)Locale("|\'|`)/', implode(' ', $query->getWhere()))
&& !$this->filtersOnLocale($query)
//&& !$query->filtersOnFK()
) {
$qry = sprintf('"%s"."Locale" = \'%s\'', $baseTable, Convert::raw2sql($locale));
@ -1435,6 +1459,7 @@ class Translatable extends DataExtension implements PermissionProvider {
$newTranslation->ID = 0;
$newTranslation->Locale = $locale;
$newTranslation->Version = 0;
$originalPage = $this->getTranslation(self::default_locale());
if ($originalPage) {

View File

@ -21,7 +21,8 @@
"require-dev": {
"silverstripe/postgresql": "*",
"silverstripe/sqlite3": "*",
"silverstripe/mssql": "*"
"silverstripe/mssql": "*",
"phpunit/PHPUnit": "~3.7@stable"
},
"extra":
{