Commit | Line | Data |
---|---|---|
bace5cd9 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
bace5cd9 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
bace5cd9 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
bace5cd9 TO |
27 | |
28 | namespace Civi\API\Subscriber; | |
8882ff5c | 29 | |
bace5cd9 TO |
30 | use Civi\API\Events; |
31 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
32 | ||
6550386a EM |
33 | /** |
34 | * Class I18nSubscriber | |
35 | * @package Civi\API\Subscriber | |
36 | */ | |
bace5cd9 | 37 | class I18nSubscriber implements EventSubscriberInterface { |
6550386a EM |
38 | /** |
39 | * @return array | |
40 | */ | |
bace5cd9 | 41 | public static function getSubscribedEvents() { |
c64f69d9 CW |
42 | return [ |
43 | Events::PREPARE => ['onApiPrepare', Events::W_MIDDLE], | |
44 | ]; | |
bace5cd9 TO |
45 | } |
46 | ||
6550386a EM |
47 | /** |
48 | * @param \Civi\API\Event\Event $event | |
8882ff5c | 49 | * API preparation event. |
6550386a EM |
50 | * |
51 | * @throws \API_Exception | |
52 | */ | |
bace5cd9 TO |
53 | public function onApiPrepare(\Civi\API\Event\Event $event) { |
54 | $apiRequest = $event->getApiRequest(); | |
55 | ||
56 | // support multi-lingual requests | |
57 | if ($language = \CRM_Utils_Array::value('option.language', $apiRequest['params'])) { | |
58 | $this->setLocale($language); | |
59 | } | |
60 | } | |
61 | ||
62 | /** | |
63 | * Sets the tsLocale and dbLocale for multi-lingual sites. | |
64 | * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() | |
65 | * to avoid regressions from refactoring. | |
257e7666 EM |
66 | * @param $lcMessagesRequest |
67 | * @throws \API_Exception | |
bace5cd9 TO |
68 | */ |
69 | public function setLocale($lcMessagesRequest) { | |
70 | // We must validate whether the locale is valid, otherwise setting a bad | |
71 | // dbLocale could probably lead to sql-injection. | |
72 | $domain = new \CRM_Core_DAO_Domain(); | |
73 | $domain->id = \CRM_Core_Config::domainID(); | |
74 | $domain->find(TRUE); | |
75 | ||
56f96145 J |
76 | // are we in a multi-language setup? |
77 | $multiLang = $domain->locales ? TRUE : FALSE; | |
78 | $lcMessages = NULL; | |
bace5cd9 | 79 | |
56f96145 J |
80 | // on multi-lang sites based on request and civicrm_uf_match |
81 | if ($multiLang) { | |
82 | $config = \CRM_Core_Config::singleton(); | |
c64f69d9 | 83 | $languageLimit = []; |
56f96145 J |
84 | if (isset($config->languageLimit) and $config->languageLimit) { |
85 | $languageLimit = $config->languageLimit; | |
86 | } | |
bace5cd9 | 87 | |
56f96145 J |
88 | if (in_array($lcMessagesRequest, array_keys($languageLimit))) { |
89 | $lcMessages = $lcMessagesRequest; | |
90 | } | |
91 | else { | |
c64f69d9 | 92 | throw new \API_Exception(ts('Language not enabled: %1', [1 => $lcMessagesRequest])); |
bace5cd9 | 93 | } |
56f96145 | 94 | } |
bace5cd9 | 95 | |
56f96145 | 96 | global $dbLocale; |
bace5cd9 | 97 | |
56f96145 J |
98 | // set suffix for table names - use views if more than one language |
99 | if ($lcMessages) { | |
100 | $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : ''; | |
bace5cd9 | 101 | |
56f96145 J |
102 | // FIXME: an ugly hack to fix CRM-4041 |
103 | global $tsLocale; | |
104 | $tsLocale = $lcMessages; | |
bace5cd9 TO |
105 | } |
106 | } | |
96025800 | 107 | |
bace5cd9 | 108 | } |