diff --git a/CHANGELOG.md b/CHANGELOG.md index e933d564ed..5ce83bd201 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ xsendfile mapping depending on databoxes configuration. - bin/console xsendfile:configuration-dumper that dumps your virtual host configuration depending on Phraseanet configuration + - Phraseanet enabled languages is now configurable. * 3.7.13 (2013-07-04) diff --git a/config/configuration.sample.yml b/config/configuration.sample.yml index 1ca1a8e1cd..e060fdcd46 100644 --- a/config/configuration.sample.yml +++ b/config/configuration.sample.yml @@ -1,6 +1,7 @@ main: servername: 'http://local.phrasea/' maintenance: false + languages: [] database: host: 127.0.0.1 port: 3306 diff --git a/lib/Alchemy/Phrasea/Controller/Admin/Databox.php b/lib/Alchemy/Phrasea/Controller/Admin/Databox.php index d6c76e5517..88ce050ccd 100644 --- a/lib/Alchemy/Phrasea/Controller/Admin/Databox.php +++ b/lib/Alchemy/Phrasea/Controller/Admin/Databox.php @@ -446,7 +446,7 @@ class Databox implements ControllerProviderInterface public function getDatabaseCGU(Application $app, Request $request, $databox_id) { return $app['twig']->render('admin/databox/cgus.html.twig', array( - 'languages' => $app->getAvailableLanguages(), + 'languages' => $app['locales.available'], 'cgus' => $app['phraseanet.appbox']->get_databox($databox_id)->get_cgus(), 'current_locale' => $app['locale'] )); diff --git a/lib/Alchemy/Phrasea/Controller/Admin/Fields.php b/lib/Alchemy/Phrasea/Controller/Admin/Fields.php index 0202f3bc5e..c9a5073755 100644 --- a/lib/Alchemy/Phrasea/Controller/Admin/Fields.php +++ b/lib/Alchemy/Phrasea/Controller/Admin/Fields.php @@ -144,7 +144,7 @@ class Fields implements ControllerProviderInterface { $languages = array(); - foreach (PhraseaApplication::getAvailableLanguages() as $code => $language) { + foreach ($app['locales.available'] as $code => $language) { $data = explode('_', $code); $languages[$data[0]] = $language; } diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Root.php b/lib/Alchemy/Phrasea/Controller/Prod/Root.php index 336830d138..dc18b45bd6 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Root.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Root.php @@ -123,7 +123,7 @@ class Root implements ControllerProviderInterface 'thesau_js_list' => $thjslist, 'thesau_json_sbas' => json_encode($sbas), 'thesau_json_bas2sbas' => json_encode($bas2sbas), - 'thesau_languages' => $app->getAvailableLanguages(), + 'thesau_languages' => $app['locales.available'], )); })->bind('prod'); diff --git a/lib/Alchemy/Phrasea/Controller/Thesaurus/Thesaurus.php b/lib/Alchemy/Phrasea/Controller/Thesaurus/Thesaurus.php index ccb01a871f..9219bc7781 100644 --- a/lib/Alchemy/Phrasea/Controller/Thesaurus/Thesaurus.php +++ b/lib/Alchemy/Phrasea/Controller/Thesaurus/Thesaurus.php @@ -369,7 +369,7 @@ class Thesaurus implements ControllerProviderInterface $lng_code = explode('_', $code); return $lng_code[0]; - }, array_keys(PhraseaApplication::getAvailableLanguages())); + }, array_keys($app['locales.available'])); } else { $t_lng[] = $request->get('piv'); } @@ -792,7 +792,7 @@ class Thesaurus implements ControllerProviderInterface $bases[$row['sbas_id']] = \phrasea::sbas_labels($row['sbas_id'], $app); } - foreach (PhraseaApplication::getAvailableLanguages() as $lng_code => $lng) { + foreach ($app['locales.available'] as $lng_code => $lng) { $lng_code = explode('_', $lng_code); $languages[$lng_code[0]] = $lng; } @@ -1125,7 +1125,7 @@ class Thesaurus implements ControllerProviderInterface { $languages = array(); - foreach (PhraseaApplication::getAvailableLanguages() as $lng_code => $lng) { + foreach ($app['locales.available'] as $lng_code => $lng) { $lng_code = explode('_', $lng_code); $languages[$lng_code[0]] = $lng; } @@ -1202,7 +1202,7 @@ class Thesaurus implements ControllerProviderInterface ); } - foreach (PhraseaApplication::getAvailableLanguages() as $code => $language) { + foreach ($app['locales.available'] as $code => $language) { $lng_code = explode('_', $code); $languages[$lng_code[0]] = $language; } @@ -1229,7 +1229,7 @@ class Thesaurus implements ControllerProviderInterface { $flags = $jsFlags = array(); - foreach (PhraseaApplication::getAvailableLanguages() as $code => $language) { + foreach ($app['locales.available'] as $code => $language) { $lng_code = explode('_', $code); $flags[$lng_code[0]] = $language; $jsFlags[$lng_code[0]] = array('w' => 18, 'h' => 13); diff --git a/lib/Alchemy/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriber.php b/lib/Alchemy/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriber.php index 5e1b9d9433..4537fd4411 100644 --- a/lib/Alchemy/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriber.php +++ b/lib/Alchemy/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriber.php @@ -77,7 +77,7 @@ class PhraseaLocaleSubscriber implements EventSubscriberInterface ); } - $languages = PhraseaApplication::getAvailableLanguages(); + $languages = $this->app['locales.available']; if ($event->getRequest()->cookies->has('locale') && isset($languages[$event->getRequest()->cookies->get('locale')])) { $event->getRequest()->setLocale($event->getRequest()->cookies->get('locale')); diff --git a/lib/Alchemy/Phrasea/Core/Provider/LocaleServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/LocaleServiceProvider.php index 20710b1c1a..85e85cd4d2 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/LocaleServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/LocaleServiceProvider.php @@ -36,12 +36,38 @@ class LocaleServiceProvider implements ServiceProviderInterface }; $app['locales.available'] = function (Application $app) { - return PhraseaApplication::getAvailableLanguages(); + $availableLanguages = PhraseaApplication::getAvailableLanguages(); + + if (isset($app['phraseanet.configuration']['main']['languages']) && !empty($app['phraseanet.configuration']['main']['languages'])) { + $languages = $app['phraseanet.configuration']['main']['languages']; + $enabledLanguages = $availableLanguages; + + foreach ($enabledLanguages as $code => $language) { + if (in_array($code, $languages)) { + continue; + } + $data = explode('_', $code); + if (in_array($data[0], $languages)) { + continue; + } + unset($enabledLanguages[$code]); + } + + if (0 === count($enabledLanguages)) { + $app['monolog']->error('Wrong language configuration, no language activated'); + + return $availableLanguages; + } + + return $enabledLanguages; + } else { + return $availableLanguages; + } }; $app['locales.mapping'] = function (Application $app) { $codes = array(); - foreach (PhraseaApplication::getAvailableLanguages() as $code => $language) { + foreach ($app['locales.available'] as $code => $language) { $data = explode('_', $code); $codes[$data[0]] = $code; } @@ -52,7 +78,8 @@ class LocaleServiceProvider implements ServiceProviderInterface $app['locales.I18n.available'] = $app->share(function (Application $app) { $locales = array(); - foreach (PhraseaApplication::getAvailableLanguages() as $code => $language) { + var_dump($app['locales.available']);exit; + foreach ($app['locales.available'] as $code => $language) { $data = explode('_', $code); $locales[$data[0]] = $language; } diff --git a/lib/classes/User/Adapter.php b/lib/classes/User/Adapter.php index 4f00455adb..e7e10bc7b2 100644 --- a/lib/classes/User/Adapter.php +++ b/lib/classes/User/Adapter.php @@ -1671,7 +1671,7 @@ class User_Adapter implements User_Interface, cache_cacheableInterface public function set_locale($locale) { - if (!array_key_exists($locale, $this->app->getAvailableLanguages())) { + if (!array_key_exists($locale, $this->app['locales.available'])) { throw new \InvalidArgumentException(sprintf('Locale %s is not recognized', $locale)); } diff --git a/lib/classes/databox.php b/lib/classes/databox.php index be7249dcf0..54c6edfd2b 100644 --- a/lib/classes/databox.php +++ b/lib/classes/databox.php @@ -1380,7 +1380,7 @@ class databox extends base $missing_locale = array(); - $avLanguages = $this->app->getAvailableLanguages(); + $avLanguages = $this->app['locales.available']; foreach ($avLanguages as $code => $language) { if (!isset($TOU[$code])) { $missing_locale[] = $code; diff --git a/lib/conf.d/_GV_template.inc b/lib/conf.d/_GV_template.inc index 7f562dc2b2..4e666c9113 100644 --- a/lib/conf.d/_GV_template.inc +++ b/lib/conf.d/_GV_template.inc @@ -4,7 +4,7 @@ use Alchemy\Phrasea\Application; return call_user_func_array(function(Application $app) { - $avLanguages = Application::getAvailableLanguages(); + $avLanguages = $app['locales.available']; $youtube_console_url = 'https://code.google.com/apis/console/'; $dashboard_youtube = 'https://code.google.com/apis/youtube/dashboard/'; diff --git a/lib/conf.d/configuration.yml b/lib/conf.d/configuration.yml index fb88373dc2..21dc8d7c58 100644 --- a/lib/conf.d/configuration.yml +++ b/lib/conf.d/configuration.yml @@ -1,6 +1,7 @@ main: servername: 'http://local.phrasea/' maintenance: false + languages: [] database: host: 'sql-host' port: 3306 diff --git a/tests/Alchemy/Tests/Phrasea/Core/Provider/LocaleServiceProviderTest.php b/tests/Alchemy/Tests/Phrasea/Core/Provider/LocaleServiceProviderTest.php index 1083b99245..ae6a917119 100644 --- a/tests/Alchemy/Tests/Phrasea/Core/Provider/LocaleServiceProviderTest.php +++ b/tests/Alchemy/Tests/Phrasea/Core/Provider/LocaleServiceProviderTest.php @@ -18,6 +18,52 @@ class LocaleServiceProvidertest extends \PhraseanetPHPUnitAbstract $this->assertEquals(Application::getAvailableLanguages(), $app['locales.available']); } + public function testLocalesAvailableCustomized() + { + $app = new Application(); + $app->register(new LocaleServiceProvider()); + $app['phraseanet.configuration'] = $this->getMock('Alchemy\Phrasea\Core\Configuration\ConfigurationInterface'); + $app['phraseanet.configuration']->expects($this->any()) + ->method('offsetExist') + ->with('main') + ->will($this->returnValue(true)); + $app['phraseanet.configuration']->expects($this->any()) + ->method('offsetGet') + ->with('main') + ->will($this->returnValue(array('languages' => array('fr_FR', 'en_US', 'de')))); + + $original = Application::getAvailableLanguages(); + unset($original['en_GB']); + unset($original['nl_NL']); + + $this->assertEquals($original, $app['locales.available']); + } + + public function testLocalesCustomizedWithError() + { + $app = new Application(); + $app->register(new LocaleServiceProvider()); + $app['phraseanet.configuration'] = $this->getMock('Alchemy\Phrasea\Core\Configuration\ConfigurationInterface'); + $app['phraseanet.configuration']->expects($this->any()) + ->method('offsetExist') + ->with('main') + ->will($this->returnValue(true)); + $app['phraseanet.configuration']->expects($this->any()) + ->method('offsetGet') + ->with('main') + ->will($this->returnValue(array('languages' => array('en_US')))); + $app['monolog'] = $this->getMock('Psr\Log\LoggerInterface'); + $app['monolog']->expects($this->once()) + ->method('error'); + + $original = Application::getAvailableLanguages(); + unset($original['nl_NL']); + unset($original['fr_FR']); + unset($original['de_DE']); + + $this->assertEquals($original, $app['locales.available']); + } + public function testLocalesI18nAvailable() { $app = new Application(); diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineAbstractTest.php b/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineAbstractTest.php index 51ed3dd85c..442bd3e9c6 100644 --- a/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineAbstractTest.php +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineAbstractTest.php @@ -375,11 +375,7 @@ abstract class SearchEngineAbstractTest extends \PhraseanetPHPUnitAuthenticatedA 'nl' => array('word' => 'lichamelijk', 'stemm' => 'licham'), ); - foreach (Application::getAvailableLanguages() as $language => $name) { - - $codes = explode('_', $language); - $languageCode = $codes[0]; - + foreach (Application::getAvailableLanguages() as $languageCode) { if (!isset($examples[$languageCode])) { $this->fail(sprintf('Missing stemm examples for language %s', $languageCode)); }