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 | |
40557979 CW |
39 | /** |
40 | * Used for rolling back language to its original setting after the api call. | |
41 | * | |
42 | * @var array | |
43 | */ | |
44 | public $originalLang = []; | |
45 | ||
6550386a EM |
46 | /** |
47 | * @return array | |
48 | */ | |
bace5cd9 | 49 | public static function getSubscribedEvents() { |
c64f69d9 CW |
50 | return [ |
51 | Events::PREPARE => ['onApiPrepare', Events::W_MIDDLE], | |
40557979 | 52 | Events::RESPOND => ['onApiRespond', Events::W_LATE], |
c64f69d9 | 53 | ]; |
bace5cd9 TO |
54 | } |
55 | ||
6550386a | 56 | /** |
3e20c1ac CW |
57 | * Support multi-lingual requests |
58 | * | |
6550386a | 59 | * @param \Civi\API\Event\Event $event |
8882ff5c | 60 | * API preparation event. |
6550386a EM |
61 | * |
62 | * @throws \API_Exception | |
63 | */ | |
bace5cd9 TO |
64 | public function onApiPrepare(\Civi\API\Event\Event $event) { |
65 | $apiRequest = $event->getApiRequest(); | |
66 | ||
3e20c1ac CW |
67 | $params = $apiRequest['params']; |
68 | if ($apiRequest['version'] < 4) { | |
69 | $language = !empty($params['options']['language']) ? $params['options']['language'] : \CRM_Utils_Array::value('option.language', $params); | |
70 | } | |
71 | else { | |
72 | $language = \CRM_Utils_Array::value('language', $params); | |
73 | } | |
74 | if ($language) { | |
40557979 CW |
75 | $this->setLocale($language, $apiRequest['id']); |
76 | } | |
77 | } | |
78 | ||
79 | /** | |
80 | * Reset language to the default. | |
81 | * | |
82 | * @param \Civi\API\Event\Event $event | |
83 | * | |
84 | * @throws \API_Exception | |
85 | */ | |
86 | public function onApiRespond(\Civi\API\Event\Event $event) { | |
87 | $apiRequest = $event->getApiRequest(); | |
88 | ||
89 | if (!empty($this->originalLang[$apiRequest['id']])) { | |
90 | global $tsLocale; | |
91 | global $dbLocale; | |
92 | $tsLocale = $this->originalLang[$apiRequest['id']]['tsLocale']; | |
93 | $dbLocale = $this->originalLang[$apiRequest['id']]['dbLocale']; | |
bace5cd9 TO |
94 | } |
95 | } | |
96 | ||
97 | /** | |
98 | * Sets the tsLocale and dbLocale for multi-lingual sites. | |
99 | * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() | |
100 | * to avoid regressions from refactoring. | |
257e7666 | 101 | * @param $lcMessagesRequest |
40557979 | 102 | * @param int $requestId |
257e7666 | 103 | * @throws \API_Exception |
bace5cd9 | 104 | */ |
40557979 | 105 | public function setLocale($lcMessagesRequest, $requestId) { |
bace5cd9 TO |
106 | // We must validate whether the locale is valid, otherwise setting a bad |
107 | // dbLocale could probably lead to sql-injection. | |
108 | $domain = new \CRM_Core_DAO_Domain(); | |
109 | $domain->id = \CRM_Core_Config::domainID(); | |
110 | $domain->find(TRUE); | |
111 | ||
56f96145 J |
112 | // are we in a multi-language setup? |
113 | $multiLang = $domain->locales ? TRUE : FALSE; | |
114 | $lcMessages = NULL; | |
bace5cd9 | 115 | |
56f96145 J |
116 | // on multi-lang sites based on request and civicrm_uf_match |
117 | if ($multiLang) { | |
118 | $config = \CRM_Core_Config::singleton(); | |
c64f69d9 | 119 | $languageLimit = []; |
56f96145 J |
120 | if (isset($config->languageLimit) and $config->languageLimit) { |
121 | $languageLimit = $config->languageLimit; | |
122 | } | |
bace5cd9 | 123 | |
56f96145 J |
124 | if (in_array($lcMessagesRequest, array_keys($languageLimit))) { |
125 | $lcMessages = $lcMessagesRequest; | |
126 | } | |
127 | else { | |
c64f69d9 | 128 | throw new \API_Exception(ts('Language not enabled: %1', [1 => $lcMessagesRequest])); |
bace5cd9 | 129 | } |
56f96145 | 130 | } |
bace5cd9 | 131 | |
56f96145 | 132 | if ($lcMessages) { |
40557979 | 133 | global $dbLocale; |
56f96145 | 134 | global $tsLocale; |
40557979 CW |
135 | |
136 | // Store original value to be restored in $this->onApiRespond | |
137 | $this->originalLang[$requestId] = [ | |
138 | 'tsLocale' => $tsLocale, | |
139 | 'dbLocale' => $dbLocale, | |
140 | ]; | |
141 | ||
142 | // Set suffix for table names - use views if more than one language | |
143 | $dbLocale = "_{$lcMessages}"; | |
144 | ||
145 | // Also set tsLocale - CRM-4041 | |
56f96145 | 146 | $tsLocale = $lcMessages; |
bace5cd9 TO |
147 | } |
148 | } | |
96025800 | 149 | |
bace5cd9 | 150 | } |