Commit | Line | Data |
---|---|---|
bace5cd9 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
41498ac5 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
bace5cd9 | 5 | | | |
41498ac5 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
bace5cd9 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
bace5cd9 TO |
11 | |
12 | namespace Civi\API\Subscriber; | |
8882ff5c | 13 | |
bace5cd9 TO |
14 | use Civi\API\Events; |
15 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
16 | ||
6550386a EM |
17 | /** |
18 | * Class I18nSubscriber | |
19 | * @package Civi\API\Subscriber | |
20 | */ | |
bace5cd9 | 21 | class I18nSubscriber implements EventSubscriberInterface { |
34f3bbd9 | 22 | |
40557979 CW |
23 | /** |
24 | * Used for rolling back language to its original setting after the api call. | |
25 | * | |
26 | * @var array | |
27 | */ | |
28 | public $originalLang = []; | |
29 | ||
6550386a EM |
30 | /** |
31 | * @return array | |
32 | */ | |
bace5cd9 | 33 | public static function getSubscribedEvents() { |
c64f69d9 CW |
34 | return [ |
35 | Events::PREPARE => ['onApiPrepare', Events::W_MIDDLE], | |
40557979 | 36 | Events::RESPOND => ['onApiRespond', Events::W_LATE], |
c64f69d9 | 37 | ]; |
bace5cd9 TO |
38 | } |
39 | ||
6550386a | 40 | /** |
3e20c1ac CW |
41 | * Support multi-lingual requests |
42 | * | |
6550386a | 43 | * @param \Civi\API\Event\Event $event |
8882ff5c | 44 | * API preparation event. |
6550386a EM |
45 | * |
46 | * @throws \API_Exception | |
47 | */ | |
bace5cd9 TO |
48 | public function onApiPrepare(\Civi\API\Event\Event $event) { |
49 | $apiRequest = $event->getApiRequest(); | |
50 | ||
3e20c1ac CW |
51 | $params = $apiRequest['params']; |
52 | if ($apiRequest['version'] < 4) { | |
9e10fb6b | 53 | $language = $params['options']['language'] ?? $params['option.language'] ?? NULL; |
3e20c1ac CW |
54 | } |
55 | else { | |
9e10fb6b | 56 | $language = $params['language'] ?? NULL; |
3e20c1ac CW |
57 | } |
58 | if ($language) { | |
40557979 CW |
59 | $this->setLocale($language, $apiRequest['id']); |
60 | } | |
61 | } | |
62 | ||
63 | /** | |
64 | * Reset language to the default. | |
65 | * | |
66 | * @param \Civi\API\Event\Event $event | |
67 | * | |
68 | * @throws \API_Exception | |
69 | */ | |
70 | public function onApiRespond(\Civi\API\Event\Event $event) { | |
71 | $apiRequest = $event->getApiRequest(); | |
72 | ||
73 | if (!empty($this->originalLang[$apiRequest['id']])) { | |
74 | global $tsLocale; | |
75 | global $dbLocale; | |
76 | $tsLocale = $this->originalLang[$apiRequest['id']]['tsLocale']; | |
77 | $dbLocale = $this->originalLang[$apiRequest['id']]['dbLocale']; | |
bace5cd9 TO |
78 | } |
79 | } | |
80 | ||
81 | /** | |
82 | * Sets the tsLocale and dbLocale for multi-lingual sites. | |
83 | * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() | |
84 | * to avoid regressions from refactoring. | |
cc493f5e | 85 | * @param string $lcMessages |
40557979 | 86 | * @param int $requestId |
257e7666 | 87 | * @throws \API_Exception |
bace5cd9 | 88 | */ |
cc493f5e | 89 | public function setLocale($lcMessages, $requestId) { |
bace5cd9 TO |
90 | $domain = new \CRM_Core_DAO_Domain(); |
91 | $domain->id = \CRM_Core_Config::domainID(); | |
92 | $domain->find(TRUE); | |
93 | ||
cc493f5e CW |
94 | // Check if the site is multi-lingual |
95 | if ($domain->locales && $lcMessages) { | |
96 | // Validate language, otherwise a bad dbLocale could probably lead to sql-injection. | |
97 | if (!array_key_exists($lcMessages, \Civi::settings()->get('languageLimit'))) { | |
98 | throw new \API_Exception(ts('Language not enabled: %1', [1 => $lcMessages])); | |
56f96145 | 99 | } |
bace5cd9 | 100 | |
40557979 | 101 | global $dbLocale; |
56f96145 | 102 | global $tsLocale; |
40557979 CW |
103 | |
104 | // Store original value to be restored in $this->onApiRespond | |
105 | $this->originalLang[$requestId] = [ | |
106 | 'tsLocale' => $tsLocale, | |
107 | 'dbLocale' => $dbLocale, | |
108 | ]; | |
109 | ||
110 | // Set suffix for table names - use views if more than one language | |
111 | $dbLocale = "_{$lcMessages}"; | |
112 | ||
113 | // Also set tsLocale - CRM-4041 | |
56f96145 | 114 | $tsLocale = $lcMessages; |
bace5cd9 TO |
115 | } |
116 | } | |
96025800 | 117 | |
bace5cd9 | 118 | } |