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 { |
34f3bbd9 | 38 | |
6550386a EM |
39 | /** |
40 | * @return array | |
41 | */ | |
bace5cd9 | 42 | public static function getSubscribedEvents() { |
c64f69d9 CW |
43 | return [ |
44 | Events::PREPARE => ['onApiPrepare', Events::W_MIDDLE], | |
45 | ]; | |
bace5cd9 TO |
46 | } |
47 | ||
6550386a | 48 | /** |
3e20c1ac CW |
49 | * Support multi-lingual requests |
50 | * | |
6550386a | 51 | * @param \Civi\API\Event\Event $event |
8882ff5c | 52 | * API preparation event. |
6550386a EM |
53 | * |
54 | * @throws \API_Exception | |
55 | */ | |
bace5cd9 TO |
56 | public function onApiPrepare(\Civi\API\Event\Event $event) { |
57 | $apiRequest = $event->getApiRequest(); | |
58 | ||
3e20c1ac CW |
59 | $params = $apiRequest['params']; |
60 | if ($apiRequest['version'] < 4) { | |
61 | $language = !empty($params['options']['language']) ? $params['options']['language'] : \CRM_Utils_Array::value('option.language', $params); | |
62 | } | |
63 | else { | |
64 | $language = \CRM_Utils_Array::value('language', $params); | |
65 | } | |
66 | if ($language) { | |
bace5cd9 TO |
67 | $this->setLocale($language); |
68 | } | |
69 | } | |
70 | ||
71 | /** | |
72 | * Sets the tsLocale and dbLocale for multi-lingual sites. | |
73 | * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() | |
74 | * to avoid regressions from refactoring. | |
257e7666 EM |
75 | * @param $lcMessagesRequest |
76 | * @throws \API_Exception | |
bace5cd9 TO |
77 | */ |
78 | public function setLocale($lcMessagesRequest) { | |
79 | // We must validate whether the locale is valid, otherwise setting a bad | |
80 | // dbLocale could probably lead to sql-injection. | |
81 | $domain = new \CRM_Core_DAO_Domain(); | |
82 | $domain->id = \CRM_Core_Config::domainID(); | |
83 | $domain->find(TRUE); | |
84 | ||
56f96145 J |
85 | // are we in a multi-language setup? |
86 | $multiLang = $domain->locales ? TRUE : FALSE; | |
87 | $lcMessages = NULL; | |
bace5cd9 | 88 | |
56f96145 J |
89 | // on multi-lang sites based on request and civicrm_uf_match |
90 | if ($multiLang) { | |
91 | $config = \CRM_Core_Config::singleton(); | |
c64f69d9 | 92 | $languageLimit = []; |
56f96145 J |
93 | if (isset($config->languageLimit) and $config->languageLimit) { |
94 | $languageLimit = $config->languageLimit; | |
95 | } | |
bace5cd9 | 96 | |
56f96145 J |
97 | if (in_array($lcMessagesRequest, array_keys($languageLimit))) { |
98 | $lcMessages = $lcMessagesRequest; | |
99 | } | |
100 | else { | |
c64f69d9 | 101 | throw new \API_Exception(ts('Language not enabled: %1', [1 => $lcMessagesRequest])); |
bace5cd9 | 102 | } |
56f96145 | 103 | } |
bace5cd9 | 104 | |
56f96145 | 105 | global $dbLocale; |
bace5cd9 | 106 | |
56f96145 J |
107 | // set suffix for table names - use views if more than one language |
108 | if ($lcMessages) { | |
109 | $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : ''; | |
bace5cd9 | 110 | |
56f96145 J |
111 | // FIXME: an ugly hack to fix CRM-4041 |
112 | global $tsLocale; | |
113 | $tsLocale = $lcMessages; | |
bace5cd9 TO |
114 | } |
115 | } | |
96025800 | 116 | |
bace5cd9 | 117 | } |