Merge pull request #14320 from sushantpaste/reporthook
[civicrm-core.git] / Civi / API / Subscriber / I18nSubscriber.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 namespace Civi\API\Subscriber;
29
30 use Civi\API\Events;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
32
33 /**
34 * Class I18nSubscriber
35 * @package Civi\API\Subscriber
36 */
37 class I18nSubscriber implements EventSubscriberInterface {
38
39 /**
40 * Used for rolling back language to its original setting after the api call.
41 *
42 * @var array
43 */
44 public $originalLang = [];
45
46 /**
47 * @return array
48 */
49 public static function getSubscribedEvents() {
50 return [
51 Events::PREPARE => ['onApiPrepare', Events::W_MIDDLE],
52 Events::RESPOND => ['onApiRespond', Events::W_LATE],
53 ];
54 }
55
56 /**
57 * Support multi-lingual requests
58 *
59 * @param \Civi\API\Event\Event $event
60 * API preparation event.
61 *
62 * @throws \API_Exception
63 */
64 public function onApiPrepare(\Civi\API\Event\Event $event) {
65 $apiRequest = $event->getApiRequest();
66
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) {
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'];
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.
101 * @param string $lcMessages
102 * @param int $requestId
103 * @throws \API_Exception
104 */
105 public function setLocale($lcMessages, $requestId) {
106 $domain = new \CRM_Core_DAO_Domain();
107 $domain->id = \CRM_Core_Config::domainID();
108 $domain->find(TRUE);
109
110 // Check if the site is multi-lingual
111 if ($domain->locales && $lcMessages) {
112 // Validate language, otherwise a bad dbLocale could probably lead to sql-injection.
113 if (!array_key_exists($lcMessages, \Civi::settings()->get('languageLimit'))) {
114 throw new \API_Exception(ts('Language not enabled: %1', [1 => $lcMessages]));
115 }
116
117 global $dbLocale;
118 global $tsLocale;
119
120 // Store original value to be restored in $this->onApiRespond
121 $this->originalLang[$requestId] = [
122 'tsLocale' => $tsLocale,
123 'dbLocale' => $dbLocale,
124 ];
125
126 // Set suffix for table names - use views if more than one language
127 $dbLocale = "_{$lcMessages}";
128
129 // Also set tsLocale - CRM-4041
130 $tsLocale = $lcMessages;
131 }
132 }
133
134 }